I am using rasterstats to compute zonal statistics within many polygons (provided with a .shp file) for a series of images (.tiff files). A standard statistic computed for each polygon is "count", which I assume is the number of non-nodata pixels. For each polygon, I am interested in determining the ratio of non-nodata pixels to total pixels - for that, I would need to additionally determine either: 1) the number of nodata pixels, or 2) the total number of pixels.
- I have tried including the "nodata" statistic, but am getting the following error returned (this error is returned many times - I am uncertain if it is for all or a subset of the polygons):
env/lib/python3.9/site-packages/rasterstats/main.py:264: UserWarning: Warning: converting a masked element to nan. feature_stats['nodata'] = float((featmasked == fsrc.nodata).sum())
- I have written my own user-defined statistic to determine the total number of pixels:
# Define total count stat function. def sb_total(x): return x.size
However, this is returning suspect values, as 1) the number can vary depending on the tiff (all span the same general area), and 2) it is indicating many more total pixels than non-nodata pixels, which I don't believe is the case.
Additionally, I have reason to believe that I am not coding up a user-defined statistic properly, as my attempt to reproduce the "count" statistic is not working:
# Define count stat function. def sb_total(x): return np.count_nonzero(np.isfinite(x))
What is the issue here?