The map draws its own icons on an overlay (#967)

An overlay drawn with a style of its own names icons, which needed the sprite sheet that style was made for.  The map now ships icons for overlays, the Maki and Temaki sets (both CC0), and always draws those, in both viewers.

The sprite sheet is the translation: its index lists every icon under its own name, under each other name it answers to (modules/map/icons/aliases.json), and in both spellings of each.  A name with no icon is drawn as a plain dot.  The icons are signed distance fields, so a style's icon color and halo apply.

A pattern is not an icon: what it filled (a fill, a line or an extrusion) is drawn in a translucent color, where removing a pattern used to leave MapLibre's opaque black.

A sprite sheet beside a map file is no longer used, served or reported.  wrolpi/scripts/build_map_icons.py builds the sheet from the SVGs; the sheet is committed.
This commit is contained in:
lrnselfreliance 2026-11-13 17:59:03 -07:00 • committed by Roland
parent 177d0dba4b
commit b2b8e2075c
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
794 changed files with 4193 additions and 252 deletions

View file

@ -290,53 +290,13 @@ def test_maps_config_is_shared(async_client):
@pytest.mark.asyncio
async def test_get_file_sprite(async_client, test_directory, make_pmtiles):
"""MapLibre asks for the sprite of a map file as `<map file>.json` and `.png`, and as `@2x` on a dense screen;
it gets the single resolution sheet when that is all there is."""
async def test_map_file_has_no_sprite_of_its_own(async_client, test_directory, make_pmtiles):
"""The map draws its own icons on an overlay: a sprite sheet beside a map file is not served or reported."""
make_pmtiles('map/extras/features.pmtiles', vector_metadata('towers'))
extras = test_directory / 'map/extras'
(extras / 'sprite.json').write_text('{"tower": {"x": 0, "y": 0, "width": 8, "height": 8, "pixelRatio": 1}}')
(extras / 'sprite.png').write_bytes(b'single')
(test_directory / 'map/extras/sprite.json').write_text('{}')
(test_directory / 'map/extras/sprite.png').write_bytes(b'png')
request, response = await async_client.get('/api/map/files')
assert 'has_sprite' not in response.json['files'][0]
request, response = await async_client.get('/api/map/files/sprite/extras/features.pmtiles.json')
assert response.status_code == HTTPStatus.OK
assert response.json['tower']['pixelRatio'] == 1
request, response = await async_client.get('/api/map/files/sprite/extras/features.pmtiles.png')
assert response.status_code == HTTPStatus.OK
assert response.body == b'single'
# There is no double resolution sheet.
request, response = await async_client.get('/api/map/files/sprite/extras/features.pmtiles@2x.png')
assert response.status_code == HTTPStatus.OK
assert response.body == b'single'
(extras / 'sprite@2x.json').write_text('{"tower": {"x": 0, "y": 0, "width": 16, "height": 16, "pixelRatio": 2}}')
(extras / 'sprite@2x.png').write_bytes(b'double')
request, response = await async_client.get('/api/map/files/sprite/extras/features.pmtiles@2x.png')
assert response.body == b'double'
request, response = await async_client.get('/api/map/files/sprite/extras/features.pmtiles@2x.json')
assert response.json['tower']['pixelRatio'] == 2
request, response = await async_client.get('/api/map/files/sprite/extras/features.pmtiles.png')
assert response.body == b'single'
@pytest.mark.asyncio
async def test_get_file_sprite_missing(async_client, test_directory, make_pmtiles):
"""Only the sprite of a map file is served."""
make_pmtiles('map/features.pmtiles', vector_metadata('towers'))
(test_directory / 'secret.json').write_text('{"secret": true}')
(test_directory / 'map/notes.json').write_text('{"notes": true}')
# The file has no sprite.
request, response = await async_client.get('/api/map/files/sprite/features.pmtiles.json')
assert response.status_code == HTTPStatus.NOT_FOUND
# There is no such map file.
request, response = await async_client.get('/api/map/files/sprite/missing.pmtiles.json')
assert response.status_code == HTTPStatus.NOT_FOUND
# Not the name of a sprite.
for name in ('notes.json', 'features.pmtiles', 'features.pmtiles.txt', 'features.pmtiles@3x.png'):
request, response = await async_client.get(f'/api/map/files/sprite/{name}')
assert response.status_code == HTTPStatus.BAD_REQUEST, name
# Outside of the map directory.
request, response = await async_client.get('/api/map/files/sprite/..%2Fsecret.pmtiles.json')
assert response.status_code in (HTTPStatus.BAD_REQUEST, HTTPStatus.NOT_FOUND)
assert b'secret' not in response.body or response.status_code == HTTPStatus.BAD_REQUEST
assert response.status_code in (HTTPStatus.NOT_FOUND, HTTPStatus.METHOD_NOT_ALLOWED)

View file

@ -0,0 +1,95 @@
import json
import pytest
from wrolpi.scripts import build_map_icons
SPRITES = build_map_icons.SPRITES_DIRECTORY
ICONS = build_map_icons.ICONS_DIRECTORY
SHEETS = ((1, ''), (2, '@2x'))
def _index(suffix: str) -> dict:
return json.loads((SPRITES / f'overlay-icons{suffix}.json').read_text())
@pytest.mark.parametrize('pixel_ratio,suffix', SHEETS)
def test_sprite_index_is_the_icons_and_their_names(pixel_ratio, suffix):
"""The committed sprite index lists every name of every icon; run wrolpi/scripts/build_map_icons.py after
changing the icons or their aliases."""
index = _index(suffix)
names = build_map_icons.find_names()
assert sorted(index) == sorted(names)
cells = {name: (i['x'], i['y']) for name, i in index.items()}
for name, icon in names.items():
# Every name of an icon is the same pixels.
assert cells[name] == cells[icon], name
# Each icon has pixels of its own (but for one whose name an alias gives to another icon).
icons = [i for i in build_map_icons.find_icons() if names[i] == i]
assert len({cells[i] for i in icons}) == len(icons)
for image in index.values():
assert image['sdf'] is True
assert image['pixelRatio'] == pixel_ratio
assert image['width'] == image['height'] == (build_map_icons.ICON_SIZE + 2 * build_map_icons.BUFFER) * pixel_ratio
def test_both_sheets_have_the_same_names():
assert sorted(_index('')) == sorted(_index('@2x'))
for suffix in ('', '@2x'):
assert (SPRITES / f'overlay-icons{suffix}.png').stat().st_size > 10_000
def test_icon_names():
"""A style may spell a name with underscores or hyphens; an alias is what a name means on a map."""
names = build_map_icons.find_names()
assert names['hospital'] == 'hospital'
assert names['fire_station'] == names['fire-station'] == 'fire-station'
assert names['drinking_water'] == 'drinking-water'
# Aliases, in both spellings.
assert names['ham_radio'] == names['ham-radio'] == 'antenna'
assert names['post_office'] == 'post'
# An alias comes before the icon which happens to have the name.
assert 'tower' in build_map_icons.find_icons()
assert names['tower'] == 'communications-tower'
def test_aliases_are_of_icons_which_exist():
icons = build_map_icons.find_icons()
aliases = json.loads((ICONS / 'aliases.json').read_text())
assert aliases
assert {k: v for k, v in aliases.items() if v not in icons} == {}
def test_excluded_icons_are_not_shipped():
"""A logo is not an icon of the map."""
excluded = json.loads((ICONS / 'exclude.json').read_text())
assert 'temaki' in excluded
names = build_map_icons.find_names()
for name in excluded:
assert name not in names
assert name not in _index('')
def test_icon_sets_have_their_licenses():
"""The icons are other people's work, given to the public domain; their licenses are shipped with them."""
for icon_set in build_map_icons.ICON_SETS:
assert 'CC0 1.0 Universal' in (ICONS / 'svg' / icon_set / 'LICENSE.txt').read_text()
notice = (ICONS / 'NOTICE.txt').read_text()
assert 'Maki' in notice and 'Temaki' in notice and 'CC0' in notice
def test_render_sdf(tmp_path):
"""An icon is a signed distance field: three quarters alpha at its edge, full inside, none far outside."""
svg = tmp_path / 'square.svg'
svg.write_text('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 15 15"><rect width="15" height="15"/></svg>')
alpha = build_map_icons.render_sdf(svg, 1)
cell = build_map_icons.ICON_SIZE + 2 * build_map_icons.BUFFER
assert alpha.shape == (cell, cell)
middle = cell // 2
assert alpha[middle, middle] == 255
# The corner of the cell is outside of the square, by the buffer.
assert alpha[0, 0] < 191
# Alpha falls going out across the edge.
assert alpha[middle, build_map_icons.BUFFER + 1] > alpha[middle, build_map_icons.BUFFER - 1] > alpha[middle, 0]
assert 150 < alpha[middle, build_map_icons.BUFFER - 1] < 230

View file

@ -263,13 +263,6 @@ def test_delete_pmtiles_file_forgets_ignored(async_client, test_directory, make_
assert file['visible'] is True
def _make_sprite(base, resolutions=('',)):
"""Create sprite sheets (`<base>.json`, `<base>.png`); each says which it is."""
for resolution in resolutions:
base.with_name(f'{base.name}{resolution}.json').write_text(f'{{"sheet": "{base.name}{resolution}"}}')
base.with_name(f'{base.name}{resolution}.png').write_bytes(f'{base.name}{resolution}'.encode())
def test_sidecar_style(test_directory, make_pmtiles):
"""A style beside a map file says how it is drawn: its own, else the one its directory shares."""
own = make_pmtiles('map/extras/own.pmtiles', vector_metadata('towers'))
@ -292,46 +285,6 @@ def test_sidecar_style(test_directory, make_pmtiles):
assert files['elsewhere.pmtiles']['style_map_path'] is None
assert files['extras/shared.pmtiles']['style_path'] == 'map/extras/style.json'
assert files['elsewhere.pmtiles']['style_path'] is None
assert {i['has_sprite'] for i in files.values()} == {False}
def test_sidecar_sprite(test_directory, make_pmtiles):
"""A sprite sheet is a .json and a .png; a file's own is used before the one its directory shares."""
own = make_pmtiles('map/extras/own.pmtiles', vector_metadata('towers'))
shared = make_pmtiles('map/extras/shared.pmtiles', vector_metadata('towers'))
extras = test_directory / 'map/extras'
# Half of a sheet is not a sheet.
(extras / 'sprite.json').write_text('{}')
assert lib.find_sidecar_sprite(shared) is None
_make_sprite(extras / 'sprite')
_make_sprite(extras / 'own.sprite')
assert lib.find_sidecar_sprite(shared, 'json') == extras / 'sprite.json'
assert lib.find_sidecar_sprite(shared, 'png') == extras / 'sprite.png'
assert lib.find_sidecar_sprite(own, 'png') == extras / 'own.sprite.png'
files = {i['map_path']: i['has_sprite'] for i in lib.get_pmtiles_files()}
assert files == {'extras/own.pmtiles': True, 'extras/shared.pmtiles': True}
def test_sidecar_sprite_resolution(test_directory, make_pmtiles):
"""The sheet of the other resolution is used when the one asked for does not exist."""
path = make_pmtiles('map/extras/features.pmtiles', vector_metadata('towers'))
extras = test_directory / 'map/extras'
_make_sprite(extras / 'sprite')
assert lib.find_sidecar_sprite(path, 'png', high_resolution=True) == extras / 'sprite.png'
_make_sprite(extras / 'sprite', ('@2x',))
assert lib.find_sidecar_sprite(path, 'png', high_resolution=True) == extras / 'sprite@2x.png'
assert lib.find_sidecar_sprite(path, 'json', high_resolution=True) == extras / 'sprite@2x.json'
assert lib.find_sidecar_sprite(path, 'png') == extras / 'sprite.png'
# Only the double resolution sheet exists.
(extras / 'sprite.json').unlink()
assert lib.find_sidecar_sprite(path, 'png') == extras / 'sprite@2x.png'
assert lib.find_sidecar_sprite(path, 'json') == extras / 'sprite@2x.json'
def test_sidecar_style_custom_map_directory(test_directory, make_pmtiles, test_wrolpi_config):