Skip to content

Commit

Permalink
fix: protect against tiny floating point deltas
Browse files Browse the repository at this point in the history
  • Loading branch information
gadomski committed Jun 30, 2023
1 parent 784fbd7 commit b3b14ca
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Fixed

- Don't produce meaningless splits when one corner of an input polygon is on the antimeridian ([#49](https://github.com/gadomski/antimeridian/pull/49))
- Correct tiny floating point deltas on the antimeridian ([#52](https://github.com/gadomski/antimeridian/pull/52))

## [0.3.0] - 2023-06-29

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

Fix shapes that cross the antimeridian.
See [the documentation](https://antimeridian.readthedocs.io) for information about the underlying algorithm.
Depends on [shapely](https://shapely.readthedocs.io).
Depends on [shapely](https://shapely.readthedocs.io) and [numpy](https://numpy.org/).

Can fix:

Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ classifiers = [
"Development Status :: 4 - Beta",
]
dependencies = [
"shapely>=2.0"
"numpy>=1.16",
"shapely>=2.0",
]

[project.urls]
Expand Down
10 changes: 8 additions & 2 deletions src/antimeridian/_implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import warnings
from typing import Any, Dict, List, Optional, Protocol, Tuple, Union, cast

import numpy
import shapely
import shapely.geometry
from shapely.geometry import (
Expand Down Expand Up @@ -426,8 +427,13 @@ def segment(coords: List[Point]) -> List[List[Point]]:
segment = []
segments = []
for i, point in enumerate(coords):
# Ensure all longitudes are between -180 and 180
if point[0] != 180:
# Ensure all longitudes are between -180 and 180, and that tiny floating
# point differences are ignored.
if numpy.isclose(point[0], 180):
coords[i] = (180, point[1])
elif numpy.isclose(point[0], -180):
coords[i] = (-180, point[1])
else:
coords[i] = (((point[0] + 180) % 360) - 180, point[1])
for start, end in zip(coords, coords[1:]):
segment.append(start)
Expand Down
27 changes: 27 additions & 0 deletions tests/data/input/almost-180.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"type": "Polygon",
"coordinates": [
[
[
-176.78115373,
-18.58572933
],
[
-173.71026732,
-14.73835555
],
[
-176.88248512,
-12.82925277
],
[
179.99999999999997,
-15.7250416
],
[
-176.78115373,
-18.58572933
]
]
]
}
27 changes: 27 additions & 0 deletions tests/data/output/almost-180.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"type": "Polygon",
"coordinates": [
[
[
-180,
-15.7250416
],
[
-176.78115373,
-18.58572933
],
[
-173.71026732,
-14.73835555
],
[
-176.88248512,
-12.82925277
],
[
-180,
-15.7250416
]
]
]
}
1 change: 1 addition & 0 deletions tests/test_polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@pytest.mark.parametrize(
("name"),
[
"almost-180",
"complex-split",
"crossing-latitude",
"extra-crossing",
Expand Down

0 comments on commit b3b14ca

Please sign in to comment.