Skip to content

Commit

Permalink
catch geos exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
cnheider committed Aug 15, 2024
1 parent 2d1bd50 commit 38f0d9f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
8 changes: 7 additions & 1 deletion jord/shapely_utilities/base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from typing import Any, Mapping, Optional

import shapely
Expand All @@ -16,6 +17,8 @@
"clean_shape",
]

logger = logging.getLogger(__name__)


def zero_buffer(
geom: BaseGeometry,
Expand All @@ -36,7 +39,10 @@ def clean_shape(
shape = zero_buffer(shape).simplify(0)

if not shape.is_valid:
shape = make_valid(shape)
try:
shape = make_valid(shape)
except shapely.errors.GEOSException as e:
logger.error(e)

return shape

Expand Down
18 changes: 18 additions & 0 deletions tests/shapely/base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
import shapely

from jord.shapely_utilities.base import clean_shape, sanitise, zero_buffer
Expand Down Expand Up @@ -71,3 +72,20 @@ def test_clean_invalid_shape():

assert not p.is_valid
assert clean_shape(p).is_valid


@pytest.mark.parametrize(
"p",
[
shapely.Point(),
shapely.LineString(),
shapely.Polygon(),
shapely.MultiPolygon(),
shapely.MultiPoint(),
shapely.MultiLineString(),
shapely.GeometryCollection(),
],
)
def test_clean_empty_shape(p: shapely.geometry.base.BaseGeometry):
assert p.is_empty
assert clean_shape(p).is_empty

0 comments on commit 38f0d9f

Please sign in to comment.