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

Adding CRS information to output of zonal stats #295

Open
AlexanderJuestel opened this issue Oct 19, 2023 · 0 comments
Open

Adding CRS information to output of zonal stats #295

AlexanderJuestel opened this issue Oct 19, 2023 · 0 comments

Comments

@AlexanderJuestel
Copy link

Hello,

I am using rasterstats and its zonal statistics where the output of everything is a GeoPandas GeoDataFrame containing the results. I am doing the following as shown below. However, the list of dicts (geojson-like) that zonal_stats returns does not contain the CRS information of the vector data and in return, a GeoDataFrame is created without CRS.

I would like to propose adding the CRS information to the output of zonal_stats so that GeoPandas can create a GeoDataFrame with a valid CRS from it. I could of course just open the vector data using GeoPandas but I like the way it is accomplished below.

I could try working on a PR if that feature is desired for rasterstats.

stats = zonal_stats("path_to_vector.shp", 
                             "path_to_raster.tif",
                             stats="count min mean max median sum std",
                             geojson_out=True)

gdf = gpd.GeoDataFrame.from_features(stats)

A place to add the CRS would be somewhere in the fiona_generator, I guess:

def fiona_generator(obj, layer=0):
with fiona.open(obj, "r", layer=layer) as src:
for feat in src:
yield fiona.model.to_dict(feat)

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

1 participant