Draw the map's missing squares as gaps, and its rings in your own units

Two faults reported from in front of the window, both of them the map saying
something confidently and wrongly.

Tiles that did not arrive.  Resizing the window does not refetch the map: the
window deliberately fetches more world than it shows, so a resize still fits
inside what is in hand and gets stretched to the new size.  It refetches when
the view leaves that box, which is what a new station does -- and that fetch
asks a volunteer-funded server for a hundred tiles that have never been on
this disk, all at once, at the sharper zoom the bigger window chose.  Some of
them are refused.

What the window did with a refusal was draw it.  A tile that never arrived
leaves its square of the canvas black, and black is not a neutral colour
here: the brightness is inverted on the way in, because a printed map is ink
on paper and this picture is the other way round.  So the darkest possible
square came out as the brightest thing on the picture, a glowing rectangle
where the map should be.  Measured on a reproduction, one missing tile in
eighteen put thirteen thousand pixels at full brightness -- and dragged the
floor of the map's own contrast down to black with it, so thirteen thousand
four hundred and ninety pixels changed in all: the whole map was redrawn
dimmer to make room for a square that was not there.  Then it was kept, cached
under the view it was fetched for, until the view moved again.

So the missing squares are asked for again at once, and only those, the rest
being on the disk by then; what is still missing is drawn as bare ground and
left out of the reckoning when the darkest and brightest of the map are worked
out, which puts the same reproduction at two pixels changed rather than
thirteen thousand four hundred and ninety, a hairline where a cell is averaged
over part of a tile and part of nothing; and the map is kept as provisional
rather than as the last word, asked for again half a minute later, four
attempts in all, each retrying its own misses once.

Found while measuring that: the politeness pause between requests was being
paid on every tile, including the ones read straight back off the disk.  Two
hundred and twenty tiles at an eighth of a second is twenty-six seconds of
sleeping to redraw a view that was entirely cached, and it would have made
asking again for three missing squares cost the wait for the two hundred that
were not.  The constant's own comment already said it should only be paid on a
tile that was not already there.  Now it is.

The APRS map's units.  Setting imperial changed nothing at all about the
window: the unit it measures in was hardcoded to kilometres, and that one
value drives the ring labels and the scale along the bottom; and --radius was
always read as kilometres, so the rings were not merely mislabelled, they were
at the wrong distance from the flag.  A ring is what a distance gets judged
against by eye, and one labelled in a unit it was not drawn in is a wrong
answer given confidently.  The aircraft side has done this properly all along
-- a radius read in whatever unit the speeds are in, and no unit suffix on the
setting because the suffix belongs to the other setting -- so this now mirrors
it exactly.  At --radius 100 in imperial the outermost ring stands seventy-
five statute miles from the flag and says so, where it used to stand seventy-
five kilometres and say kilometres whatever you had asked for.

Twenty-one new tests against sixteen deliberately broken builds.  One
survived, and removing what it broke was the right answer rather than
strengthening a test: a check that the remembered request still matched the
map in hand could not be made to fail, a request for a different view being
taken up only after the slot it guards is already full.  The tests do not
trust the drawing to mark its own homework -- the one that matters walks north
from the flag by each ring's radius and measures the great-circle distance
with a haversine written in the test, then checks that against the printed
label.  Full suite 2685 passed.  Built as 2026-09-21_03.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016PsWPTweCT6pwxKngvVxcg
This commit is contained in:
The Dust Council 2026-09-21 02:36:43 -07:00
parent e05b66ec3d
commit e203b3e581
11 changed files with 780 additions and 73 deletions

View file

@ -215,8 +215,9 @@ def counting_fetcher(answers=None, fail_on=()):
def test_the_tiles_are_stitched_in_the_right_places():
fetch = counting_fetcher()
raster, ox, oy = bm.mosaic(47.0, -122.8, 48.5, -121.0, 9, fetch=fetch,
pause=0)
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
@ -233,28 +234,33 @@ def test_a_tile_that_does_not_arrive_leaves_a_hole_rather_than_an_error():
seen = counting_fetcher()
bm.mosaic(47.0, -122.8, 48.5, -121.0, 9, fetch=seen, pause=0)
missing = seen.asked[:1]
raster, _, _ = bm.mosaic(47.0, -122.8, 48.5, -121.0, 9,
fetch=counting_fetcher(fail_on=missing), pause=0)
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, _, _ = bm.mosaic(47.0, -122.8, 48.5, -121.0, 9, fetch=nothing,
pause=0)
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, _, _ = bm.mosaic(47.0, -122.8, 48.5, -121.0, 9, fetch=rubbish,
pause=0)
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()
# ---------------------------------------------------------------------------
@ -269,14 +275,197 @@ def gradient_tile(z, x, y, **kw):
def test_the_ground_comes_back_as_levels_the_map_can_paint():
levels = bm.ground_under(47.0, -122.8, 48.5, -121.0, 200, 150,
shades=32, fetch=gradient_tile, pause=0)
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()
whole, _ = bm.ground_under(*BOX, 400, 400, fetch=_patchy(()), pause=0,
zoom=zoom)
holed, _ = bm.ground_under(*BOX, 400, 400, fetch=_patchy({hole}),
pause=0, retry_pause=0, zoom=zoom)
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)
holed, _ = bm.ground_under(*BOX, 400, 400, fetch=_patchy({hole}),
pause=0, retry_pause=0, zoom=zoom)
# 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)
assert whole is not None and settled
holed, settled = bm.ground_under(*BOX, 400, 400, fetch=_patchy({hole}),
pause=0, retry_pause=0)
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)
healed, settled = bm.ground_under(*BOX, 400, 400, fetch=flaky, pause=0,
retry_pause=0)
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.
@ -298,7 +487,8 @@ 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 = bm.ground_under(south, west, north, east, 64, 64, shades=32,
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.
@ -313,7 +503,8 @@ def test_north_is_at_the_top():
return make_png(px)
south, west, north, east = tile_bounds(9, 81, 178)
levels = bm.ground_under(south, west, north, east, 40, 40, shades=32,
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
@ -325,7 +516,8 @@ def test_east_is_to_the_right():
return make_png(px)
south, west, north, east = tile_bounds(9, 81, 178)
levels = bm.ground_under(south, west, north, east, 40, 40, shades=32,
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()
@ -338,7 +530,8 @@ def test_one_tile_covers_its_own_box_exactly():
return make_png(px)
south, west, north, east = tile_bounds(9, 81, 178)
levels = bm.ground_under(south, west, north, east, 128, 128, shades=32,
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()
@ -349,13 +542,18 @@ def test_no_tiles_means_no_ground_rather_than_an_exception():
def nothing(z, x, y, **kw):
return None
assert bm.ground_under(47.0, -122.8, 48.5, -121.0, 50, 50,
fetch=nothing, pause=0) is 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():
assert bm.ground_under(48.0, -122.0, 47.0, -123.0, 50, 50,
fetch=gradient_tile, pause=0) is None
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
# ---------------------------------------------------------------------------
@ -655,7 +853,8 @@ def test_a_cell_smaller_than_a_source_pixel_takes_that_pixel():
def test_a_gradient_still_comes_out_as_a_gradient():
south, west, north, east = tile_bounds(9, 81, 178)
levels = bm.ground_under(south, west, north, east, 64, 64, shades=32,
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"
@ -664,16 +863,18 @@ def test_a_gradient_still_comes_out_as_a_gradient():
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 = bm.ground_under(south, west, north, east, width, height,
shades=32, fetch=gradient_tile, zoom=9,
pause=0)
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 = bm.ground_under(south, west, north, east, 2000, 2000, shades=32,
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()