Skip to content

Commit

Permalink
Ensure backward compatibility for shapely < 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
adehecq committed Sep 28, 2023
1 parent 47dfe3f commit d253636
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
11 changes: 6 additions & 5 deletions geoutils/projtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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 = []
Expand All @@ -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

Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions tests/test_projtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)))

Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit d253636

Please sign in to comment.