-
Notifications
You must be signed in to change notification settings - Fork 116
add gdf_zonal_stat to main.py #293
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
Open
TomKenda
wants to merge
3
commits into
perrygeo:master
Choose a base branch
from
TomKenda:patch-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,13 +4,14 @@ | |
| import sys | ||
|
|
||
| import numpy as np | ||
| import geopandas as gpd | ||
| 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 | ||
|
|
||
|
|
@@ -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): | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this seems like a copy of what's in main.py. Why duplicate it instead of importing it? |
||
| """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>) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
gpd = pytest.importorskip("geopandas")in your test functions should be enough. If it's available, you getgpd, otherwise the test is skipped. You don't want to import it at the top level.