Rasterstats – Resolving None Values Returned by Zonal_Stats Function on Raster

pythonrasterrasteriorasterstatszonal statistics

I am trying to run simple stats on a shapefile using the rasterstats's zonal_stats function. However, when I run the stats, the function only returns "none" for every feature and every statistic. The raster is one I created using rasterio and pulling data from a NetCDF.

My code looks like this right now:

from rasterstats import zonal_stats

shapefile = "RGdS_AdUnits_for_pop.shp"
rasterfile = "GDD245_2020_tobacco.tif"

stats = zonal_stats(shapefile, rasterfile, all_touched=True)
stats

which returns something like this:

[{'count': 0, 'min': None, 'max': None, 'mean': None},
 {'count': 0, 'min': None, 'max': None, 'mean': None},
 {'count': 0, 'min': None, 'max': None, 'mean': None},
 {'count': 0, 'min': None, 'max': None, 'mean': None},
 {'count': 0, 'min': None, 'max': None, 'mean': None},
 {'count': 0, 'min': None, 'max': None, 'mean': None},]

I've seen a couple of solutions for questions about the same problem suggest that a mismatching CRS could be the issue but both the shapefile and TIFF here are in EPSG:4326.

I'm not sure if this is an issue with my raster file here but I'm able to open the raster in ArcGIS Pro and run summary stats as table just fine. I'd use that instead but I need the all_touched=True parameter here since I have some pretty small features. Here's a zipfile of the raster and shapefile I'm using if anyone wants to look:
Data.zip

Best Answer

Your raster is georeferenced from 0 to 360 instead of -180 to 180.

enter image description here

So zonal_stats thinks your vector data doesn't overlap the raster.

You need to warp the raster to -180 to 180

gdalwarp GDD245_2020_tobacco.tif GDD245_2020_tobacco_180.tif --config CENTER_LONG 0

[enter image description here

Then you can run zonal_stats

from rasterstats import zonal_stats

shapefile = "RGdS_AdUnits_for_pop.shp"
rasterfile = "GDD245_2020_tobacco_180.tif"

stats = zonal_stats(shapefile, rasterfile, all_touched=True)
stats

[{'min': 0.0, 'max': 275.0, 'mean': 201.24242424242425, 'count': 33}, etc...}]