I want to calculate the mean NDVI with the EthNdvi.tif file for each zone (there are around 650 admin zones) in the the woreda shapefile. I'm having some issues with running the following lines of code:

from rasterstats import zonal_stats
stats = zonal_stats("Eth_Woreda_2013.shp", "EthNdvi.tif")

Specifically it gives me a memory error. I tried to do zonal statistics in QGIS and this works fine. However when I code this in a python script it results in memory errors.

I've uploaded the two files in a google link: https://drive.google.com/open?id=1go78VkkH8MdAyfpGr4Sg6CvI-WU3WgrC if you want to replicate it.

My laptop has 8gb of ram.

Could anybody explain me why this doesn't work in a python script, but it does in QGIS?

Moreover does anyone have a tip how I could still script this in python; should I buy more memory for my laptop or should I do something else?

  • Are you using a 64-bit Python interpreter?
    – mikewatt
    Nov 2, 2018 at 16:55
  • @gberard I use anaconda 64 bit yes. Does this code with the files work with you?
    – Vindicare
    Nov 2, 2018 at 19:54
  • I just tried it and got the same error, but it worked fine after reprojecting the raster to match the shapefile. I suspect the raster's geographic coordinate system was the issue
    – mikewatt
    Nov 2, 2018 at 20:06
  • I used gdalwarp if you're curious: gdalwarp EthNdvi.tif EthNdviUTM37.tif -t_srs EPSG:20137
    – mikewatt
    Nov 2, 2018 at 20:07
  • @gberard I ran your code from gdalwarp and tried to run the code again. However it still gives me a memory error. How much gb of ram do you have?
    – Vindicare
    Nov 3, 2018 at 12:23


Browse other questions tagged or ask your own question.