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 180th meridian case for tms.tiles() #150

Merged
merged 3 commits into from
Jul 26, 2024
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
7 changes: 5 additions & 2 deletions morecantile/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
_parse_tile_arg,
bbox_to_feature,
check_quadkey_support,
lons_contain_antimeridian,
meters_per_unit,
point_in_bbox,
to_rasterio_crs,
Expand Down Expand Up @@ -1252,9 +1253,11 @@ def tiles( # noqa: C901

for w, s, e, n in bboxes:
# Clamp bounding values.
w = max(self.bbox.left, w)
ws_contain_180th = lons_contain_antimeridian(w, self.bbox.left)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we just do a tests to see if the w > e

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The specific case I am hoping to handle is when the tms.bbox lons contain the antimeridian, but also the bounds provided for tiles is on either side of the antimeridian or overlaps. This requires we swap the underlying operator (min to max or max to min on a w/e basis) in order to truly clamp and avoid generating many many tiles that don't intersect the provided bounds.

Here is an example:
Given a tms.bbox (110, 0, -158, 90) and bounds used to find tiles of (-179, 45, -178, 46), the final clamped box we want is the bounds itself since it is fully contained inside the tms.bbox i.e. (-179, 45, -178, 46).

  • Assuming you mean checking if w > e for bounds only: w > e evaluates to False, and we keep w = max(self.bbox.left, w) giving us a new w of 110, but we should have -179 so that won't work (hence the need to swap max to min).
  • Assuming you mean checking w > e for tms.bbox only: w > e evaluates to True, so we would swap max to min and get -179, which is what we want! However, consider the same tms.bbox, but new bounds that are completely west of the antimeridian (150, 45, 155, 46) and still contained inside tms.bbox, then applying the swapped max to min would give us 110, which is not what we want. We would actually need to keep max (not min) here to get the expected 150. That is the reason for needing to check if the anti-meridian is inside interval (tms.bbox.west, bounds.west) to swap the operator in order to truly clamp to intersection of the provided bounds and tms.bbox.

Sorry for all the words, happy to illustrate this better if the added test does not highlight this well enough.

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)
s = max(self.bbox.bottom, s)
e = min(self.bbox.right, e)
e = max(self.bbox.right, e) if es_contain_180th else min(self.bbox.right, e)
n = min(self.bbox.top, n)

for z in zooms:
Expand Down
24 changes: 24 additions & 0 deletions morecantile/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@ def _parse_tile_arg(*args) -> Tile:
)


def lons_contain_antimeridian(lon1: float, lon2: float) -> bool:
"""
Check if the antimeridian (180th meridian) is between two longitude points

Parameters
----------
lon1: float
The first longitude.
lon2: float
The second longitude

Returns
-------
A bool representing whether two longs contain the 180th meridian.
"""
lon1_clipped = max(-180.0, min(lon1, 180))
lon2_clipped = max(-180.0, min(lon2, 180))
lon1_converted = (lon1_clipped + 360) % 360
lon2_converted = (lon2_clipped + 360) % 360
ws = [lon1_converted, lon2_converted]
sorted(ws)
return ws[0] < 180 < ws[1]


def meters_per_unit(crs: CRS) -> float:
"""
Coefficient to convert the coordinate reference system (CRS)
Expand Down
15 changes: 15 additions & 0 deletions tests/test_morecantile.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,21 @@ 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)
rs_extent = utm.area_of_use.bounds
tms = morecantile.TileMatrixSet.custom(
crs=utm, extent_crs=CRS.from_epsg(4326), extent=list(rs_extent)
)
# tms.tiles needs to be aware if tms bounds and input bounds crosses the
# 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


def test_tiles_for_tms_with_non_standard_row_col_order():
"""Test tiles from bbox when TMS has non-standard row/col alignment with lat/lon."""
crs = CRS.from_proj4(
Expand Down
7 changes: 7 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,10 @@
def test_mpu(crs, unit):
"""test meters_per_unit."""
assert utils.meters_per_unit(crs) == unit


@pytest.mark.parametrize(
"lon1, lon2, contains", [(-180, 180, False), (179, -179, True)]
)
def test_lons_contain_antimeridian(lon1: float, lon2: float, contains: bool):
assert utils.lons_contain_antimeridian(lon1, lon2) == contains
Loading