"""The ground under the aircraft: reading tiles, and putting them on the map. Nothing here touches the network. The tiles are made up in the test and fed in through the same door the real ones come through, and the PNGs are built here from the specification rather than by the decoder they are testing. """ import json import os import struct import time import zlib import numpy as np import pytest from bandsaunter import basemap as bm from bandsaunter import flightmap as fm from test_flightmap import _has_text, two_aircraft # --------------------------------------------------------------------------- # Making PNGs the hard way, so the decoder is tested against the format # --------------------------------------------------------------------------- def _chunk(tag: bytes, body: bytes) -> bytes: return (struct.pack(">I", len(body)) + tag + body + struct.pack(">I", zlib.crc32(tag + body) & 0xFFFFFFFF)) def _paeth(a: int, b: int, c: int) -> int: p = a + b - c pa, pb, pc = abs(p - a), abs(p - b), abs(p - c) if pa <= pb and pa <= pc: return a return b if pb <= pc else c def make_png(pixels: np.ndarray, colour: int = 2, filter_type: int = 0, palette: np.ndarray | None = None) -> bytes: """A PNG with every row written using one filter, built from the spec.""" pixels = np.asarray(pixels, dtype=np.uint8) height, width = pixels.shape[0], pixels.shape[1] channels = {0: 1, 2: 3, 3: 1, 4: 2, 6: 4}[colour] flat = pixels.reshape(height, width * channels) raw = bytearray() previous = bytearray(width * channels) for row in flat: line = bytearray(int(v) for v in row) out = bytearray(len(line)) for i, value in enumerate(line): left_raw = line[i - channels] if i >= channels else 0 above = previous[i] upleft = previous[i - channels] if i >= channels else 0 if filter_type == 0: out[i] = value elif filter_type == 1: out[i] = (value - left_raw) & 0xFF elif filter_type == 2: out[i] = (value - above) & 0xFF elif filter_type == 3: out[i] = (value - ((left_raw + above) >> 1)) & 0xFF else: out[i] = (value - _paeth(left_raw, above, upleft)) & 0xFF raw.append(filter_type) raw += out previous = line body = (b"\x89PNG\r\n\x1a\n" + _chunk(b"IHDR", struct.pack(">IIBBBBB", width, height, 8, colour, 0, 0, 0))) if palette is not None: body += _chunk(b"PLTE", np.asarray(palette, dtype=np.uint8).tobytes()) body += _chunk(b"IDAT", zlib.compress(bytes(raw), 6)) return body + _chunk(b"IEND", b"") def a_picture(height=9, width=7, seed=3) -> np.ndarray: rng = np.random.default_rng(seed) return rng.integers(0, 256, size=(height, width, 3), dtype=np.uint8) # --------------------------------------------------------------------------- # Reading a PNG # --------------------------------------------------------------------------- @pytest.mark.parametrize("filter_type", [0, 1, 2, 3, 4]) def test_every_filter_the_format_defines_is_undone(filter_type): """None, Sub, Up, Average and Paeth: all five, or a tile comes back as coloured noise and nobody notices until the map looks wrong.""" want = a_picture() assert np.array_equal(bm.decode_png(make_png(want, 2, filter_type)), want) def test_what_this_program_writes_it_can_read_back(): from bandsaunter.images import write_png import tempfile from pathlib import Path want = a_picture(height=20, width=13, seed=9) with tempfile.TemporaryDirectory() as tmp: path = write_png(Path(tmp) / "x.png", want) assert np.array_equal(bm.decode_png(path.read_bytes()), want) def test_a_palette_image_is_looked_up(): """The tiles most servers send are palette images.""" palette = np.array([[0, 0, 0], [255, 0, 0], [0, 128, 255]], dtype=np.uint8) indices = np.array([[0, 1, 2], [2, 1, 0]], dtype=np.uint8)[:, :, None] got = bm.decode_png(make_png(indices, colour=3, palette=palette)) assert np.array_equal(got, palette[indices[:, :, 0]]) def test_a_grey_image_becomes_grey_pixels(): grey = np.array([[0, 64], [128, 255]], dtype=np.uint8)[:, :, None] got = bm.decode_png(make_png(grey, colour=0)) assert got.shape == (2, 2, 3) assert np.array_equal(got[:, :, 0], got[:, :, 2]) assert got[1, 1, 0] == 255 def test_transparency_is_dropped_rather_than_misread(): rng = np.random.default_rng(1) rgba = rng.integers(0, 256, size=(4, 4, 4), dtype=np.uint8) got = bm.decode_png(make_png(rgba, colour=6)) assert np.array_equal(got, rgba[:, :, :3]) @pytest.mark.parametrize("body,why", [ (b"not a png at all", "not a PNG"), (b"\x89PNG\r\n\x1a\n", "no image"), ]) def test_something_that_is_not_a_tile_is_refused(body, why): with pytest.raises(bm.PNGError): bm.decode_png(body) def test_a_depth_this_cannot_read_says_so_rather_than_guessing(): header = (b"\x89PNG\r\n\x1a\n" + _chunk(b"IHDR", struct.pack(">IIBBBBB", 2, 2, 16, 2, 0, 0, 0)) + _chunk(b"IEND", b"")) with pytest.raises(bm.PNGError): bm.decode_png(header) def test_an_interlaced_tile_is_refused(): header = (b"\x89PNG\r\n\x1a\n" + _chunk(b"IHDR", struct.pack(">IIBBBBB", 2, 2, 8, 2, 0, 0, 1)) + _chunk(b"IEND", b"")) with pytest.raises(bm.PNGError): bm.decode_png(header) # --------------------------------------------------------------------------- # Which tiles, and where they go # --------------------------------------------------------------------------- def test_the_middle_of_the_world_is_the_middle_of_the_grid(): assert bm.tile_of(0.0, 0.0, 0) == pytest.approx((0.5, 0.5)) assert bm.tile_of(0.0, -180.0, 1)[0] == pytest.approx(0.0) assert bm.tile_of(85.05, 0.0, 1)[1] == pytest.approx(0.0, abs=0.001) def test_a_known_place_lands_in_its_known_tile(): """Seattle at zoom 10 is tile 164, 357 -- worked out from the standard formula, not from this module.""" import math lat, lon, z = 47.6062, -122.3321, 10 n = 2 ** z x = int((lon + 180.0) / 360.0 * n) y = int((1.0 - math.asinh(math.tan(math.radians(lat))) / math.pi) / 2.0 * n) got = bm.tile_of(lat, lon, z) assert (int(got[0]), int(got[1])) == (x, y) def test_a_small_box_gets_more_detail_than_a_large_one(): tight = bm.choose_zoom(47.5, -122.4, 47.7, -122.2) wide = bm.choose_zoom(30.0, -130.0, 50.0, -70.0) assert tight > wide assert tight <= bm.MAX_ZOOM and wide >= bm.MIN_ZOOM def test_the_zoom_is_chosen_to_stay_inside_the_tile_budget(): south, west, north, east = 47.0, -122.8, 48.5, -121.0 zoom = bm.choose_zoom(south, west, north, east, max_tiles=12) x0, y0 = bm.tile_of(north, west, zoom) x1, y1 = bm.tile_of(south, east, zoom) tiles = (int(x1) - int(x0) + 1) * (int(y1) - int(y0) + 1) assert tiles <= 12 # --------------------------------------------------------------------------- # Stitching, without a network # --------------------------------------------------------------------------- def solid(value): """A tile of one colour, as PNG bytes.""" px = np.zeros((bm.TILE_PIXELS, bm.TILE_PIXELS, 3), dtype=np.uint8) + value return make_png(px) def counting_fetcher(answers=None, fail_on=()): """A stand-in for the tile server that records what it was asked for.""" asked = [] def fetch(z, x, y, **kw): asked.append((z, x, y)) if (z, x, y) in fail_on: return None if answers is not None: return answers(z, x, y) return solid((x * 37 + y * 11) % 256) fetch.asked = asked return fetch def test_the_tiles_are_stitched_in_the_right_places(): fetch = counting_fetcher() raster, ox, oy, covered = bm.mosaic(47.0, -122.8, 48.5, -121.0, 9, fetch=fetch, pause=0) assert covered.all() assert raster is not None assert raster.shape[2] == 3 assert raster.shape[0] % bm.TILE_PIXELS == 0 assert len(fetch.asked) == (raster.shape[0] // bm.TILE_PIXELS) * \ (raster.shape[1] // bm.TILE_PIXELS) # The first tile fetched is the north-west corner, and it is drawn there. z, x, y = fetch.asked[0] assert (ox, oy) == (x * bm.TILE_PIXELS, y * bm.TILE_PIXELS) assert raster[0, 0, 0] == (x * 37 + y * 11) % 256 def test_a_tile_that_does_not_arrive_leaves_a_hole_rather_than_an_error(): """One tile missing is a hole in the map, not a failed drawing.""" seen = counting_fetcher() bm.mosaic(47.0, -122.8, 48.5, -121.0, 9, fetch=seen, pause=0) missing = seen.asked[:1] raster, _, _, covered = bm.mosaic( 47.0, -122.8, 48.5, -121.0, 9, fetch=counting_fetcher(fail_on=missing), pause=0, retry_pause=0) assert raster is not None assert not raster[:bm.TILE_PIXELS, :bm.TILE_PIXELS].any() # the hole # And the hole is reported rather than left to be mistaken for map. assert not covered[0, 0] and covered.sum() == covered.size - 1 def test_nothing_at_all_gives_no_map(): def nothing(z, x, y, **kw): return None raster, _, _, covered = bm.mosaic(47.0, -122.8, 48.5, -121.0, 9, fetch=nothing, pause=0, retry_pause=0) assert raster is None assert not covered.any() def test_a_corrupt_tile_is_skipped(): def rubbish(z, x, y, **kw): return b"not a png" raster, _, _, covered = bm.mosaic(47.0, -122.8, 48.5, -121.0, 9, fetch=rubbish, pause=0, retry_pause=0) assert raster is None assert not covered.any() # --------------------------------------------------------------------------- # On to the picture # --------------------------------------------------------------------------- def gradient_tile(z, x, y, **kw): """A tile that is black at the top and white at the bottom.""" column = np.linspace(0, 255, bm.TILE_PIXELS).astype(np.uint8) px = np.repeat(column[:, None], bm.TILE_PIXELS, axis=1) return make_png(np.repeat(px[:, :, None], 3, axis=2)) def test_the_ground_comes_back_as_levels_the_map_can_paint(): levels, _settled = bm.ground_under(47.0, -122.8, 48.5, -121.0, 200, 150, shades=32, fetch=gradient_tile, pause=0) assert levels is not None assert levels.shape == (150, 200) assert levels.dtype == np.uint8 assert levels.max() <= 31 def _patchy(fail_on, note=None): """A tile server that refuses some squares. What a busy one does.""" def fetch(z, x, y, **kw): if note is not None: note.append((z, x, y)) if (x, y) in fail_on: return None # Something to see, and different in each tile, so that a hole is # distinguishable from map rather than from a flat field. return solid(40 if (x + y) % 3 == 0 else 230) return fetch BOX = (47.0, -123.0, 48.0, -122.0) def _hole_at(): """The grid position of one tile inside BOX, worked out here rather than asked of the code whose arithmetic is under test elsewhere.""" zoom = bm.choose_zoom(*BOX, width=400) x0, y0 = bm.tile_of(BOX[2], BOX[1], zoom) return zoom, (int(x0) + 1, int(y0) + 1) def _hole_mask(hole, zoom, width=400, height=400, grow=0): """Which output pixels the missing tile covers. Worked out from the tile's own corners rather than from the picture, so that a test of what is drawn in the gap is not asking the code that drew it where the gap is. ``grow`` widens it, for the band along the edge where a cell is averaged over part of a tile and part of nothing. """ south, west, north, east = tile_bounds(zoom, hole[0], hole[1]) top = (BOX[2] - min(north, BOX[2])) / (BOX[2] - BOX[0]) * height bottom = (BOX[2] - max(south, BOX[0])) / (BOX[2] - BOX[0]) * height left = (max(west, BOX[1]) - BOX[1]) / (BOX[3] - BOX[1]) * width right = (min(east, BOX[3]) - BOX[1]) / (BOX[3] - BOX[1]) * width mask = np.zeros((height, width), dtype=bool) mask[max(0, int(top) - grow):int(bottom) + 1 + grow, max(0, int(left) - grow):int(right) + 1 + grow] = True return mask def test_a_missing_tile_is_a_gap_rather_than_the_brightest_thing_on_the_map(): """The canvas starts black and the brightness is inverted further down, so a square that never arrived used to come out as the brightest thing on the picture: a glowing rectangle where the map should be.""" zoom, hole = _hole_at() # remember=False throughout: these two are the same view with different # tiles under it, which cannot happen on the air and would otherwise be # answered from the first one's cache entry. whole, _ = bm.ground_under(*BOX, 400, 400, fetch=_patchy(()), pause=0, zoom=zoom, remember=False) holed, _ = bm.ground_under(*BOX, 400, 400, fetch=_patchy({hole}), pause=0, retry_pause=0, zoom=zoom, remember=False) assert whole is not None and holed is not None gap = _hole_mask(hole, zoom) assert gap.sum() > 1000, "the hole is not where this test thinks it is" # There was map there, and now there is nothing -- rather than the # brightest shade the picture has. assert whole[gap].max() > 0 assert holed[_hole_mask(hole, zoom, grow=-1)].max() == 0 # And what leaks in is a hairline round the edge, where a cell is # averaged over part of a tile and part of nothing, rather than a # fraction of the hole: the averaging weighs the real pixels only. assert int((holed[gap] > 0).sum()) < gap.sum() // 100 assert holed.max() == whole.max(), "the map lost its brightest shade" def test_a_missing_tile_does_not_dim_the_rest_of_the_map(): """Black is darker than any real tile, so counting a hole when working out the darkest and brightest of what was fetched drags the floor down, and every other pixel is drawn dimmer to make room for a square that is not there.""" zoom, hole = _hole_at() whole, _ = bm.ground_under(*BOX, 400, 400, fetch=_patchy(()), pause=0, zoom=zoom, remember=False) holed, _ = bm.ground_under(*BOX, 400, 400, fetch=_patchy({hole}), pause=0, retry_pause=0, zoom=zoom, remember=False) # Outside the gap and the averaged band along its edge, the map is the # map: the hole took nothing else with it. elsewhere = ~_hole_mask(hole, zoom, grow=2) assert elsewhere.sum() > holed.size // 2 assert bool((holed[elsewhere] == whole[elsewhere]).all()) def test_a_map_with_squares_missing_says_it_is_not_the_whole_answer(): """Most of a map is worth drawing and is not worth keeping: the caller has to be able to tell the two cases apart to know whether to ask again.""" _zoom, hole = _hole_at() whole, settled = bm.ground_under(*BOX, 400, 400, fetch=_patchy(()), pause=0, remember=False) assert whole is not None and settled holed, settled = bm.ground_under(*BOX, 400, 400, fetch=_patchy({hole}), pause=0, retry_pause=0, remember=False) assert holed is not None and not settled def test_a_tile_that_fails_once_is_asked_for_again(): """The usual reason for a hole is that a hundred tiles were asked for in the preceding second, which is a server asking to be slowed down rather than a tile that is not there.""" _zoom, hole = _hole_at() refused = {"left": 1} def flaky(z, x, y, **kw): if (x, y) == hole and refused["left"]: refused["left"] -= 1 return None return solid(40 if (x + y) % 3 == 0 else 230) whole, _ = bm.ground_under(*BOX, 400, 400, fetch=_patchy(()), pause=0, remember=False) healed, settled = bm.ground_under(*BOX, 400, 400, fetch=flaky, pause=0, retry_pause=0, remember=False) assert refused["left"] == 0, "the tile was never asked for a second time" assert settled, "a recovered map is the whole answer" assert bool((healed == whole).all()) def test_only_the_tiles_that_failed_are_asked_for_again(): """A retry that fetched the lot again would treat a busy server by asking it for everything twice.""" _zoom, hole = _hole_at() asked = [] bm.ground_under(*BOX, 400, 400, fetch=_patchy({hole}, note=asked), pause=0, retry_pause=0) twice = [where for where in set(asked) if asked.count(where) > 1] assert len(twice) == 1 and twice[0][1:] == hole def test_retrying_can_be_turned_off(): _zoom, hole = _hole_at() asked = [] bm.ground_under(*BOX, 400, 400, fetch=_patchy({hole}, note=asked), pause=0, retries=0) assert len(asked) == len(set(asked)) def test_only_a_tile_that_had_to_be_fetched_costs_politeness(monkeypatch): """The pause is an apology to a volunteer-funded server, and a file that was already on the disk was never asked of it. Paying it anyway is how a view whose tiles are all in hand takes half a minute to redraw -- and how asking again for three missing squares costs the wait for two hundred that are not missing.""" slept = [] monkeypatch.setattr(bm.time, "sleep", lambda s: slept.append(s)) zoom, _hole = _hole_at() _r, _x, _y, covered = bm.mosaic(*BOX, zoom, fetch=_patchy(()), pause=0.05, cached=lambda z, x, y, **kw: False) assert len(slept) == int(covered.sum()) > 1 slept.clear() bm.mosaic(*BOX, zoom, fetch=_patchy(()), pause=0.05, cached=lambda z, x, y, **kw: True) assert slept == [] def test_asking_again_for_what_is_missing_does_not_wait_for_what_is_not( monkeypatch): """Which is what makes a second ask affordable at all: the tiles that arrived the first time are on the disk, so the only thing paid for is the handful that did not.""" slept = [] monkeypatch.setattr(bm.time, "sleep", lambda s: slept.append(s)) zoom, hole = _hole_at() # Everything arrived but the one square, so everything but that square # is now on the disk. bm.mosaic(*BOX, zoom, fetch=_patchy({hole}), pause=0.05, retries=0, cached=lambda z, x, y, **kw: (x, y) != hole) assert len(slept) <= 1, slept def test_a_tile_is_on_the_disk_once_it_has_been_fetched(tmp_path): zoom, hole = _hole_at() assert not bm.on_disk(zoom, hole[0], hole[1], cache=tmp_path) where = tmp_path / str(zoom) / str(hole[0]) where.mkdir(parents=True) (where / f"{hole[1]}.png").write_bytes(solid(200)) assert bm.on_disk(zoom, hole[0], hole[1], cache=tmp_path) def tile_bounds(zoom: int, x: int, y: int): """The corners of one tile, from the inverse of the standard formula. Written here rather than taken from the module, so that a test of where the pixels land is not asking the code under test where they land. """ import math n = 2.0 ** zoom def lat_of(row): return math.degrees(math.atan(math.sinh(math.pi * (1 - 2 * row / n)))) return (lat_of(y + 1), x / n * 360.0 - 180.0, # south, west lat_of(y), (x + 1) / n * 360.0 - 180.0) # north, east def test_the_map_is_inverted_so_that_ink_shows_on_a_dark_picture(): """A printed map is dark ink on white paper; this picture is the other way round, so the dark parts of a tile are the bright parts here.""" south, west, north, east = tile_bounds(9, 81, 178) levels, _settled = bm.ground_under(south, west, north, east, 64, 64, shades=32, fetch=gradient_tile, zoom=9, pause=0) # The tile is black at the top and white at the bottom, so the picture # has to be bright at the top and dark at the bottom. assert levels[0].mean() > levels[-1].mean() assert levels[0].mean() > 25 and levels[-1].mean() < 6 def test_north_is_at_the_top(): def half_and_half(z, x, y, **kw): px = np.zeros((bm.TILE_PIXELS, bm.TILE_PIXELS, 3), dtype=np.uint8) px[:bm.TILE_PIXELS // 2] = 255 # white in the north return make_png(px) south, west, north, east = tile_bounds(9, 81, 178) levels, _settled = bm.ground_under(south, west, north, east, 40, 40, shades=32, fetch=half_and_half, zoom=9, pause=0) assert levels[0].mean() < levels[-1].mean() # white inverts to dark def test_east_is_to_the_right(): def half_and_half(z, x, y, **kw): px = np.zeros((bm.TILE_PIXELS, bm.TILE_PIXELS, 3), dtype=np.uint8) px[:, :bm.TILE_PIXELS // 2] = 255 # white in the west return make_png(px) south, west, north, east = tile_bounds(9, 81, 178) levels, _settled = bm.ground_under(south, west, north, east, 40, 40, shades=32, fetch=half_and_half, zoom=9, pause=0) assert levels[:, 0].mean() < levels[:, -1].mean() def test_one_tile_covers_its_own_box_exactly(): """The reprojection has to put the tile where the tile says it is.""" def corner_marks(z, x, y, **kw): px = np.zeros((bm.TILE_PIXELS, bm.TILE_PIXELS, 3), dtype=np.uint8) px[:8, :8] = 255 # a mark in the north-west return make_png(px) south, west, north, east = tile_bounds(9, 81, 178) levels, _settled = bm.ground_under(south, west, north, east, 128, 128, shades=32, fetch=corner_marks, zoom=9, pause=0) dark = levels < levels.max() / 2 # the white corner, inverted assert dark[:4, :4].all() assert not dark[64:, 64:].any() def test_no_tiles_means_no_ground_rather_than_an_exception(): def nothing(z, x, y, **kw): return None levels, settled = bm.ground_under(47.0, -122.8, 48.5, -121.0, 50, 50, fetch=nothing, pause=0, retry_pause=0) assert levels is None # Settled on purpose: a machine with no network must not spend the # night asking for tiles it is never going to be given. assert settled def test_a_box_that_makes_no_sense_is_refused_quietly(): levels, settled = bm.ground_under(48.0, -122.0, 47.0, -123.0, 50, 50, fetch=gradient_tile, pause=0) assert levels is None and settled # --------------------------------------------------------------------------- # Fetching once, and saying who it came from # --------------------------------------------------------------------------- def test_a_tile_is_fetched_once_and_then_read_from_the_disk(tmp_path, monkeypatch): calls = [] class _Answer: def __init__(self, body): self.body = body def read(self, *a): return self.body def __enter__(self): return self def __exit__(self, *exc): return False def fake_urlopen(request, timeout=None): calls.append(request.full_url) assert "bandsaunter" in request.headers.get("User-agent", ""), \ "a tile server is told who is asking" return _Answer(solid(120)) monkeypatch.setattr(bm.urllib.request, "urlopen", fake_urlopen) first = bm.fetch_tile(9, 81, 178, cache=tmp_path) second = bm.fetch_tile(9, 81, 178, cache=tmp_path) assert first == second assert len(calls) == 1, "the second one came off the disk" assert (tmp_path / "9" / "81" / "178.png").is_file() def test_a_tile_server_that_is_not_there_is_not_an_error(tmp_path, monkeypatch): def refuse(request, timeout=None): raise OSError("no route to host") monkeypatch.setattr(bm.urllib.request, "urlopen", refuse) assert bm.fetch_tile(9, 81, 178, cache=tmp_path) is None def test_the_tile_server_can_be_pointed_somewhere_else(tmp_path, monkeypatch): seen = [] def fake_urlopen(request, timeout=None): seen.append(request.full_url) raise OSError("stop here") monkeypatch.setattr(bm.urllib.request, "urlopen", fake_urlopen) bm.fetch_tile(4, 2, 3, url="https://example.invalid/{z}/{x}/{y}.png", cache=tmp_path) assert seen == ["https://example.invalid/4/2/3.png"] # --------------------------------------------------------------------------- # And under the aircraft # --------------------------------------------------------------------------- def test_the_map_goes_under_the_picture_and_is_credited(tmp_path): out = fm.animate(two_aircraft(), tmp_path / "on-the-map.png", width=400, ground=True, fetch=gradient_tile, airports=False) assert out is not None and out.ground assert "on the map" in out.summary() from bandsaunter.images import PNG_SIGNATURE assert out.path.read_bytes()[:8] == PNG_SIGNATURE def test_the_ground_is_drawn_in_its_own_shades(): view = fm.fit(two_aircraft(), width=400) levels, credit = fm.ground_for(view, fetch=gradient_tile) assert levels is not None and credit base = fm.background(view, ground=levels, attribution=credit) body = base[view.top:view.top + view.height, view.left:view.left + view.width] ground = (body >= fm.GROUND) & (body < fm.GROUND + fm.GROUND_SHADES) assert ground.mean() > 0.9 # nearly all of the body is map def test_without_it_the_picture_is_the_plain_grid_it_always_was(): view = fm.fit(two_aircraft(), width=400) base = fm.background(view) assert not ((base >= fm.GROUND) & (base < fm.GROUND + fm.GROUND_SHADES)).any() def test_no_network_falls_back_to_the_plain_grid(monkeypatch): def nothing(z, x, y, **kw): return None view = fm.fit(two_aircraft(), width=400) levels, credit = fm.ground_for(view, fetch=nothing) assert levels is None and credit == "" def test_the_credit_is_written_on_the_picture_itself(): """A GIF travels without the readme that would otherwise carry it.""" view = fm.fit(two_aircraft(), width=520) levels, credit = fm.ground_for(view, fetch=gradient_tile) base = fm.background(view, ground=levels, attribution=credit) assert _has_text(base, "OPENSTREETMAP") # --------------------------------------------------------------------------- # What aerodromes are under the picture # --------------------------------------------------------------------------- def _overpass(elements): """Stand in for the map data, in the shape it really answers with.""" def ask(box, url, timeout): return {"elements": elements} return ask def _node(code=None, name="", lat=32.1, lon=-110.9, ref=None, kind="node"): tags = {"aeroway": "aerodrome", "name": name} if code: tags["icao"] = code if ref: tags["ref"] = ref element = {"type": kind, "tags": tags} if kind == "node": element.update({"lat": lat, "lon": lon}) else: element["center"] = {"lat": lat, "lon": lon} return element def test_the_aerodromes_in_a_box_come_back_with_a_code_and_a_place(tmp_path): got = bm.airports_in(32.0, -111.2, 32.4, -110.7, cache=tmp_path / "a.json", ask=_overpass([_node("KTUS", "Tucson International")])) assert len(got) == 1 assert got[0]["code"] == "KTUS" assert got[0]["latitude"] == pytest.approx(32.1) def test_the_big_airports_are_relations_and_must_be_asked_for(): """Tucson International and Davis-Monthan are both relations; asking only for nodes and ways finds every airstrip in the county and misses the two the county is known for.""" import inspect source = inspect.getsource(bm._ask_overpass) for kind in ("node", "way", "relation"): assert f'{kind}["aeroway"="aerodrome"]' in source, kind def test_a_relation_is_placed_by_its_middle(tmp_path): got = bm.airports_in(32.0, -111.2, 32.4, -110.7, cache=tmp_path / "a.json", ask=_overpass([_node("KDMA", "Davis-Monthan", kind="relation")])) assert got and got[0]["code"] == "KDMA" def test_a_landing_strip_with_no_real_code_is_left_off(tmp_path): """A local identifier like 14AZ names nothing anybody would recognise, and turns a map into a list of airstrips.""" got = bm.airports_in(32.0, -111.2, 32.4, -110.7, cache=tmp_path / "a.json", ask=_overpass([_node(None, "Ruby Star", ref="14AZ"), _node(None, "Nowhere", ref="MX-0492"), _node(None, "Unnamed strip")])) assert got == [] def test_a_four_letter_reference_is_good_enough(tmp_path): got = bm.airports_in(32.0, -111.2, 32.4, -110.7, cache=tmp_path / "a.json", ask=_overpass([_node(None, "Somewhere", ref="EGLL")])) assert [a["code"] for a in got] == ["EGLL"] def test_one_airport_tagged_twice_is_marked_once(tmp_path): """A point for the terminal and an outline for the field: marking both writes the name over itself.""" got = bm.airports_in(32.0, -111.2, 32.4, -110.7, cache=tmp_path / "a.json", ask=_overpass([_node("KFHU", "Sierra Vista"), _node("KFHU", "Sierra Vista", kind="relation")])) assert [a["code"] for a in got] == ["KFHU"] def test_the_ones_with_real_codes_come_first(tmp_path): got = bm.airports_in(32.0, -111.2, 32.4, -110.7, cache=tmp_path / "a.json", ask=_overpass([_node(None, "Strip", ref="ZZZZ"), _node("KTUS", "Tucson")])) assert [a["code"] for a in got][0] == "KTUS" def test_a_view_full_of_airstrips_is_capped(tmp_path): many = [_node(f"K{i:03d}"[:4], f"Strip {i}") for i in range(200)] got = bm.airports_in(32.0, -111.2, 32.4, -110.7, cache=tmp_path / "a.json", ask=_overpass(many)) assert len(got) <= bm.MOST_AIRPORTS def test_the_answer_is_kept_so_the_question_is_asked_once(tmp_path): """A runway does not move, and the service being asked is a volunteer one.""" asked = [] def ask(box, url, timeout): asked.append(box) return {"elements": [_node("KTUS", "Tucson")]} where = tmp_path / "a.json" first = bm.airports_in(32.0, -111.2, 32.4, -110.7, cache=where, ask=ask) second = bm.airports_in(32.0, -111.2, 32.4, -110.7, cache=where, ask=ask) assert first == second assert len(asked) == 1, "asked twice for the same piece of the world" def test_an_answer_from_an_older_question_is_asked_again(tmp_path): where = tmp_path / "a.json" where.write_text(json.dumps({"fetched_at": time.time(), "version": 1, "airports": [{"code": "OLD"}]})) got = bm.airports_in(32.0, -111.2, 32.4, -110.7, cache=where, ask=_overpass([_node("KTUS", "Tucson")])) assert [a["code"] for a in got] == ["KTUS"] def test_no_network_means_no_airports_rather_than_no_map(tmp_path): def refuse(box, url, timeout): raise OSError("no route to host") assert bm.airports_in(32.0, -111.2, 32.4, -110.7, cache=tmp_path / "a.json", ask=refuse) == [] def test_nonsense_from_the_service_is_survived(tmp_path): for answer in ({}, {"elements": None}, {"elements": [{"tags": None}]}, {"elements": [{"tags": {"icao": "KTUS"}}]}): got = bm.airports_in(32.0, -111.2, 32.4, -110.7, cache=tmp_path / f"{id(answer)}.json", ask=lambda b, u, t, a=answer: a) assert got == [] def test_the_map_asks_for_the_airports_under_it(): """A route names where its aircraft are going; the airports underneath are what say where on the map you are looking.""" view = fm.fit(two_aircraft(), width=500) got = fm.local_airports(view, ask=_overpass([_node("EGLL", "Heathrow", lat=view.south + 0.1, lon=view.west + 0.1)])) assert got and got[0][0] == "EGLL" def test_a_service_that_is_not_there_costs_the_airports_and_nothing_else(): def refuse(box, url, timeout): raise OSError("no") view = fm.fit(two_aircraft(), width=500) assert fm.local_airports(view, ask=refuse) == [] # --------------------------------------------------------------------------- # How sharp the map is # --------------------------------------------------------------------------- def test_detail_is_averaged_down_rather_than_thrown_away(): """Taking the nearest source pixel keeps a fraction of a tile and turns the rest into aliasing: hard, broken lettering and roads that come and go along their length. A checkerboard is half black and half white, so averaging it lands in the middle and sampling it lands at one end.""" fine = np.tile(np.array([0.0, 255.0], dtype=np.float32), 128)[None, :] out = bm._resample(fine, np.linspace(0, 256, 41), axis=1) assert out.shape == (1, 40) assert abs(float(out.mean()) - 127.5) < 4.0 assert float(out.max()) < 160.0 and float(out.min()) > 95.0 def test_averaging_takes_the_whole_cell_and_no_more(): """Each output cell is the mean of the source pixels under it.""" values = np.arange(100, dtype=np.float32)[None, :] out = bm._resample(values, np.linspace(0, 100, 11), axis=1) assert out.shape == (1, 10) for i in range(10): assert abs(float(out[0, i]) - (i * 10 + 4.5)) < 0.01 def test_averaging_works_the_other_way_up_too(): values = np.arange(60, dtype=np.float32)[:, None] out = bm._resample(values, np.linspace(0, 60, 7), axis=0) assert out.shape == (6, 1) assert float(out[0, 0]) < float(out[-1, 0]) def test_a_cell_smaller_than_a_source_pixel_takes_that_pixel(): """Zoomed in past the tiles, a cell covers less than one of them.""" values = np.array([[10.0, 20.0, 30.0]], dtype=np.float32) out = bm._resample(values, np.linspace(0, 3, 10), axis=1) assert out.shape == (1, 9) assert set(np.round(out[0]).astype(int)) <= {10, 20, 30} def test_a_gradient_still_comes_out_as_a_gradient(): south, west, north, east = tile_bounds(9, 81, 178) levels, _settled = bm.ground_under(south, west, north, east, 64, 64, shades=32, fetch=gradient_tile, zoom=9, pause=0) rows = levels.mean(axis=1) assert (np.diff(rows) <= 0.51).all(), "the gradient came out lumpy" def test_averaging_is_the_same_shape_as_the_picture_asked_for(): south, west, north, east = tile_bounds(9, 81, 178) for width, height in ((40, 40), (137, 91), (300, 200), (17, 5)): levels, _settled = bm.ground_under(south, west, north, east, width, height, shades=32, fetch=gradient_tile, zoom=9, pause=0) assert levels.shape == (height, width) def test_a_map_asked_for_at_more_detail_than_the_tiles_hold_still_works(): """Zoomed in past the tiles, a cell covers less than one source pixel.""" south, west, north, east = tile_bounds(9, 81, 178) levels, _settled = bm.ground_under(south, west, north, east, 2000, 2000, shades=32, fetch=gradient_tile, zoom=9, pause=0) assert levels.shape == (2000, 2000) assert levels[0].mean() != levels[-1].mean() # --------------------------------------------------------------------------- # Keeping the finished map # --------------------------------------------------------------------------- def test_a_map_already_built_is_not_built_again(monkeypatch): """The tiles were always cached; the work done on them was not. Opening the same window twice used to decode forty PNGs and resample a megapixel and a half into this program's own projection, both times, for an answer that cannot have changed.""" box = (47.0, -123.0, 48.0, -122.0) asked = [] def counting(z, x, y, **kw): asked.append((z, x, y)) return gradient_tile(z, x, y, **kw) first, settled = bm.ground_under(*box, 300, 300, shades=32, fetch=counting, pause=0) assert settled and first is not None built = len(asked) assert built > 1 asked.clear() again, settled = bm.ground_under(*box, 300, 300, shades=32, fetch=counting, pause=0) assert settled assert asked == [], "it went back to the tiles for a map it already had" assert bool((again == first).all()), "the kept map is a different map" def test_a_map_with_squares_missing_is_not_kept(): """Caching a hole would keep it for a month, and the whole point of calling a partial map provisional is that it gets asked for again.""" zoom, hole = _hole_at() holed, settled = bm.ground_under(*BOX, 400, 400, fetch=_patchy({hole}), pause=0, retry_pause=0, zoom=zoom) assert holed is not None and not settled assert list(bm.ground_cache_dir().glob("*.npz")) == [] def test_a_different_picture_is_a_different_map(): """Same piece of world, bigger window: reusing the smaller one would be exactly the upscale this program went to some trouble to avoid.""" box = (47.0, -123.0, 48.0, -122.0) small, _ = bm.ground_under(*box, 200, 200, shades=32, fetch=gradient_tile, pause=0) large, _ = bm.ground_under(*box, 400, 400, shades=32, fetch=gradient_tile, pause=0) assert small.shape == (200, 200) and large.shape == (400, 400) elsewhere, _ = bm.ground_under(48.0, -123.0, 49.0, -122.0, 200, 200, shades=32, fetch=gradient_tile, pause=0) assert len(list(bm.ground_cache_dir().glob("*.npz"))) == 3 def test_a_view_that_drifted_a_few_metres_is_the_same_view(): """Rounded to about a hundred metres, which is finer than a tile and far finer than anything visible: a map rebuilt because the middle moved by a pixel would defeat the point of keeping it.""" a, _ = bm.ground_under(47.0, -123.0, 48.0, -122.0, 200, 200, shades=32, fetch=gradient_tile, pause=0) asked = [] def counting(z, x, y, **kw): asked.append((z, x, y)) return gradient_tile(z, x, y, **kw) b, _ = bm.ground_under(47.00002, -123.00002, 48.00002, -121.99998, 200, 200, shades=32, fetch=counting, pause=0) assert asked == [] assert bool((a == b).all()) def test_keeping_the_map_can_be_turned_off(): box = (47.0, -123.0, 48.0, -122.0) bm.ground_under(*box, 250, 250, shades=32, fetch=gradient_tile, pause=0, remember=False) assert list(bm.ground_cache_dir().glob("*.npz")) == [] def test_a_truncated_cache_file_is_a_miss_rather_than_a_crash(): """Which is what a cache looks like after a power cut.""" box = (47.0, -123.0, 48.0, -122.0) bm.ground_under(*box, 220, 220, shades=32, fetch=gradient_tile, pause=0) kept = list(bm.ground_cache_dir().glob("*.npz")) assert len(kept) == 1 kept[0].write_bytes(b"PK\x03\x04 truncated") levels, settled = bm.ground_under(*box, 220, 220, shades=32, fetch=gradient_tile, pause=0) assert levels is not None and settled and levels.shape == (220, 220) def test_a_map_kept_too_long_ago_is_built_again(monkeypatch): box = (47.0, -123.0, 48.0, -122.0) bm.ground_under(*box, 210, 210, shades=32, fetch=gradient_tile, pause=0) later = time.time() + bm.GROUND_CACHE_DAYS * 86_400 + 1 monkeypatch.setattr(bm.time, "time", lambda: later) asked = [] def counting(z, x, y, **kw): asked.append(1) return gradient_tile(z, x, y, **kw) bm.ground_under(*box, 210, 210, shades=32, fetch=counting, pause=0) assert asked, "a month-old map was believed" def test_forgetting_the_maps_keeps_the_tiles(): """The maps are quick to rebuild from tiles and the tiles are not quick to fetch again, so the cheap thing goes first.""" bm.ground_under(47.0, -123.0, 48.0, -122.0, 230, 230, shades=32, fetch=gradient_tile, pause=0) before = bm.cache_size() assert before["ground"]["files"] == 1 assert bm.forget_ground() == 1 after = bm.cache_size() assert after["ground"]["files"] == 0 assert after["tiles"]["files"] == before["tiles"]["files"] def test_the_cache_says_how_big_it_is(tmp_path, monkeypatch): monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path)) empty = bm.cache_size() assert empty["total"]["bytes"] == 0 and empty["total"]["files"] == 0 root = tmp_path / "bandsaunter" / "tiles" / "9" / "81" root.mkdir(parents=True) (root / "178.png").write_bytes(b"x" * 1000) got = bm.cache_size() assert got["tiles"] == {"bytes": 1000, "files": 1} assert got["total"]["bytes"] == 1000 def test_pruning_throws_away_the_least_recently_used(tmp_path, monkeypatch): """Least recently *used*, not oldest: a tile fetched a year ago and looked at last night is the receiver's own neighbourhood, and throwing that away to keep last week's holiday is the wrong way round.""" monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path)) where = tmp_path / "bandsaunter" / "tiles" / "9" / "81" where.mkdir(parents=True) now = time.time() for i, age in enumerate((100.0, 0.0, 50.0)): # middle one is freshest path = where / f"{i}.png" path.write_bytes(b"x" * 400_000) os.utime(path, (now - age * 86_400, now - age * 86_400)) assert bm.cache_size()["tiles"]["files"] == 3 gone = bm.prune_cache(limit_mb=0.5) assert gone == 2 left = list(where.glob("*.png")) assert [p.name for p in left] == ["1.png"], "it kept the wrong one" def test_pruning_a_cache_that_already_fits_does_nothing(): assert bm.prune_cache(limit_mb=10_000) == 0