FOSS4G How-to: Get zonal statistics in QGIS and GRASS using a polygon layer as zones

28 Jul 2017 ᛫ 3 min read

SHARE:

In the previous entry, we learned how to sample raster and vector values at points. In this post, we’ll take a look at how we can compute for zonal statistics using a polygon layer in QGIS and GRASS.

Zonal statistics work by computing for statistics of a raster dataset (i.e. minimum, maximum, mean, etc.) within certain zones provided by another dataset (either another raster or a vector layer). Here, we’ll use a polygon vector layer to define our zones.

The tools we’ll be using are the Zonal statistics plugin in QGIS and the v.rast.stats or Update area attributes from raster module in GRASS.

The data layers for this how-to are:

  1. sample-polygons – a polygon (vector) layer
  2. solar-radiation – a (raster) layer of Global Horizontal Irradiance (GHI) values

We’ll try to get the minimum, maximum, and average solar radiation values for each zone provided in our sample-polygons layer.

Zonal statistics

The Zonal statistics plugin is a Core plugin in QGIS 2.18.XX that allows the user to calculate statistics on pixels of a raster band that are within polygons/zones in a vector layer. It creates additional fields in the vector layer using a user-defined prefix to hold the calculated statistics which include the minimum value, maximum value, mean value, count, and sum. You can read more about it here.

Check this post if you need help with installing/activating plugins.

Once activated, the plugin can be accessed via Raster -> Zonal statistics -> Zonal statistics.

Zonal statistics plugin in QGIS menu bar

DIRECTIONS:

Make sure that the layers you want to use are active (checked).

Open the Zonal statistics plugin.

  1. Select the Raster layer containing the band you want to compute statistics from (solar-radiation).
  2. Select the Band you want to compute statistics from (Band 1).
  3. Select the Polygon layer containing the zones (sample-polygons).
  4. Set the Output column prefix (GHI_).
  5. Check the Statistics to calculate.

Click OK.

Using the zonal statistics pllugin in QGIS

If you open the attribute table of the sample-polygons layer, it should contain three additional fields named GHI_mean, GHI_min, and GHI_max.

Attribute table of result from zonal statistics plugin in QGIS

v.rast.stats

The v.rast.stats module calculates univariate statistics from a raster map based on a vector map and uploads statistics to new attribute columns. You can read more about it here. It can be accessed by typing v.rast.stats in the Command console or via Vector -> Update attributes -> Update area attributes from raster [v.rast.stats].

v.rast.stats algorithm raster zonal statistics in GRASS GIS

DIRECTIONS:

On the Required tab:

  1. Select the Name of the vector map containing the zones (sample-polygons).
  2. Select the Name of the input raster to calculate statistics from (solar-radiation).
  3. Set the Column prefix for new attribute columns (GHI).

On the Optional tab:

  1. Select the Layer number or name to compute statistics from (1).
  2. Select the methods to use (or statistics to compute).

Click Run

v.rast.stats parameters for zonal statistics in GRASS GIS
More v.rast.stats parameters for zonal statistics in GRASS GIS

If you open the attribute table of the sample-polygons layer, it should contain three additional fields named GHI_minimum, GHI_maximum, and GHI_average.

Attribute table of layer output of v.rast.stats for zonal statistics in GRASS GIS

Final thoughts

If you check their values, the results obtained using Zonal statistics in QGIS and v.rast.stats in GRASS are the same. Only the names of the additional attribute columns/fields created are different.

With that, you should now be able to compute zonal statistics using a polygon vector in QGIS and GRASS.

Tune in for the next installment of FOSS4G How-to and if you have any queries or requests, you can contact me or leave a comment below.

Cheers!

SHARE:
comments powered by Disqus

You may also like:

Towards a Spatial Analysis of Philippine Basketball: Applications in the UAAP MBT (Season 81) [Part 1]

02 May 2021 ᛫ 4 min read

Win and let win: On being unconventional, openness, and building communities

30 Apr 2021 ᛫ 1 min read

QGIS Styles based on HLURB Land Use Categories and Color Coding (CLUP Guidebook Vol 3, 2014)

15 Dec 2020 ᛫ 2 min read

Select duplicate geometries from a layer in QGIS

09 Dec 2020 ᛫ 3 min read

Get Data from DPWH Road and Bridge Inventory in QGIS

29 Nov 2020 ᛫ 2 min read

BNHR

[email protected]

Creative Commons License
Except when explicitly stated otherwise, this work and its contents by Ben Hur S. Pintor is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Other works (software, source code, etc.) referenced in this website are under their own respective licenses.
This site is powered by Jekyll and hosted on Github (view source)