Skip to content

Commit

Permalink
Backward-compatiblity with Shapely < 2.0 (#392)
Browse files Browse the repository at this point in the history
* Use BaseGeometry to be backward-compatible

* Linting
  • Loading branch information
rhugonnet authored Aug 16, 2023
1 parent b32839a commit 3b633d0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
11 changes: 6 additions & 5 deletions geoutils/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from rasterio import features, warp
from rasterio.crs import CRS
from scipy.spatial import Voronoi
from shapely.geometry.base import BaseGeometry
from shapely.geometry.polygon import Polygon

import geoutils as gu
Expand Down Expand Up @@ -67,7 +68,7 @@ class Vector:
See the API for more details.
"""

def __init__(self, filename_or_dataset: str | pathlib.Path | gpd.GeoDataFrame | gpd.GeoSeries | shapely.Geometry):
def __init__(self, filename_or_dataset: str | pathlib.Path | gpd.GeoDataFrame | gpd.GeoSeries | BaseGeometry):
"""
Instantiate a vector from either a filename, a GeoPandas dataframe or series, or a Shapely geometry.
Expand All @@ -83,7 +84,7 @@ def __init__(self, filename_or_dataset: str | pathlib.Path | gpd.GeoDataFrame |
self._ds = ds
self._name: str | gpd.GeoDataFrame | None = filename_or_dataset
# If GeoPandas or Shapely object is passed
elif isinstance(filename_or_dataset, (gpd.GeoDataFrame, gpd.GeoSeries, shapely.Geometry)):
elif isinstance(filename_or_dataset, (gpd.GeoDataFrame, gpd.GeoSeries, BaseGeometry)):
self._name = None
if isinstance(filename_or_dataset, gpd.GeoDataFrame):
self._ds = filename_or_dataset
Expand Down Expand Up @@ -300,12 +301,12 @@ def save(
############################################################################

def _override_gdf_output(
self, other: gpd.GeoDataFrame | gpd.GeoSeries | shapely.Geometry | pd.Series | Any
self, other: gpd.GeoDataFrame | gpd.GeoSeries | BaseGeometry | pd.Series | Any
) -> Vector | pd.Series:
"""Parse outputs of GeoPandas functions to facilitate object manipulation."""

# Raise error if output is not treated separately, should appear in tests
if not isinstance(other, (gpd.GeoDataFrame, gpd.GeoDataFrame, pd.Series, shapely.Geometry)):
if not isinstance(other, (gpd.GeoDataFrame, gpd.GeoDataFrame, pd.Series, BaseGeometry)):
raise ValueError("Not implemented. This error should only be raised in tests.")

# If a GeoDataFrame is the output, return it
Expand All @@ -315,7 +316,7 @@ def _override_gdf_output(
elif isinstance(other, gpd.GeoSeries):
return Vector(gpd.GeoDataFrame(geometry=other))
# If a Shapely Geometry is the output, re-encapsulate in a GeoDataFrame and return it
elif isinstance(other, shapely.Geometry):
elif isinstance(other, BaseGeometry):
return Vector(gpd.GeoDataFrame({"geometry": [other]}, crs=self.crs))
# If a Pandas Series is the output, append it to that of the GeoDataFrame
else:
Expand Down
6 changes: 3 additions & 3 deletions tests/test_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
import numpy as np
import pyproj
import pytest
import shapely
from geopandas.testing import assert_geodataframe_equal, assert_geoseries_equal
from pandas.testing import assert_series_equal
from scipy.ndimage import binary_erosion
from shapely.geometry.base import BaseGeometry
from shapely.geometry.linestring import LineString
from shapely.geometry.multilinestring import MultiLineString
from shapely.geometry.multipolygon import MultiPolygon
Expand Down Expand Up @@ -787,13 +787,13 @@ def test_geo_properties(self, vector: gu.Vector, method: str) -> None:

# Assert output types
assert isinstance(output_geoutils, gu.Vector)
assert isinstance(output_geopandas, (gpd.GeoSeries, gpd.GeoDataFrame, shapely.Geometry))
assert isinstance(output_geopandas, (gpd.GeoSeries, gpd.GeoDataFrame, BaseGeometry))

# Separate cases depending on GeoPandas' output
if isinstance(output_geopandas, gpd.GeoSeries):
# Assert geoseries equality
assert_geoseries_equal(output_geoutils.ds.geometry, output_geopandas)
elif isinstance(output_geopandas, shapely.Geometry):
elif isinstance(output_geopandas, BaseGeometry):
assert_geodataframe_equal(
output_geoutils.ds, gpd.GeoDataFrame({"geometry": [output_geopandas]}, crs=vector.crs)
)
Expand Down

0 comments on commit 3b633d0

Please sign in to comment.