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

COMPAT: geopandas 1.0 compatibility fix in h3fy #209

Merged
merged 4 commits into from
Apr 25, 2024
Merged
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
8 changes: 7 additions & 1 deletion tobler/dasymetric/raster_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
import rasterio as rio
import rasterstats as rs
from joblib import Parallel, delayed
from packaging.version import Version
from rasterio import features
from rasterio.mask import mask
from shapely.geometry import shape

from ..util.util import _check_presence_of_crs

GPD_10 = Version(gpd.__version__) >= Version("1.0.0dev")


def _chunk_dfs(geoms_to_chunk, n_jobs):
chunk_size = geoms_to_chunk.shape[0] // n_jobs + 1
Expand Down Expand Up @@ -107,7 +110,10 @@ def extract_raster_features(
with rio.open(raster_path) as src:
raster_crs = src.crs.to_dict()
gdf = gdf.to_crs(raster_crs)
geomask = [gdf.unary_union.__geo_interface__]
if GPD_10:
geomask = [gdf.union_all().__geo_interface__]
else:
geomask = [gdf.unary_union.__geo_interface__]

out_image, out_transform = mask(
src, geomask, nodata=nodata, crop=True
Expand Down
1 change: 1 addition & 0 deletions tobler/tests/test_area_interpolators.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def test_area_interpolate_categorical():
assert_almost_equal(area.animal_capybara.sum(), 20, decimal=0)


@pytest.mark.xfail(reason="dask_geopandas is broken with dask-expr backend")
def test_area_interpolate_categorical_dask():
sac1, sac2 = datasets()
sac1["animal"] = sac1["animal"].astype("category")
Expand Down
12 changes: 10 additions & 2 deletions tobler/util/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import numpy as np
import pandas
import shapely
from packaging.version import Version
from shapely.geometry import Polygon

GPD_10 = Version(geopandas.__version__) >= Version("1.0.0dev")


def circumradius(resolution):
"""Find the circumradius of an h3 hexagon at given resolution.
Expand Down Expand Up @@ -139,7 +142,10 @@ def h3fy(source, resolution=6, clip=False, buffer=False, return_geoms=True):
else:
source = source.to_crs(4326)

source_unary = shapely.force_2d(source.unary_union)
if GPD_10:
source_unary = shapely.force_2d(source.union_all())
Copy link
Member Author

Choose a reason for hiding this comment

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

I future, we may want to be able to pass kwargs here geopandas/geopandas#3151

else:
source_unary = shapely.force_2d(source.unary_union)

if type(source_unary) == Polygon:
hexagons = _to_hex(
Expand Down Expand Up @@ -208,6 +214,8 @@ def _to_hex(source, resolution=6, return_geoms=True, buffer=True):
lambda hex_id: Polygon(h3.h3_to_geo_boundary(hex_id, geo_json=True)),
)

hexs = geopandas.GeoDataFrame(hexids, geometry=polys, crs=4326).set_index("hex_id")
hexs = geopandas.GeoDataFrame(hexids, geometry=polys.values, crs=4326).set_index(
"hex_id"
)

return hexs
Loading