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

add gdf_zonal_stat to main.py #293

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/rasterstats/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ def zonal_stats(*args, **kwargs):
return a list rather than a generator."""
return list(gen_zonal_stats(*args, **kwargs))

def gdf_zonal_stats(vectors, *args, **kwargs):
"""The primary zonal statistics entry point.
All arguments are passed directly to ``gen_zonal_stats``.
See its docstring for details.

The difference is that ``gdf_zonal_stats`` will
return a GeoDataFrame rather than a generator.
Besides this, we make sure the new gdf as the right
metadata by conserving the CRS
"""

gdf = gpd.GeoDataFrame.from_features(
list(gen_zonal_stats(vectors, geojson_out=True, *args, **kwargs))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from_features should be able to take an iterable sequence - we could remove the intermediate list.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed, I have just tried without the list and it works !

)
gdf.crs = vectors.crs

return gdf


def gen_zonal_stats(
vectors,
Expand Down