From 9938a59d57721da0209cbabef72a06c0475b7430 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Wed, 18 Nov 2026 10:16:19 +0100 Subject: [PATCH 1/7] fix(tiles): raster tiles and WMS pictures are sharp on HiDPI displays (#2390) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Raster tiles pick their zoom by the display's own pixels, up to twice the logical detail, and single pictures are asked for at the same detail; a WMS GetMap picture says the resolution it came out at (DPI, MAP_RESOLUTION, FORMAT_OPTIONS=dpi:). Settings › Map Tiles › Sharp Tiles on HiDPI Displays (on by default, ui.sharpTiles) turns it off. --- crates/engine/src/cmd/raster.rs | 2 +- crates/render/src/single_image.rs | 72 +++++++++++++++++++++++++++++-- crates/render/src/tiles.rs | 44 +++++++++++++++++-- crates/tiles/src/draw.rs | 3 +- crates/tiles/src/template.rs | 39 +++++++++++++++++ crates/ui-egui/src/chrome.rs | 1 + crates/ui-egui/src/lib.rs | 45 +++++++++++++++++++ crates/ui-egui/src/ribbon.rs | 12 +++++- docs/user-guide/data-formats.md | 28 ++++++++++-- 9 files changed, 231 insertions(+), 15 deletions(-) diff --git a/crates/engine/src/cmd/raster.rs b/crates/engine/src/cmd/raster.rs index 0b094adf53..b04cbaae22 100644 --- a/crates/engine/src/cmd/raster.rs +++ b/crates/engine/src/cmd/raster.rs @@ -88,7 +88,7 @@ pub fn commands() -> Vec { ), C::new("raster.info", "Raster Information", "Data › Information", info).view().when(need_raster).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"layer"?}"#), C::new("share.exportRaster", "Export Raster", "Data › Information", |s, p| super::exports::write_here(s, p, "share.exportRaster")).view().when(need_exportable).inputs(export_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).params( - r#"{"layer"?, "path": "*.tif", "band"?: 1.. (rasters that aren't colour ones; a colour raster writes its colour bands), "cog"?: bool (a cloud-optimised GeoTIFF: 512 px tiles, Deflate, overviews; default false, an untiled GeoTIFF), "tiles"?: "a tile layer's name, or its index (0 = top): its raster tiles (XYZ, WMTS, WMS, a raster archive) put together as an RGBA GeoTIFF in Web Mercator; by default the tile layer picked in the Layers pane, when no layer is given or active", "bbox"?: "[west, south, east, north] in degrees, for a tile layer (default: the view's)", "zoom"?: "0..30, the tiles' zoom (default: the zoom the view shows); at most 1024 tiles and 8192 pixels a side"}"#, + r#"{"layer"?, "path": "*.tif", "band"?: 1.. (rasters that aren't colour ones; a colour raster writes its colour bands), "cog"?: bool (a cloud-optimised GeoTIFF: 512 px tiles, Deflate, overviews; default false, an untiled GeoTIFF), "tiles"?: "a tile layer's name, or its index (0 = top): its raster tiles (XYZ, WMTS, WMS, a raster archive) put together as an RGBA GeoTIFF in Web Mercator; by default the tile layer picked in the Layers pane, when no layer is given or active", "bbox"?: "[west, south, east, north] in degrees, for a tile layer (default: the view's)", "zoom"?: "0..30, the tiles' zoom (default: the zoom the view shows at one pixel a point, as on a standard-density screen); at most 1024 tiles and 8192 pixels a side"}"#, ), ] } diff --git a/crates/render/src/single_image.rs b/crates/render/src/single_image.rs index 3670c4d12f..08c0588e5a 100644 --- a/crates/render/src/single_image.rs +++ b/crates/render/src/single_image.rs @@ -149,6 +149,33 @@ fn picture(area: [f64; 4], res: f64, max_side: u32) -> Option<([f64; 4], u32, u3 out.iter().all(|v| v.is_finite()).then_some((out, w as u32, h as u32)) } +/// A WMS GetMap address for a picture of `detail` pixels a logical one (as the picture asked +/// for came out, past any size limit), with the +/// resolution the servers that read one draw it at: `DPI` (GeoServer, QGIS Server), +/// `MAP_RESOLUTION` (MapServer, QGIS Server) and `FORMAT_OPTIONS=dpi:` (GeoServer), so their +/// symbols and text keep their size in the larger picture. Any other address, a picture at a +/// logical pixel's detail, or one naming a resolution already, is left as it is. +fn with_dpi(url: String, detail: f64) -> String { + if !(detail > 1.0 && detail.is_finite()) { + return url; + } + let (address, fragment) = url.split_once('#').map_or((url.as_str(), None), |(a, f)| (a, Some(f))); + let Some((_, query)) = address.split_once('?') else { return url }; + let keys: Vec<(&str, &str)> = query.split('&').map(|part| part.split_once('=').unwrap_or((part, ""))).collect(); + let has = |key: &str| keys.iter().any(|(k, _)| k.eq_ignore_ascii_case(key)); + let is = |key: &str, value: &str| keys.iter().any(|(k, v)| k.eq_ignore_ascii_case(key) && v.eq_ignore_ascii_case(value)); + if !is("REQUEST", "GetMap") || ["DPI", "MAP_RESOLUTION", "FORMAT_OPTIONS"].iter().any(|k| has(k)) { + return url; + } + let dpi = (96.0 * detail).round(); + let mut out = format!("{address}&DPI={dpi}&MAP_RESOLUTION={dpi}&FORMAT_OPTIONS=dpi:{dpi}"); + if let Some(f) = fragment { + out.push('#'); + out.push_str(f); + } + out +} + /// How long a layer whose large picture failed is asked for smaller ones before a large one is /// tried again (the failure may have been passing: a timeout, a busy server). const SMALL_FOR: web_time::Duration = web_time::Duration::from_secs(300); @@ -223,10 +250,13 @@ pub(crate) fn draw(pm: &mut Pixmap, req: &Request, t: &TileLayer, view: &View, f if rustgis_tiles::template::zoom_for(merc, t.tile_size, rustgis_map::tiles::MAX_ZOOM) < t.min_zoom { return nothing; } - // Closer in than its finest zoom, its picture there is stretched (as its finest tiles are). - let finer = (zoom_res(t.max_zoom, t.tile_size) / merc).max(1.0); let cam = view.camera; let (w, h, k) = (view.width, view.height, view.pixels_per_point.max(0.01)); + // A pixel of the picture for each of the display's own on a HiDPI one, when sharp tiles are + // on, so the picture isn't stretched (#2390). + let detail = f64::from(rustgis_tiles::template::detail(k as f32)); + // Closer in than its finest zoom, its picture there is stretched (as its finest tiles are). + let finer = (zoom_res(t.max_zoom, t.tile_size) * detail / merc).max(1.0); let to_system = |m: Coord| -> Option { match &req.transformer { None => Some(m), @@ -257,7 +287,10 @@ pub(crate) fn draw(pm: &mut Pixmap, req: &Request, t: &TileLayer, view: &View, f let (Some(a), Some(b)) = (to_system(cam.to_map(w / 2.0, h / 2.0, w, h)), to_system(cam.to_map(w / 2.0 + 1.0, h / 2.0, w, h))) else { return nothing; }; - let res = (b.x - a.x).hypot(b.y - a.y) * finer; + // The system's units a logical pixel, and a pixel of the picture asked for: coarser where it + // is stretched past its finest zoom. + let logical = (b.x - a.x).hypot(b.y - a.y); + let res = logical * finer / detail; if !(lo.0 < hi.0 && lo.1 < hi.1) { return nothing; } @@ -298,6 +331,7 @@ pub(crate) fn draw(pm: &mut Pixmap, req: &Request, t: &TileLayer, view: &View, f }, None => (at, pw, ph), }; + // The picture's own units a pixel: coarser than asked past the most a service draws. let r = (at[2] - at[0]) / f64::from(pw.max(1)); // The layer's last picture holding the whole view at this detail, with room to move, is // drawn instead: a view turned (its box wider or narrower at each angle) or made smaller @@ -317,7 +351,7 @@ pub(crate) fn draw(pm: &mut Pixmap, req: &Request, t: &TileLayer, view: &View, f paint(pm, img, *last_at); return nothing; } - let url = Warp::map_address(&req.base, &req.system, at, pw, ph); + let url = with_dpi(Warp::map_address(&req.base, &req.system, at, pw, ph), logical / r); match cache::get(&url, Want::Image) { State::Ready(payload) => { if let Payload::Image(img) = &*payload { @@ -584,6 +618,36 @@ mod tests { assert_eq!(in_world([0.0, h + 10.0, 100.0, h + 20.0], 100, 10, world), Some(None)); } + /// A WMS picture of more pixels than the view's logical ones says the resolution it is for, + /// in the parameters servers read (#2390); any other is asked as it was. + #[test] + fn a_hidpi_wms_picture_says_its_resolution() { + let wms = "https://m.example.org/wms?SERVICE=WMS&REQUEST=GetMap&LAYERS=a&WIDTH=800&HEIGHT=600"; + assert_eq!(with_dpi(wms.into(), 2.0), format!("{wms}&DPI=192&MAP_RESOLUTION=192&FORMAT_OPTIONS=dpi:192")); + assert_eq!(with_dpi(wms.into(), 1.5), format!("{wms}&DPI=144&MAP_RESOLUTION=144&FORMAT_OPTIONS=dpi:144")); + // At a logical pixel's detail, or stretched past its finest zoom: as it was. + for detail in [1.0, 0.4, f64::NAN, f64::INFINITY] { + assert_eq!(with_dpi(wms.into(), detail), wms, "{detail}"); + } + // Lower case keys, and a fragment kept last. + let lower = "https://m.example.org/wms?service=wms&request=getmap&layers=a#rustgis-x"; + assert_eq!( + with_dpi(lower.into(), 2.0), + "https://m.example.org/wms?service=wms&request=getmap&layers=a&DPI=192&MAP_RESOLUTION=192&FORMAT_OPTIONS=dpi:192#rustgis-x" + ); + // A resolution of its own, a request that isn't GetMap, or no query: as it was. + for own in [ + format!("{wms}&dpi=300"), + format!("{wms}&FORMAT_OPTIONS=antialias:none"), + format!("{wms}&map_resolution=72"), + "https://m.example.org/wms?SERVICE=WMS&REQUEST=GetCapabilities".into(), + "https://s.example.org/x/MapServer/export?bbox=1,2,3,4&size=800,600&f=image".into(), + "https://m.example.org/tile.png".into(), + ] { + assert_eq!(with_dpi(own.clone(), 2.0), own); + } + } + #[test] fn asked_for_by_its_own_address() { let wms = "https://m.example.org/wms?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&LAYERS=a&STYLES=&CRS=EPSG:3857&BBOX={bbox-epsg-3857}&WIDTH=256&HEIGHT=256&FORMAT=image%2Fpng&TRANSPARENT=TRUE"; diff --git a/crates/render/src/tiles.rs b/crates/render/src/tiles.rs index f9ea676bd7..062c379032 100644 --- a/crates/render/src/tiles.rs +++ b/crates/render/src/tiles.rs @@ -1072,6 +1072,12 @@ pub(crate) mod tests { ([px(5, 5), px(200, 100), px(394, 194)], std::mem::take(&mut *ASKED_ONE.lock().unwrap())) }; let green = [20, 200, 20, 255]; + // The resolution a picture says it is for (#2390): 96 a logical pixel's worth of its own + // pixels, which are up to a quarter of a level finer than asked for. + let dpi = |u: &str| -> Option { + let d: f64 = u.split("&DPI=").nth(1)?.split('&').next()?.parse().ok()?; + u.contains(&format!("&MAP_RESOLUTION={d}&FORMAT_OPTIONS=dpi:{d}")).then_some(d) + }; // In Web Mercator. let mut m = Map::new("Map"); m.graticule = false; @@ -1079,21 +1085,50 @@ pub(crate) mod tests { m.tiles.push(TileLayer::new("WMS", TileKind::Raster, url)); let (_, tiled) = at(&mut m, 30.0, 0.0); assert!(tiled.len() > 1 && tiled.iter().all(|u| u.contains("&WIDTH=256&HEIGHT=256&")), "{tiled:?}"); + // Tiles for the display's own pixels (#2390): 30 m a point is 15 a pixel at 2 a point, + // zoom 13 (4.9 km tiles) rather than 12. + let bbox_width = |u: &str| -> Option { + let b: Vec = u.split("BBOX=").nth(1)?.split('&').next()?.split(',').filter_map(|v| v.parse().ok()).collect(); + Some(b.get(2)? - b.first()?) + }; + let z13 = 2.0 * rustgis_tiles::template::HALF_WORLD / 8192.0; + assert!(tiled.iter().all(|u| bbox_width(u).is_some_and(|w| (w - z13).abs() < 1.0)), "{tiled:?}"); m.tiles[0].single_image = true; let (p, asked) = at(&mut m, 30.0, 0.0); assert_eq!(p, [green; 3], "the whole view drawn"); - // One picture, past the view (200 × 100 logical pixels: 512 × 256 with the margin). + // One picture, past the view, for the display's own pixels (200 × 100 logical pixels at + // 2 a point: 768 × 512 with the margin), at their resolution (#2390). assert_eq!(asked.len(), 1, "{asked:?}"); - assert!(asked[0].contains("&CRS=EPSG:3857&BBOX=") && asked[0].contains("&WIDTH=512&HEIGHT=256&"), "{}", asked[0]); + assert!(asked[0].contains("&CRS=EPSG:3857&BBOX=") && asked[0].contains("&WIDTH=768&HEIGHT=512&"), "{}", asked[0]); + assert!(dpi(&asked[0]).is_some_and(|d| (192.0..229.0).contains(&d)), "{}", asked[0]); // A small pan: the same picture, from the cache. let (p, asked) = at(&mut m, 30.0, 300.0); assert_eq!((p, asked.len()), ([green; 3], 0)); + // A view whose picture would pass the most a service draws (1500 points at 2 a point: + // 5632 pixels with the margin) is asked coarser, and says the resolution it came out at, + // not the screen's. + { + let center = m.to_map(bern).unwrap(); + let view = View { + width: 1500.0, + height: 100.0, + pixels_per_point: 2.0, + camera: Camera { center, res: 30.0, bearing: 0.0, pitch: 0.0 }, + sky: false, + }; + ASKED_ONE.lock().unwrap().clear(); + crate::render_png(&mut m, &view).unwrap(); + let asked = std::mem::take(&mut *ASKED_ONE.lock().unwrap()); + let said: Vec = asked.iter().filter_map(|u| dpi(u)).collect(); + assert!(said.len() == 1 && (135.0..162.0).contains(&said[0]), "{asked:?}"); + } // Closer in than its finest zoom (10: 153 m a pixel), its picture there stretched, as its // finest tiles are: fewer pixels for the same view. m.tiles[0].max_zoom = 10; let (p, asked) = at(&mut m, 30.0, 0.0); assert_eq!((p, asked.len()), ([green; 3], 1)); assert!(asked[0].contains("&WIDTH=256&HEIGHT=256&"), "{}", asked[0]); + assert!(!asked[0].contains("DPI"), "a stretched picture is asked at its own resolution: {}", asked[0]); // Zoomed out past its coarsest zoom: nothing asked, nothing drawn. m.tiles[0].max_zoom = 19; m.tiles[0].min_zoom = 15; @@ -1115,7 +1150,7 @@ pub(crate) mod tests { } assert_eq!(p, [green; 3], "drawn in tiles"); assert!(took < std::time::Duration::from_secs(10), "waited {took:?} for another's fetch"); - assert!(asked.iter().any(|u| u.contains("&WIDTH=512&HEIGHT=256&")), "its picture asked first: {asked:?}"); + assert!(asked.iter().any(|u| u.contains("&WIDTH=768&HEIGHT=512&")), "its picture asked first: {asked:?}"); // In a system of its own, on a map in it. let lv95 = System { epsg: 2056, name: "EPSG:2056".into(), lat_first: false }; let wms = Warp { epsg: 2056, grid: Grid::Map { lat_first: false }, area: Some([5.9, 45.8, 10.5, 47.8]), systems: vec![lv95] }.address( @@ -1130,7 +1165,8 @@ pub(crate) mod tests { let (p, asked) = at(&mut m, 5.0, 0.0); assert_eq!(p, [green; 3]); assert_eq!(asked.len(), 1, "{asked:?}"); - assert!(asked[0].contains("&CRS=EPSG:2056&BBOX=2") && asked[0].ends_with("&WIDTH=512&HEIGHT=256"), "{}", asked[0]); + assert!(asked[0].contains("&CRS=EPSG:2056&BBOX=2") && asked[0].contains("&WIDTH=768&HEIGHT=512&DPI="), "{}", asked[0]); + assert!(dpi(&asked[0]).is_some_and(|d| (192.0..229.0).contains(&d)), "{}", asked[0]); } /// Turning the map round asks a single picture layer for a picture or two, not one every few diff --git a/crates/tiles/src/draw.rs b/crates/tiles/src/draw.rs index 5744567ee3..af94dec069 100644 --- a/crates/tiles/src/draw.rs +++ b/crates/tiles/src/draw.rs @@ -624,7 +624,8 @@ pub(crate) fn tiles_in( let (tiles, from, min_z, max_z, want, z) = match src { Source::Raster { tiles, from, tile_size, min_zoom, max_zoom, .. } | Source::RasterDem { tiles, from, tile_size, min_zoom, max_zoom, .. } => { - let z = template::zoom_for(frame.m_per_px * f64::from(frame.ppp), *tile_size, *max_zoom); + // By the display's own pixels on a HiDPI one, when sharp tiles are on (#2390). + let z = template::zoom_for(frame.m_per_px * f64::from(frame.ppp / template::detail(frame.ppp)), *tile_size, *max_zoom); (tiles, from, *min_zoom, *max_zoom, Want::Image, z) } Source::Vector { tiles, from, min_zoom, max_zoom, .. } => { diff --git a/crates/tiles/src/template.rs b/crates/tiles/src/template.rs index 8e77330e70..7d3c4be623 100644 --- a/crates/tiles/src/template.rs +++ b/crates/tiles/src/template.rs @@ -125,6 +125,38 @@ pub fn covering(e: &Extent, z: u8) -> Vec { out } +/// The most a raster tile's or a single picture's detail follows a HiDPI display: twice its +/// logical pixels. A 3x phone or a 300-DPI print would ask for nine times or more the tiles, and +/// the text drawn into them would come out unreadably small. +pub const MAX_DETAIL: f32 = 2.0; + +static SHARP: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(true); + +/// Turns sharp raster tiles on HiDPI displays on or off (Settings › Map Tiles; on by default): +/// raster tiles and single pictures are fetched for the display's own pixels, up to +/// [`MAX_DETAIL`], rather than for its logical ones. Their text and lines come out sharp and +/// smaller, as in a HiDPI view of the service's own map. +pub fn set_sharp(on: bool) { + SHARP.store(on, std::sync::atomic::Ordering::Relaxed); +} + +/// Whether sharp raster tiles are on ([`set_sharp`]). +pub fn sharp() -> bool { + SHARP.load(std::sync::atomic::Ordering::Relaxed) +} + +/// The device pixels a raster tile's or a single picture's pixel is fetched for at `ppp` device +/// pixels a logical one: `ppp` up to [`MAX_DETAIL`] when sharp tiles are on, else 1 (a pixel a +/// logical pixel, stretched over `ppp` device pixels). +pub fn detail(ppp: f32) -> f32 { + detail_with(ppp, sharp()) +} + +/// [`detail`] with the setting given. +pub fn detail_with(ppp: f32, sharp: bool) -> f32 { + if sharp && ppp.is_finite() { ppp.clamp(1.0, MAX_DETAIL) } else { 1.0 } +} + /// The zoom level whose `tile_size`-pixel tiles best match `m_per_px` metres per pixel, rounded /// to the sharper level a little before halfway. Never below 0 or above `max`. pub fn zoom_for(m_per_px: f64, tile_size: u32, max: u8) -> u8 { @@ -461,6 +493,13 @@ mod tests { assert_eq!(zoom_for(156_543.03 / 1024.0, 512, 22), 9); assert_eq!(zoom_for(0.001, 256, 19), 19); assert_eq!(zoom_for(f64::NAN, 256, 19), 0); + // On a 2x display a sharp view takes the next zoom; a 3x one no further (#2390). + let logical = 156_543.03 / 1024.0; + assert_eq!(zoom_for(logical / f64::from(detail_with(2.0, true)), 256, 22), 11); + assert_eq!(zoom_for(logical / f64::from(detail_with(3.0, true)), 256, 22), 11); + assert_eq!(zoom_for(logical / f64::from(detail_with(2.0, false)), 256, 22), 10); + assert_eq!(detail_with(0.5, true), 1.0, "never coarser than a logical pixel"); + assert_eq!(detail_with(f32::NAN, true), 1.0); assert!((style_zoom(78_271.517) - 0.0).abs() < 1e-3); let e = TileId { z: 1, x: 1, y: 0 }.extent(); assert!((e.min_x - 0.0).abs() < 1e-6 && (e.max_y - HALF_WORLD).abs() < 1e-6); diff --git a/crates/ui-egui/src/chrome.rs b/crates/ui-egui/src/chrome.rs index f8cc290878..7273ec5208 100644 --- a/crates/ui-egui/src/chrome.rs +++ b/crates/ui-egui/src/chrome.rs @@ -893,6 +893,7 @@ pub(crate) const UI_COMMANDS: &[(&str, &str, &str)] = &[ ("ui.themeReload", "Reload Themes", "Settings › Theme"), ("ui.scale", "Reset Interface Scale", "Settings › Theme"), ("ui.datumGrids", "Toggle Downloading Datum Grids", "Settings › Datum Grids"), + ("ui.sharpTiles", "Toggle Sharp Tiles on HiDPI Displays", "Settings › Map Tiles"), ("ui.map.gpu", "Toggle GPU Rendering", "View"), ("ui.importShapefile", "Import Shapefile with Projection…", "Home › Add"), ("ui.measurementUnits", "Toggle Imperial Measurements", "Home › Inspect › Measure"), diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 07a2b8dbb0..960efd3154 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -766,6 +766,9 @@ pub struct UiState { /// Whether datum grids a coordinate system needs are downloaded from PROJ's grid server /// (#2032 A4): off unless turned on, as it reaches a server. pub download_grids: bool, + /// Whether raster tiles and single pictures are fetched for a HiDPI display's own pixels + /// (#2390): on unless turned off. + pub sharp_tiles: bool, /// Key combinations the native menu bar keeps for itself (macOS: Quit, Close Window, Hide…): /// a shortcut on one would never reach the app, so the shortcut editor refuses them. pub reserved_shortcuts: Vec, @@ -1041,6 +1044,7 @@ impl Default for UiState { start_with: StartWith::Sample, ask_layers: true, download_grids: false, + sharp_tiles: true, reserved_shortcuts: Vec::new(), choosing: Vec::new(), last_project: None, @@ -1650,6 +1654,8 @@ impl RustGisApp { self.ui.ask_layers = self.services.load_pref.as_ref().and_then(|load| load("ask_file_layers")).is_none_or(|v| v != "off"); self.ui.download_grids = self.services.load_pref.as_ref().and_then(|load| load("download_datum_grids")).is_some_and(|v| v == "on"); rustgis_engine::grids::set_enabled(self.ui.download_grids); + self.ui.sharp_tiles = self.services.load_pref.as_ref().and_then(|load| load("sharp_tiles")).is_none_or(|v| v != "off"); + rustgis_engine::render::rustgis_tiles::template::set_sharp(self.ui.sharp_tiles); // A stored projection this version can't use means Web Mercator. if let Some(code) = self.services.load_pref.as_ref().and_then(|load| load("start_projection")) && let Ok(p) = rustgis_engine::map::Projection::from_code(&code) @@ -3622,6 +3628,20 @@ impl RustGisApp { } return Ok(json!({"on": self.ui.download_grids})); } + // Whether raster tiles are sharp on a HiDPI display (#2390). + "ui.sharpTiles" => { + // Without `on` it toggles; a value that isn't true or false is refused. + self.ui.sharp_tiles = match p.get("on") { + None | Some(Value::Null) => !self.ui.sharp_tiles, + Some(v) => v.as_bool().ok_or("on must be true or false")?, + }; + rustgis_engine::render::rustgis_tiles::template::set_sharp(self.ui.sharp_tiles); + if let Some(save) = &self.services.save_pref { + save("sharp_tiles", if self.ui.sharp_tiles { "on" } else { "off" }); + } + self.map.invalidate(); + return Ok(json!({"on": self.ui.sharp_tiles})); + } "ui.recover" => return autosave::recover(self, p), "ui.startup" => { // `project` (#377) or, as before, `blank`; `project` wins when both are given. @@ -9220,6 +9240,31 @@ mod datum_grid_setting_tests { use rustgis_engine::{Project, Session}; use serde_json::json; + /// Sharp tiles on HiDPI displays (#2390) are on by default, kept as a choice, read at start + /// and turned off by it; a value that isn't true or false is refused. + #[test] + fn sharp_tiles_are_a_kept_choice_on_by_default() { + let saved = std::sync::Arc::new(std::sync::Mutex::new(Vec::new())); + let log = saved.clone(); + let services = crate::Services { + load_pref: Some(Box::new(|k| (k == "sharp_tiles").then(|| "off".to_string()))), + save_pref: Some(Box::new(move |k, v| log.lock().unwrap().push((k.to_string(), v.to_string())))), + ..crate::Services::default() + }; + let mut app = RustGisApp::new(Session::new(Project::default())).with_services(services); + // The setting itself is shared by the tests running beside this one: only the choice is + // looked at, and it is left on. + assert!(!app.ui.sharp_tiles, "read at start"); + assert_eq!(app.run("ui.sharpTiles", json!({"on": true})).unwrap(), json!({"on": true})); + assert_eq!(saved.lock().unwrap().last().cloned(), Some(("sharp_tiles".to_string(), "on".to_string()))); + for junk in [json!({"on": "false"}), json!({"on": 0})] { + assert!(app.run("ui.sharpTiles", junk).is_err()); + assert!(app.ui.sharp_tiles); + } + // Without a choice kept: on. + assert!(RustGisApp::new(Session::new(Project::default())).ui.sharp_tiles); + } + /// Downloading datum grids is off unless turned on (it reaches a server), and the choice is /// kept as a preference. Turned on only by `rustgis-engine`'s own test, which reaches no /// network: here, where other tests run beside it, it stays off. diff --git a/crates/ui-egui/src/ribbon.rs b/crates/ui-egui/src/ribbon.rs index 8d60c3131c..ec6a42356b 100644 --- a/crates/ui-egui/src/ribbon.rs +++ b/crates/ui-egui/src/ribbon.rs @@ -3717,7 +3717,8 @@ fn plugin_tab(app: &mut RustGisApp, ui: &mut Ui, plugin: &str) { } } -/// The Settings tab: theme, scale, customization, language, start-up and autosave. +/// The Settings tab: theme, scale, customization, language, start-up, datum grids, map tiles, +/// performance and autosave. fn settings_tab(app: &mut RustGisApp, ui: &mut Ui) { group(ui, "Theme", |ui| { theme_menu(app, ui); @@ -3769,6 +3770,15 @@ fn settings_tab(app: &mut RustGisApp, ui: &mut Ui) { let _ = app.run("ui.datumGrids", json!({"on": on})); } }); + group(ui, "Map Tiles", |ui| { + let mut on = app.ui.sharp_tiles; + let tip = tl!( + "On a high-resolution (HiDPI) display, fetch raster tiles and WMS pictures for the display's own pixels, up to twice the detail: text and lines in them come out sharp and smaller. Off, they are fetched for its logical pixels and stretched: softer, with larger text." + ); + if ui.checkbox(&mut on, tl!("Sharp Tiles on HiDPI Displays")).on_hover_text(tip).clicked() { + let _ = app.run("ui.sharpTiles", json!({"on": on})); + } + }); // The browser has no threads to set. if !cfg!(target_arch = "wasm32") { group(ui, "Performance", |ui| threads_menu(app, ui)); diff --git a/docs/user-guide/data-formats.md b/docs/user-guide/data-formats.md index 784adf89b8..c235595bb3 100644 --- a/docs/user-guide/data-formats.md +++ b/docs/user-guide/data-formats.md @@ -450,10 +450,14 @@ it's given `"singleImage": false` or a `"tileSize"`. - The picture reaches past the view, its middle on a grid, so small pans and zooms (most of a quarter of a level) ask for nothing new; while the next picture loads, the last one stays in place. -- It is asked for in the map's logical pixels, as tiles are, so the service's symbols keep their - size on a high-density screen; at most 4096 pixels a side (2048 for five minutes after a larger - picture of the layer fails), coarser for a view larger than that. A picture the service fails - to draw leaves the layer drawn in tiles there. +- On a high-density (HiDPI) screen it is asked for in the screen's own pixels, up to twice the + map's logical pixels, as tiles are (see [Sharp tiles on HiDPI displays](#sharp-tiles-on-hidpi-displays)). + A WMS picture then says the resolution it is for (`DPI`, `MAP_RESOLUTION` and + `FORMAT_OPTIONS=dpi:`, unless its address names one), so GeoServer, MapServer and QGIS Server + keep their symbols and text at their size; a service that reads none of them draws them + smaller. It is at most 4096 pixels a side (2048 for five minutes after a larger picture of the + layer fails), coarser for a view larger than that. A picture the service fails to draw leaves + the layer drawn in tiles there. - Nothing is asked for zoomed out past the layer's scale range or outside its bounds, and closer in than its finest zoom its picture there is stretched, as its tiles are. - It is drawn so on a flat map, north up or turned, in Web Mercator or in a projection with an @@ -472,6 +476,22 @@ tiles of 256, 512 or 1024 pixels (`"tileSize"`): larger tiles cut fewer labels a Tiles are fetched while the map moves, and kept for panning back. A layer in a project saved before keeps its tiles until you switch it. +### Sharp tiles on HiDPI displays + +On a high-density screen (a Retina or 4K display scaled 2x, a phone), raster tile layers (XYZ, +WMTS, WMS in tiles, raster DEMs) and pictures of the view are fetched for the screen's own pixels, +up to twice the detail of its logical pixels (#2390): a 2x screen takes the next zoom's tiles. Text +and lines drawn into them come out sharp, and smaller, as in QGIS. A screen at more than 2x (most +phones), and a map export or layout at a high resolution, still take tiles at twice the detail, +not more: three times would ask for nine times the tiles, with their text too small to read. + +**Settings › Map Tiles › Sharp Tiles on HiDPI Displays** (on by default; `ui.sharpTiles`) turns it +off: tiles and pictures are then fetched for the logical pixels and stretched, softer but with +their text at its usual size. Vector tiles are drawn for the screen's pixels either way. The +command line and Python have no such setting: their exports and layouts above one pixel a point +take sharp tiles. A tile layer exported as a GeoTIFF without a zoom takes the zoom the view shows +at one pixel a point. + ### Services in other coordinate systems National and regional services often draw their maps in a coordinate system of their own From e6b10b458ddd0264997ec73ba25e3dec4fbc413f Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Wed, 18 Nov 2026 10:44:49 +0100 Subject: [PATCH 2/7] fix(tiles): WMS pictures send a resolution only on HiDPI, retry smaller first, and tiles keep to the budget A picture at a logical pixel's detail sends no resolution. GeoServer's FORMAT_OPTIONS dpi is from the OGC standard pixel (90.7) and is added to the address's own format options; MAP_RESOLUTION is no longer sent. A refused large picture is asked smaller before the resolution is dropped. The sharper raster zoom steps back where the view's tiles pass the budget, tiles in a system of their own follow the setting and its cap, and turning the setting on or off redraws an open layout page. --- crates/render/src/native_tiles.rs | 6 +- crates/render/src/single_image.rs | 112 +++++++++++++++++++++++------- crates/render/src/tiles.rs | 88 +++++++++++++++++++++-- crates/tiles/src/draw.rs | 32 ++++++++- crates/ui-egui/src/lib.rs | 2 + docs/user-guide/data-formats.md | 28 +++++--- 6 files changed, 222 insertions(+), 46 deletions(-) diff --git a/crates/render/src/native_tiles.rs b/crates/render/src/native_tiles.rs index fda47b6ec3..e514469823 100644 --- a/crates/render/src/native_tiles.rs +++ b/crates/render/src/native_tiles.rs @@ -165,8 +165,10 @@ pub(crate) fn draw(pm: &mut Pixmap, n: &Native, t: &TileLayer, view: &View, flat let (Some(a), Some(b)) = (to_native(cam.to_map(w / 2.0, h / 2.0, w, h)), to_native(cam.to_map(w / 2.0 + 1.0, h / 2.0, w, h))) else { return (false, None); }; - // The system's units per pixel of the map's pixmap. - let per_px = (b.x - a.x).hypot(b.y - a.y) / k; + // The system's units per pixel its tiles are fetched for: the map's pixmap's on a HiDPI + // display when sharp tiles are on, up to twice a logical pixel's detail; else a logical + // pixel's (#2390). + let per_px = (b.x - a.x).hypot(b.y - a.y) / f64::from(rustgis_tiles::template::detail(k as f32)); if !(lo.0 < hi.0 && lo.1 < hi.1 && per_px.is_finite() && per_px > 0.0) { return (false, None); } diff --git a/crates/render/src/single_image.rs b/crates/render/src/single_image.rs index 08c0588e5a..80e3d5e581 100644 --- a/crates/render/src/single_image.rs +++ b/crates/render/src/single_image.rs @@ -6,7 +6,9 @@ //! The picture reaches past the view, its middle on a grid and its detail rounded down to a //! quarter of a power of two, so a small pan or zoom asks for nothing new; one on its way is //! stood in for by the layer's last picture. Its size is in the map's logical pixels, as tiles -//! are asked for, so the service's symbols keep their size on a high-density screen. +//! are asked for; on a high-density screen with sharp tiles on, in the screen's own pixels up to +//! twice those, and a WMS picture then says the resolution it is for, so the services that read it +//! keep their symbols' size (#2390). use std::collections::HashMap; use std::sync::{Arc, Mutex, OnceLock}; @@ -123,10 +125,10 @@ fn export_in(base: &str, epsg: u16) -> Option { } /// The picture to ask for: its area (west, south, east, north in the system) and size in -/// pixels, for a view showing `area` at `res` units a logical pixel, `max_side` pixels a side at -/// most. `None` for a view with no size. +/// pixels, for a view showing `area` at `res` units a pixel of the picture (a logical pixel's, or +/// finer on a HiDPI display), `max_side` pixels a side at most. `None` for a view with no size. fn picture(area: [f64; 4], res: f64, max_side: u32) -> Option<([f64; 4], u32, u32)> { - // The view's box in logical pixels: the same as the map zooms (a window's size). + // The view's box in the picture's pixels: the same as the map zooms (a window's size). let (bw, bh) = ((area[2] - area[0]) / res, (area[3] - area[1]) / res); if !(bw.is_finite() && bh.is_finite() && bw > 0.0 && bh > 0.0 && res.is_finite() && res > 0.0) { return None; @@ -149,26 +151,60 @@ fn picture(area: [f64; 4], res: f64, max_side: u32) -> Option<([f64; 4], u32, u3 out.iter().all(|v| v.is_finite()).then_some((out, w as u32, h as u32)) } -/// A WMS GetMap address for a picture of `detail` pixels a logical one (as the picture asked -/// for came out, past any size limit), with the -/// resolution the servers that read one draw it at: `DPI` (GeoServer, QGIS Server), -/// `MAP_RESOLUTION` (MapServer, QGIS Server) and `FORMAT_OPTIONS=dpi:` (GeoServer), so their -/// symbols and text keep their size in the larger picture. Any other address, a picture at a -/// logical pixel's detail, or one naming a resolution already, is left as it is. +/// The resolution of the OGC standard pixel (0.28 mm), which GeoServer draws at. +const OGC_DPI: f64 = 25.4 / 0.28; + +/// A WMS GetMap address for a picture of `detail` pixels a logical one (as the picture came +/// out, past any size limit), with the resolution it is for in the parameters servers read: +/// `DPI` (QGIS Server, from 96) and `FORMAT_OPTIONS=dpi:` (GeoServer, from the OGC standard +/// pixel's 90.7; added to the options the address has), so their symbols and text keep their size +/// in the larger picture. Any other address, a picture at a logical pixel's detail or less, or +/// one that names a resolution already, is left as it is. fn with_dpi(url: String, detail: f64) -> String { if !(detail > 1.0 && detail.is_finite()) { return url; } let (address, fragment) = url.split_once('#').map_or((url.as_str(), None), |(a, f)| (a, Some(f))); - let Some((_, query)) = address.split_once('?') else { return url }; - let keys: Vec<(&str, &str)> = query.split('&').map(|part| part.split_once('=').unwrap_or((part, ""))).collect(); - let has = |key: &str| keys.iter().any(|(k, _)| k.eq_ignore_ascii_case(key)); - let is = |key: &str, value: &str| keys.iter().any(|(k, v)| k.eq_ignore_ascii_case(key) && v.eq_ignore_ascii_case(value)); - if !is("REQUEST", "GetMap") || ["DPI", "MAP_RESOLUTION", "FORMAT_OPTIONS"].iter().any(|k| has(k)) { + let Some((path, query)) = address.split_once('?') else { return url }; + let parts: Vec<(&str, Option<&str>)> = + query.split('&').map(|part| part.split_once('=').map_or((part, None), |(k, v)| (k, Some(v)))).collect(); + let key = |k: &str, want: &str| k.eq_ignore_ascii_case(want); + let get_map = parts.iter().any(|(k, v)| key(k, "REQUEST") && v.is_some_and(|v| v.eq_ignore_ascii_case("GetMap"))); + // A resolution of its own: `DPI`, `MAP_RESOLUTION`, or a `dpi` among its format options. + let options_dpi = |v: &str| v.split([';', ',']).any(|o| o.split_once(':').is_some_and(|(n, _)| n.trim().eq_ignore_ascii_case("dpi"))); + let own = parts.iter().any(|(k, v)| { + key(k, "DPI") + || key(k, "MAP_RESOLUTION") + || (key(k, "FORMAT_OPTIONS") + && v.is_some_and(|v| options_dpi(&v.replace("%3A", ":").replace("%3a", ":").replace("%3B", ";").replace("%3b", ";")))) + }); + if !get_map || own { return url; } let dpi = (96.0 * detail).round(); - let mut out = format!("{address}&DPI={dpi}&MAP_RESOLUTION={dpi}&FORMAT_OPTIONS=dpi:{dpi}"); + let ogc = (OGC_DPI * detail).round(); + let mut options = false; + let mut query: Vec = parts + .iter() + .map(|(k, v)| match v { + Some(v) if key(k, "FORMAT_OPTIONS") && !options => { + options = true; + if v.is_empty() { format!("{k}=dpi:{ogc}") } else { format!("{k}={v};dpi:{ogc}") } + } + Some(v) => format!("{k}={v}"), + // A bare key: the options are the resolution alone. + None if key(k, "FORMAT_OPTIONS") && !options => { + options = true; + format!("{k}=dpi:{ogc}") + } + None => (*k).to_string(), + }) + .collect(); + query.push(format!("DPI={dpi}")); + if !options { + query.push(format!("FORMAT_OPTIONS=dpi:{ogc}")); + } + let mut out = format!("{path}?{}", query.join("&")); if let Some(f) = fragment { out.push('#'); out.push_str(f); @@ -180,12 +216,17 @@ fn with_dpi(url: String, detail: f64) -> String { /// tried again (the failure may have been passing: a timeout, a busy server). const SMALL_FOR: web_time::Duration = web_time::Duration::from_secs(300); +/// How long a layer whose picture saying its resolution failed is asked for pictures without one. +const PLAIN_FOR: web_time::Duration = SMALL_FOR; + /// What is kept between frames, by the layer's address: its last picture drawn (the address, -/// area and units a pixel), and when a large picture of it last failed. +/// area and units a pixel), when a large picture of it last failed, and when a picture saying its +/// resolution last failed. #[derive(Default)] struct Kept { last: HashMap, small: HashMap, + plain: HashMap, } fn kept() -> std::sync::MutexGuard<'static, Kept> { @@ -320,9 +361,11 @@ pub(crate) fn draw(pm: &mut Pixmap, req: &Request, t: &TileLayer, view: &View, f crate::native_tiles::paint_blended(pm, img, at, at, opacity, blend, &place); } }; - // Asked again smaller when the service refuses a large picture. - for _ in 0..2 { + // Asked again smaller when the service refuses a large picture, and then without its + // resolution when it refuses one saying it. + for _ in 0..3 { let small = kept().small.get(&key).is_some_and(|at| at.elapsed() < SMALL_FOR); + let plain = detail <= 1.0 || kept().plain.get(&key).is_some_and(|at| at.elapsed() < PLAIN_FOR); let Some((at, pw, ph)) = picture(area, res, if small { SMALL_SIDE } else { MAX_SIDE }) else { return nothing }; let (at, pw, ph) = match world_of(req.system.epsg) { Some(world) => match in_world(at, pw, ph, world)? { @@ -351,7 +394,8 @@ pub(crate) fn draw(pm: &mut Pixmap, req: &Request, t: &TileLayer, view: &View, f paint(pm, img, *last_at); return nothing; } - let url = with_dpi(Warp::map_address(&req.base, &req.system, at, pw, ph), logical / r); + let address = Warp::map_address(&req.base, &req.system, at, pw, ph); + let url = if plain { address.clone() } else { with_dpi(address.clone(), logical / r) }; match cache::get(&url, Want::Image) { State::Ready(payload) => { if let Payload::Image(img) = &*payload { @@ -375,6 +419,8 @@ pub(crate) fn draw(pm: &mut Pixmap, req: &Request, t: &TileLayer, view: &View, f return Some((true, None)); } // Asked again at most 2048 pixels a side for a while: some services draw no larger. + // Tried first, with its resolution still: a failure of a large picture says nothing + // of the resolution. State::Failed(_) if !small && pw.max(ph) > SMALL_SIDE => { let mut k = kept(); if k.small.len() >= MAX_KEPT { @@ -382,6 +428,15 @@ pub(crate) fn draw(pm: &mut Pixmap, req: &Request, t: &TileLayer, view: &View, f } k.small.insert(key.clone(), web_time::Instant::now()); } + // Asked again without its resolution for a while: a service may refuse a parameter it + // doesn't know. + State::Failed(_) if url != address => { + let mut k = kept(); + if k.plain.len() >= MAX_KEPT { + k.plain.clear(); + } + k.plain.insert(key.clone(), web_time::Instant::now()); + } // A picture the service doesn't draw: its tiles may draw (and say what fails). State::Failed(_) => return None, } @@ -623,8 +678,9 @@ mod tests { #[test] fn a_hidpi_wms_picture_says_its_resolution() { let wms = "https://m.example.org/wms?SERVICE=WMS&REQUEST=GetMap&LAYERS=a&WIDTH=800&HEIGHT=600"; - assert_eq!(with_dpi(wms.into(), 2.0), format!("{wms}&DPI=192&MAP_RESOLUTION=192&FORMAT_OPTIONS=dpi:192")); - assert_eq!(with_dpi(wms.into(), 1.5), format!("{wms}&DPI=144&MAP_RESOLUTION=144&FORMAT_OPTIONS=dpi:144")); + // QGIS Server's from 96, GeoServer's from the OGC standard pixel (90.7). + assert_eq!(with_dpi(wms.into(), 2.0), format!("{wms}&DPI=192&FORMAT_OPTIONS=dpi:181")); + assert_eq!(with_dpi(wms.into(), 1.5), format!("{wms}&DPI=144&FORMAT_OPTIONS=dpi:136")); // At a logical pixel's detail, or stretched past its finest zoom: as it was. for detail in [1.0, 0.4, f64::NAN, f64::INFINITY] { assert_eq!(with_dpi(wms.into(), detail), wms, "{detail}"); @@ -633,12 +689,20 @@ mod tests { let lower = "https://m.example.org/wms?service=wms&request=getmap&layers=a#rustgis-x"; assert_eq!( with_dpi(lower.into(), 2.0), - "https://m.example.org/wms?service=wms&request=getmap&layers=a&DPI=192&MAP_RESOLUTION=192&FORMAT_OPTIONS=dpi:192#rustgis-x" + "https://m.example.org/wms?service=wms&request=getmap&layers=a&DPI=192&FORMAT_OPTIONS=dpi:181#rustgis-x" + ); + // Format options of its own keep theirs, with the resolution added. + assert_eq!( + with_dpi(format!("{wms}&FORMAT_OPTIONS=antialias:none"), 2.0), + format!("{wms}&FORMAT_OPTIONS=antialias:none;dpi:181&DPI=192") ); + // A bare options key takes the resolution, not a second key. + assert_eq!(with_dpi(format!("{wms}&FORMAT_OPTIONS"), 2.0), format!("{wms}&FORMAT_OPTIONS=dpi:181&DPI=192")); // A resolution of its own, a request that isn't GetMap, or no query: as it was. for own in [ format!("{wms}&dpi=300"), - format!("{wms}&FORMAT_OPTIONS=antialias:none"), + format!("{wms}&format_options=antialias:full;DPI:300"), + format!("{wms}&FORMAT_OPTIONS=dpi%3A300"), format!("{wms}&map_resolution=72"), "https://m.example.org/wms?SERVICE=WMS&REQUEST=GetCapabilities".into(), "https://s.example.org/x/MapServer/export?bbox=1,2,3,4&size=800,600&f=image".into(), diff --git a/crates/render/src/tiles.rs b/crates/render/src/tiles.rs index 062c379032..6dc0b78fb7 100644 --- a/crates/render/src/tiles.rs +++ b/crates/render/src/tiles.rs @@ -931,8 +931,13 @@ pub(crate) mod tests { done(Ok(color(20, 200, 20))); } else if url.starts_with("https://one.test/") { ASKED_ONE.lock().unwrap().push(url.clone()); - // Under `tiles-only/`, a service drawing no picture larger than a tile. - if url.starts_with("https://one.test/tiles-only/") && !url.contains("&WIDTH=256&HEIGHT=256") { + // Under `tiles-only/`, a service drawing no picture larger than a tile; under + // `no-dpi/`, one refusing a resolution it doesn't know. + if (url.starts_with("https://one.test/tiles-only/") && !url.contains("&WIDTH=256&HEIGHT=256")) + || (url.starts_with("https://one.test/no-dpi/") && url.contains("DPI=")) + || (url.starts_with("https://one.test/max2048/") + && url.split("&WIDTH=").nth(1).and_then(|w| w.split('&').next()?.parse::().ok()).is_some_and(|w| w > 2048)) + { done(Err("400 Bad Request".into())); } else { done(Ok(color(20, 200, 20))); @@ -1002,12 +1007,13 @@ pub(crate) mod tests { let mut m = Map::new("Map"); m.graticule = false; m.tiles.push(TileLayer::new(name, TileKind::Raster, url)); - let at = |m: &mut Map, res: f64| { - let center = m.to_map(bern).unwrap(); + let at_ppp = |m: &mut Map, res: f64, ppp: f64, dx: f64| { + let c = m.to_map(bern).unwrap(); + let center = Coord::new(c.x + dx, c.y); let view = View { width: 200.0, height: 100.0, - pixels_per_point: 1.0, + pixels_per_point: ppp, camera: Camera { center, res, bearing: 0.0, pitch: 0.0 }, sky: false, }; @@ -1017,6 +1023,7 @@ pub(crate) mod tests { let p = pm.pixel(100, 50).unwrap(); ([p.red(), p.green(), p.blue(), p.alpha()], std::mem::take(&mut *ASKED.lock().unwrap())) }; + let at = |m: &mut Map, res: f64| at_ppp(m, res, 1.0, 0.0); // On Web Mercator: in LV95, warped. let (p, asked) = at(&mut m, 8.0); assert_eq!(p, [20, 200, 20, 255], "{name} on Web Mercator"); @@ -1042,6 +1049,14 @@ pub(crate) mod tests { } else { // The 5 m matrix's. assert!(!asked.is_empty() && asked.iter().all(|u| u.starts_with("https://wmts.test/lv95/m2/")), "{asked:?}"); + // At 10 m a point on a 2x display, the 5 m matrix's too (#2390); on a 3x one no + // finer (the 2.5 m matrix), as sharp tiles go to twice a point's detail at most. + // Each somewhere new, so its tiles are asked for. + for (ppp, dx) in [(2.0, 20_000.0), (3.0, 40_000.0)] { + let (p, asked) = at_ppp(&mut m, 10.0, ppp, dx); + assert_eq!(p, [20, 200, 20, 255]); + assert!(!asked.is_empty() && asked.iter().all(|u| u.starts_with("https://wmts.test/lv95/m2/")), "{ppp}: {asked:?}"); + } } } } @@ -1076,7 +1091,8 @@ pub(crate) mod tests { // pixels, which are up to a quarter of a level finer than asked for. let dpi = |u: &str| -> Option { let d: f64 = u.split("&DPI=").nth(1)?.split('&').next()?.parse().ok()?; - u.contains(&format!("&MAP_RESOLUTION={d}&FORMAT_OPTIONS=dpi:{d}")).then_some(d) + let ogc: f64 = u.split("FORMAT_OPTIONS=dpi:").nth(1)?.split('&').next()?.parse().ok()?; + ((d * 25.4 / 0.28 / 96.0 - ogc).abs() <= 1.0).then_some(d) }; // In Web Mercator. let mut m = Map::new("Map"); @@ -1122,6 +1138,66 @@ pub(crate) mod tests { let said: Vec = asked.iter().filter_map(|u| dpi(u)).collect(); assert!(said.len() == 1 && (135.0..162.0).contains(&said[0]), "{asked:?}"); } + // At one pixel a point the picture is asked for as before: its logical size, and no + // resolution, though its pixel is rounded a little finer than asked. + { + let center = m.to_map(bern).unwrap(); + let view = View { + width: 200.0, + height: 100.0, + pixels_per_point: 1.0, + camera: Camera { center, res: 31.0, bearing: 0.0, pitch: 0.0 }, + sky: false, + }; + ASKED_ONE.lock().unwrap().clear(); + crate::render_png(&mut m, &view).unwrap(); + let asked = std::mem::take(&mut *ASKED_ONE.lock().unwrap()); + assert!(asked.len() == 1 && asked[0].contains("&WIDTH=512&HEIGHT=256&") && !asked[0].contains("DPI"), "{asked:?}"); + } + // A service refusing a picture too large: asked again smaller, and not taken to refuse + // the resolution (the size was refused). + { + let mut m = Map::new("Map"); + m.graticule = false; + let mut t = TileLayer::new("WMS", TileKind::Raster, url.replace("one.test/merc/", "one.test/max2048/")); + t.single_image = true; + m.tiles.push(t); + let center = m.to_map(bern).unwrap(); + let view = View { + width: 1500.0, + height: 100.0, + pixels_per_point: 2.0, + camera: Camera { center, res: 30.0, bearing: 0.0, pitch: 0.0 }, + sky: false, + }; + ASKED_ONE.lock().unwrap().clear(); + crate::render_png(&mut m, &view).unwrap(); + let asked = std::mem::take(&mut *ASKED_ONE.lock().unwrap()); + let width = |u: &String| u.split("&WIDTH=").nth(1).and_then(|w| w.split('&').next()?.parse::().ok()); + assert!(asked.len() == 2 && width(&asked[0]) > Some(2048) && width(&asked[1]) <= Some(2048), "{asked:?}"); + assert!(dpi(&asked[0]).is_some(), "{asked:?}"); + // Its resolution wasn't what was refused: a view whose picture fits still says it. + let narrow = View { width: 200.0, ..view }; + ASKED_ONE.lock().unwrap().clear(); + crate::render_png(&mut m, &narrow).unwrap(); + let asked = std::mem::take(&mut *ASKED_ONE.lock().unwrap()); + assert!(asked.len() == 1 && dpi(&asked[0]).is_some(), "{asked:?}"); + } + // A service refusing the resolution: asked again without it, and drawn so. + { + let mut m = Map::new("Map"); + m.graticule = false; + let mut t = TileLayer::new("WMS", TileKind::Raster, url.replace("one.test/merc/", "one.test/no-dpi/")); + t.single_image = true; + m.tiles.push(t); + let (p, asked) = at(&mut m, 30.0, 0.0); + assert_eq!(p, [green; 3], "{asked:?}"); + assert!(asked.len() == 2 && asked[0].contains("DPI=") && !asked[1].contains("DPI="), "{asked:?}"); + assert!(asked[1].contains("&WIDTH=768&HEIGHT=512&"), "at the same size: {asked:?}"); + // And so for a while after: not asked with it again. + let (p, asked) = at(&mut m, 30.0, 5000.0); + assert!(p == [green; 3] && asked.iter().all(|u| !u.contains("DPI=")), "{asked:?}"); + } // Closer in than its finest zoom (10: 153 m a pixel), its picture there stretched, as its // finest tiles are: fewer pixels for the same view. m.tiles[0].max_zoom = 10; diff --git a/crates/tiles/src/draw.rs b/crates/tiles/src/draw.rs index af94dec069..f44dad82b3 100644 --- a/crates/tiles/src/draw.rs +++ b/crates/tiles/src/draw.rs @@ -608,6 +608,16 @@ fn tiles_for(src: &Source, view: &Extent, frame: &Frame, zoom: f32, pending: &mu tiles_in(src, view, None, frame, zoom, pending, error).items } +/// The zoom of a raster source's `tile_size`-pixel tiles drawn at `frame` over `view`: by the +/// display's own pixels on a HiDPI one when sharp tiles are on (#2390), up to twice a logical +/// pixel's detail; back towards a logical pixel's zoom where the view's tiles there would pass the +/// most a source draws (a 5K or 6K display), rather than leave the view's corners out. +fn raster_zoom(frame: &Frame, view: &Extent, tile_size: u32, max_zoom: u8) -> u8 { + let logical = template::zoom_for(frame.m_per_px * f64::from(frame.ppp), tile_size, max_zoom); + let sharp = template::zoom_for(frame.m_per_px * f64::from(frame.ppp / template::detail(frame.ppp)), tile_size, max_zoom); + if sharp > logical { template::fitting_zoom(view, sharp, logical) } else { sharp } +} + /// The tiles of a source in view and out to `reach` (those in the cache only), with stand-ins /// for those not here yet. Expanded destinations retain the supplying document's reach; /// refused tiles are counted in `error` and network history without requesting or reusing bytes. Valid @@ -624,9 +634,7 @@ pub(crate) fn tiles_in( let (tiles, from, min_z, max_z, want, z) = match src { Source::Raster { tiles, from, tile_size, min_zoom, max_zoom, .. } | Source::RasterDem { tiles, from, tile_size, min_zoom, max_zoom, .. } => { - // By the display's own pixels on a HiDPI one, when sharp tiles are on (#2390). - let z = template::zoom_for(frame.m_per_px * f64::from(frame.ppp / template::detail(frame.ppp)), *tile_size, *max_zoom); - (tiles, from, *min_zoom, *max_zoom, Want::Image, z) + (tiles, from, *min_zoom, *max_zoom, Want::Image, raster_zoom(frame, view, *tile_size, *max_zoom)) } Source::Vector { tiles, from, min_zoom, max_zoom, .. } => { (tiles, from, *min_zoom, *max_zoom, Want::Vector, (zoom.floor() as u8).min(*max_zoom)) @@ -1319,6 +1327,24 @@ fn area(r: &[(f32, f32)]) -> f32 { #[cfg(test)] pub(crate) mod tests { + /// On a HiDPI display a raster source takes the next zoom's tiles while the view's fit the most + /// a source draws, and goes back towards a logical pixel's zoom where they don't (#2390). + #[test] + fn sharp_raster_tiles_keep_to_the_tile_budget() { + use super::{Frame, raster_zoom, template}; + // A zoom-10 logical pixel (152.9 m), at 2 device pixels a point. + let logical = 2.0 * template::HALF_WORLD / (256.0 * 1024.0); + let zoom_of = |w: f64, h: f64, ppp: f32| { + let frame = Frame { left: 0.0, top: h * logical, m_per_px: logical / f64::from(ppp), ppp }; + raster_zoom(&frame, &rustgis_geom::Extent::new(0.0, 0.0, w * logical, h * logical), 256, 22) + }; + // A 1500 × 900 point view: 6 × 4 tiles of zoom 10, 12 × 7 of zoom 11. + assert_eq!(zoom_of(1500.0, 900.0, 1.0), 10); + assert_eq!(zoom_of(1500.0, 900.0, 2.0), 11); + // A 6000 × 3400 point one: 47 × 27 of zoom 11 pass 400; zoom 10's fit. + assert_eq!(zoom_of(6000.0, 3400.0, 2.0), 10); + } + #[test] fn refusal_counts_are_not_substring_duplicates() { let mut error = Some("12 tiles left out: refused".to_string()); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 960efd3154..81cafed03f 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -3639,7 +3639,9 @@ impl RustGisApp { if let Some(save) = &self.services.save_pref { save("sharp_tiles", if self.ui.sharp_tiles { "on" } else { "off" }); } + // The map and an open layout page are drawn again with tiles at the new detail. self.map.invalidate(); + self.layout.redraw(); return Ok(json!({"on": self.ui.sharp_tiles})); } "ui.recover" => return autosave::recover(self, p), diff --git a/docs/user-guide/data-formats.md b/docs/user-guide/data-formats.md index c235595bb3..669ade25db 100644 --- a/docs/user-guide/data-formats.md +++ b/docs/user-guide/data-formats.md @@ -452,12 +452,13 @@ it's given `"singleImage": false` or a `"tileSize"`. place. - On a high-density (HiDPI) screen it is asked for in the screen's own pixels, up to twice the map's logical pixels, as tiles are (see [Sharp tiles on HiDPI displays](#sharp-tiles-on-hidpi-displays)). - A WMS picture then says the resolution it is for (`DPI`, `MAP_RESOLUTION` and - `FORMAT_OPTIONS=dpi:`, unless its address names one), so GeoServer, MapServer and QGIS Server - keep their symbols and text at their size; a service that reads none of them draws them - smaller. It is at most 4096 pixels a side (2048 for five minutes after a larger picture of the - layer fails), coarser for a view larger than that. A picture the service fails to draw leaves - the layer drawn in tiles there. + A WMS picture then says the resolution it is for, unless its address names one: `DPI` (QGIS + Server, from 96) and `FORMAT_OPTIONS=dpi:` (GeoServer, from the OGC standard pixel's 90.7), so + those servers keep their symbols and text at their size. A service that reads neither draws them + smaller; one that refuses the picture with them is asked again without them, for five + minutes. The picture is at most 4096 pixels a side (2048 for five minutes after a larger + picture of the layer fails), coarser for a view larger than that. A picture the service fails + to draw leaves the layer drawn in tiles there. - Nothing is asked for zoomed out past the layer's scale range or outside its bounds, and closer in than its finest zoom its picture there is stretched, as its tiles are. - It is drawn so on a flat map, north up or turned, in Web Mercator or in a projection with an @@ -479,11 +480,16 @@ before keeps its tiles until you switch it. ### Sharp tiles on HiDPI displays On a high-density screen (a Retina or 4K display scaled 2x, a phone), raster tile layers (XYZ, -WMTS, WMS in tiles, raster DEMs) and pictures of the view are fetched for the screen's own pixels, -up to twice the detail of its logical pixels (#2390): a 2x screen takes the next zoom's tiles. Text -and lines drawn into them come out sharp, and smaller, as in QGIS. A screen at more than 2x (most -phones), and a map export or layout at a high resolution, still take tiles at twice the detail, -not more: three times would ask for nine times the tiles, with their text too small to read. +WMTS, WMS in tiles, raster archives, raster DEMs, and those in a system of their own) and pictures +of the view are fetched for the screen's own pixels, up to twice the detail of its logical pixels +(#2390): a 2x screen takes the next zoom's tiles (on a 5K or 6K screen, where those would be too +many for the view, the finest zoom whose tiles fit). Text and lines drawn into them come out +sharp, and smaller, as in QGIS. A screen at more than 2x (most phones), and a map export or layout +at a high resolution, still take tiles at twice the detail, not more: three times would ask for +nine times the tiles, with their text too small to read. + +Only a WMS layer drawn as one picture says the resolution it is for (above); in tiles it doesn't, +and its symbols come out smaller. **Settings › Map Tiles › Sharp Tiles on HiDPI Displays** (on by default; `ui.sharpTiles`) turns it off: tiles and pictures are then fetched for the logical pixels and stretched, softer but with From 27244a596b2d8d993eed1e5d962c34447e3dafe4 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Wed, 18 Nov 2026 11:07:51 +0100 Subject: [PATCH 3/7] fix(tiles): keep a WMS resolution after an outage, and keep the tile fallback in the source's zooms A picture with its resolution that fails is retried without it at once, but the layer is asked without it for a while only once that picture draws: a service that was down keeps getting the resolution when it is back. An address's own resolution is recognised when percent-encoded. The tile-budget fallback never goes below the source's coarsest zoom. --- crates/engine/src/cmd/raster.rs | 2 +- crates/render/src/single_image.rs | 68 +++++++++++++++++++++++-------- crates/render/src/tiles.rs | 19 +++++++++ crates/tiles/src/draw.rs | 20 +++++---- docs/user-guide/data-formats.md | 17 ++++---- 5 files changed, 92 insertions(+), 34 deletions(-) diff --git a/crates/engine/src/cmd/raster.rs b/crates/engine/src/cmd/raster.rs index b04cbaae22..a2cad8dcf7 100644 --- a/crates/engine/src/cmd/raster.rs +++ b/crates/engine/src/cmd/raster.rs @@ -88,7 +88,7 @@ pub fn commands() -> Vec { ), C::new("raster.info", "Raster Information", "Data › Information", info).view().when(need_raster).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"layer"?}"#), C::new("share.exportRaster", "Export Raster", "Data › Information", |s, p| super::exports::write_here(s, p, "share.exportRaster")).view().when(need_exportable).inputs(export_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).params( - r#"{"layer"?, "path": "*.tif", "band"?: 1.. (rasters that aren't colour ones; a colour raster writes its colour bands), "cog"?: bool (a cloud-optimised GeoTIFF: 512 px tiles, Deflate, overviews; default false, an untiled GeoTIFF), "tiles"?: "a tile layer's name, or its index (0 = top): its raster tiles (XYZ, WMTS, WMS, a raster archive) put together as an RGBA GeoTIFF in Web Mercator; by default the tile layer picked in the Layers pane, when no layer is given or active", "bbox"?: "[west, south, east, north] in degrees, for a tile layer (default: the view's)", "zoom"?: "0..30, the tiles' zoom (default: the zoom the view shows at one pixel a point, as on a standard-density screen); at most 1024 tiles and 8192 pixels a side"}"#, + r#"{"layer"?, "path": "*.tif", "band"?: 1.. (rasters that aren't colour ones; a colour raster writes its colour bands), "cog"?: bool (a cloud-optimised GeoTIFF: 512 px tiles, Deflate, overviews; default false, an untiled GeoTIFF), "tiles"?: "a tile layer's name, or its index (0 = top): its raster tiles (XYZ, WMTS, WMS, a raster archive) put together as an RGBA GeoTIFF in Web Mercator; by default the tile layer picked in the Layers pane, when no layer is given or active", "bbox"?: "[west, south, east, north] in degrees, for a tile layer (default: the view's)", "zoom"?: "0..30, the tiles' zoom (default: about the zoom the view shows at one pixel a point, as on a standard-density screen); at most 1024 tiles and 8192 pixels a side"}"#, ), ] } diff --git a/crates/render/src/single_image.rs b/crates/render/src/single_image.rs index 80e3d5e581..0401a41774 100644 --- a/crates/render/src/single_image.rs +++ b/crates/render/src/single_image.rs @@ -168,16 +168,16 @@ fn with_dpi(url: String, detail: f64) -> String { let Some((path, query)) = address.split_once('?') else { return url }; let parts: Vec<(&str, Option<&str>)> = query.split('&').map(|part| part.split_once('=').map_or((part, None), |(k, v)| (k, Some(v)))).collect(); - let key = |k: &str, want: &str| k.eq_ignore_ascii_case(want); + // Keys compared decoded (`FORMAT%5FOPTIONS`), and written back as they came. + let key = |k: &str, want: &str| percent_decoded(k).eq_ignore_ascii_case(want); let get_map = parts.iter().any(|(k, v)| key(k, "REQUEST") && v.is_some_and(|v| v.eq_ignore_ascii_case("GetMap"))); // A resolution of its own: `DPI`, `MAP_RESOLUTION`, or a `dpi` among its format options. - let options_dpi = |v: &str| v.split([';', ',']).any(|o| o.split_once(':').is_some_and(|(n, _)| n.trim().eq_ignore_ascii_case("dpi"))); - let own = parts.iter().any(|(k, v)| { - key(k, "DPI") - || key(k, "MAP_RESOLUTION") - || (key(k, "FORMAT_OPTIONS") - && v.is_some_and(|v| options_dpi(&v.replace("%3A", ":").replace("%3a", ":").replace("%3B", ";").replace("%3b", ";")))) - }); + // Looked at decoded (`%64pi%3A300` is `dpi:300`); the address is kept as it is. + let options_dpi = |v: &str| { + percent_decoded(v).split([';', ',']).any(|o| o.split_once(':').is_some_and(|(n, _)| n.trim().eq_ignore_ascii_case("dpi"))) + }; + let own = + parts.iter().any(|(k, v)| key(k, "DPI") || key(k, "MAP_RESOLUTION") || (key(k, "FORMAT_OPTIONS") && v.is_some_and(options_dpi))); if !get_map || own { return url; } @@ -212,6 +212,27 @@ fn with_dpi(url: String, detail: f64) -> String { out } +/// `text` with its percent escapes decoded (an invalid one kept as it is), for looking at only. +fn percent_decoded(text: &str) -> String { + let bytes = text.as_bytes(); + let mut out = Vec::with_capacity(bytes.len()); + let mut i = 0; + while let Some(&b) = bytes.get(i) { + let hex = |at: usize| bytes.get(at).and_then(|c| (*c as char).to_digit(16)); + match (b, hex(i + 1), hex(i + 2)) { + (b'%', Some(h), Some(l)) => { + out.push((h * 16 + l) as u8); + i += 3; + } + _ => { + out.push(b); + i += 1; + } + } + } + String::from_utf8_lossy(&out).into_owned() +} + /// How long a layer whose large picture failed is asked for smaller ones before a large one is /// tried again (the failure may have been passing: a timeout, a busy server). const SMALL_FOR: web_time::Duration = web_time::Duration::from_secs(300); @@ -363,9 +384,10 @@ pub(crate) fn draw(pm: &mut Pixmap, req: &Request, t: &TileLayer, view: &View, f }; // Asked again smaller when the service refuses a large picture, and then without its // resolution when it refuses one saying it. + let mut without = false; for _ in 0..3 { let small = kept().small.get(&key).is_some_and(|at| at.elapsed() < SMALL_FOR); - let plain = detail <= 1.0 || kept().plain.get(&key).is_some_and(|at| at.elapsed() < PLAIN_FOR); + let plain = detail <= 1.0 || without || kept().plain.get(&key).is_some_and(|at| at.elapsed() < PLAIN_FOR); let Some((at, pw, ph)) = picture(area, res, if small { SMALL_SIDE } else { MAX_SIDE }) else { return nothing }; let (at, pw, ph) = match world_of(req.system.epsg) { Some(world) => match in_world(at, pw, ph, world)? { @@ -404,6 +426,15 @@ pub(crate) fn draw(pm: &mut Pixmap, req: &Request, t: &TileLayer, view: &View, f if k.last.len() >= MAX_KEPT { k.last.clear(); } + // Drawn without its resolution where the picture with it failed: most likely + // the service refused the resolution, so the layer is asked without it for a + // while. Where both fail (a service down, a timeout), nothing is kept. + if without { + if k.plain.len() >= MAX_KEPT { + k.plain.clear(); + } + k.plain.insert(key.clone(), web_time::Instant::now()); + } k.last.insert(key, (url, at, r)); } return nothing; @@ -428,15 +459,9 @@ pub(crate) fn draw(pm: &mut Pixmap, req: &Request, t: &TileLayer, view: &View, f } k.small.insert(key.clone(), web_time::Instant::now()); } - // Asked again without its resolution for a while: a service may refuse a parameter it - // doesn't know. - State::Failed(_) if url != address => { - let mut k = kept(); - if k.plain.len() >= MAX_KEPT { - k.plain.clear(); - } - k.plain.insert(key.clone(), web_time::Instant::now()); - } + // Asked again without its resolution: a service may refuse a parameter it doesn't + // know (kept for a while only once that picture draws). + State::Failed(_) if url != address => without = true, // A picture the service doesn't draw: its tiles may draw (and say what fails). State::Failed(_) => return None, } @@ -691,6 +716,11 @@ mod tests { with_dpi(lower.into(), 2.0), "https://m.example.org/wms?service=wms&request=getmap&layers=a&DPI=192&FORMAT_OPTIONS=dpi:181#rustgis-x" ); + // Format options of its own keep theirs, with the resolution added, its key as it came. + assert_eq!( + with_dpi(format!("{wms}&FORMAT%5FOPTIONS=antialias:none"), 2.0), + format!("{wms}&FORMAT%5FOPTIONS=antialias:none;dpi:181&DPI=192") + ); // Format options of its own keep theirs, with the resolution added. assert_eq!( with_dpi(format!("{wms}&FORMAT_OPTIONS=antialias:none"), 2.0), @@ -703,6 +733,8 @@ mod tests { format!("{wms}&dpi=300"), format!("{wms}&format_options=antialias:full;DPI:300"), format!("{wms}&FORMAT_OPTIONS=dpi%3A300"), + format!("{wms}&FORMAT_OPTIONS=%64pi%3A300"), + format!("{wms}&%44PI=300"), format!("{wms}&map_resolution=72"), "https://m.example.org/wms?SERVICE=WMS&REQUEST=GetCapabilities".into(), "https://s.example.org/x/MapServer/export?bbox=1,2,3,4&size=800,600&f=image".into(), diff --git a/crates/render/src/tiles.rs b/crates/render/src/tiles.rs index 6dc0b78fb7..123421b239 100644 --- a/crates/render/src/tiles.rs +++ b/crates/render/src/tiles.rs @@ -869,6 +869,8 @@ pub(crate) mod tests { /// What `one.test` was asked for (a test of its own: tests run at once). static ASKED_ONE: std::sync::Mutex> = std::sync::Mutex::new(Vec::new()); + /// `one.test/down/` fails every request while this is set: a service down for a while. + static ONE_DOWN: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); /// Held by the tests reading `ASKED_ONE`, one at a time. static ONE_TEST: std::sync::Mutex<()> = std::sync::Mutex::new(()); @@ -935,6 +937,7 @@ pub(crate) mod tests { // `no-dpi/`, one refusing a resolution it doesn't know. if (url.starts_with("https://one.test/tiles-only/") && !url.contains("&WIDTH=256&HEIGHT=256")) || (url.starts_with("https://one.test/no-dpi/") && url.contains("DPI=")) + || (url.starts_with("https://one.test/down/") && ONE_DOWN.load(std::sync::atomic::Ordering::Relaxed)) || (url.starts_with("https://one.test/max2048/") && url.split("&WIDTH=").nth(1).and_then(|w| w.split('&').next()?.parse::().ok()).is_some_and(|w| w > 2048)) { @@ -1183,6 +1186,22 @@ pub(crate) mod tests { let asked = std::mem::take(&mut *ASKED_ONE.lock().unwrap()); assert!(asked.len() == 1 && dpi(&asked[0]).is_some(), "{asked:?}"); } + // A service down for a while fails the picture with its resolution and without: once it + // is back, the next picture says its resolution again (it wasn't what was refused). + { + let mut m = Map::new("Map"); + m.graticule = false; + let mut t = TileLayer::new("WMS", TileKind::Raster, url.replace("one.test/merc/", "one.test/down/")); + t.single_image = true; + m.tiles.push(t); + ONE_DOWN.store(true, std::sync::atomic::Ordering::Relaxed); + let (_, asked) = at(&mut m, 30.0, 0.0); + ONE_DOWN.store(false, std::sync::atomic::Ordering::Relaxed); + // Its picture with the resolution and without, then its tiles. + assert!(asked.len() > 2 && dpi(&asked[0]).is_some() && !asked[1].contains("DPI="), "{asked:?}"); + let (p, asked) = at(&mut m, 30.0, 50_000.0); + assert!(p == [green; 3] && asked.len() == 1 && dpi(&asked[0]).is_some(), "{asked:?}"); + } // A service refusing the resolution: asked again without it, and drawn so. { let mut m = Map::new("Map"); diff --git a/crates/tiles/src/draw.rs b/crates/tiles/src/draw.rs index f44dad82b3..ca80ad0c3c 100644 --- a/crates/tiles/src/draw.rs +++ b/crates/tiles/src/draw.rs @@ -612,10 +612,11 @@ fn tiles_for(src: &Source, view: &Extent, frame: &Frame, zoom: f32, pending: &mu /// display's own pixels on a HiDPI one when sharp tiles are on (#2390), up to twice a logical /// pixel's detail; back towards a logical pixel's zoom where the view's tiles there would pass the /// most a source draws (a 5K or 6K display), rather than leave the view's corners out. -fn raster_zoom(frame: &Frame, view: &Extent, tile_size: u32, max_zoom: u8) -> u8 { +fn raster_zoom(frame: &Frame, view: &Extent, tile_size: u32, min_zoom: u8, max_zoom: u8) -> u8 { let logical = template::zoom_for(frame.m_per_px * f64::from(frame.ppp), tile_size, max_zoom); let sharp = template::zoom_for(frame.m_per_px * f64::from(frame.ppp / template::detail(frame.ppp)), tile_size, max_zoom); - if sharp > logical { template::fitting_zoom(view, sharp, logical) } else { sharp } + // Never below the source's own coarsest zoom, which would draw nothing. + if sharp > logical { template::fitting_zoom(view, sharp, logical.max(min_zoom)) } else { sharp } } /// The tiles of a source in view and out to `reach` (those in the cache only), with stand-ins @@ -634,7 +635,7 @@ pub(crate) fn tiles_in( let (tiles, from, min_z, max_z, want, z) = match src { Source::Raster { tiles, from, tile_size, min_zoom, max_zoom, .. } | Source::RasterDem { tiles, from, tile_size, min_zoom, max_zoom, .. } => { - (tiles, from, *min_zoom, *max_zoom, Want::Image, raster_zoom(frame, view, *tile_size, *max_zoom)) + (tiles, from, *min_zoom, *max_zoom, Want::Image, raster_zoom(frame, view, *tile_size, *min_zoom, *max_zoom)) } Source::Vector { tiles, from, min_zoom, max_zoom, .. } => { (tiles, from, *min_zoom, *max_zoom, Want::Vector, (zoom.floor() as u8).min(*max_zoom)) @@ -1334,15 +1335,18 @@ pub(crate) mod tests { use super::{Frame, raster_zoom, template}; // A zoom-10 logical pixel (152.9 m), at 2 device pixels a point. let logical = 2.0 * template::HALF_WORLD / (256.0 * 1024.0); - let zoom_of = |w: f64, h: f64, ppp: f32| { + let zoom_of = |w: f64, h: f64, ppp: f32, min: u8| { let frame = Frame { left: 0.0, top: h * logical, m_per_px: logical / f64::from(ppp), ppp }; - raster_zoom(&frame, &rustgis_geom::Extent::new(0.0, 0.0, w * logical, h * logical), 256, 22) + raster_zoom(&frame, &rustgis_geom::Extent::new(0.0, 0.0, w * logical, h * logical), 256, min, 22) }; // A 1500 × 900 point view: 6 × 4 tiles of zoom 10, 12 × 7 of zoom 11. - assert_eq!(zoom_of(1500.0, 900.0, 1.0), 10); - assert_eq!(zoom_of(1500.0, 900.0, 2.0), 11); + assert_eq!(zoom_of(1500.0, 900.0, 1.0, 0), 10); + assert_eq!(zoom_of(1500.0, 900.0, 2.0, 0), 11); // A 6000 × 3400 point one: 47 × 27 of zoom 11 pass 400; zoom 10's fit. - assert_eq!(zoom_of(6000.0, 3400.0, 2.0), 10); + assert_eq!(zoom_of(6000.0, 3400.0, 2.0, 0), 10); + // A source drawn from zoom 11 keeps it, its tiles nearest the view's centre, rather than + // falling back to a zoom it has none of. + assert_eq!(zoom_of(6000.0, 3400.0, 2.0, 11), 11); } #[test] diff --git a/docs/user-guide/data-formats.md b/docs/user-guide/data-formats.md index 669ade25db..b1d874f430 100644 --- a/docs/user-guide/data-formats.md +++ b/docs/user-guide/data-formats.md @@ -482,11 +482,14 @@ before keeps its tiles until you switch it. On a high-density screen (a Retina or 4K display scaled 2x, a phone), raster tile layers (XYZ, WMTS, WMS in tiles, raster archives, raster DEMs, and those in a system of their own) and pictures of the view are fetched for the screen's own pixels, up to twice the detail of its logical pixels -(#2390): a 2x screen takes the next zoom's tiles (on a 5K or 6K screen, where those would be too -many for the view, the finest zoom whose tiles fit). Text and lines drawn into them come out -sharp, and smaller, as in QGIS. A screen at more than 2x (most phones), and a map export or layout -at a high resolution, still take tiles at twice the detail, not more: three times would ask for -nine times the tiles, with their text too small to read. +(#2390). On the Web Mercator tile pyramid a 2x screen takes the next zoom's tiles; on a 5K or 6K +screen, where those would be too many for the view, it takes a zoom between that and the usual one +whose tiles fit, if any does, else the usual one (or the layer's coarsest zoom, where it has none +coarser). A layer in a system of its own (a WMTS matrix set) takes the coarsest of its service's +matrices with that much detail, which may be more than twice. Text and lines drawn into them come +out sharp, and smaller, as in QGIS. A screen at more than 2x (most phones), and a map export or +layout at a high resolution, still take tiles at twice the detail, not more: three times would ask +for nine times the tiles, with their text too small to read. Only a WMS layer drawn as one picture says the resolution it is for (above); in tiles it doesn't, and its symbols come out smaller. @@ -495,8 +498,8 @@ and its symbols come out smaller. off: tiles and pictures are then fetched for the logical pixels and stretched, softer but with their text at its usual size. Vector tiles are drawn for the screen's pixels either way. The command line and Python have no such setting: their exports and layouts above one pixel a point -take sharp tiles. A tile layer exported as a GeoTIFF without a zoom takes the zoom the view shows -at one pixel a point. +take sharp tiles. A tile layer exported as a GeoTIFF without a zoom takes about the zoom the view +shows at one pixel a point. ### Services in other coordinate systems From 7e04da77ea76e9cdab97bb8409045302c8b59433 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Wed, 18 Nov 2026 11:32:05 +0100 Subject: [PATCH 4/7] fix(tiles): a failed HiDPI WMS picture is retried without its resolution, smaller, then at standard detail Each step once a frame: the same picture without DPI (kept for five minutes only when it draws and the server refused the first with a 4xx), at most 2048 pixels with DPI again, then at a standard screen's detail (kept for five minutes once it draws). A 5xx, timeout or lost connection never turns the resolution off. REQUEST is matched decoded, the shared percent_decode is used, and an empty query part adds no '&&'. --- crates/render/src/single_image.rs | 131 +++++++++++++++++++----------- crates/render/src/tiles.rs | 88 +++++++++++++++++++- docs/user-guide/data-formats.md | 18 ++-- 3 files changed, 180 insertions(+), 57 deletions(-) diff --git a/crates/render/src/single_image.rs b/crates/render/src/single_image.rs index 0401a41774..87aabd4a95 100644 --- a/crates/render/src/single_image.rs +++ b/crates/render/src/single_image.rs @@ -169,12 +169,16 @@ fn with_dpi(url: String, detail: f64) -> String { let parts: Vec<(&str, Option<&str>)> = query.split('&').map(|part| part.split_once('=').map_or((part, None), |(k, v)| (k, Some(v)))).collect(); // Keys compared decoded (`FORMAT%5FOPTIONS`), and written back as they came. - let key = |k: &str, want: &str| percent_decoded(k).eq_ignore_ascii_case(want); - let get_map = parts.iter().any(|(k, v)| key(k, "REQUEST") && v.is_some_and(|v| v.eq_ignore_ascii_case("GetMap"))); + let key = |k: &str, want: &str| rustgis_data::address::percent_decode(k).eq_ignore_ascii_case(want); + let get_map = parts + .iter() + .any(|(k, v)| key(k, "REQUEST") && v.is_some_and(|v| rustgis_data::address::percent_decode(v).eq_ignore_ascii_case("GetMap"))); // A resolution of its own: `DPI`, `MAP_RESOLUTION`, or a `dpi` among its format options. // Looked at decoded (`%64pi%3A300` is `dpi:300`); the address is kept as it is. let options_dpi = |v: &str| { - percent_decoded(v).split([';', ',']).any(|o| o.split_once(':').is_some_and(|(n, _)| n.trim().eq_ignore_ascii_case("dpi"))) + rustgis_data::address::percent_decode(v) + .split([';', ',']) + .any(|o| o.split_once(':').is_some_and(|(n, _)| n.trim().eq_ignore_ascii_case("dpi"))) }; let own = parts.iter().any(|(k, v)| key(k, "DPI") || key(k, "MAP_RESOLUTION") || (key(k, "FORMAT_OPTIONS") && v.is_some_and(options_dpi))); @@ -199,6 +203,8 @@ fn with_dpi(url: String, detail: f64) -> String { } None => (*k).to_string(), }) + // An empty part (a trailing `&`) left out, so none is added before the resolution. + .filter(|part| !part.is_empty()) .collect(); query.push(format!("DPI={dpi}")); if !options { @@ -212,27 +218,6 @@ fn with_dpi(url: String, detail: f64) -> String { out } -/// `text` with its percent escapes decoded (an invalid one kept as it is), for looking at only. -fn percent_decoded(text: &str) -> String { - let bytes = text.as_bytes(); - let mut out = Vec::with_capacity(bytes.len()); - let mut i = 0; - while let Some(&b) = bytes.get(i) { - let hex = |at: usize| bytes.get(at).and_then(|c| (*c as char).to_digit(16)); - match (b, hex(i + 1), hex(i + 2)) { - (b'%', Some(h), Some(l)) => { - out.push((h * 16 + l) as u8); - i += 3; - } - _ => { - out.push(b); - i += 1; - } - } - } - String::from_utf8_lossy(&out).into_owned() -} - /// How long a layer whose large picture failed is asked for smaller ones before a large one is /// tried again (the failure may have been passing: a timeout, a busy server). const SMALL_FOR: web_time::Duration = web_time::Duration::from_secs(300); @@ -241,13 +226,14 @@ const SMALL_FOR: web_time::Duration = web_time::Duration::from_secs(300); const PLAIN_FOR: web_time::Duration = SMALL_FOR; /// What is kept between frames, by the layer's address: its last picture drawn (the address, -/// area and units a pixel), when a large picture of it last failed, and when a picture saying its -/// resolution last failed. +/// area and units a pixel), when a large picture of it last failed, when a picture saying its +/// resolution was last refused, and when a HiDPI picture of it last gave way to a standard one. #[derive(Default)] struct Kept { last: HashMap, small: HashMap, plain: HashMap, + standard: HashMap, } fn kept() -> std::sync::MutexGuard<'static, Kept> { @@ -382,13 +368,28 @@ pub(crate) fn draw(pm: &mut Pixmap, req: &Request, t: &TileLayer, view: &View, f crate::native_tiles::paint_blended(pm, img, at, at, opacity, blend, &place); } }; - // Asked again smaller when the service refuses a large picture, and then without its - // resolution when it refuses one saying it. + // The units a pixel of a picture at a logical pixel's detail, as on a standard screen. + let logical_res = logical * (zoom_res(t.max_zoom, t.tile_size) / merc).max(1.0); + // What is tried when a picture fails, in turn, each once a frame: + // 1. the same picture without its resolution (a service may refuse a parameter it doesn't + // know); kept for a while only when that one draws and the server refused the first + // (a 4xx other than 408 or 429: not a server error, a timeout or a lost connection); + // 2. one at most 2048 pixels a side, with its resolution again, kept for a while (some + // services draw no larger); + // 3. the picture at a logical pixel's detail, as on a standard screen (a service may draw + // no picture as large as a HiDPI one), kept for a while once it draws. + // Past those, its tiles are drawn (and say what fails). let mut without = false; - for _ in 0..3 { + let was_standard = kept().standard.get(&key).is_some_and(|at| at.elapsed() < SMALL_FOR); + let mut standard = was_standard; + // The address (without its resolution) of the picture the server refused with it. + let mut refused: Option = None; + for _ in 0..6 { let small = kept().small.get(&key).is_some_and(|at| at.elapsed() < SMALL_FOR); - let plain = detail <= 1.0 || without || kept().plain.get(&key).is_some_and(|at| at.elapsed() < PLAIN_FOR); - let Some((at, pw, ph)) = picture(area, res, if small { SMALL_SIDE } else { MAX_SIDE }) else { return nothing }; + let sharp = detail > 1.0 && !standard; + let plain = !sharp || without || kept().plain.get(&key).is_some_and(|at| at.elapsed() < PLAIN_FOR); + let ask = if sharp { res } else { logical_res }; + let Some((at, pw, ph)) = picture(area, ask, if small { SMALL_SIDE } else { MAX_SIDE }) else { return nothing }; let (at, pw, ph) = match world_of(req.system.epsg) { Some(world) => match in_world(at, pw, ph, world)? { Some(p) => p, @@ -414,6 +415,9 @@ pub(crate) fn draw(pm: &mut Pixmap, req: &Request, t: &TileLayer, view: &View, f && let Payload::Image(img) = &*payload { paint(pm, img, *last_at); + if standard && !was_standard && detail > 1.0 { + remember_standard(&key); + } return nothing; } let address = Warp::map_address(&req.base, &req.system, at, pw, ph); @@ -426,49 +430,79 @@ pub(crate) fn draw(pm: &mut Pixmap, req: &Request, t: &TileLayer, view: &View, f if k.last.len() >= MAX_KEPT { k.last.clear(); } - // Drawn without its resolution where the picture with it failed: most likely - // the service refused the resolution, so the layer is asked without it for a - // while. Where both fail (a service down, a timeout), nothing is kept. - if without { + // The same picture drew without its resolution where it was refused with + // it: the layer is asked without it for a while. + if refused.as_deref() == Some(address.as_str()) { if k.plain.len() >= MAX_KEPT { k.plain.clear(); } k.plain.insert(key.clone(), web_time::Instant::now()); } - k.last.insert(key, (url, at, r)); + k.last.insert(key.clone(), (url, at, r)); + drop(k); + // A standard screen's picture drew where the HiDPI one failed this frame: so + // for a while, then the HiDPI one is tried again. + if standard && !was_standard && detail > 1.0 { + remember_standard(&key); + } } return nothing; } State::Pending => { - // The layer's last picture, where it still is in the cache, stands in. - if let Some((u, at, _)) = last - && let Some(payload) = cache::peek(&u) + // The layer's last picture, where it still is in the cache, stands in; failing + // that, the same picture without its resolution, where it came from a try + // before (the failure with it may have been forgotten since). + let plain_here = (url != address).then(|| (address.clone(), at)); + let stand_in = last.map(|(u, at, _)| (u, at)).into_iter().chain(plain_here).find_map(|(u, at)| match cache::peek(&u) { + Some(payload) if matches!(&*payload, Payload::Image(_)) => Some((payload, at)), + _ => None, + }); + if let Some((payload, at)) = stand_in && let Payload::Image(img) = &*payload { paint(pm, img, at); } return Some((true, None)); } - // Asked again at most 2048 pixels a side for a while: some services draw no larger. - // Tried first, with its resolution still: a failure of a large picture says nothing - // of the resolution. + State::Failed(e) if url != address => { + without = true; + if refusal(&e) { + refused = Some(address.clone()); + } + } State::Failed(_) if !small && pw.max(ph) > SMALL_SIDE => { let mut k = kept(); if k.small.len() >= MAX_KEPT { k.small.clear(); } k.small.insert(key.clone(), web_time::Instant::now()); + // The smaller picture is asked with its resolution again: the size was refused. + (without, refused) = (false, None); } - // Asked again without its resolution: a service may refuse a parameter it doesn't - // know (kept for a while only once that picture draws). - State::Failed(_) if url != address => without = true, - // A picture the service doesn't draw: its tiles may draw (and say what fails). + State::Failed(_) if sharp => standard = true, State::Failed(_) => return None, } } None } +/// Keeps the layer `key` to a standard screen's pictures for a while: its HiDPI one failed and +/// the standard one drew. +fn remember_standard(key: &str) { + let mut k = kept(); + if k.standard.len() >= MAX_KEPT { + k.standard.clear(); + } + k.standard.insert(key.to_string(), web_time::Instant::now()); +} + +/// Whether a fetch's failure is the server refusing what it was asked: a 4xx other than a +/// timeout (408) or too many requests (429). A server error, a timeout or a lost connection says +/// nothing of what was asked, and neither does an answer that isn't a picture (it may be any). +fn refusal(error: &str) -> bool { + matches!(cache::http_status(error), Some(400..=499)) && !matches!(cache::http_status(error), Some(408 | 429)) +} + /// Cells a side a picture in another system is warped in: each placed by its own corners, so /// it is off by at most the bend within a cell. const WARP_CELLS: u32 = 16; @@ -710,6 +744,11 @@ mod tests { for detail in [1.0, 0.4, f64::NAN, f64::INFINITY] { assert_eq!(with_dpi(wms.into(), detail), wms, "{detail}"); } + // A trailing `&`: no empty part before the resolution. + assert_eq!(with_dpi(format!("{wms}&"), 2.0), format!("{wms}&DPI=192&FORMAT_OPTIONS=dpi:181")); + // An encoded request name. + let encoded = "https://m.example.org/wms?SERVICE=WMS&REQUEST=%47etMap&LAYERS=a"; + assert_eq!(with_dpi(encoded.into(), 2.0), format!("{encoded}&DPI=192&FORMAT_OPTIONS=dpi:181")); // Lower case keys, and a fragment kept last. let lower = "https://m.example.org/wms?service=wms&request=getmap&layers=a#rustgis-x"; assert_eq!( diff --git a/crates/render/src/tiles.rs b/crates/render/src/tiles.rs index 123421b239..4a2aad3e2c 100644 --- a/crates/render/src/tiles.rs +++ b/crates/render/src/tiles.rs @@ -938,10 +938,15 @@ pub(crate) mod tests { if (url.starts_with("https://one.test/tiles-only/") && !url.contains("&WIDTH=256&HEIGHT=256")) || (url.starts_with("https://one.test/no-dpi/") && url.contains("DPI=")) || (url.starts_with("https://one.test/down/") && ONE_DOWN.load(std::sync::atomic::Ordering::Relaxed)) + || (url.starts_with("https://one.test/max1024/") + && url.split("&WIDTH=").nth(1).and_then(|w| w.split('&').next()?.parse::().ok()).is_some_and(|w| w > 1024)) || (url.starts_with("https://one.test/max2048/") && url.split("&WIDTH=").nth(1).and_then(|w| w.split('&').next()?.parse::().ok()).is_some_and(|w| w > 2048)) { - done(Err("400 Bad Request".into())); + done(Err("http status: 400".into())); + } else if url.starts_with("https://one.test/busy-dpi/") && url.contains("DPI=") { + // A passing failure of the picture with its resolution. + done(Err("http status: 503".into())); } else { done(Ok(color(20, 200, 20))); } @@ -1092,6 +1097,10 @@ pub(crate) mod tests { let green = [20, 200, 20, 255]; // The resolution a picture says it is for (#2390): 96 a logical pixel's worth of its own // pixels, which are up to a quarter of a level finer than asked for. + // An address with the resolution it says taken out again. + let without_dpi = |u: &str| -> String { + u.split('&').filter(|p| !p.starts_with("DPI=") && !p.starts_with("FORMAT_OPTIONS=dpi:")).collect::>().join("&") + }; let dpi = |u: &str| -> Option { let d: f64 = u.split("&DPI=").nth(1)?.split('&').next()?.parse().ok()?; let ogc: f64 = u.split("FORMAT_OPTIONS=dpi:").nth(1)?.split('&').next()?.parse().ok()?; @@ -1177,8 +1186,10 @@ pub(crate) mod tests { crate::render_png(&mut m, &view).unwrap(); let asked = std::mem::take(&mut *ASKED_ONE.lock().unwrap()); let width = |u: &String| u.split("&WIDTH=").nth(1).and_then(|w| w.split('&').next()?.parse::().ok()); - assert!(asked.len() == 2 && width(&asked[0]) > Some(2048) && width(&asked[1]) <= Some(2048), "{asked:?}"); - assert!(dpi(&asked[0]).is_some(), "{asked:?}"); + // With its resolution, without, then at most 2048 pixels a side. + assert_eq!(asked.len(), 3, "{asked:?}"); + assert!(dpi(&asked[0]).is_some() && asked[1] == without_dpi(&asked[0]), "{asked:?}"); + assert!(width(&asked[2]) <= Some(2048), "{asked:?}"); // Its resolution wasn't what was refused: a view whose picture fits still says it. let narrow = View { width: 200.0, ..view }; ASKED_ONE.lock().unwrap().clear(); @@ -1186,6 +1197,31 @@ pub(crate) mod tests { let asked = std::mem::take(&mut *ASKED_ONE.lock().unwrap()); assert!(asked.len() == 1 && dpi(&asked[0]).is_some(), "{asked:?}"); } + // A view whose HiDPI picture is over 2048 pixels but whose standard one isn't (700 points + // at 2 a point): asked at most 2048 pixels a side, still sharper than a standard screen's + // and saying its resolution, not at a standard screen's detail. + { + let mut m = Map::new("Map"); + m.graticule = false; + // An address of its own: the layer before is kept to 2048 pixels for a while. + let mut t = TileLayer::new("WMS", TileKind::Raster, url.replace("one.test/merc/", "one.test/max2048/b/")); + t.single_image = true; + m.tiles.push(t); + let center = m.to_map(bern).unwrap(); + let view = View { + width: 700.0, + height: 100.0, + pixels_per_point: 2.0, + camera: Camera { center, res: 30.0, bearing: 0.0, pitch: 0.0 }, + sky: false, + }; + ASKED_ONE.lock().unwrap().clear(); + crate::render_png(&mut m, &view).unwrap(); + let asked = std::mem::take(&mut *ASKED_ONE.lock().unwrap()); + let width = |u: &String| u.split("&WIDTH=").nth(1).and_then(|w| w.split('&').next()?.parse::().ok()); + assert!(asked.len() == 3 && width(&asked[0]) > Some(2048), "{asked:?}"); + assert!(width(&asked[2]).is_some_and(|w| w > 1280 && w <= 2048) && dpi(&asked[2]).is_some(), "{asked:?}"); + } // A service down for a while fails the picture with its resolution and without: once it // is back, the next picture says its resolution again (it wasn't what was refused). { @@ -1198,10 +1234,54 @@ pub(crate) mod tests { let (_, asked) = at(&mut m, 30.0, 0.0); ONE_DOWN.store(false, std::sync::atomic::Ordering::Relaxed); // Its picture with the resolution and without, then its tiles. - assert!(asked.len() > 2 && dpi(&asked[0]).is_some() && !asked[1].contains("DPI="), "{asked:?}"); + assert!(asked.len() > 2 && dpi(&asked[0]).is_some() && asked[1] == without_dpi(&asked[0]), "{asked:?}"); let (p, asked) = at(&mut m, 30.0, 50_000.0); assert!(p == [green; 3] && asked.len() == 1 && dpi(&asked[0]).is_some(), "{asked:?}"); } + // A service drawing no picture over 1024 pixels a side: the sharp picture fails with its + // resolution and without, and the picture at a standard screen's detail draws, as on main. + { + let mut m = Map::new("Map"); + m.graticule = false; + let mut t = TileLayer::new("WMS", TileKind::Raster, url.replace("one.test/merc/", "one.test/max1024/")); + t.single_image = true; + m.tiles.push(t); + let center = m.to_map(bern).unwrap(); + let view = View { + width: 400.0, + height: 100.0, + pixels_per_point: 2.0, + camera: Camera { center, res: 30.0, bearing: 0.0, pitch: 0.0 }, + sky: false, + }; + ASKED_ONE.lock().unwrap().clear(); + let png = crate::render_png(&mut m, &view).unwrap(); + let asked = std::mem::take(&mut *ASKED_ONE.lock().unwrap()); + let pm = tiny_skia::Pixmap::decode_png(&png).unwrap(); + let p = pm.pixel(400, 100).map(|p| [p.red(), p.green(), p.blue(), p.alpha()]).unwrap(); + assert!(p == green && asked.len() == 3, "{p:?} {asked:?}"); + assert!(dpi(&asked[0]).is_some() && asked[1] == without_dpi(&asked[0]), "{asked:?}"); + assert!(asked[2].contains("&WIDTH=768&") && !asked[2].contains("DPI="), "{asked:?}"); + // And for a while: a panned view asks for the standard screen's picture at once. + let view = View { camera: Camera { center: Coord::new(center.x + 50_000.0, center.y), ..view.camera }, ..view }; + ASKED_ONE.lock().unwrap().clear(); + crate::render_png(&mut m, &view).unwrap(); + let asked = std::mem::take(&mut *ASKED_ONE.lock().unwrap()); + assert!(asked.len() == 1 && asked[0].contains("&WIDTH=768&"), "{asked:?}"); + } + // A passing failure (a 503) of the picture with its resolution, the one without drawing: + // the resolution wasn't refused, so the next picture says it again. + { + let mut m = Map::new("Map"); + m.graticule = false; + let mut t = TileLayer::new("WMS", TileKind::Raster, url.replace("one.test/merc/", "one.test/busy-dpi/")); + t.single_image = true; + m.tiles.push(t); + let (p, asked) = at(&mut m, 30.0, 0.0); + assert!(p == [green; 3] && asked.len() == 2 && asked[1] == without_dpi(&asked[0]), "{asked:?}"); + let (_, asked) = at(&mut m, 30.0, 50_000.0); + assert!(asked.first().is_some_and(|u| dpi(u).is_some()), "{asked:?}"); + } // A service refusing the resolution: asked again without it, and drawn so. { let mut m = Map::new("Map"); diff --git a/docs/user-guide/data-formats.md b/docs/user-guide/data-formats.md index b1d874f430..f4bd0f8194 100644 --- a/docs/user-guide/data-formats.md +++ b/docs/user-guide/data-formats.md @@ -455,8 +455,12 @@ it's given `"singleImage": false` or a `"tileSize"`. A WMS picture then says the resolution it is for, unless its address names one: `DPI` (QGIS Server, from 96) and `FORMAT_OPTIONS=dpi:` (GeoServer, from the OGC standard pixel's 90.7), so those servers keep their symbols and text at their size. A service that reads neither draws them - smaller; one that refuses the picture with them is asked again without them, for five - minutes. The picture is at most 4096 pixels a side (2048 for five minutes after a larger + smaller. A picture that fails is asked again, in turn: without them; at most 2048 pixels a side, + with them; then at a standard screen's detail. A layer whose picture the server refuses with + them (a 4xx answer) and draws without them is asked without them for five minutes; a server + error, a timeout or a lost connection doesn't count. One that only draws at a standard screen's + detail is asked so for five minutes too. + The picture is at most 4096 pixels a side (2048 for five minutes after a larger picture of the layer fails), coarser for a view larger than that. A picture the service fails to draw leaves the layer drawn in tiles there. - Nothing is asked for zoomed out past the layer's scale range or outside its bounds, and closer @@ -485,11 +489,11 @@ of the view are fetched for the screen's own pixels, up to twice the detail of i (#2390). On the Web Mercator tile pyramid a 2x screen takes the next zoom's tiles; on a 5K or 6K screen, where those would be too many for the view, it takes a zoom between that and the usual one whose tiles fit, if any does, else the usual one (or the layer's coarsest zoom, where it has none -coarser). A layer in a system of its own (a WMTS matrix set) takes the coarsest of its service's -matrices with that much detail, which may be more than twice. Text and lines drawn into them come -out sharp, and smaller, as in QGIS. A screen at more than 2x (most phones), and a map export or -layout at a high resolution, still take tiles at twice the detail, not more: three times would ask -for nine times the tiles, with their text too small to read. +coarser). A layer in a system of its own (a WMTS matrix set) takes a matrix of its service's +near that detail, or a coarser one where the view would need too many of its tiles. Text and lines +drawn into them come out sharp, and smaller, as in QGIS. A screen at more than 2x (most phones), +and a map export or layout at a high resolution, still take tiles at twice the detail, not more: +three times would ask for nine times the tiles, with their text too small to read. Only a WMS layer drawn as one picture says the resolution it is for (above); in tiles it doesn't, and its symbols come out smaller. From c221e1ad32985e6b7f80af6c07febdcc3c4f0bf8 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Wed, 18 Nov 2026 11:57:49 +0100 Subject: [PATCH 5/7] fix(tiles): keep a WMS layer to standard detail only after a refusal, and only for pictures as large The standard-detail memory is recorded only when the server refused the HiDPI picture (a 4xx other than 408 or 429), with the side it refused: a smaller view's HiDPI picture is asked again. A 5xx or timeout keeps nothing. An empty answer to a picture with its resolution is retried without it. --- crates/render/src/single_image.rs | 51 ++++++++++++++++++++--------- crates/render/src/tiles.rs | 54 ++++++++++++++++++++++++++++++- docs/user-guide/data-formats.md | 46 +++++++++++++------------- 3 files changed, 112 insertions(+), 39 deletions(-) diff --git a/crates/render/src/single_image.rs b/crates/render/src/single_image.rs index 87aabd4a95..bd09ba110e 100644 --- a/crates/render/src/single_image.rs +++ b/crates/render/src/single_image.rs @@ -227,13 +227,14 @@ const PLAIN_FOR: web_time::Duration = SMALL_FOR; /// What is kept between frames, by the layer's address: its last picture drawn (the address, /// area and units a pixel), when a large picture of it last failed, when a picture saying its -/// resolution was last refused, and when a HiDPI picture of it last gave way to a standard one. +/// resolution was last refused, and when (and how large) a HiDPI picture of it last refused by +/// the server gave way to a standard one. #[derive(Default)] struct Kept { last: HashMap, small: HashMap, plain: HashMap, - standard: HashMap, + standard: HashMap, } fn kept() -> std::sync::MutexGuard<'static, Kept> { @@ -377,11 +378,23 @@ pub(crate) fn draw(pm: &mut Pixmap, req: &Request, t: &TileLayer, view: &View, f // 2. one at most 2048 pixels a side, with its resolution again, kept for a while (some // services draw no larger); // 3. the picture at a logical pixel's detail, as on a standard screen (a service may draw - // no picture as large as a HiDPI one), kept for a while once it draws. + // no picture as large as a HiDPI one); kept for a while, for HiDPI pictures as large, + // when the server refused the HiDPI one and the standard one draws. // Past those, its tiles are drawn (and say what fails). let mut without = false; - let was_standard = kept().standard.get(&key).is_some_and(|at| at.elapsed() < SMALL_FOR); + // Kept to a standard screen's pictures while its HiDPI ones are as large as one the server + // refused: a smaller view's may draw. + let was_standard = { + let side = |max| picture(area, res, max).map(|(_, w, h)| w.max(h)); + let k = kept(); + let max = if k.small.get(&key).is_some_and(|at| at.elapsed() < SMALL_FOR) { SMALL_SIDE } else { MAX_SIDE }; + k.standard.get(&key).is_some_and(|(at, refused)| at.elapsed() < SMALL_FOR && side(max).is_some_and(|s| s >= *refused)) + }; let mut standard = was_standard; + // The side of the HiDPI picture the server refused (without its resolution), which keeps the + // layer to a standard screen's pictures for a while once one draws; `None` for a passing + // failure (a 5xx, a timeout), which keeps nothing. + let mut refused_side: Option = None; // The address (without its resolution) of the picture the server refused with it. let mut refused: Option = None; for _ in 0..6 { @@ -415,14 +428,17 @@ pub(crate) fn draw(pm: &mut Pixmap, req: &Request, t: &TileLayer, view: &View, f && let Payload::Image(img) = &*payload { paint(pm, img, *last_at); - if standard && !was_standard && detail > 1.0 { - remember_standard(&key); + if let Some(side) = refused_side.filter(|_| standard && !was_standard) { + remember_standard(&key, side); } return nothing; } let address = Warp::map_address(&req.base, &req.system, at, pw, ph); let url = if plain { address.clone() } else { with_dpi(address.clone(), logical / r) }; match cache::get(&url, Want::Image) { + // No picture (an empty answer) with its resolution: asked again without it, as a + // failure, though not one the layer is kept without it for. + State::Ready(payload) if url != address && !matches!(&*payload, Payload::Image(_)) => without = true, State::Ready(payload) => { if let Payload::Image(img) = &*payload { paint(pm, img, at); @@ -440,10 +456,10 @@ pub(crate) fn draw(pm: &mut Pixmap, req: &Request, t: &TileLayer, view: &View, f } k.last.insert(key.clone(), (url, at, r)); drop(k); - // A standard screen's picture drew where the HiDPI one failed this frame: so - // for a while, then the HiDPI one is tried again. - if standard && !was_standard && detail > 1.0 { - remember_standard(&key); + // A standard screen's picture drew where the server refused the HiDPI one this + // frame: so for a while, then the HiDPI one is tried again. + if let Some(side) = refused_side.filter(|_| standard && !was_standard) { + remember_standard(&key, side); } } return nothing; @@ -479,28 +495,31 @@ pub(crate) fn draw(pm: &mut Pixmap, req: &Request, t: &TileLayer, view: &View, f // The smaller picture is asked with its resolution again: the size was refused. (without, refused) = (false, None); } - State::Failed(_) if sharp => standard = true, + State::Failed(e) if sharp => { + standard = true; + refused_side = refusal(&e).then_some(pw.max(ph)); + } State::Failed(_) => return None, } } None } -/// Keeps the layer `key` to a standard screen's pictures for a while: its HiDPI one failed and -/// the standard one drew. -fn remember_standard(key: &str) { +/// Keeps the layer `key` to a standard screen's pictures for a while, while its HiDPI ones are +/// `side` pixels or more: the server refused one that large and the standard one drew. +fn remember_standard(key: &str, side: u32) { let mut k = kept(); if k.standard.len() >= MAX_KEPT { k.standard.clear(); } - k.standard.insert(key.to_string(), web_time::Instant::now()); + k.standard.insert(key.to_string(), (web_time::Instant::now(), side)); } /// Whether a fetch's failure is the server refusing what it was asked: a 4xx other than a /// timeout (408) or too many requests (429). A server error, a timeout or a lost connection says /// nothing of what was asked, and neither does an answer that isn't a picture (it may be any). fn refusal(error: &str) -> bool { - matches!(cache::http_status(error), Some(400..=499)) && !matches!(cache::http_status(error), Some(408 | 429)) + matches!(cache::http_status(error), Some(status) if (400..=499).contains(&status) && status != 408 && status != 429) } /// Cells a side a picture in another system is warped in: each placed by its own corners, so diff --git a/crates/render/src/tiles.rs b/crates/render/src/tiles.rs index 4a2aad3e2c..23779c7475 100644 --- a/crates/render/src/tiles.rs +++ b/crates/render/src/tiles.rs @@ -944,6 +944,14 @@ pub(crate) mod tests { && url.split("&WIDTH=").nth(1).and_then(|w| w.split('&').next()?.parse::().ok()).is_some_and(|w| w > 2048)) { done(Err("http status: 400".into())); + } else if url.starts_with("https://one.test/busy-big/") + && url.split("&WIDTH=").nth(1).and_then(|w| w.split('&').next()?.parse::().ok()).is_some_and(|w| w > 1024) + { + // A passing failure of every picture over 1024 pixels. + done(Err("http status: 503".into())); + } else if url.starts_with("https://one.test/empty-dpi/") && url.contains("DPI=") { + // An empty answer to the picture with its resolution. + done(Ok(Vec::new())); } else if url.starts_with("https://one.test/busy-dpi/") && url.contains("DPI=") { // A passing failure of the picture with its resolution. done(Err("http status: 503".into())); @@ -1189,7 +1197,7 @@ pub(crate) mod tests { // With its resolution, without, then at most 2048 pixels a side. assert_eq!(asked.len(), 3, "{asked:?}"); assert!(dpi(&asked[0]).is_some() && asked[1] == without_dpi(&asked[0]), "{asked:?}"); - assert!(width(&asked[2]) <= Some(2048), "{asked:?}"); + assert!(width(&asked[2]).is_some_and(|w| (1..=2048).contains(&w)), "{asked:?}"); // Its resolution wasn't what was refused: a view whose picture fits still says it. let narrow = View { width: 200.0, ..view }; ASKED_ONE.lock().unwrap().clear(); @@ -1268,6 +1276,50 @@ pub(crate) mod tests { crate::render_png(&mut m, &view).unwrap(); let asked = std::mem::take(&mut *ASKED_ONE.lock().unwrap()); assert!(asked.len() == 1 && asked[0].contains("&WIDTH=768&"), "{asked:?}"); + // A narrower view's HiDPI picture (768 pixels) is smaller than the one refused: asked + // sharp again, with its resolution, and drawn. + let view = View { width: 200.0, camera: Camera { center: Coord::new(center.x + 100_000.0, center.y), ..view.camera }, ..view }; + ASKED_ONE.lock().unwrap().clear(); + crate::render_png(&mut m, &view).unwrap(); + let asked = std::mem::take(&mut *ASKED_ONE.lock().unwrap()); + assert!(asked.len() == 1 && asked[0].contains("&WIDTH=768&") && dpi(&asked[0]).is_some(), "{asked:?}"); + } + // A passing failure (a 503) of the HiDPI picture with its resolution and without: the + // standard screen's picture draws for that frame, but nothing is kept, so the next view's + // HiDPI picture is asked again. + { + let mut m = Map::new("Map"); + m.graticule = false; + let mut t = TileLayer::new("WMS", TileKind::Raster, url.replace("one.test/merc/", "one.test/busy-big/")); + t.single_image = true; + m.tiles.push(t); + let center = m.to_map(bern).unwrap(); + let view = View { + width: 400.0, + height: 100.0, + pixels_per_point: 2.0, + camera: Camera { center, res: 30.0, bearing: 0.0, pitch: 0.0 }, + sky: false, + }; + ASKED_ONE.lock().unwrap().clear(); + crate::render_png(&mut m, &view).unwrap(); + let asked = std::mem::take(&mut *ASKED_ONE.lock().unwrap()); + assert!(asked.len() == 3 && asked[2].contains("&WIDTH=768&"), "{asked:?}"); + let view = View { camera: Camera { center: Coord::new(center.x + 50_000.0, center.y), ..view.camera }, ..view }; + ASKED_ONE.lock().unwrap().clear(); + crate::render_png(&mut m, &view).unwrap(); + let asked = std::mem::take(&mut *ASKED_ONE.lock().unwrap()); + assert!(asked.first().is_some_and(|u| dpi(u).is_some() && u.contains("&WIDTH=1536&")), "{asked:?}"); + } + // An empty answer to the picture with its resolution: asked again without it, and drawn. + { + let mut m = Map::new("Map"); + m.graticule = false; + let mut t = TileLayer::new("WMS", TileKind::Raster, url.replace("one.test/merc/", "one.test/empty-dpi/")); + t.single_image = true; + m.tiles.push(t); + let (p, asked) = at(&mut m, 30.0, 0.0); + assert!(p == [green; 3] && asked.len() == 2 && asked[1] == without_dpi(&asked[0]), "{asked:?}"); } // A passing failure (a 503) of the picture with its resolution, the one without drawing: // the resolution wasn't refused, so the next picture says it again. diff --git a/docs/user-guide/data-formats.md b/docs/user-guide/data-formats.md index f4bd0f8194..7b7cb84bed 100644 --- a/docs/user-guide/data-formats.md +++ b/docs/user-guide/data-formats.md @@ -450,19 +450,20 @@ it's given `"singleImage": false` or a `"tileSize"`. - The picture reaches past the view, its middle on a grid, so small pans and zooms (most of a quarter of a level) ask for nothing new; while the next picture loads, the last one stays in place. -- On a high-density (HiDPI) screen it is asked for in the screen's own pixels, up to twice the - map's logical pixels, as tiles are (see [Sharp tiles on HiDPI displays](#sharp-tiles-on-hidpi-displays)). - A WMS picture then says the resolution it is for, unless its address names one: `DPI` (QGIS - Server, from 96) and `FORMAT_OPTIONS=dpi:` (GeoServer, from the OGC standard pixel's 90.7), so - those servers keep their symbols and text at their size. A service that reads neither draws them - smaller. A picture that fails is asked again, in turn: without them; at most 2048 pixels a side, - with them; then at a standard screen's detail. A layer whose picture the server refuses with - them (a 4xx answer) and draws without them is asked without them for five minutes; a server - error, a timeout or a lost connection doesn't count. One that only draws at a standard screen's - detail is asked so for five minutes too. - The picture is at most 4096 pixels a side (2048 for five minutes after a larger - picture of the layer fails), coarser for a view larger than that. A picture the service fails - to draw leaves the layer drawn in tiles there. +- On a high-density (HiDPI) screen it is asked for in the screen's own pixels, up to about twice + the map's logical pixels (its detail rounded a little finer, as for any picture), as tiles are + (see [Sharp tiles on HiDPI displays](#sharp-tiles-on-hidpi-displays)). A WMS picture then says + the resolution it is for, unless its address names one: `DPI` (QGIS Server, from 96) and + `FORMAT_OPTIONS=dpi:` (GeoServer, from the OGC standard pixel's 90.7), so those servers keep + their symbols and text at their size. A service that reads neither draws them smaller. A picture + that fails is asked again, in turn: without them; at most 2048 pixels a side, with them; then at + a standard screen's detail. A layer whose picture the server refuses with them (a 4xx answer + other than 408 or 429) and draws without them is asked without them for five minutes; a server + error, a timeout or a lost connection doesn't count. One whose HiDPI picture the server refuses + (the same 4xx) and whose standard one draws is asked at a standard screen's detail for five + minutes, while its HiDPI pictures would be as large. The picture is at most 4096 pixels a side + (2048 for five minutes after a larger picture of the layer fails), coarser for a view larger + than that. A picture the service fails to draw leaves the layer drawn in tiles there. - Nothing is asked for zoomed out past the layer's scale range or outside its bounds, and closer in than its finest zoom its picture there is stretched, as its tiles are. - It is drawn so on a flat map, north up or turned, in Web Mercator or in a projection with an @@ -485,15 +486,16 @@ before keeps its tiles until you switch it. On a high-density screen (a Retina or 4K display scaled 2x, a phone), raster tile layers (XYZ, WMTS, WMS in tiles, raster archives, raster DEMs, and those in a system of their own) and pictures -of the view are fetched for the screen's own pixels, up to twice the detail of its logical pixels -(#2390). On the Web Mercator tile pyramid a 2x screen takes the next zoom's tiles; on a 5K or 6K -screen, where those would be too many for the view, it takes a zoom between that and the usual one -whose tiles fit, if any does, else the usual one (or the layer's coarsest zoom, where it has none -coarser). A layer in a system of its own (a WMTS matrix set) takes a matrix of its service's -near that detail, or a coarser one where the view would need too many of its tiles. Text and lines -drawn into them come out sharp, and smaller, as in QGIS. A screen at more than 2x (most phones), -and a map export or layout at a high resolution, still take tiles at twice the detail, not more: -three times would ask for nine times the tiles, with their text too small to read. +of the view are fetched for the screen's own pixels, up to about twice the detail of its logical +pixels (#2390; a zoom or picture is rounded a little finer, as on a standard screen). On the Web +Mercator tile pyramid a 2x screen takes the next zoom's tiles; on a 5K or 6K screen, where those +would be too many for the view, it takes a zoom between that and the usual one whose tiles fit, if +any does, else the usual one (or the layer's coarsest zoom, where it has none coarser). A layer in +a system of its own (a WMTS matrix set) takes a matrix of its service's near that detail, or a +coarser one where the view would need too many of its tiles. Text and lines drawn into them come +out sharp, and smaller, as in QGIS. A screen at more than 2x (most phones), and a map export or +layout at a high resolution, still take tiles at twice the detail, not more: three times would ask +for nine times the tiles, with their text too small to read. Only a WMS layer drawn as one picture says the resolution it is for (above); in tiles it doesn't, and its symbols come out smaller. From b65957813aa01181cd2e195c55a413b539c56124 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Wed, 18 Nov 2026 12:19:22 +0100 Subject: [PATCH 6/7] fix(tiles): a WMS exception counts as a refusal, an empty HiDPI answer as a failure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A picture that doesn't decode now fails with 'not a picture: …' in the cache, so a WMS exception sent as a document counts as a refusal of the resolution. An empty answer to a HiDPI picture moves to the next step without keeping anything; to a standard screen's picture it is still nothing to draw. The standard-detail memory compares width and height, as the picture is clipped. --- crates/render/src/single_image.rs | 49 +++++++++++------ crates/render/src/tiles.rs | 90 ++++++++++++++++++++++++++++++- crates/tiles/src/cache.rs | 11 ++-- docs/user-guide/data-formats.md | 9 ++-- 4 files changed, 134 insertions(+), 25 deletions(-) diff --git a/crates/render/src/single_image.rs b/crates/render/src/single_image.rs index bd09ba110e..5e549a9626 100644 --- a/crates/render/src/single_image.rs +++ b/crates/render/src/single_image.rs @@ -234,7 +234,7 @@ struct Kept { last: HashMap, small: HashMap, plain: HashMap, - standard: HashMap, + standard: HashMap, } fn kept() -> std::sync::MutexGuard<'static, Kept> { @@ -385,16 +385,23 @@ pub(crate) fn draw(pm: &mut Pixmap, req: &Request, t: &TileLayer, view: &View, f // Kept to a standard screen's pictures while its HiDPI ones are as large as one the server // refused: a smaller view's may draw. let was_standard = { - let side = |max| picture(area, res, max).map(|(_, w, h)| w.max(h)); + // The HiDPI picture's size as it would be asked: kept to the world, as below. + let size = |max| { + let (at, w, h) = picture(area, res, max)?; + match world_of(req.system.epsg) { + Some(world) => in_world(at, w, h, world).flatten().map(|(_, w, h)| (w, h)), + None => Some((w, h)), + } + }; let k = kept(); let max = if k.small.get(&key).is_some_and(|at| at.elapsed() < SMALL_FOR) { SMALL_SIDE } else { MAX_SIDE }; - k.standard.get(&key).is_some_and(|(at, refused)| at.elapsed() < SMALL_FOR && side(max).is_some_and(|s| s >= *refused)) + k.standard.get(&key).is_some_and(|(at, (rw, rh))| at.elapsed() < SMALL_FOR && size(max).is_some_and(|(w, h)| w >= *rw && h >= *rh)) }; let mut standard = was_standard; - // The side of the HiDPI picture the server refused (without its resolution), which keeps the + // The size of the HiDPI picture the server refused (without its resolution), which keeps the // layer to a standard screen's pictures for a while once one draws; `None` for a passing // failure (a 5xx, a timeout), which keeps nothing. - let mut refused_side: Option = None; + let mut refused_side: Option<(u32, u32)> = None; // The address (without its resolution) of the picture the server refused with it. let mut refused: Option = None; for _ in 0..6 { @@ -435,10 +442,15 @@ pub(crate) fn draw(pm: &mut Pixmap, req: &Request, t: &TileLayer, view: &View, f } let address = Warp::map_address(&req.base, &req.system, at, pw, ph); let url = if plain { address.clone() } else { with_dpi(address.clone(), logical / r) }; - match cache::get(&url, Want::Image) { - // No picture (an empty answer) with its resolution: asked again without it, as a - // failure, though not one the layer is kept without it for. - State::Ready(payload) if url != address && !matches!(&*payload, Payload::Image(_)) => without = true, + // An empty answer to a HiDPI picture is taken as a failure, so the steps below are + // tried (a service may draw nothing that large), but not as a refusal: nothing is kept + // for it. To a standard screen's picture it is "nothing here", drawn as such. (An + // answer that doesn't decode, such as a WMS exception, fails in the cache already.) + let state = match cache::get(&url, Want::Image) { + State::Ready(payload) if sharp && !matches!(&*payload, Payload::Image(_)) => State::Failed(EMPTY.into()), + state => state, + }; + match state { State::Ready(payload) => { if let Payload::Image(img) = &*payload { paint(pm, img, at); @@ -486,7 +498,7 @@ pub(crate) fn draw(pm: &mut Pixmap, req: &Request, t: &TileLayer, view: &View, f refused = Some(address.clone()); } } - State::Failed(_) if !small && pw.max(ph) > SMALL_SIDE => { + State::Failed(e) if !small && pw.max(ph) > SMALL_SIDE && e != EMPTY => { let mut k = kept(); if k.small.len() >= MAX_KEPT { k.small.clear(); @@ -497,7 +509,7 @@ pub(crate) fn draw(pm: &mut Pixmap, req: &Request, t: &TileLayer, view: &View, f } State::Failed(e) if sharp => { standard = true; - refused_side = refusal(&e).then_some(pw.max(ph)); + refused_side = refusal(&e).then_some((pw, ph)); } State::Failed(_) => return None, } @@ -506,8 +518,8 @@ pub(crate) fn draw(pm: &mut Pixmap, req: &Request, t: &TileLayer, view: &View, f } /// Keeps the layer `key` to a standard screen's pictures for a while, while its HiDPI ones are -/// `side` pixels or more: the server refused one that large and the standard one drew. -fn remember_standard(key: &str, side: u32) { +/// at least `side` (width, height): the server refused one that large and the standard one drew. +fn remember_standard(key: &str, side: (u32, u32)) { let mut k = kept(); if k.standard.len() >= MAX_KEPT { k.standard.clear(); @@ -515,11 +527,16 @@ fn remember_standard(key: &str, side: u32) { k.standard.insert(key.to_string(), (web_time::Instant::now(), side)); } +/// What an empty answer to a HiDPI picture fails with (a failure, not a refusal). +const EMPTY: &str = "an empty answer"; + /// Whether a fetch's failure is the server refusing what it was asked: a 4xx other than a -/// timeout (408) or too many requests (429). A server error, a timeout or a lost connection says -/// nothing of what was asked, and neither does an answer that isn't a picture (it may be any). +/// timeout (408) or too many requests (429), or a document that isn't a picture (a WMS +/// exception). A server error, a timeout, a lost connection or an empty answer says nothing of +/// what was asked. fn refusal(error: &str) -> bool { - matches!(cache::http_status(error), Some(status) if (400..=499).contains(&status) && status != 408 && status != 429) + error.starts_with(cache::NOT_A_PICTURE) + || matches!(cache::http_status(error), Some(status) if (400..=499).contains(&status) && status != 408 && status != 429) } /// Cells a side a picture in another system is warped in: each placed by its own corners, so diff --git a/crates/render/src/tiles.rs b/crates/render/src/tiles.rs index 23779c7475..f980a75b62 100644 --- a/crates/render/src/tiles.rs +++ b/crates/render/src/tiles.rs @@ -949,6 +949,20 @@ pub(crate) mod tests { { // A passing failure of every picture over 1024 pixels. done(Err("http status: 503".into())); + } else if url.starts_with("https://one.test/xml-dpi/") && url.contains("DPI=") { + // A WMS exception, sent as a document, for a parameter it doesn't know. + done(Ok( + b"DPI" + .to_vec(), + )); + } else if url.starts_with("https://one.test/empty-big/") + && url.split("&WIDTH=").nth(1).and_then(|w| w.split('&').next()?.parse::().ok()).is_some_and(|w| w > 1024) + { + // An empty answer for every picture over 1024 pixels. + done(Ok(Vec::new())); + } else if url.starts_with("https://one.test/empty-all/") { + // Nothing here: an empty answer to every request. + done(Ok(Vec::new())); } else if url.starts_with("https://one.test/empty-dpi/") && url.contains("DPI=") { // An empty answer to the picture with its resolution. done(Ok(Vec::new())); @@ -1283,6 +1297,18 @@ pub(crate) mod tests { crate::render_png(&mut m, &view).unwrap(); let asked = std::mem::take(&mut *ASKED_ONE.lock().unwrap()); assert!(asked.len() == 1 && asked[0].contains("&WIDTH=768&") && dpi(&asked[0]).is_some(), "{asked:?}"); + // A tall view's HiDPI picture (512 × 1536), narrower than the refused one (1536 × 512) + // though as long: asked sharp again. + let view = View { + width: 100.0, + height: 400.0, + camera: Camera { center: Coord::new(center.x + 150_000.0, center.y), ..view.camera }, + ..view + }; + ASKED_ONE.lock().unwrap().clear(); + crate::render_png(&mut m, &view).unwrap(); + let asked = std::mem::take(&mut *ASKED_ONE.lock().unwrap()); + assert!(asked.len() == 1 && asked[0].contains("&WIDTH=512&HEIGHT=1536&") && dpi(&asked[0]).is_some(), "{asked:?}"); } // A passing failure (a 503) of the HiDPI picture with its resolution and without: the // standard screen's picture draws for that frame, but nothing is kept, so the next view's @@ -1302,15 +1328,75 @@ pub(crate) mod tests { sky: false, }; ASKED_ONE.lock().unwrap().clear(); - crate::render_png(&mut m, &view).unwrap(); + let png = crate::render_png(&mut m, &view).unwrap(); let asked = std::mem::take(&mut *ASKED_ONE.lock().unwrap()); - assert!(asked.len() == 3 && asked[2].contains("&WIDTH=768&"), "{asked:?}"); + let pm = tiny_skia::Pixmap::decode_png(&png).unwrap(); + let p = pm.pixel(400, 100).map(|p| [p.red(), p.green(), p.blue(), p.alpha()]).unwrap(); + assert!(p == green && asked.len() == 3, "{p:?} {asked:?}"); + assert!(asked[1] == without_dpi(&asked[0]) && asked[2].contains("&WIDTH=768&"), "{asked:?}"); let view = View { camera: Camera { center: Coord::new(center.x + 50_000.0, center.y), ..view.camera }, ..view }; ASKED_ONE.lock().unwrap().clear(); crate::render_png(&mut m, &view).unwrap(); let asked = std::mem::take(&mut *ASKED_ONE.lock().unwrap()); assert!(asked.first().is_some_and(|u| dpi(u).is_some() && u.contains("&WIDTH=1536&")), "{asked:?}"); } + // A WMS exception sent as a document for the resolution: a refusal, so the picture without + // it draws and the layer is asked without it for a while. + { + let mut m = Map::new("Map"); + m.graticule = false; + let mut t = TileLayer::new("WMS", TileKind::Raster, url.replace("one.test/merc/", "one.test/xml-dpi/")); + t.single_image = true; + m.tiles.push(t); + let (p, asked) = at(&mut m, 30.0, 0.0); + assert!(p == [green; 3] && asked.len() == 2 && asked[1] == without_dpi(&asked[0]), "{asked:?}"); + let (p, asked) = at(&mut m, 30.0, 50_000.0); + assert!(p == [green; 3] && asked.len() == 1 && !asked[0].contains("DPI="), "{asked:?}"); + } + // Empty answers to the HiDPI picture with its resolution and without: the standard + // screen's picture draws. + { + let mut m = Map::new("Map"); + m.graticule = false; + let mut t = TileLayer::new("WMS", TileKind::Raster, url.replace("one.test/merc/", "one.test/empty-big/")); + t.single_image = true; + m.tiles.push(t); + let center = m.to_map(bern).unwrap(); + let view = View { + width: 400.0, + height: 100.0, + pixels_per_point: 2.0, + camera: Camera { center, res: 30.0, bearing: 0.0, pitch: 0.0 }, + sky: false, + }; + ASKED_ONE.lock().unwrap().clear(); + let png = crate::render_png(&mut m, &view).unwrap(); + let asked = std::mem::take(&mut *ASKED_ONE.lock().unwrap()); + let pm = tiny_skia::Pixmap::decode_png(&png).unwrap(); + let p = pm.pixel(400, 100).map(|p| [p.red(), p.green(), p.blue(), p.alpha()]).unwrap(); + assert!(p == green && asked.len() == 3 && asked[2].contains("&WIDTH=768&"), "{p:?} {asked:?}"); + } + // An empty answer to a standard screen's picture is "nothing here", as before: one + // request, no tiles asked. + { + let mut m = Map::new("Map"); + m.graticule = false; + let mut t = TileLayer::new("WMS", TileKind::Raster, url.replace("one.test/merc/", "one.test/empty-all/")); + t.single_image = true; + m.tiles.push(t); + let center = m.to_map(bern).unwrap(); + let view = View { + width: 200.0, + height: 100.0, + pixels_per_point: 1.0, + camera: Camera { center, res: 30.0, bearing: 0.0, pitch: 0.0 }, + sky: false, + }; + ASKED_ONE.lock().unwrap().clear(); + crate::render_png(&mut m, &view).unwrap(); + let asked = std::mem::take(&mut *ASKED_ONE.lock().unwrap()); + assert_eq!(asked.len(), 1, "{asked:?}"); + } // An empty answer to the picture with its resolution: asked again without it, and drawn. { let mut m = Map::new("Map"); diff --git a/crates/tiles/src/cache.rs b/crates/tiles/src/cache.rs index add37bd170..973fdb141a 100644 --- a/crates/tiles/src/cache.rs +++ b/crates/tiles/src/cache.rs @@ -1485,7 +1485,7 @@ fn decode_with( b, Box::new(move |r| { settle(match r { - Ok(raw) => decode_image(&raw).map(Payload::Image), + Ok(raw) => decode_image(&raw).map(Payload::Image).map_err(|e| format!("{NOT_A_PICTURE}: {e}")), Err(b) => decode(&b, want), }) }), @@ -2363,6 +2363,9 @@ fn payload_bytes(p: &Payload) -> usize { } } +/// What an image fetch whose answer isn't a picture fails with first (then why). +pub const NOT_A_PICTURE: &str = "not a picture"; + /// Decodes fetched bytes. pub fn decode(bytes: &[u8], want: Want) -> Result { if bytes.is_empty() { @@ -2372,7 +2375,9 @@ pub fn decode(bytes: &[u8], want: Want) -> Result { return Err(format!("{} bytes is too large for a tile", bytes.len())); } match want { - Want::Image => decode_image(bytes).map(Payload::Image), + // Said as such, so a service's answer that isn't one (a WMS exception document) is told + // from a failed fetch. + Want::Image => decode_image(bytes).map(Payload::Image).map_err(|e| format!("{NOT_A_PICTURE}: {e}")), Want::Vector => mvt::decode(bytes).map(Payload::Vector), Want::Json => serde_json::from_slice(bytes).map(Payload::Json).map_err(|e| format!("not JSON: {e}")), } @@ -4714,7 +4719,7 @@ mod tests { assert_eq!((back.width(), back.height()), (2, 2)); assert!(matches!(decode(b"{\"a\": 1}", Want::Json), Ok(Payload::Json(_)))); assert!(matches!(decode(b"", Want::Image), Ok(Payload::Empty))); - assert!(decode(b"not a picture", Want::Image).is_err()); + assert!(decode(b"not a picture", Want::Image).is_err_and(|e| e.starts_with(NOT_A_PICTURE))); assert!(decode(b"{", Want::Json).is_err()); } diff --git a/docs/user-guide/data-formats.md b/docs/user-guide/data-formats.md index 7b7cb84bed..acfb77f6d7 100644 --- a/docs/user-guide/data-formats.md +++ b/docs/user-guide/data-formats.md @@ -458,10 +458,11 @@ it's given `"singleImage": false` or a `"tileSize"`. their symbols and text at their size. A service that reads neither draws them smaller. A picture that fails is asked again, in turn: without them; at most 2048 pixels a side, with them; then at a standard screen's detail. A layer whose picture the server refuses with them (a 4xx answer - other than 408 or 429) and draws without them is asked without them for five minutes; a server - error, a timeout or a lost connection doesn't count. One whose HiDPI picture the server refuses - (the same 4xx) and whose standard one draws is asked at a standard screen's detail for five - minutes, while its HiDPI pictures would be as large. The picture is at most 4096 pixels a side + other than 408 or 429, or a document that isn't a picture, such as a WMS exception) and draws + without them is asked without them for five minutes; a server error, a timeout, a lost + connection or an empty answer doesn't count. One whose HiDPI picture the server refuses so and + whose standard one draws is asked at a standard screen's detail for five minutes, while its + HiDPI pictures would be at least as wide and as tall. The picture is at most 4096 pixels a side (2048 for five minutes after a larger picture of the layer fails), coarser for a view larger than that. A picture the service fails to draw leaves the layer drawn in tiles there. - Nothing is asked for zoomed out past the layer's scale range or outside its bounds, and closer From 07956c3fdc67f6e8a548a4814b263b116425118f Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Wed, 18 Nov 2026 12:41:29 +0100 Subject: [PATCH 7/7] fix(tiles): an empty answer over 2048 pixels tries the 2048-pixel picture; only non-pictures are 'not a picture' An empty answer to a HiDPI picture over 2048 pixels now tries one of at most 2048 this frame, keeping no size limit. The cache's 'not a picture' marks only answers in no picture format (a WMS exception document); a picture that can't be read keeps its own message and isn't taken as a refusal. --- crates/render/src/single_image.rs | 34 ++++++++++++++++++------------ crates/render/src/tiles.rs | 35 +++++++++++++++++++++++++++++++ crates/tiles/src/cache.rs | 15 ++++++++----- docs/user-guide/data-formats.md | 5 +++-- 4 files changed, 69 insertions(+), 20 deletions(-) diff --git a/crates/render/src/single_image.rs b/crates/render/src/single_image.rs index 5e549a9626..64de99558f 100644 --- a/crates/render/src/single_image.rs +++ b/crates/render/src/single_image.rs @@ -375,13 +375,13 @@ pub(crate) fn draw(pm: &mut Pixmap, req: &Request, t: &TileLayer, view: &View, f // 1. the same picture without its resolution (a service may refuse a parameter it doesn't // know); kept for a while only when that one draws and the server refused the first // (a 4xx other than 408 or 429: not a server error, a timeout or a lost connection); - // 2. one at most 2048 pixels a side, with its resolution again, kept for a while (some - // services draw no larger); + // 2. one at most 2048 pixels a side, with its resolution again (some services draw no + // larger), kept for a while unless the larger one's answer was empty; // 3. the picture at a logical pixel's detail, as on a standard screen (a service may draw // no picture as large as a HiDPI one); kept for a while, for HiDPI pictures as large, // when the server refused the HiDPI one and the standard one draws. // Past those, its tiles are drawn (and say what fails). - let mut without = false; + let (mut without, mut smaller) = (false, false); // Kept to a standard screen's pictures while its HiDPI ones are as large as one the server // refused: a smaller view's may draw. let was_standard = { @@ -393,9 +393,11 @@ pub(crate) fn draw(pm: &mut Pixmap, req: &Request, t: &TileLayer, view: &View, f None => Some((w, h)), } }; - let k = kept(); - let max = if k.small.get(&key).is_some_and(|at| at.elapsed() < SMALL_FOR) { SMALL_SIDE } else { MAX_SIDE }; - k.standard.get(&key).is_some_and(|(at, (rw, rh))| at.elapsed() < SMALL_FOR && size(max).is_some_and(|(w, h)| w >= *rw && h >= *rh)) + let held = kept(); + let max = if held.small.get(&key).is_some_and(|at| at.elapsed() < SMALL_FOR) { SMALL_SIDE } else { MAX_SIDE }; + held.standard + .get(&key) + .is_some_and(|(at, (rw, rh))| at.elapsed() < SMALL_FOR && size(max).is_some_and(|(w, h)| w >= *rw && h >= *rh)) }; let mut standard = was_standard; // The size of the HiDPI picture the server refused (without its resolution), which keeps the @@ -405,7 +407,9 @@ pub(crate) fn draw(pm: &mut Pixmap, req: &Request, t: &TileLayer, view: &View, f // The address (without its resolution) of the picture the server refused with it. let mut refused: Option = None; for _ in 0..6 { - let small = kept().small.get(&key).is_some_and(|at| at.elapsed() < SMALL_FOR); + // At most 2048 pixels: kept for a while after a refusal, or this frame after an empty + // answer (which keeps nothing). + let small = smaller || kept().small.get(&key).is_some_and(|at| at.elapsed() < SMALL_FOR); let sharp = detail > 1.0 && !standard; let plain = !sharp || without || kept().plain.get(&key).is_some_and(|at| at.elapsed() < PLAIN_FOR); let ask = if sharp { res } else { logical_res }; @@ -498,13 +502,17 @@ pub(crate) fn draw(pm: &mut Pixmap, req: &Request, t: &TileLayer, view: &View, f refused = Some(address.clone()); } } - State::Failed(e) if !small && pw.max(ph) > SMALL_SIDE && e != EMPTY => { - let mut k = kept(); - if k.small.len() >= MAX_KEPT { - k.small.clear(); + State::Failed(e) if !small && pw.max(ph) > SMALL_SIDE => { + if e == EMPTY { + smaller = true; + } else { + let mut k = kept(); + if k.small.len() >= MAX_KEPT { + k.small.clear(); + } + k.small.insert(key.clone(), web_time::Instant::now()); } - k.small.insert(key.clone(), web_time::Instant::now()); - // The smaller picture is asked with its resolution again: the size was refused. + // The smaller picture is asked with its resolution again: the size failed. (without, refused) = (false, None); } State::Failed(e) if sharp => { diff --git a/crates/render/src/tiles.rs b/crates/render/src/tiles.rs index f980a75b62..7ab591d0e0 100644 --- a/crates/render/src/tiles.rs +++ b/crates/render/src/tiles.rs @@ -960,6 +960,11 @@ pub(crate) mod tests { { // An empty answer for every picture over 1024 pixels. done(Ok(Vec::new())); + } else if url.starts_with("https://one.test/empty2048/") + && url.split("&WIDTH=").nth(1).and_then(|w| w.split('&').next()?.parse::().ok()).is_some_and(|w| w > 2048) + { + // An empty answer for every picture over 2048 pixels. + done(Ok(Vec::new())); } else if url.starts_with("https://one.test/empty-all/") { // Nothing here: an empty answer to every request. done(Ok(Vec::new())); @@ -1376,6 +1381,36 @@ pub(crate) mod tests { let p = pm.pixel(400, 100).map(|p| [p.red(), p.green(), p.blue(), p.alpha()]).unwrap(); assert!(p == green && asked.len() == 3 && asked[2].contains("&WIDTH=768&"), "{p:?} {asked:?}"); } + // Empty answers over 2048 pixels: the picture of at most 2048 pixels is asked this frame + // and draws, and nothing is kept (the next view asks its full size first again). + { + let mut m = Map::new("Map"); + m.graticule = false; + let mut t = TileLayer::new("WMS", TileKind::Raster, url.replace("one.test/merc/", "one.test/empty2048/")); + t.single_image = true; + m.tiles.push(t); + let center = m.to_map(bern).unwrap(); + let view = View { + width: 1500.0, + height: 100.0, + pixels_per_point: 2.0, + camera: Camera { center, res: 30.0, bearing: 0.0, pitch: 0.0 }, + sky: false, + }; + let width = |u: &String| u.split("&WIDTH=").nth(1).and_then(|w| w.split('&').next()?.parse::().ok()); + ASKED_ONE.lock().unwrap().clear(); + let png = crate::render_png(&mut m, &view).unwrap(); + let asked = std::mem::take(&mut *ASKED_ONE.lock().unwrap()); + let pm = tiny_skia::Pixmap::decode_png(&png).unwrap(); + let p = pm.pixel(1500, 100).map(|p| [p.red(), p.green(), p.blue(), p.alpha()]).unwrap(); + assert!(p == green && asked.len() == 3, "{p:?} {asked:?}"); + assert!(width(&asked[2]).is_some_and(|w| (1..=2048).contains(&w)), "{asked:?}"); + let view = View { camera: Camera { center: Coord::new(center.x + 200_000.0, center.y), ..view.camera }, ..view }; + ASKED_ONE.lock().unwrap().clear(); + crate::render_png(&mut m, &view).unwrap(); + let asked = std::mem::take(&mut *ASKED_ONE.lock().unwrap()); + assert!(asked.first().and_then(width).is_some_and(|w| w > 2048), "{asked:?}"); + } // An empty answer to a standard screen's picture is "nothing here", as before: one // request, no tiles asked. { diff --git a/crates/tiles/src/cache.rs b/crates/tiles/src/cache.rs index 973fdb141a..e677972341 100644 --- a/crates/tiles/src/cache.rs +++ b/crates/tiles/src/cache.rs @@ -1485,7 +1485,8 @@ fn decode_with( b, Box::new(move |r| { settle(match r { - Ok(raw) => decode_image(&raw).map(Payload::Image).map_err(|e| format!("{NOT_A_PICTURE}: {e}")), + // The browser read it as a picture: a failure here isn't a document. + Ok(raw) => decode_image(&raw).map(Payload::Image), Err(b) => decode(&b, want), }) }), @@ -2363,7 +2364,7 @@ fn payload_bytes(p: &Payload) -> usize { } } -/// What an image fetch whose answer isn't a picture fails with first (then why). +/// What an image fetch whose answer is in no picture format fails with first (then why). pub const NOT_A_PICTURE: &str = "not a picture"; /// Decodes fetched bytes. @@ -2375,9 +2376,11 @@ pub fn decode(bytes: &[u8], want: Want) -> Result { return Err(format!("{} bytes is too large for a tile", bytes.len())); } match want { - // Said as such, so a service's answer that isn't one (a WMS exception document) is told - // from a failed fetch. - Want::Image => decode_image(bytes).map(Payload::Image).map_err(|e| format!("{NOT_A_PICTURE}: {e}")), + // An answer in no picture format (a WMS exception document) is said as such, so it is + // told from a failed fetch and from a picture that couldn't be read (one too large). + Want::Image => decode_image(bytes) + .map(Payload::Image) + .map_err(|e| if bytes.starts_with(RAW) || image::guess_format(bytes).is_ok() { e } else { format!("{NOT_A_PICTURE}: {e}") }), Want::Vector => mvt::decode(bytes).map(Payload::Vector), Want::Json => serde_json::from_slice(bytes).map(Payload::Json).map_err(|e| format!("not JSON: {e}")), } @@ -4720,6 +4723,8 @@ mod tests { assert!(matches!(decode(b"{\"a\": 1}", Want::Json), Ok(Payload::Json(_)))); assert!(matches!(decode(b"", Want::Image), Ok(Payload::Empty))); assert!(decode(b"not a picture", Want::Image).is_err_and(|e| e.starts_with(NOT_A_PICTURE))); + // A picture that can't be read (here, cut short) is no document: said as it is. + assert!(decode(b"\x89PNG\r\n\x1a\n\0\0\0\rIHDR", Want::Image).is_err_and(|e| !e.starts_with(NOT_A_PICTURE))); assert!(decode(b"{", Want::Json).is_err()); } diff --git a/docs/user-guide/data-formats.md b/docs/user-guide/data-formats.md index acfb77f6d7..5bb42c27e5 100644 --- a/docs/user-guide/data-formats.md +++ b/docs/user-guide/data-formats.md @@ -463,8 +463,9 @@ it's given `"singleImage": false` or a `"tileSize"`. connection or an empty answer doesn't count. One whose HiDPI picture the server refuses so and whose standard one draws is asked at a standard screen's detail for five minutes, while its HiDPI pictures would be at least as wide and as tall. The picture is at most 4096 pixels a side - (2048 for five minutes after a larger picture of the layer fails), coarser for a view larger - than that. A picture the service fails to draw leaves the layer drawn in tiles there. + (2048 for five minutes after a larger picture of the layer fails, other than with an empty + answer), coarser for a view larger than that. A picture the service fails to draw leaves the + layer drawn in tiles there. - Nothing is asked for zoomed out past the layer's scale range or outside its bounds, and closer in than its finest zoom its picture there is stretched, as its tiles are. - It is drawn so on a flat map, north up or turned, in Web Mercator or in a projection with an