2018-03-22 16:09:00 8 Comments

There are a couple Stackexchange posts asking this question, but neither of them were sufficiently answered:

- How does Zonal Statistics work exactly?
- How does Zonal statistics handle pixels that are on the border of the polygon?

I am writing Python scripts that perform essentially the same operations as Zonal Stats, and I have been using Zonal Stats to check my scripts' outputs.

I have noticed that when there are many cells in the polygon being queried, my results match perfectly, but there are discrepancies when there are only a few raster cells in the polygon. The following is an example of this. I am computing "count" for each polygon.

Exhibit 1:

The returned count is 0.45, which looks roughly correct *if pixels can be treated fractionally rather than simply in/out*.

Exhibit 2:

Similar result; count is 0.39.

Exhibit 3:

With a larger poylgon, now we see the count is *exactly* 2. Note that there are exactly 2 pixel centers contained in the polygon.

Exhibit 4:

Here, only one pixel center is within the polygon. The count is 1.008, though. If you exclude the polygon area from the pixel whose center is in the polygon, the remaining area is obviously greater than 0.008 of a pixel.

From these exhibits, it seems that Zonal Statistics treats these cases specially. It looks like to me that if there are less than 2 pixel centers within the polygon, it performs some kind of averaging such as polygon_area/raster_cell_area to return the count. I should note that I have 48 of these polygons, and all returned counts greater than or equal to 2 are integers; fractional counts are only returned for <2.

How are these fractional-pixel counts incorporated into the other statistics output by Zonal Stats?

How does Zonal Statistics work exactly? indicates strange behavior with max/min when using large raster cells, and my own testing returns mean values not congruous with simple "pixel center in/out of the polygon." To reiterate, I get exact agreement when the underlying raster contains many pixels within the polygon. I couldn't find any documentation about this, and it may be important when people have never had problems with Zonal Stats but use a coarse raster and remain unaware that there is a different behavior when there are only a few pixels within the polygon.

### Related Questions

#### Sponsored Content

#### 1 Answered Questions

### QGIS equivalent to the ArcMap Zonal Statistics as Table

**2017-03-21 20:53:07****CTPresz****355**View**2**Score**1**Answer- Tags: qgis zonal-statistics histogram

#### 1 Answered Questions

### [SOLVED] Zonal stats for complete pixels qgis

**2018-07-27 13:38:40****Fabio Daniel Trinco****167**View**3**Score**1**Answer- Tags: qgis zonal-statistics

#### 0 Answered Questions

### How does Zonal statistics handle pixels that are on border of polygon?

**2013-02-17 17:58:42****johnrobot****253**View**4**Score**0**Answer- Tags: qgis raster zonal-statistics

#### 1 Answered Questions

### [SOLVED] Getting zonal statistics of polygons not touching center raster cells in PostGIS?

**2018-07-08 02:13:25****Isaque Daniel****120**View**4**Score**1**Answer- Tags: postgis zonal-statistics

#### 2 Answered Questions

### [SOLVED] How does Zonal Statistics work exactly?

**2017-09-11 15:57:37****GeoEki****7224**View**8**Score**2**Answer- Tags: qgis raster layers saga zonal-statistics

#### 1 Answered Questions

### [SOLVED] Zones missing after running zonal statistics in ArcGIS Spatial Analyst?

**2017-05-17 14:42:29****Oh-No****249**View**5**Score**1**Answer- Tags: arcgis-desktop spatial-analyst zonal-statistics missing-data

#### 0 Answered Questions

### Calculating Mean and Sum in QGIS Zonal Statistics?

**2018-01-11 15:26:16****zepedroramiao****120**View**5**Score**0**Answer- Tags: qgis zonal-statistics

#### 2 Answered Questions

### [SOLVED] Alternatives to Zonal Statistics which include partial pixels (QGIS)

**2017-02-03 13:46:08****Tim K****778**View**7**Score**2**Answer- Tags: qgis raster pyqgis polygon zonal-statistics

#### 1 Answered Questions

### [SOLVED] How does ArcGIS calculate zonal statistics with partially overlapping zones?

**2016-04-05 20:17:19****MJ Kim****435**View**2**Score**1**Answer- Tags: arcgis-desktop arcpy arcgis-10.2 zonal-statistics

#### 2 Answered Questions

### [SOLVED] How to Calculate Custom Zonal Statistics?

**2013-12-19 19:08:17****Ben Thompson****1261**View**4**Score**2**Answer- Tags: qgis arcgis-10.0 arcgis-desktop raster zonal-statistics

## 2 comments

## @dbaston 2018-05-03 15:53:43

QGIS first makes an initial pass, checking to see if the center of each raster cell is within the polygon. If fewer than two cell centers are within the polygon, it performs a vector-based intersection for all intersecting cells, whether their center is within the polygon or not, and computes a weight that is the fraction of each cell that is covered by the polygon. If two or more cell centers are within the polygon, those cells are assigned a weight of 1 and all other cells are assigned a weight of 0.

How these weights are applied depends on the statistic in question.

sumis calculated as the sum of each pixel value times its weight.countis calculated as the sum of all pixel weights.meanis calculated as the sum divided by the count.median,variance,standard deviation,minimum, andmaximumare calculated for all pixels with a weight > 0, but do not take individual pixel weights into account in the calculations.Relevant source code:

Selection of pixel inclusion/exclusion algorithm

Statistics calculation

## @xunilk 2018-03-23 04:51:40

I can handle partially overlapping pixels adapting code (for PyQGIS in QGIS 3) in the answer to this question: Robust Zonal stats in R and QGIS.

Polygons_newreferred at above code can be observed at following image:After running it at Python Console of QGIS 3, result was:

Attributes table has weighted values for pixel for each intersected polygon area (with a temporal grid that coincides with raster cells).

At following example can be observed a shapefile (

Polygons_new2) that intersects a complete pixel. Its weighted value at attributes table is the same as pixel value (as expected). Remaining pixel values are proportional to covered area by shapefile.## @Jon 2018-03-23 14:57:12

Thanks for the contribution; I see you make a polygon grid from your raster grid to find the relative contribution of each pixel. This would be nice to incorporate into Zonal Statistics to handle these coarse raster cases. The question about Zonal Statistics's behavior remains.