diff --git a/crates/engine/src/cmd/raster.rs b/crates/engine/src/cmd/raster.rs index 0b094adf5..a2cad8dcf 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: 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/native_tiles.rs b/crates/render/src/native_tiles.rs index fda47b6ec..e51446982 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 3670c4d12..64de99558 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,16 +151,90 @@ 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)) } +/// 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((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(); + // Keys compared decoded (`FORMAT%5FOPTIONS`), and written back as they came. + 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| { + 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))); + if !get_map || own { + return url; + } + let dpi = (96.0 * detail).round(); + 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(), + }) + // 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 { + 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); + } + 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); +/// 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, when a picture saying its +/// 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, } fn kept() -> std::sync::MutexGuard<'static, Kept> { @@ -223,10 +299,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 +336,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; } @@ -287,10 +369,51 @@ 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 { - let small = kept().small.get(&key).is_some_and(|at| at.elapsed() < SMALL_FOR); - let Some((at, pw, ph)) = picture(area, res, if small { SMALL_SIDE } else { MAX_SIDE }) else { return nothing }; + // 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 (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, 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 = { + // 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 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 + // 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<(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 { + // 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 }; + 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, @@ -298,6 +421,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 @@ -315,10 +439,22 @@ 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 let Some(side) = refused_side.filter(|_| standard && !was_standard) { + remember_standard(&key, side); + } return nothing; } - let url = Warp::map_address(&req.base, &req.system, at, pw, ph); - match cache::get(&url, Want::Image) { + 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) }; + // 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); @@ -326,35 +462,91 @@ pub(crate) fn draw(pm: &mut Pixmap, req: &Request, t: &TileLayer, view: &View, f if k.last.len() >= MAX_KEPT { k.last.clear(); } - k.last.insert(key, (url, at, r)); + // 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.clone(), (url, at, r)); + drop(k); + // 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; } 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. - State::Failed(_) if !small && pw.max(ph) > SMALL_SIDE => { - let mut k = kept(); - if k.small.len() >= MAX_KEPT { - k.small.clear(); + State::Failed(e) if url != address => { + without = true; + if refusal(&e) { + refused = Some(address.clone()); } - k.small.insert(key.clone(), web_time::Instant::now()); } - // A picture the service doesn't draw: its tiles may draw (and say what fails). + 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()); + } + // The smaller picture is asked with its resolution again: the size failed. + (without, refused) = (false, None); + } + State::Failed(e) if sharp => { + standard = true; + refused_side = refusal(&e).then_some((pw, ph)); + } State::Failed(_) => return None, } } None } +/// Keeps the layer `key` to a standard screen's pictures for a while, while its HiDPI ones are +/// 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(); + } + 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), 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 { + 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 /// it is off by at most the bend within a cell. const WARP_CELLS: u32 = 16; @@ -584,6 +776,57 @@ 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"; + // 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}"); + } + // 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!( + 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), + 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: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(), + "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 f9ea676bd..7ab591d0e 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(()); @@ -931,9 +933,47 @@ 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") { - done(Err("400 Bad Request".into())); + // 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/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("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/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/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())); + } 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())); } else { done(Ok(color(20, 200, 20))); } @@ -1002,12 +1042,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 +1058,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 +1084,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:?}"); + } } } } @@ -1072,6 +1122,17 @@ 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. + // 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()?; + ((d * 25.4 / 0.28 / 96.0 - ogc).abs() <= 1.0).then_some(d) + }; // In Web Mercator. let mut m = Map::new("Map"); m.graticule = false; @@ -1079,21 +1140,343 @@ 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:?}"); + } + // 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()); + // 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]).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(); + 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 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). + { + 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] == 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 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 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 + // 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(); + 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!(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:?}"); + } + // 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. + { + 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"); + 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. + { + 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"); + 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; 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 +1498,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 +1513,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/cache.rs b/crates/tiles/src/cache.rs index add37bd17..e67797234 100644 --- a/crates/tiles/src/cache.rs +++ b/crates/tiles/src/cache.rs @@ -1485,6 +1485,7 @@ fn decode_with( b, Box::new(move |r| { settle(match r { + // 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,6 +2364,9 @@ fn payload_bytes(p: &Payload) -> usize { } } +/// 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. pub fn decode(bytes: &[u8], want: Want) -> Result { if bytes.is_empty() { @@ -2372,7 +2376,11 @@ 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), + // 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}")), } @@ -4714,7 +4722,9 @@ 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))); + // 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/crates/tiles/src/draw.rs b/crates/tiles/src/draw.rs index 5744567ee..ca80ad0c3 100644 --- a/crates/tiles/src/draw.rs +++ b/crates/tiles/src/draw.rs @@ -608,6 +608,17 @@ 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, 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); + // 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 /// 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,8 +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, .. } => { - let z = template::zoom_for(frame.m_per_px * f64::from(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, *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)) @@ -1318,6 +1328,27 @@ 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, 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, 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, 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, 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] fn refusal_counts_are_not_substring_duplicates() { let mut error = Some("12 tiles left out: refused".to_string()); diff --git a/crates/tiles/src/template.rs b/crates/tiles/src/template.rs index 8e77330e7..7d3c4be62 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 f8cc29087..7273ec520 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 07a2b8dbb..81cafed03 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,22 @@ 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" }); + } + // 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), "ui.startup" => { // `project` (#377) or, as before, `blank`; `project` wins when both are given. @@ -9220,6 +9242,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 8d60c3131..ec6a42356 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 784adf89b..5bb42c27e 100644 --- a/docs/user-guide/data-formats.md +++ b/docs/user-guide/data-formats.md @@ -450,10 +450,22 @@ 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 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, 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, 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 @@ -472,6 +484,31 @@ 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 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 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. + +**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 about 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