Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix antimeridian polygon winding order #278

Merged
merged 2 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Skip Click v8.1.0 as it broke decorator typing ([#266](https://github.com/stac-utils/stactools/pull/266))
- Use black (instead of yapf) for formatting ([#274](https://github.com/stac-utils/stactools/pull/274))

### Fixed

- Antimeridian winding order ([#278](https://github.com/stac-utils/stactools/pull/278))

## [0.3.0] - 2022-03-25

### Added
Expand Down
3 changes: 2 additions & 1 deletion src/stactools/core/utils/antimeridian.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def split(polygon: Polygon) -> Optional[MultiPolygon]:
return None
geoms = list()
for geom in split.geoms:
geom = shapely.geometry.polygon.orient(geom)
bounds = geom.bounds
if bounds[0] < -180:
geoms.append(shapely.affinity.translate(geom, xoff=360))
Expand Down Expand Up @@ -129,7 +130,7 @@ def normalize(polygon: Polygon) -> Optional[Polygon]:
coords[index + 1] = (end[0] - math.copysign(360, delta), end[1])
if not has_changes:
return None
polygon = Polygon(coords)
polygon = shapely.geometry.polygon.orient(Polygon(coords))
centroid = polygon.centroid
if centroid.x > 180:
return shapely.affinity.translate(polygon, xoff=-360)
Expand Down
33 changes: 29 additions & 4 deletions tests/core/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def test_antimeridian_split() -> None:
)
)
for actual, expected in zip(split.geoms, expected.geoms):
assert actual.exterior.is_ccw
assert actual.equals(expected)

doesnt_cross = Polygon(((170, 40), (170, 50), (180, 50), (180, 40), (170, 40)))
Expand All @@ -69,6 +70,7 @@ def test_antimeridian_split() -> None:
)
)
for actual, expected in zip(split.geoms, expected.geoms):
assert actual.exterior.is_ccw
assert actual.equals(expected), f"actual={actual}, expected={expected}"


Expand All @@ -80,20 +82,38 @@ def test_antimeridian_split_complicated() -> None:
assert split
expected = MultiPolygon(
(
Polygon(((170, 40), (170, 45), (180, 42.5), (180, 40), (170, 40))),
Polygon(((170, 45), (170, 50), (180, 50), (180, 47.5), (170, 45))),
Polygon(((-180, 50), (-170, 50), (-180, 47.5), (-180, 50))),
Polygon(((-180, 42.5), (-170, 40), (-180, 40), (-180, 42.5))),
Polygon(
[
(180.0, 40.0),
(180.0, 42.5),
(170.0, 45.0),
(170.0, 40.0),
(180.0, 40.0),
]
),
Polygon([(-180.0, 42.5), (-180.0, 40.0), (-170.0, 40.0), (-180.0, 42.5)]),
Polygon(
[
(180.0, 47.5),
(180.0, 50.0),
(170.0, 50.0),
(170.0, 45.0),
(180.0, 47.5),
]
),
Polygon([(-180.0, 50.0), (-180.0, 47.5), (-170.0, 50.0), (-180.0, 50.0)]),
)
)
for actual, expected in zip(split.geoms, expected.geoms):
assert actual.exterior.is_ccw
assert actual.equals(expected), f"actual={actual}, expected={expected}"


def test_antimeridian_normalize() -> None:
canonical = Polygon(((170, 40), (170, 50), (-170, 50), (-170, 40), (170, 40)))
normalized = antimeridian.normalize(canonical)
assert normalized
assert normalized.exterior.is_ccw
expected = shapely.geometry.box(170, 40, 190, 50)
assert normalized.equals(expected), f"actual={normalized}, expected={expected}"

Expand All @@ -102,6 +122,7 @@ def test_antimeridian_normalize() -> None:
)
normalized = antimeridian.normalize(canonical_other_way)
assert normalized
assert normalized.exterior.is_ccw
expected = shapely.geometry.box(-170, 40, -190, 50)
assert normalized.equals(expected), f"actual={normalized}, expected={expected}"

Expand All @@ -110,6 +131,8 @@ def test_antimeridian_normalize_westerly() -> None:
westerly = Polygon(((170, 40), (170, 50), (-140, 50), (-140, 40), (170, 40)))
normalized = antimeridian.normalize(westerly)
assert normalized
assert normalized.exterior.is_ccw
expected = shapely.geometry.box(-170, 40, -190, 50)
expected = shapely.geometry.box(-190, 40, -140, 50)
assert normalized.equals(expected), f"actual={normalized}, expected={expected}"

Expand All @@ -118,6 +141,8 @@ def test_antimeridian_normalize_easterly() -> None:
easterly = Polygon(((-170, 40), (140, 40), (140, 50), (-170, 50), (-170, 40)))
normalized = antimeridian.normalize(easterly)
assert normalized
assert normalized.exterior.is_ccw
expected = shapely.geometry.box(-170, 40, -190, 50)
expected = shapely.geometry.box(140, 40, 190, 50)
assert normalized.equals(expected), f"actual={normalized}, expected={expected}"

Expand Down