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
Show file tree
Hide file tree
Changes from all commits
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
31 changes: 31 additions & 0 deletions src/rasterstats/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import warnings

import numpy as np
import geopandas as gpd
from affine import Affine
from shapely.geometry import shape

Expand Down Expand Up @@ -35,6 +36,36 @@ 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(
gen_zonal_stats(vectors, geojson_out=True, *args, **kwargs)
)

# if the input is a file path : open has gdf and get crs
if isinstance(vectors, str):
gdf.crs = gpd.read_file(vectors).crs

# if the vectors has a 'crs' attribute use it to keep the crs
elif hasattr(vectors, "crs"):
gdf.crs = vectors.crs

# otherwise, we cannot store the crs ? (not sure if that is possible)
else:
gdf.crs = None
print("Warning : the crs is not stored in the output GeoDataFrame")

return gdf


def gen_zonal_stats(
vectors,
Expand Down
49 changes: 48 additions & 1 deletion tests/test_zonal.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import sys

import numpy as np
import geopandas as gpd
Copy link
Author

Choose a reason for hiding this comment

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

I see in the test you runned that there are problems with geopandas import. I don't know what to do to solve this. Maybe I was not supposed to add this line to the test file ?

import pytest
import rasterio
import simplejson
from affine import Affine
from shapely.geometry import Polygon

from rasterstats import raster_stats, zonal_stats
from rasterstats import raster_stats, zonal_stats, gen_zonal_stats #, gdf_zonal_stats
from rasterstats.io import read_featurecollection, read_features
from rasterstats.utils import VALID_STATS

Expand Down Expand Up @@ -564,6 +565,52 @@ def test_geodataframe_zonal():
assert zonal_stats(df, raster) == expected


#%% # add a unit test for the function gdf_zonal_stats

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(
gen_zonal_stats(vectors, geojson_out=True, *args, **kwargs)
)

# if the input is a file path : open has gdf and get crs
if isinstance(vectors, str):
gdf.crs = gpd.read_file(vectors).crs

# if the vectors has a 'crs' attribute use it to keep the crs
elif hasattr(vectors, "crs"):
gdf.crs = vectors.crs

# otherwise, we cannot store the crs ? (not sure if that is possible)
else:
gdf.crs = None
print("Warning : the crs is not stored in the output GeoDataFrame")

return gdf


def test_gdf_zonal_stats():
gpd = pytest.importorskip("geopandas")
polygons = os.path.join(DATA, "polygons.shp")
gdf = gpd.read_file(polygons)
if not hasattr(gdf, "__geo_interface__"):
pytest.skip("This version of geopandas doesn't support df.__geo_interface__")

expected_with_path = gdf_zonal_stats(polygons, raster)
expected_with_gdf = gdf_zonal_stats(gdf, raster)
assert expected_with_gdf.equals(expected_with_path) # Use equals() to compare DataFrames
assert expected_with_gdf.crs == gdf.crs # Check that the crs is conserved


# TODO # gen_zonal_stats(<features_without_props>)
# TODO # gen_zonal_stats(stats=nodata)
# TODO # gen_zonal_stats(<raster with non-integer dtype>)
Expand Down