Skip to content

Commit

Permalink
Shapely 2.0 compat (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
caspervdw committed Aug 22, 2022
1 parent 956a250 commit f0791fa
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
9 changes: 3 additions & 6 deletions dask_geomodeling/geometry/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
from dask_geomodeling import utils
from .base import GeometryBlock

from shapely.errors import WKTReadingError
from shapely.wkt import loads as load_wkt


# this import is a copy from geopandas.io.files

Expand Down Expand Up @@ -175,8 +172,8 @@ def __init__(self, wkt, projection):
if not isinstance(projection, str):
raise TypeError("'{}' object is not allowed".format(type(projection)))
try:
load_wkt(wkt)
except WKTReadingError:
utils.shapely_from_wkt(wkt)
except utils.WKTReadingError:
raise ValueError("The provided geometry is not a valid WKT")
try:
utils.get_sr(projection)
Expand Down Expand Up @@ -208,7 +205,7 @@ def process(data, request):
raise ValueError("Unknown mode '{}'".format(mode))

# load the geometry and transform it into the requested projection
geometry = load_wkt(data["wkt"])
geometry = utils.shapely_from_wkt(data["wkt"])
if data["projection"] != request["projection"]:
geometry = utils.shapely_transform(
geometry, data["projection"], request["projection"]
Expand Down
10 changes: 4 additions & 6 deletions dask_geomodeling/raster/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

from shapely.geometry import box
from shapely.geometry import Point
from shapely.errors import WKTReadingError
from shapely.wkt import loads as load_wkt

from dask import config
from dask_geomodeling.geometry import GeometryBlock
Expand Down Expand Up @@ -708,8 +706,8 @@ def __init__(self, wkt, projection):
if not isinstance(projection, str):
raise TypeError("'{}' object is not allowed".format(type(projection)))
try:
load_wkt(wkt)
except WKTReadingError:
utils.shapely_from_wkt(wkt)
except utils.WKTReadingError:
raise ValueError("The provided geometry is not a valid WKT")
try:
utils.get_sr(projection)
Expand Down Expand Up @@ -741,7 +739,7 @@ def period(self):
def extent(self):
return tuple(
utils.shapely_transform(
load_wkt(self.wkt), self.projection, "EPSG:4326"
utils.shapely_from_wkt(self.wkt), self.projection, "EPSG:4326"
).bounds
)

Expand Down Expand Up @@ -778,7 +776,7 @@ def process(data, request):
elif mode == "meta":
return {"meta": [None]}
# load the geometry and transform it into the requested projection
geometry = load_wkt(data["wkt"])
geometry = utils.shapely_from_wkt(data["wkt"])
if data["projection"] != request["projection"]:
geometry = utils.shapely_transform(
geometry, data["projection"], request["projection"]
Expand Down
23 changes: 21 additions & 2 deletions dask_geomodeling/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@

from osgeo import gdal, ogr, osr, gdal_array
from shapely.geometry import box, Point
from shapely import wkb as shapely_wkb

try: # shapely 2.*
from shapely import from_wkt, from_wkb, GEOSException
except ImportError: # shapely 1.*
from shapely.errors import ShapelyError as GEOSException
from shapely.wkt import loads as from_wkt
from shapely.wkb import loads as from_wkb


from pyproj import CRS

import fiona
Expand Down Expand Up @@ -437,9 +445,20 @@ def shapely_transform(geometry, src_srs, dst_srs):
wkt, src_srs, dst_srs
)
)
return shapely_wkb.loads(output_wkb)
return from_wkb(output_wkb)


def shapely_from_wkt(wkt):
"""Return a shapely geometry from a WKT representation."""
try:
return from_wkt(wkt)
except GEOSException as e:
raise WKTReadingError(str(e))


class WKTReadingError(Exception):
pass

def geoseries_transform(df, src_srs, dst_srs):
"""
Transform a GeoSeries to a different SRS. Returns a copy.
Expand Down

0 comments on commit f0791fa

Please sign in to comment.