Skip to content

Commit

Permalink
Make transformation exceptions more comprehensible (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
caspervdw committed Aug 26, 2020
1 parent 0f5a136 commit 891c818
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Changelog of dask-geomodeling
2.2.11 (unreleased)
-------------------

- Make transformation exceptions more comprehensible.

- Check for matching time resolutions in raster.Clip.

- Added 'product' to raster.reduction STATISTICS.
Expand Down
7 changes: 7 additions & 0 deletions dask_geomodeling/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@ def test_shapely_transform(self):
box4326 = utils.shapely_transform(box28992, src_srs=src_srs, dst_srs=dst_srs)
assert_almost_equal((5.4, 52.0), list(box4326.centroid.coords)[0], decimal=1)

def test_shapely_transform_invalid(self):
src_srs = "EPSG:4326"
dst_srs = "EPSG:28992"
box4326 = geometry.box(100000, 400000, 200000, 500000)
with pytest.raises(utils.TransformException):
utils.shapely_transform(box4326, src_srs=src_srs, dst_srs=dst_srs)

@mock.patch("dask_geomodeling.utils.shapely_transform")
def test_min_size_transform(self, shapely_transform):
min_size = 100
Expand Down
27 changes: 24 additions & 3 deletions dask_geomodeling/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,12 @@ def crs_to_srs(crs):
return crs.to_string()


class TransformException(Exception):
"""Exception used for errors occuring while transforming between spatial
reference systems"""
pass


def wkb_transform(wkb, src_sr, dst_sr):
"""
Return a shapely geometry transformed from src_sr to dst_sr.
Expand All @@ -434,9 +440,24 @@ def shapely_transform(geometry, src_srs, dst_srs):
Note that we do not use geopandas for the transformation, because this is
much slower than OGR.
"""
return shapely_wkb.loads(
wkb_transform(geometry.wkb, src_sr=get_sr(src_srs), dst_sr=get_sr(dst_srs))
)
transform_kwargs = {
"wkb": geometry.wkb,
"src_sr": get_sr(src_srs),
"dst_sr": get_sr(dst_srs),
}
try:
output_wkb = wkb_transform(**transform_kwargs)
except RuntimeError:
# include the geometry in the error message, truncated to 64 chars
wkt = geometry.wkt
if len(wkt) > 64:
wkt = wkt[:61] + "..."
raise TransformException(
"An error occured while transforming {} from {} to {}.".format(
wkt, src_srs, dst_srs
)
)
return shapely_wkb.loads(output_wkb)


def geoseries_transform(df, src_srs, dst_srs):
Expand Down

0 comments on commit 891c818

Please sign in to comment.