Skip to content
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
"""

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Move the import geopandas as gpd into the function body and out of the top level. Geopandas must remain optional.

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

Copy link
Copy Markdown
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
Copy Markdown
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