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: handle utm west and east antimeridian #154

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
3 changes: 1 addition & 2 deletions morecantile/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1250,9 +1250,8 @@ def tiles( # noqa: C901

for w, s, e, n in bboxes:
# Clamp bounding values.
ws_contain_180th = lons_contain_antimeridian(w, self.bbox.left)
es_contain_180th = lons_contain_antimeridian(e, self.bbox.right)
w = min(self.bbox.left, w) if ws_contain_180th else max(self.bbox.left, w)
w = max(self.bbox.left, w)
s = max(self.bbox.bottom, s)
e = max(self.bbox.right, e) if es_contain_180th else min(self.bbox.right, e)
n = min(self.bbox.top, n)
Expand Down
30 changes: 25 additions & 5 deletions tests/test_morecantile.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,29 @@ def test_tiles():
assert len(list(tms.tiles(*bounds, zooms=[2]))) == 2


def test_tiles_when_tms_bounds_and_provided_bounds_cross_antimeridian():
bounds = (119.1, -32.86, 119.2, -32.82)
utm = CRS.from_epsg(32750)
@pytest.mark.parametrize(
("bounds", "expected", "crs", "tms_bbox"),
[
# case where east tms bbox crosses antimeridian
(
(119.1, -32.86, 119.2, -32.82),
6,
32750,
(100.23646734667152, -79.99407435445299, -158.6052850376368, 0.0),
),
# case where west tms bbox crosses antimeridian
(
(11.700978, 52.056474, 11.711114, 52.062706),
4,
32632,
(-17.582877658817317, 0.0, 95.87417095917766, 83.95429547980198),
),
],
)
def test_tiles_when_tms_bounds_and_provided_bounds_cross_antimeridian(
bounds: tuple, expected: int, crs: int, tms_bbox: tuple
):
utm = CRS.from_epsg(crs)
rs_extent = utm.area_of_use.bounds
tms = morecantile.TileMatrixSet.custom(
crs=utm, extent_crs=CRS.from_epsg(4326), extent=list(rs_extent)
Expand All @@ -407,8 +427,8 @@ def test_tiles_when_tms_bounds_and_provided_bounds_cross_antimeridian():
# antimeridian e.g. min(119.2, -158.605) clamps to much larger area. Now
# that we check to see if lons contain antimeridian, we build tiles that
# actually overlap the provided bounds to tiles.
assert tms.bbox == (100.23646734667152, -79.99407435445299, -158.6052850376368, 0.0)
assert len(list(tms.tiles(*bounds, zooms=4))) == 1
assert tms.bbox == tms_bbox
assert len(list(tms.tiles(*bounds, zooms=11))) == expected


def test_tiles_for_tms_with_non_standard_row_col_order():
Expand Down
Loading