2

I am using the zonal_stats function in the rasterstats package to calculate summary statistics from GeoTiffs containing vegetative index values. The summary statistics come out fine, however with each iteration of the zonal_stats function I run, I encounter the warning UserWarning: Setting nodata to -999; specify nodata explicitly warnings.warn("Setting nodata to -999; specify nodata explicitly")

When I created each GeoTIFF file, I set the nodata value as outband.SetNoDataValue(np.nan). Each geotiff is opened and VI statistics calculated with:

    raster = [FILE PATH]
    raster = rasterio.open(raster, mode='r')

    # Assign raster values to numpy nd array
    vi_array = raster.read(1)
    affine = raster.transform

    # Calculate VI stats
    zonal_stats = rasterstats.zonal_stats(shapefile, vi_array, affine=affine,
                                       stats="mean count std median",
                                       geojson_out=True)

How does one get rid of this warning message?

4
  • Maybe see here for an answer: github.com/perrygeo/python-rasterstats/issues/115
    – GBG
    Apr 19 at 14:59
  • @GBG The numpy version I am using in this environment is 1.21.5
    – nburner
    Apr 19 at 15:11
  • Post an issue on the rasterstats github page. Get around the error and use a Python try/except block to catch the exception. Then use pass to ignore and move on?
    – GBG
    Apr 19 at 18:48
  • @GBG it's not an exception, it's a warning.
    – user2856
    Jun 18 at 9:42

1 Answer 1

0

You may have set nodata on your raster, but you are not passing that raster to rasterstats.zonal_stats, you are passing a numpy array. And numpy arrays don't have a nodata property.

Just like you specify the affine transform when passing an array to rasterstats.zonal_stats instead of a raster, you can also specify the nodata value:

# Calculate VI stats
zonal_stats = zonal_stats(shapefile, vi_array,
                          affine=affine, nodata=np.nan,
                          stats="mean count std median",
                          geojson_out=True)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.