|
| 1 | +"""Tests for mixed-CRS mosaic in precomputed_tessera (GitHub issue #6). |
| 2 | +
|
| 3 | +When an ROI sits on a UTM zone boundary (e.g. lon = 120°) the tile |
| 4 | +store can return tiles in *two* EPSG codes (e.g. 32650 / 32651). The |
| 5 | +mosaic helper must reproject them to a common CRS instead of rejecting |
| 6 | +the operation. |
| 7 | +""" |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import numpy as np |
| 11 | +import pytest |
| 12 | + |
| 13 | +from affine import Affine |
| 14 | +from pyproj import Transformer |
| 15 | + |
| 16 | +from rs_embed.core.specs import BBox |
| 17 | +from rs_embed.core.errors import ModelError |
| 18 | +from rs_embed.embedders.precomputed_tessera import ( |
| 19 | + _mosaic_and_crop_strict_roi, |
| 20 | + _reproject_tile, |
| 21 | +) |
| 22 | + |
| 23 | +rasterio = pytest.importorskip("rasterio", reason="rasterio needed for mixed-CRS tests") |
| 24 | + |
| 25 | +# --------------------------------------------------------------------------- |
| 26 | +# Helpers |
| 27 | +# --------------------------------------------------------------------------- |
| 28 | + |
| 29 | +D = 64 # embedding dimension (must be in _to_hwc's allowlist) |
| 30 | +TILE_HW = 8 # small tiles for fast tests |
| 31 | +PX_M = 500.0 # 500 m pixels |
| 32 | + |
| 33 | + |
| 34 | +def _make_tile(lon: float, lat: float, epsg: str, value: float = 1.0): |
| 35 | + """Create a synthetic (year, lon, lat, embedding, crs, transform) tuple.""" |
| 36 | + tfm = Transformer.from_crs("EPSG:4326", epsg, always_xy=True) |
| 37 | + x, y = tfm.transform(lon, lat) |
| 38 | + |
| 39 | + # north-up transform: origin at top-left of tile |
| 40 | + transform = Affine(PX_M, 0.0, x, 0.0, -PX_M, y + TILE_HW * PX_M) |
| 41 | + emb = np.full((TILE_HW, TILE_HW, D), value, dtype=np.float32) |
| 42 | + return (2021, lon, lat, emb, epsg, transform) |
| 43 | + |
| 44 | + |
| 45 | +# --------------------------------------------------------------------------- |
| 46 | +# Tests |
| 47 | +# --------------------------------------------------------------------------- |
| 48 | + |
| 49 | + |
| 50 | +class TestMixedCRSMosaic: |
| 51 | + """Regression tests for issue #6 – UTM zone boundary.""" |
| 52 | + |
| 53 | + def test_same_crs_still_works(self): |
| 54 | + """Sanity: single-CRS mosaic must still succeed.""" |
| 55 | + tile_a = _make_tile(119.5, 30.0, "EPSG:32650", value=1.0) |
| 56 | + tile_b = _make_tile(119.6, 30.0, "EPSG:32650", value=2.0) |
| 57 | + |
| 58 | + bbox = BBox(minlon=119.45, minlat=29.95, maxlon=119.65, maxlat=30.05) |
| 59 | + chw, meta = _mosaic_and_crop_strict_roi([tile_a, tile_b], bbox_4326=bbox) |
| 60 | + |
| 61 | + assert chw.ndim == 3 |
| 62 | + assert chw.shape[0] == D |
| 63 | + assert meta["tile_crs"] == "EPSG:32650" |
| 64 | + |
| 65 | + def test_mixed_crs_at_utm_boundary(self): |
| 66 | + """Two tiles in EPSG:32650 / 32651 at lon ≈ 120° must mosaic.""" |
| 67 | + tile_a = _make_tile(119.95, 30.0, "EPSG:32650", value=1.0) |
| 68 | + tile_b = _make_tile(120.05, 30.0, "EPSG:32651", value=2.0) |
| 69 | + |
| 70 | + bbox = BBox(minlon=119.90, minlat=29.95, maxlon=120.10, maxlat=30.05) |
| 71 | + chw, meta = _mosaic_and_crop_strict_roi([tile_a, tile_b], bbox_4326=bbox) |
| 72 | + |
| 73 | + assert chw.ndim == 3 |
| 74 | + assert chw.shape[0] == D |
| 75 | + # target CRS should be one of the two (whichever has more tiles, or |
| 76 | + # first in case of tie) |
| 77 | + assert meta["tile_crs"] in ("EPSG:32650", "EPSG:32651") |
| 78 | + |
| 79 | + def test_mixed_crs_majority_wins(self): |
| 80 | + """Target CRS is the most-common one among tiles.""" |
| 81 | + tile_a = _make_tile(119.8, 30.0, "EPSG:32650", value=1.0) |
| 82 | + tile_b = _make_tile(119.9, 30.0, "EPSG:32650", value=1.5) |
| 83 | + tile_c = _make_tile(120.1, 30.0, "EPSG:32651", value=2.0) |
| 84 | + |
| 85 | + bbox = BBox(minlon=119.75, minlat=29.95, maxlon=120.15, maxlat=30.05) |
| 86 | + chw, meta = _mosaic_and_crop_strict_roi( |
| 87 | + [tile_a, tile_b, tile_c], bbox_4326=bbox, |
| 88 | + ) |
| 89 | + |
| 90 | + assert chw.ndim == 3 |
| 91 | + assert chw.shape[0] == D |
| 92 | + # 2 tiles in 32650, 1 in 32651 → target should be 32650 |
| 93 | + assert meta["tile_crs"] == "EPSG:32650" |
| 94 | + |
| 95 | + def test_empty_tiles_raises(self): |
| 96 | + """No tiles at all should still raise.""" |
| 97 | + bbox = BBox(minlon=119.0, minlat=29.0, maxlon=120.0, maxlat=30.0) |
| 98 | + with pytest.raises(ModelError, match="No tiles"): |
| 99 | + _mosaic_and_crop_strict_roi([], bbox_4326=bbox) |
| 100 | + |
| 101 | + |
| 102 | +class TestReprojectTile: |
| 103 | + """Unit tests for _reproject_tile helper.""" |
| 104 | + |
| 105 | + def test_identity_reproject(self): |
| 106 | + """Reprojecting to the same CRS should return data of similar shape.""" |
| 107 | + hwc = np.ones((4, 4, D), dtype=np.float32) |
| 108 | + transform = Affine(PX_M, 0.0, 500_000.0, 0.0, -PX_M, 3_500_000.0) |
| 109 | + |
| 110 | + out_hwc, out_tf = _reproject_tile( |
| 111 | + hwc, transform, "EPSG:32650", "EPSG:32650", |
| 112 | + ) |
| 113 | + |
| 114 | + assert out_hwc.ndim == 3 |
| 115 | + assert out_hwc.shape[-1] == D |
| 116 | + np.testing.assert_allclose(out_hwc, 1.0, atol=1e-5) |
| 117 | + |
| 118 | + def test_cross_zone_reproject(self): |
| 119 | + """Reproject from zone 50 to zone 51 preserves data values.""" |
| 120 | + hwc = np.full((4, 4, D), 42.0, dtype=np.float32) |
| 121 | + transform = Affine(PX_M, 0.0, 800_000.0, 0.0, -PX_M, 3_500_000.0) |
| 122 | + |
| 123 | + out_hwc, out_tf = _reproject_tile( |
| 124 | + hwc, transform, "EPSG:32650", "EPSG:32651", |
| 125 | + ) |
| 126 | + |
| 127 | + assert out_hwc.ndim == 3 |
| 128 | + assert out_hwc.shape[-1] == D |
| 129 | + # nearest-neighbour: non-zero pixels should carry the original value |
| 130 | + nonzero = out_hwc[out_hwc != 0] |
| 131 | + if nonzero.size: |
| 132 | + np.testing.assert_allclose(nonzero, 42.0, atol=1e-5) |
| 133 | + |
| 134 | + def test_target_res_snaps_resolution(self): |
| 135 | + """Providing target_res should lock the output pixel size.""" |
| 136 | + hwc = np.ones((4, 4, D), dtype=np.float32) |
| 137 | + transform = Affine(PX_M, 0.0, 500_000.0, 0.0, -PX_M, 3_500_000.0) |
| 138 | + |
| 139 | + target_res = (PX_M, PX_M) |
| 140 | + out_hwc, out_tf = _reproject_tile( |
| 141 | + hwc, transform, "EPSG:32650", "EPSG:32651", target_res=target_res, |
| 142 | + ) |
| 143 | + |
| 144 | + assert abs(abs(float(out_tf.a)) - PX_M) < 1e-3 |
| 145 | + assert abs(abs(float(out_tf.e)) - PX_M) < 1e-3 |
0 commit comments