FOSS4G How-to: Get Zonal Statistics in QGIS and GRASS using a Polygon Layer as Zones
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
Update area attributes from raster module in GRASS.
The data layers for this how-to are:
- sample-polygons – a polygon (vector) layer
- 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.
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.
Make sure that the layers you want to use are active (checked).
Open the Zonal statistics plugin.
- Select the Raster layer containing the band you want to compute statistics from (solar-radiation).
- Select the Band you want to compute statistics from (Band 1).
- Select the Polygon layer containing the zones (sample-polygons).
- Set the Output column prefix (GHI_).
- Check the Statistics to calculate.
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.
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].
- Select the Name of the vector map containing the zones (sample-polygons).
- Select the Name of the input raster to calculate statistics from (solar-radiation).
- Set the Column prefix for new attribute columns (GHI).
- Select the Layer number or name to compute statistics from (1).
- Select the methods to use (or statistics to compute).
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.
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.