Skip to content

Commit

Permalink
fix: Orient exterior rings counter-clockwise TDE-1205
Browse files Browse the repository at this point in the history
[GeoJSON requires this](https://datatracker.ietf.org/doc/html/rfc7946#section-3.1.6):

> A linear ring MUST follow the right-hand rule with respect to the area
> it bounds, i.e., exterior rings are counterclockwise, and holes are
> clockwise.

However,
[Shapely does not do this by default](shapely/shapely#622).
  • Loading branch information
l0b0 committed Jun 14, 2024
1 parent eab8596 commit 8f7060d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
5 changes: 3 additions & 2 deletions scripts/stac/imagery/capture_area.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
from typing import Any, Sequence
from collections.abc import Sequence
from typing import Any

from linz_logger import get_log
from shapely import BufferCapStyle, BufferJoinStyle, to_geojson, union_all
Expand Down Expand Up @@ -75,7 +76,7 @@ def merge_polygons(polygons: Sequence[BaseGeometry], buffer_distance: float) ->
union_buffered = union_all(buffered_polygons)
# Negative buffer back in the polygons
union_unbuffered = union_buffered.buffer(-buffer_distance, cap_style=BufferCapStyle.flat, join_style=BufferJoinStyle.mitre)
union_simplified = union_unbuffered.simplify(buffer_distance)
union_simplified = union_unbuffered.simplify(buffer_distance).reverse()

return union_simplified

Expand Down
4 changes: 2 additions & 2 deletions scripts/stac/imagery/tests/capture_area_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_merge_polygons() -> None:
polygons = []
polygons.append(Polygon([(0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0), (0.0, 1.0)]))
polygons.append(Polygon([(1.0, 1.0), (2.0, 1.0), (2.0, 0.0), (1.0, 0.0), (1.0, 1.0)]))
expected_merged_polygon = Polygon([(0.0, 1.0), (2.0, 1.0), (2.0, 0.0), (0.0, 0.0), (0.0, 1.0)])
expected_merged_polygon = Polygon([(0.0, 1.0), (0.0, 0.0), (2.0, 0.0), (2.0, 1.0), (0.0, 1.0)])
merged_polygons = merge_polygons(polygons, 0)

print(f"Polygon A: {to_feature(polygons[0])}")
Expand All @@ -20,7 +20,7 @@ def test_merge_polygons() -> None:

# Using `Polygon.equals()` as merge_polygons might return a different set of coordinates for the same geometry
# In this example: `Polygon([(2.0, 1.0), (2.0, 0.0), (0.0, 0.0), (0.0, 1.0), (2.0, 1.0)])`
assert merged_polygons.equals(expected_merged_polygon)
assert merged_polygons.equals_exact(expected_merged_polygon, 0.0)


def test_merge_polygons_with_rounding() -> None:
Expand Down
4 changes: 2 additions & 2 deletions scripts/stac/imagery/tests/collection_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,8 @@ def test_capture_area_added(metadata: CollectionMetadata, subtests: SubTests) ->

with subtests.test():
assert collection.stac["assets"]["capture_area"]["file:checksum"] in (
"1220b15694be7495af38e0f70af67cfdc4f19b8bc415a2eb77d780e7a32c6e5b42c2", # geos 3.11
"122040fc8700d5d2d04600f730e10677b19d33f3b1e43b02c7867f4cfc2101930863", # geos 3.12
"1220369cd5d4179f5f68ca0fd9be70b9f66033fcc6bb2f3305c0ad977adc79d7ad53", # geos 3.11
"122060feab333d28f33f165cee2d3db31a71ba9fee40d163e922dad09581a50c19e6", # geos 3.12
)

with subtests.test():
Expand Down

0 comments on commit 8f7060d

Please sign in to comment.