"""`tiles/` on synthetic GeoTIFFs whose answer is known. No torch, no checkpoint: `pip install numpy rasterio pillow pytest` and run `pytest tests/test_tiles.py -q` from the repo root. """ from __future__ import annotations import json import numpy as np import pytest rasterio = pytest.importorskip("rasterio") from rasterio.crs import CRS # noqa: E402 from rasterio.enums import ColorInterp # noqa: E402 from rasterio.transform import from_origin # noqa: E402 from rasterio.warp import transform as warp_transform # noqa: E402 from tiles import catalog as cat # noqa: E402 from tiles.build_catalog import build # noqa: E402 from tiles.mosaic import AreaNotAvailable, AreaTooLarge, build_area # noqa: E402 from tiles.outputs import ( # noqa: E402 corners_wgs84, decode_terrain_rgb, encode_terrain_rgb, objects_geojson, write_height_tif, write_heights_png, write_imagery_jpg, write_overlay_png, ) UTM45 = CRS.from_epsg(32645) # Bhubaneswar's zone GSD = 0.5 SIDE = 200 # px -> 100 m tiles X0, Y0 = 300_000.0, 2_250_000.0 # top-left of tile A def write_tif(path, bands, x0, y0, nodata=None, interp=None, crs=UTM45): bands = np.asarray(bands) with rasterio.open( path, "w", driver="GTiff", width=bands.shape[2], height=bands.shape[1], count=bands.shape[0], dtype=bands.dtype, crs=crs, transform=from_origin(x0, y0, GSD, GSD), nodata=nodata, ) as dst: dst.write(bands) if interp: dst.colorinterp = interp return path def const(values, dtype=np.uint8, side=SIDE): return np.stack([np.full((side, side), v, dtype) for v in values]) def lonlat_box(x0, y0, x1, y1): """UTM45 rectangle -> WGS84 bbox (w, s, e, n).""" lons, lats = warp_transform(UTM45, "EPSG:4326", [x0, x1, x1, x0], [y0, y0, y1, y1]) return min(lons), min(lats), max(lons), max(lats) @pytest.fixture() def repo(tmp_path): src = tmp_path / "src" src.mkdir() # A: plain RGB, 8-bit write_tif(src / "a.tif", const([50, 100, 150]), X0, Y0, interp=[ColorInterp.red, ColorInterp.green, ColorInterp.blue]) # B: Cartosat-style B, G, R, NIR to the east of A write_tif(src / "b.tif", const([10, 20, 30, 40]), X0 + SIDE * GSD, Y0, interp=[ColorInterp.gray] + [ColorInterp.undefined] * 3) # C: 16-bit with a nodata collar over its left half, south of A c = const([300, 600, 900, 1200], np.uint16) c[:, :, : SIDE // 2] = 0 c[0, :, SIDE // 2:] += np.arange(SIDE // 2, dtype=np.uint16) write_tif(src / "c.tif", c, X0, Y0 - SIDE * GSD, nodata=0, interp=[ColorInterp.gray] + [ColorInterp.undefined] * 3) # no CRS, and a height raster (not imagery) -> both skipped write_tif(src / "nocrs.tif", const([1, 2, 3]), 0, 0, crs=None) write_tif(src / "dsm_m.tif", const([12.5], np.float32), X0, Y0) out = tmp_path / "tiles" summary = build([src], out, source="test") cat._cache.clear() return out, summary def test_build_catalog(repo): out, summary = repo assert sorted(summary["added"]) == ["test-a", "test-b", "test-c"] skipped = " ".join(summary["skipped"]) assert len(summary["skipped"]) == 2 and "nocrs" in skipped and "float32" in skipped features = {f["properties"]["id"]: f for f in cat.load_catalog(out)["features"]} assert features["test-a"]["properties"]["rgb_bands"] == [1, 2, 3] assert features["test-b"]["properties"]["rgb_bands"] == [3, 2, 1] assert features["test-c"]["properties"]["dtype"] == "uint16" assert features["test-a"]["properties"]["gsd_m"] == pytest.approx(GSD) for f in features.values(): assert (out / f["properties"]["path"]).is_file() with rasterio.open(out / f["properties"]["path"]) as cog: assert cog.crs == UTM45 and cog.overviews(1) is not None # C's footprint covers only its valid (right) half c = features["test-c"]["properties"] assert c["valid_frac"] == pytest.approx(0.5, abs=0.02) full = lonlat_box(X0, Y0 - 2 * SIDE * GSD, X0 + SIDE * GSD, Y0 - SIDE * GSD) assert c["bbox"][0] > full[0] + (full[2] - full[0]) * 0.4 def test_build_is_incremental(repo, tmp_path): out, _ = repo more = tmp_path / "more" more.mkdir() write_tif(more / "d.tif", const([1, 2, 3]), X0 + 10_000, Y0, interp=[ColorInterp.red, ColorInterp.green, ColorInterp.blue]) summary = build([more], out, source="test") assert summary["added"] == ["test-d"] and summary["total"] == 4 def test_query(repo): out, _ = repo inside_a = lonlat_box(X0 + 20, Y0 - 80, X0 + 80, Y0 - 20) assert [t.id for t in cat.query(inside_a, out)] == ["test-a"] assert cat.query((10.0, 10.0, 10.01, 10.01), out) == [] def test_full_coverage_keeps_rgb(repo): out, _ = repo bbox = lonlat_box(X0 + 20, Y0 - 80, X0 + 80, Y0 - 20) area = build_area(bbox, cat.query(bbox, out), out, GSD) assert area.coverage == pytest.approx(1.0) assert area.valid.all() assert area.crs.to_epsg() == 32645 assert tuple(area.rgb[area.rgb.shape[0] // 2, area.rgb.shape[1] // 2]) == (50, 100, 150) def test_band_order_bgrn_to_rgb(repo): out, _ = repo bbox = lonlat_box(X0 + SIDE * GSD + 20, Y0 - 80, X0 + SIDE * GSD + 80, Y0 - 20) area = build_area(bbox, cat.query(bbox, out), out, GSD) assert tuple(area.rgb[area.rgb.shape[0] // 2, area.rgb.shape[1] // 2]) == (30, 20, 10) def test_partial_coverage_is_cropped(repo): out, _ = repo # 50 m west of A (empty) + 50 m into A bbox = lonlat_box(X0 - 50, Y0 - 80, X0 + 50, Y0 - 20) area = build_area(bbox, cat.query(bbox, out), out, GSD) assert 0.3 < area.coverage < 0.7 assert area.transform.c >= X0 - GSD # cropped to start at A's west edge assert area.rgb.shape[1] <= 50 / GSD + 4 assert area.valid.mean() > 0.9 def test_nodata_collar_counts_as_uncovered(repo): out, _ = repo # entirely inside C's nodata (left) half bbox = lonlat_box(X0 + 10, Y0 - SIDE * GSD - 80, X0 + 40, Y0 - SIDE * GSD - 20) with pytest.raises(AreaNotAvailable): build_area(bbox, cat.query(bbox, out), out, GSD) def test_16bit_is_stretched_to_8bit(repo): out, _ = repo bbox = lonlat_box(X0 + 60, Y0 - SIDE * GSD - 90, X0 + 95, Y0 - SIDE * GSD - 10) area = build_area(bbox, cat.query(bbox, out), out, GSD) assert area.rgb.dtype == np.uint8 assert area.rgb[..., 2].max() > 200 # blue channel had a gradient def test_no_coverage_and_too_large(repo): out, _ = repo with pytest.raises(AreaNotAvailable): build_area((10.0, 10.0, 10.002, 10.002), [], out, GSD) with pytest.raises(AreaTooLarge): build_area((85.0, 20.0, 85.1, 20.1), [], out, GSD) def test_outputs(tmp_path): transform = from_origin(X0, Y0, GSD, GSD) corners = corners_wgs84(transform, UTM45, SIDE, SIDE) lons, lats = warp_transform(UTM45, "EPSG:4326", [X0], [Y0]) assert corners[0] == pytest.approx([lons[0], lats[0]], abs=1e-7) assert corners[1][0] > corners[0][0] and corners[3][1] < corners[0][1] height = np.full((SIDE, SIDE), 5.0, np.float32) height[:10] = np.nan write_height_tif(tmp_path / "h.tif", height, transform, UTM45) with rasterio.open(tmp_path / "h.tif") as src: assert src.crs == UTM45 and src.transform == transform assert np.isnan(src.read(1)[0, 0]) and src.read(1)[50, 50] == 5.0 gray = lambda a, vmax: np.repeat((a / vmax * 255).astype(np.uint8)[..., None], 3, -1) # noqa: E731 write_overlay_png(tmp_path / "o.png", height, 10.0, colorizer=gray) from PIL import Image rgba = np.asarray(Image.open(tmp_path / "o.png")) assert rgba.shape == (SIDE, SIDE, 4) and rgba[0, 0, 3] == 0 and rgba[50, 50, 3] == 255 objects = { "buildings": [{"poly": [[10, 10], [30, 10], [30, 30], [10, 30]], "h": 12.0, "h_max": 14.0, "area_m2": 100.0}], "water": [], "trees": [{"x": 50.5, "y": 60.5, "h": 8.0, "r": 2.0}], } fc = objects_geojson(objects, transform, UTM45) kinds = [f["properties"]["kind"] for f in fc["features"]] assert kinds == ["building", "tree"] ring = fc["features"][0]["geometry"]["coordinates"][0] assert ring[0] == ring[-1] and len(ring) == 5 json.dumps(fc) def test_3d_surface_outputs(tmp_path): from PIL import Image height = np.array([[0.0, 0.05, 12.34], [87.6, np.nan, -1.2]], np.float32) back = decode_terrain_rgb(encode_terrain_rgb(height)) assert back[1, 1] == 0.0 # nodata -> ground finite = np.isfinite(height) assert np.abs(back[finite] - height[finite]).max() <= 0.05 + 1e-4 # 3000 x 1500 -> fits 1024 on the long side, aspect kept big = np.full((1500, 3000), 7.5, np.float32) write_heights_png(tmp_path / "h.png", big) rgb = np.asarray(Image.open(tmp_path / "h.png")) assert rgb.shape == (512, 1024, 3) assert decode_terrain_rgb(rgb) == pytest.approx(7.5, abs=0.05) write_imagery_jpg(tmp_path / "i.jpg", np.zeros((40, 60, 3), np.uint8)) assert Image.open(tmp_path / "i.jpg").size == (60, 40) write_imagery_jpg(tmp_path / "i2.jpg", np.zeros((50, 4100, 3), np.uint8)) assert max(Image.open(tmp_path / "i2.jpg").size) == 2048 @pytest.mark.parametrize("bad", ["", "[1,2,3]", "[2,0,1,1]", "[0,0,1,90]", "nope"]) def test_parse_bbox_rejects(bad): with pytest.raises(ValueError): cat.parse_bbox(bad) def test_parse_bbox_accepts(): assert cat.parse_bbox("[85.79, 20.33, 85.80, 20.34]") == (85.79, 20.33, 85.80, 20.34)