Skip to content
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
12 changes: 11 additions & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@ jobs:
with:
flake8_version: 6.0.0
max_line_length: 120
plugins: flake8-isort==6.1.2 flake8-quotes==3.4.0 flake8-commas==4.0.0
plugins: flake8-isort==6.1.2 flake8-quotes==3.4.0

ruff-format:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: astral-sh/ruff-action@v3
with:
version: "~=0.13.3"
args: format --check --diff --output-format=github

mypy:
runs-on: ubuntu-latest
Expand Down
23 changes: 15 additions & 8 deletions geo_extensions/transformations/cartesian.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,26 @@ def split(polygon: Polygon) -> TransformationResult:
def _shift_polygon(polygon: Polygon) -> Polygon:
"""Shift into [0, 360) range."""

return Polygon([
((360.0 + lon) % 360, lat)
for lon, lat in polygon.exterior.coords
])
return Polygon(
[
# ruff hint
((360.0 + lon) % 360, lat)
for lon, lat in polygon.exterior.coords
]
)


def _shift_polygon_back(polygon: Polygon) -> Polygon:
"""Shift back to [-180, 180] range."""

_, _, max_lon, _ = polygon.bounds
return Polygon([
(_adjust_lon(lon, max_lon), lat)
for lon, lat in polygon.exterior.coords
])
return Polygon(
[
# ruff hint
(_adjust_lon(lon, max_lon), lat)
for lon, lat in polygon.exterior.coords
]
)


