Skip to content

Commit db5ef4e

Browse files
authored
fix: add error if geometry is unfixable (#190)
* fix: add error if input polygon is unfixable * chore: update ruff alias
1 parent b2cdb83 commit db5ef4e

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ repos:
1515
- repo: https://github.com/charliermarsh/ruff-pre-commit
1616
rev: v0.14.6
1717
hooks:
18-
- id: ruff
18+
- id: ruff-check
1919
types_or: [python, pyi, jupyter]
2020
- id: ruff-format
2121
types_or: [python, pyi, jupyter]

src/antimeridian/_implementation.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,19 @@ def fix_polygon(
358358
if shapely.is_ccw(polygon.exterior):
359359
return polygon
360360
else:
361-
return Polygon(
361+
pole_covering_polygon = Polygon(
362362
[(-180, 90), (-180, -90), (180, -90), (180, 90)],
363363
[polygon.exterior.coords],
364364
)
365+
if shapely.is_valid(pole_covering_polygon):
366+
return pole_covering_polygon
367+
else:
368+
raise ValueError(
369+
"Fixed polygon is invalid, check your input polygon for validity. "
370+
"Reason your polygon is invalid: "
371+
+ shapely.is_valid_reason(polygon)
372+
)
373+
365374
else:
366375
return MultiPolygon(polygons)
367376

tests/data/input/issues-182.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"coordinates": [
3+
[
4+
[41.701686, 27.555683],
5+
[41.677091, 27.556499],
6+
[41.701686, 27.555683],
7+
[41.701676, 27.552681],
8+
[41.694555, 27.537487],
9+
[41.69456, 27.536652],
10+
[41.684332, 27.538833],
11+
[41.683195, 27.539543],
12+
[41.674979, 27.541187],
13+
[41.672853, 27.540324],
14+
[41.671549, 27.538989],
15+
[41.671067, 27.537625],
16+
[41.665152, 27.538274],
17+
[41.664217, 27.538102],
18+
[41.661835, 27.538401],
19+
[41.66299, 27.541284],
20+
[41.667426, 27.54781],
21+
[41.67182, 27.554058],
22+
[41.674274, 27.555888],
23+
[41.677091, 27.556499],
24+
[41.701686, 27.555683]
25+
]
26+
],
27+
"type": "Polygon"
28+
}

tests/test_polygon.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
"complex-split",
1919
"crossing-latitude",
2020
"extra-crossing",
21-
"issues-81",
2221
"issues-171",
22+
"issues-187",
23+
"issues-81",
2324
"latitude-band",
2425
"north-pole",
2526
"one-hole",
@@ -30,7 +31,6 @@
3031
"south-pole",
3132
"split",
3233
"two-holes",
33-
"issues-187",
3434
],
3535
)
3636
@pytest.mark.parametrize(
@@ -253,3 +253,10 @@ def test_great_circle(
253253
output = read_output("great-circle", subdirectory)
254254
fixed = antimeridian.fix_polygon(input, great_circle=great_circle)
255255
assert fixed.normalize() == output.normalize()
256+
257+
258+
def test_invalid_polygon(read_input: Reader) -> None:
259+
input = read_input("issues-182")
260+
with pytest.warns(FixWindingWarning):
261+
with pytest.raises(ValueError):
262+
antimeridian.fix_polygon(input)

0 commit comments

Comments
 (0)