I am working with the Rasterstats package to run zonal statistics on two raster datasets. The documentation calls for the following:
import rasterio
from rasterstats import zonal_stats
with rasterio.open('tests/data/slope.tif') as src:
affine = src.affine
array = src.read(1)
zs = zonal_stats('tests/data/polygons.shp', array, affine=affine)
However, src.affine
is no longer supported in Rasterio. Instead, the documentation recommends using src.transform
, which yields something like:
Affine(1.0, 0.0, 670625.0016573452,
0.0, -1.0, 5282947.795216726)
When I do something like the following, I get an error:
from rasterstats import zonal_stats
dataset = rasterio.open('/path/to/image.tif')
affine = dataset.transform * (0,0) # upper left corner
zs = zonal_stats(segments, ndvi, affine=affine, stats="mean")
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
What affine format does the Rasterstats zonal_stats
function require?