Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError when running zonal_stats #173

Closed
HenryW95 opened this issue Oct 9, 2018 · 9 comments
Closed

AttributeError when running zonal_stats #173

HenryW95 opened this issue Oct 9, 2018 · 9 comments

Comments

@HenryW95
Copy link

HenryW95 commented Oct 9, 2018

I'm trying to run zonal_stats and I'm getting the following error in the _init_ function of io.py (line 242):
AttributeError: 'GeneratorContextManager' object has no attribute 'transform'

I've recreated the error with a variety of raster and vector data on a fresh virtual environment with rasterstats version 0.13.0. I've experimented with using different combinations of rasterstats/rasterio versions but I'm still seeing the issue. Any insight would be greatly appreciated.

Thanks!

@HenryW95
Copy link
Author

HenryW95 commented Oct 9, 2018

Apologies, I was using reading the raster to memory before calling zonal_stats. Simply supplying the file path to zonal_stats fixed the issue.

@HenryW95 HenryW95 closed this as completed Oct 9, 2018
@Jwely
Copy link
Contributor

Jwely commented Aug 20, 2019

I'd request this issue is re-opened.

An in-memory raster should be an accepted input. Being required to write it to disk and manage temporary files is a needlessly burdensome workaround.

@mhweber
Copy link

mhweber commented Dec 18, 2019

I would second request to re-open - I'm encountering the exact same issue with trying to use in-memory raster as opposed to passing the file path to zonal_stats.

@perrygeo
Copy link
Owner

In-memory rasters (more specifically a numpy array) can be passed as the raster data source but you need to also specify affine transform to tie it to geographic coordinates. See https://pythonhosted.org/rasterstats/manual.html#raster-data-sources

@janderson5417
Copy link

janderson5417 commented Dec 18, 2021

I see this issue is closed, but I've been stuck on specifying the affine instance most of the afternoon without any success. I've tried to baby-step my way through:

tracts = fiona.open('data/tl_2021_06_tract.shp')

land_src = rasterio.open('data/land/NLCD_2019_Land_Cover_L48_20210604_rNuMWD3Z0SuUkIIVRc8K.tiff')
land_dict = land_src.profile

land_aff = land_dict['transform']
land_aff
Affine(30.0, 0.0, -2158455.0,
0.0, -30.0, 1591005.0)

land_arr = land_src.read(1)
land_arr
array([[52, 52, 52, ..., 52, 52, 52],
[52, 52, 52, ..., 52, 52, 71],
[52, 52, 52, ..., 52, 52, 71],
...,
[ 0, 0, 0, ..., 31, 31, 31],
[ 0, 0, 0, ..., 31, 31, 31],
[ 0, 0, 0, ..., 31, 31, 31]], dtype=uint8)

land_stats = zonal_stats(tracts, land_src, array = land_arr, affine = land_aff)
...
AttributeError: '_GeneratorContextManager' object has no attribute 'transform'

What am I missing?

@perrygeo
Copy link
Owner

@janderson5417 please see the link above for a usage example. You need to use profile.affine and pass the array, not the rasterio dataset, as the raster argument (2nd position)

@janderson5417
Copy link

janderson5417 commented Dec 21, 2021

@perrygeo thank you! You can likely discern I'm learning (!) so really appreciate your help. I was traveling yesterday and couldn't try it out until today...if I can stretch your patience a bit, I tried the following based on your input:

land_stats = zonal_stats(tracts, land_arr, affine = land_aff, nodata = -9999)

It returned an empty list, although I can confirm data in QGIS for both the vector + raster data.

Maybe I implemented your response incorrectly?

@janderson5417
Copy link

@perrygeo --just for clarification, affine = src.affine yields the following error for me:

AttributeError: 'DatasetReader' object has no attribute 'affine'

@janderson5417
Copy link

@perrygeo --apologies, I've downloaded the repo files and am walking through your notes (which are fantastic)--thank you!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants