qgis / QGIS

QGIS is a free, open source, cross platform (lin/win/mac) geographical information system (GIS)
https://qgis.org
GNU General Public License v2.0
10.13k stars 2.93k forks source link

Cell counts of zonal statistics and zonal histogram is different #33456

Open queenmedley opened 4 years ago

queenmedley commented 4 years ago

When anlaystis raster with Toolbox->Raster analysis->zonal statistics/histogram, cell counts is different. For example, raster is classified with 10~20, 20~30, 30~40, zonal statistics' count is 30. but zonal histogram is 10-20 : 10 20-30 : 10 30-40 : 11 I think the more complicated the polygon, the bigger the difference.

gioman commented 4 years ago

@queenmedley can you please attach sample data?

queenmedley commented 4 years ago

2 sample datas are attaced,.. sample1.zip sample2.zip

gioman commented 2 years ago

Seems to still be the cased on 3.22.2

kgjenkins commented 2 years ago

I'm running into the same issue on QGIS 3.24.3 for Windows.

Here is a minimal set of test data: a small landuse raster and a single rectangular polygon that covers 6x3 pixels: zonaltest.zip

image

Somehow Zonal Histogram only counts 5 of the pink pixels (value=36), although there are 11 such pixels in the polygon. The counts for the other values are correct.

Zonal Statistics correctly counts 18 pixels in the polygon.

kgjenkins commented 2 years ago

Curiouser and curiouser... I just tried with a polygon that falls just within six value=36 pixels, and it reports the exact same zonal histogram values as in the test above. This even persists after closing and restarting QGIS. zonaltest2.zip

image

MrChebur commented 2 days ago

I can confirm that the Zonal Histogram tool gives incorrect results. I attach an archive with test data. On the screenshot you can see that polygons 3 and 6 should contain pixels with values 3, but they don't.

The tool description says:

Appends fields representing counts of each unique value from a raster layer contained within polygon features.

The output layer attribute table will have as many fields as the unique values of the raster layer that intersects the polygon(s).

So it should select ALL INTERSECTING pixels, but the tool works somehow differently. I couldn't understand the logic behind the selection, I thought the pixels were selected by centroid or maximum area. But polygons with fid 5, 4 disprove it (somehow they get all the intersecting polygons right).

Screenshot

nyalldawson commented 2 days ago

QGIS zonal stats uses a heuristic approach where the pixel size is compared against the geometry size, and depending on the relative sizes of these a precise intersection of the pixel geometry vs the vector geometry will either be used (or not).

This is unique to the zonal stats tool, and is NOT used by zonal histogram (which uses only the pixel centroid for calculations).

MrChebur commented 2 days ago

@nyalldawson Thank you for your comment.

However, if the “zonal histogram” tool compares the centroid location with the polygon location, how did the object with fid=4 have a “HISTO_6” field with a value of “1”?

The geometry of this polygon does NOT intersect the centroid of the pixel:

изображение