Python Zonal statistics script: Problem after trying to install rioxarray
I have a script that used to work for calculating zonal statistics (median), but now I get the AttributeError: 'DatasetReader' object has no attribute 'affine'. Here is my code:
with rasterio.open(f'{project_data}/ras.tif') as raster:
array = raster.read(1)
affine = raster.affine
stat = zonal_stats(f'{project_data}/Lila.shp', array, affine=affine,
stats=['median'], geojson_out=True)
result = {"type": "FeatureCollection", "features": stat}
outname = f'{project_data}/files/Lala_test.geojson'
with open(outname, 'w') as outfile:
json.dump(result, outfile)
I unsucessfully tried to install rioxarray yesterday with Anaconda, and I am using an virtual conda - environment (Python 3.8 Interpreter), could that be the problem? If yes, how can I fix that? I am on windows 10...
affine
has been deprecated. transform
now accepts GDAL and Affine style transforms.
From Migrating to Rasterio 1.0 affine.Affine() vs. GDAL-style geotransforms:
https://rasterio.readthedocs.io/en/latest/topics/migrating-to-v1.html
Since the above changes, several functions have been added to Rasterio that accept a transform argument. Rather than add an affine argument to each, the transform argument could be either an Affine() object or a GDAL geotransform, the latter issuing the same deprecation warning.
The original plan was to remove the affine argument + property, and assume that the object passed to transform is an Affine(). However, after further discussion it was determined that since Affine() and GDAL geotransforms are both 6 element tuples users may experience unexplained errors and outputs, so an exception is raised instead to better highlight the error.