From 7548c2bb83ff06d2338d3fc52845257906b90207 Mon Sep 17 00:00:00 2001 From: "Bruno D." Date: Wed, 2 Dec 2026 17:04:42 -0500 Subject: [PATCH] LRC-MAP-VIEW: OpenStreetMap's tile servers keep their credit and zoom under any id A custom source pointed at OpenStreetMap's own tile servers (tile.openstreetmap.org, its a./b./c. names, any path, a trailing dot) is now the OpenStreetMap source whatever its id, so it always shows OpenStreetMap's licence credit and asks at most zoom 19; another credit or a higher zoom is refused. docs/map.md says so, and says exactly which files clearing the tile cache deletes. --- crates/engine/src/map/source.rs | 29 +++++++++++++++++++++++++++-- docs/map.md | 8 ++++++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/map/source.rs b/crates/engine/src/map/source.rs index adfb497f2a..d13fd75df9 100644 --- a/crates/engine/src/map/source.rs +++ b/crates/engine/src/map/source.rs @@ -251,10 +251,15 @@ impl TileSource { pub fn custom(id: Option<&str>, url_template: &str, attribution: Option<&str>, max_zoom: Option) -> Result { let url_template = url_template.trim(); validate_template(url_template)?; - if url_template == OSM_TILE_URL && id.is_none_or(|i| i.trim().is_empty() || i.trim() == "osm") { + // OpenStreetMap's tile servers, whatever the id or path: its source, with its credit, zoom and + // usage policy (a custom id must not drop the licence credit or ask past its zoom) + if is_osm_tile_host(url_template) { if max_zoom.is_some_and(|z| z != OSM_MAX_ZOOM) { return Err(bad(format!("OpenStreetMap has tiles up to zoom {OSM_MAX_ZOOM}"))); } + if attribution.map(str::trim).is_some_and(|a| !a.is_empty() && !a.contains("OpenStreetMap")) { + return Err(bad("OpenStreetMap's tiles are credited to OpenStreetMap contributors; leave the attribution empty")); + } return Ok(TileSource::Osm); } let attribution = match attribution.map(str::trim).filter(|a| !a.is_empty()) { @@ -293,6 +298,16 @@ impl TileSource { } } +/// Whether `template` asks OpenStreetMap's own tile servers (`tile.openstreetmap.org`, also its +/// older `a.`/`b.`/`c.` names). +fn is_osm_tile_host(template: &str) -> bool { + let host = template.split_once("://").map_or("", |(_, rest)| rest.split(['/', '?', '#']).next().unwrap_or("")); + let host = host.rsplit_once(':').map_or(host, |(h, port)| if port.bytes().all(|b| b.is_ascii_digit()) { h } else { host }); + // (a trailing dot names the same host) + let host = host.trim_end_matches('.').to_ascii_lowercase(); + host == "tile.openstreetmap.org" || host.ends_with(".tile.openstreetmap.org") +} + /// The part of the server's tile that makes a 256 px tile. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Crop { @@ -430,8 +445,16 @@ mod tests { ); let named = TileSource::custom(Some("my-tiles"), "https://a.example/{z}/{y}/{x}", Some("© A"), None).unwrap(); assert_eq!((named.id(), named.max_zoom()), ("my-tiles", 18)); - // OpenStreetMap's own template is OpenStreetMap + // OpenStreetMap's own servers are OpenStreetMap, whatever the id: its credit and zoom stay assert_eq!(TileSource::custom(None, OSM_TILE_URL, None, None).unwrap(), TileSource::Osm); + for (id, template, attribution) in [ + (Some("my-osm"), OSM_TILE_URL, None), + (Some("my-osm"), "https://a.tile.openstreetmap.org/{z}/{x}/{y}.png", Some("© OpenStreetMap contributors")), + (None, "https://TILE.openstreetmap.org/{z}/{x}/{y}.png?x=1", None), + (Some("my-osm"), "https://tile.openstreetmap.org./{z}/{x}/{y}.png", None), + ] { + assert_eq!(TileSource::custom(id, template, attribution, None).unwrap(), TileSource::Osm, "{template}"); + } for (template, attribution, zoom, id) in [ ("http://tiles.example.org/{z}/{x}/{y}.png", Some("x"), None, None), ("ftp://tiles.example.org/{z}/{x}/{y}.png", Some("x"), None, None), @@ -453,6 +476,8 @@ mod tests { ("https://tiles.example.org/{z}/{x}/{y}.png", Some("x"), None, Some("../etc")), ("https://tiles.example.org/{z}/{x}/{y}.png", Some("x"), None, Some("Upper")), (OSM_TILE_URL, None, Some(20), None), + (OSM_TILE_URL, Some("Other provider"), None, Some("my-osm")), + (OSM_TILE_URL, None, Some(20), Some("my-osm")), ] { assert!(TileSource::custom(id, template, attribution, zoom).is_err(), "{template} {attribution:?} {zoom:?} {id:?}"); } diff --git a/docs/map.md b/docs/map.md index 86cc9cb1eb..49824cade1 100644 --- a/docs/map.md +++ b/docs/map.md @@ -266,12 +266,16 @@ the `map.tileSource` command: The credit of the source in use is always painted at the bottom right of the map, never behind a toggle. A stored source is checked again when the library opens: one that is damaged or not `https://` falls back -to OpenStreetMap and the rest of `prefs.json` is kept. +to OpenStreetMap and the rest of `prefs.json` is kept. A custom source that points at OpenStreetMap's own +tile servers (`tile.openstreetmap.org`, whatever its id) is the OpenStreetMap source, with its credit and +zoom 19: another credit, or a higher zoom, is refused. **Cache.** Tiles are cached on disk in the per-user cache folder: `~/Library/Caches/LightCraft/maptiles` on macOS, `%LOCALAPPDATA%\LightCraft\Cache\maptiles` on Windows, `$XDG_CACHE_HOME/lightcraft/maptiles` (else `~/.cache/lightcraft/maptiles`) elsewhere, `/LightCraftData/Cache/maptiles` in portable mode, or -`$LIGHTCRAFT_MAP_CACHE` when set. Each source has its own subfolder. The cache follows the server's +`$LIGHTCRAFT_MAP_CACHE` when set. Each source has its own subfolder. Clearing and trimming the cache only +delete files named as LightCraft names its tiles (`///.tile`, with numeric `z`, `x` and +`y`, and their temporary files); point `$LIGHTCRAFT_MAP_CACHE` at a folder of its own. The cache follows the server's `Cache-Control` / `Expires` headers (7 days when there are none), refreshes an expired tile with a conditional request (`If-None-Match` / `If-Modified-Since`; a `304` only renews it) while still showing it, writes atomically (a temporary file renamed into place), deletes and fetches again an entry that is