def _adjust_lon(lon: float, max_lon: float) -> float:
Expand All @@ -136,6 +142,7 @@ def _split_polygon(
split_collection = shapely.ops.split(polygon, line)

return [
# ruff hint
orient(geom)
for geom in split_collection.geoms
if isinstance(geom, Polygon) and not _ignore_polygon(geom)
Expand Down
1 change: 1 addition & 0 deletions geo_extensions/transformations/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def drop_z_coordinate(polygon: Polygon) -> TransformationResult:
yield Polygon(
shell=((x, y) for x, y, *_ in polygon.exterior.coords),
holes=[
# ruff hint
((x, y) for x, y, *_ in interior.coords)
for interior in polygon.interiors
],
Expand Down
1 change: 1 addition & 0 deletions geo_extensions/transformations/geodetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def densify(polygon: Polygon) -> TransformationResult:
yield Polygon(
shell=_densify_ring(polygon.exterior.coords, tolerance_meters),
holes=[
# ruff hint
_densify_ring(interior.coords, tolerance_meters)
for interior in polygon.interiors
],
Expand Down
7 changes: 7 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,10 @@ types-shapely = "^2.0.3"

[tool.isort]
profile = "black"

[tool.ruff]
line-length = 120

[tool.ruff.lint]
# Add the `line-too-long` rule to the enforced rule set.
extend-select = ["E501"]
53 changes: 37 additions & 16 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ def data_path():
@pytest.fixture
def rectangle():
"""A rectanglular polygon"""
polygon = Polygon([
(160., 60.), (170., 60.),
(170., 70.), (160., 70.), (160., 60.),
])
polygon = Polygon(
[
(160.0, 60.0),
(170.0, 60.0),
(170.0, 70.0),
(160.0, 70.0),
(160.0, 60.0),
]
)
assert polygon.exterior.is_ccw
assert polygon.exterior.is_valid

Expand All @@ -25,10 +30,15 @@ def rectangle():
@pytest.fixture
def centered_rectangle():
"""A rectanglular polygon centered at 0, 0"""
polygon = Polygon([
(-30., 10.), (-30., -10.),
(30., -10.), (30., 10.), (-30., 10.),
])
polygon = Polygon(
[
(-30.0, 10.0),
(-30.0, -10.0),
(30.0, -10.0),
(30.0, 10.0),
(-30.0, 10.0),
]
)
assert polygon.exterior.is_ccw
assert polygon.exterior.is_valid

Expand All @@ -38,10 +48,15 @@ def centered_rectangle():
@pytest.fixture
def antimeridian_centered_rectangle():
"""A rectanglular polygon centered over the antimeridian"""
polygon = Polygon([
(150., 10.), (150., -10.),
(-150., -10.), (-150., 10.), (150., 10.),
])
polygon = Polygon(
[
(150.0, 10.0),
(150.0, -10.0),
(-150.0, -10.0),
(-150.0, 10.0),
(150.0, 10.0),
]
)
assert not polygon.exterior.is_ccw
assert polygon.exterior.is_valid

Expand All @@ -57,10 +72,16 @@ def multi_crossing_polygon():
| \
--------
"""
polygon = Polygon([
(150., -10.), (-150., -10.), (160., 0.),
(-150., 10.), (150., 10.), (150., -10.),
])
polygon = Polygon(
[
(150.0, -10.0),
(-150.0, -10.0),
(160.0, 0.0),
(-150.0, 10.0),
(150.0, 10.0),
(150.0, -10.0),
]
)
assert not polygon.exterior.is_ccw
assert not polygon.exterior.is_valid

Expand Down
72 changes: 50 additions & 22 deletions tests/test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@ def test_polygon_crosses_antimeridian_ccw_tricky():
/ \
--------
"""
polygon = Polygon([
(-30., 10.), (-10., 0.), (-30., -10.),
(30., -10.), (10., 0.), (30., 10.), (-30., 10.),
])
polygon = Polygon(
[
(-30.0, 10.0),
(-10.0, 0.0),
(-30.0, -10.0),
(30.0, -10.0),
(10.0, 0.0),
(30.0, 10.0),
(-30.0, 10.0),
]
)
assert polygon_crosses_antimeridian_ccw(polygon) is False


Expand All @@ -31,10 +38,17 @@ def test_polygon_crosses_antimeridian_ccw_tricky_crosses():
/ \
--------
"""
polygon = Polygon([
(150., 10.), (170., 0.), (150., -10.),
(-150., 10.), (-170., 0.), (-150., -10.), (150., 10.),
])
polygon = Polygon(
[
(150.0, 10.0),
(170.0, 0.0),
(150.0, -10.0),
(-150.0, 10.0),
(-170.0, 0.0),
(-150.0, -10.0),
(150.0, 10.0),
]
)
assert polygon_crosses_antimeridian_ccw(polygon) is True


Expand All @@ -50,12 +64,14 @@ def test_polygon_crosses_antimeridian_ccw_alos2_antarctica():

ALOS2 granule: ALOS2075945400-151019-WBDR1.1__D
"""
polygon = Polygon([
(-178.328, -79.438),
(179.625, -76.163),
(166.084, -76.163),
(164.037, -79.438),
])
polygon = Polygon(
[
(-178.328, -79.438),
(179.625, -76.163),
(166.084, -76.163),
(164.037, -79.438),
]
)

assert polygon_crosses_antimeridian_ccw(polygon) is True

Expand All @@ -72,19 +88,31 @@ def test_polygon_crosses_antimeridian_fixed_size_tricky_crosses():
/ \
--------
"""
polygon = Polygon([
(150., 10.), (170., 0.), (150., -10.),
(-150., 10.), (-170., 0.), (-150., -10.), (150., 10.),
])
polygon = Polygon(
[
(150.0, 10.0),
(170.0, 0.0),
(150.0, -10.0),
(-150.0, 10.0),
(-170.0, 0.0),
(-150.0, -10.0),
(150.0, 10.0),
]
)
assert polygon_crosses_antimeridian_fixed_size(polygon, 30) is True


def test_polygon_crosses_antimeridian_fixed_size_antarctica():
r"""A real polygon from ALOS2 granule ALOS2014555550-140830 which is located
close to the south pole, and also crosses the antimeridian
"""
polygon = Polygon([
(-164.198, -82.125), (172.437, -83.885), (165.618, -80.869),
(-176.331, -79.578), (-164.198, -82.125),
])
polygon = Polygon(
[
(-164.198, -82.125),
(172.437, -83.885),
(165.618, -80.869),
(-176.331, -79.578),
(-164.198, -82.125),
]
)
assert polygon_crosses_antimeridian_fixed_size(polygon, 40) is True
26 changes: 15 additions & 11 deletions tests/test_geo_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@
def test_default_transformer(data_path):
wkt_path = data_path / "OPERA_L2_RTC-S1_T114-243299-IW1_20230722T122534Z_20230818T184635Z_S1A_30_v0.4.wkt"

transformer = Transformer([
split_polygon_on_antimeridian_ccw,
simplify_polygon(0.1),
])
transformer = Transformer(
[
split_polygon_on_antimeridian_ccw,
simplify_polygon(0.1),
]
)

polygons = transformer.from_wkt(wkt_path.read_text())
assert polygons == [
Polygon([
(-64.18242781701073, 80.92318071697005),
(-68.40445755029818, 81.31609026531473),
(-68.93161319601728, 81.16999707795055),
(-64.74688665108201, 80.78217738556877),
(-64.18242781701073, 80.92318071697005),
]),
Polygon(
[
(-64.18242781701073, 80.92318071697005),
(-68.40445755029818, 81.31609026531473),
(-68.93161319601728, 81.16999707795055),
(-64.74688665108201, 80.78217738556877),
(-64.18242781701073, 80.92318071697005),
]
),
]
Loading