# How does Zonal Statistics work exactly?

I'm a bit confused on how the zonal statistics tools work in QGIS (either the 'Zonal Statistics' tool under the Raster tools from the QGIS geoalgorithms toolbox or the 'Raster statistics from polygons' tool under the SAGA Vector<->Raster tools).

Basically I have a raster map layer and some small polygons in a separate layer. I want to assign each polygon the value of the raster below. In case a polygon overlaps multiple raster cells, I just want to have the maximum.

Yet the output doesn't make any sense and I can't get behind the reasoning of those results (which are obviously not the maximum of the underlain raster cell): Any ideas what I'm doing wrong or what I didn't consider yet? The results with the python API in `qgis.analysis.QgsZonalStatistics()` (according to this question here) are much better, but it does only offer `count`, `mean` and `sum`...but for some reason no `max` or `min`.

• Just tried this in ArcMap by converting my Raster to Points and it looks like the QGIS Zonal Stats tool is doing the same internally and uses only the cases where a point intersects a polygon. However, there seems not to be an appropriate match option in QGIS (something comparable to those offered in ArcMap's Spatial Join tool, e.g. INTERSECT, WITHIN_A_DISTANCE, CLOSEST,...). – GeoEki Sep 11 '17 at 16:11
• As a clue to figuring this out, I computed the "count" for a coarse raster layer (roughly one pixel is in the polygon) with QGIS's Zonal Statistics and got a value of 0.45--surprisingly not an integer. This suggests that QGIS may somehow be weighting the pixel values. This might be why the max/min don't correspond to what you expect--possibly a poor implementation of this weighting when computing max/min. – Jon Mar 22 '18 at 15:33
• See gis.stackexchange.com/a/281753/18189 for a description of how the QGIS zonal stats algorithm works. – dbaston May 3 '18 at 16:01

Update as April 2018 and QGIS 2.18

The current version of Zonal Statistics Plugin can provide:

``````Count: to count the number of pixels
Sum: to sum the pixel values
Mean: to get the mean of pixel values
Median: to get the median of pixel values
StDev: to get the standard deviation of pixel values
Min: to get the minimum of pixel values
Max: to get the maximum of pixel values
Range: to get the range (max - min) of pixel values
Minority: to get the less represented pixel value
Majority: to get the most represented pixel value
Variety: to count the number of distinct pixel values
``````

In short, this approach with QGIS, as far as I'm aware is not possible to view the raster pixel maximum as a result. Only the count, mean and sum. I've provided some resources that covers this.

Here is the doc page for the plugin Zonal Statistics Plugin

With the icon_zonal_statistics Zonal statistics plugin, you can analyze the results of a thematic classification. It allows you to calculate several values of the pixels of a raster layer with the help of a polygonal vector layer (see figure_zonal_statistics). You can calculate the sum, the mean value and the total count of the pixels that are within a polygon. The plugin generates output columns in the vector layer with a user-defined prefix.

Also, this QGIS Tutorial Sampling Raster Data using Points or Polygons provides a sample data to achieve zonal statistics return. I've included screenshot they provide as a example of their workflow and results.

Enable the Zonal Statistics Plugins. This is a core plugin so it is already installed. See Using Plugins to know to how enable core plugin   Once the processing finishes, select the counties layer. Use the Identify tool and click on any county polygon. You will see three new attributes added to the layer: ZS_count, ZS_mean and ZS_sum. These attributes contain the count of raster pixels, mean of raster pixel values and sum of raster pixel values respectively. Since we are interested in average temperature, the ZS_mean field will be the one to use. As of QGIS 3.0 (possibly earlier, I'm not sure), the following statistics are available:

• Count
• Sum
• Mean
• Median
• Standard deviation
• Minimum
• Maximum
• Range
• Minority
• Majority (mode)
• Variety
• Variance