python-rasterstats icon indicating copy to clipboard operation
python-rasterstats copied to clipboard

Wrong sum stats

Open adbedada opened this issue 7 years ago • 4 comments

I recently computed the sum zonal stats for a shapefile with 416 admin 3 level boundaries and the values for some of the rows is way off. here is what it looks like:

 FID      |  RASTERSTATS  |  QGIS
 -----------------------------------  
 320      |       14925   | 12842
 321      | 15032404941   | 13401
 322      |       23455   | 19222
 323      | 21474859155   | 23215
 324      | 12884922386   | 23014
 325      |       33255   | 22410
 326      | 17179901970   | 33255
 327      |       16922   | 32157
 328      | 85899495020   | 16153
 329      | 12884921516   | 13093
 330      |       23524   | 21181

For values under FID, 321, 323, 334, 326, 328 and 329 the sum generated by rasterstats is way off. I saw similar issues but according to the thread, this is fixed by now. The data can be found here https://drive.google.com/file/d/1v-Y7cK7DOsrxIPG5h56H9Q_ij0YGVeCe/view?usp=sharing

I also included the script and if it can help here is a list of versions of packages on my pc:

numpy==1.14.3 GDAL==2.3.2 geopandas==0.4.0 rasterio==1.0.13 rasterstats==0.13.0

adbedada avatar Dec 18 '18 19:12 adbedada

@adbeda thanks for the report. I'm not able to access those files on google drive, could you check the permissions?

perrygeo avatar Dec 18 '18 21:12 perrygeo

sorry, check this one: https://drive.google.com/file/d/1v-Y7cK7DOsrxIPG5h56H9Q_ij0YGVeCe/view?usp=sharing

adbedada avatar Dec 18 '18 22:12 adbedada

It looks like your script is incorrectly setting nodata.

GDAL confirms that the nodata value is 2147483647

gdalinfo /data/landscan/ls_2012_final_v2.tif | grep NoData
  NoData Value=2147483647

When you set nodata=0 in your zonal_stats call, you're declaring that 0 is the true nodata value, which is incorrect. When it encounters one of these large values, it's considered valid since it's not equal to nodata.

Omitting the nodata argument will allow zonal_stats to read the true nodata value from the raster's metadata. That should fix the problem for polygons which intersect these nodata holes

screen shot 2018-12-18 at 5 02 50 pm

perrygeo avatar Dec 19 '18 00:12 perrygeo

Perfect. that makes a whole lot of sense now. Thanks!

adbedada avatar Dec 20 '18 15:12 adbedada