From d25363675d39892c3d5eab425cb9366ec757d371 Mon Sep 17 00:00:00 2001 From: adehecq <3285905+adehecq@users.noreply.github.com> Date: Thu, 28 Sep 2023 16:00:31 +0200 Subject: [PATCH] Ensure backward compatibility for shapely < 2.0 --- geoutils/projtools.py | 11 ++++++----- tests/test_projtools.py | 10 +++++----- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/geoutils/projtools.py b/geoutils/projtools.py index bc73addd..840c07e9 100644 --- a/geoutils/projtools.py +++ b/geoutils/projtools.py @@ -13,6 +13,7 @@ import pyproj import rasterio as rio import shapely.ops +import shapely.geometry from rasterio.crs import CRS from shapely.geometry.base import BaseGeometry from shapely.geometry.polygon import Polygon @@ -351,7 +352,7 @@ def _get_bounds_projected( return new_bounds -def _densify_geometry(line_geometry: shapely.LineString, densify_pts: int = 5000) -> shapely.LineString: +def _densify_geometry(line_geometry: shapely.geometry.LineString, densify_pts: int = 5000) -> shapely.geometry.LineString: """ Densify a linestring geometry. @@ -364,7 +365,7 @@ def _densify_geometry(line_geometry: shapely.LineString, densify_pts: int = 5000 """ # Get the segments (list of linestrings) - segments = list(map(shapely.LineString, zip(line_geometry.coords[:-1], line_geometry.coords[1:]))) + segments = list(map(shapely.geometry.LineString, zip(line_geometry.coords[:-1], line_geometry.coords[1:]))) # To store new coordinate tuples xy = [] @@ -390,7 +391,7 @@ def _densify_geometry(line_geometry: shapely.LineString, densify_pts: int = 5000 xy.append((xp, yp)) # Recreate a new line with densified points - densified_line_geometry = shapely.LineString(xy) + densified_line_geometry = shapely.geometry.LineString(xy) return densified_line_geometry @@ -414,13 +415,13 @@ def _get_footprint_projected( left, bottom, right, top = bounds # Create linestring - linestring = shapely.LineString([[left, bottom], [left, top], [right, top], [right, bottom], [left, bottom]]) + linestring = shapely.geometry.LineString([[left, bottom], [left, top], [right, top], [right, bottom], [left, bottom]]) # Densify linestring densified_line_geometry = _densify_geometry(linestring, densify_pts=densify_pts) # Get polygon from new linestring - densified_poly = shapely.Polygon(densified_line_geometry) + densified_poly = Polygon(densified_line_geometry) # Reproject the polygon df = gpd.GeoDataFrame({"geometry": [densified_poly]}, crs=in_crs) diff --git a/tests/test_projtools.py b/tests/test_projtools.py index 9643abbd..4afa9bd7 100644 --- a/tests/test_projtools.py +++ b/tests/test_projtools.py @@ -7,7 +7,7 @@ import numpy as np import pyproj.exceptions import pytest -import shapely +from shapely.geometry import Point, Polygon import geoutils as gu import geoutils.projtools as pt @@ -108,8 +108,8 @@ def test_get_metric_crs_ups(self) -> None: """Check that the function works for UPS with points at high latitude.""" # Create a vector of a single point in geographic coordinates - point_north = shapely.Point([0, 84]) - point_south = shapely.Point([0, -84]) + point_north = Point([0, 84]) + point_south = Point([0, -84]) vect_north = gu.Vector(gpd.GeoDataFrame({"geometry": [point_north]}, crs=pyproj.CRS.from_epsg(4326))) vect_south = gu.Vector(gpd.GeoDataFrame({"geometry": [point_south]}, crs=pyproj.CRS.from_epsg(4326))) @@ -204,11 +204,11 @@ def test_get_footprint_projected(self, fn_raster_or_vector: str, out_crs: pyproj # Assert it is a vector containing a polygon geometry assert isinstance(footprint, gu.Vector) - assert isinstance(footprint.geometry[0], shapely.Polygon) + assert isinstance(footprint.geometry[0], Polygon) # Check that the original corner points were conserved left, bottom, right, top = rast_or_vect.bounds - corners = [shapely.Point([x, y]) for (x, y) in [(left, bottom), (left, top), (right, top), (right, bottom)]] + corners = [Point([x, y]) for (x, y) in [(left, bottom), (left, top), (right, top), (right, bottom)]] df = gpd.GeoDataFrame({"geometry": corners}, crs=rast_or_vect.crs) df_reproj = df.to_crs(crs=out_crs)