diff --git a/crates/engine/src/cmd/mod.rs b/crates/engine/src/cmd/mod.rs index f4fb1a7500..fbfb3d2fcd 100644 --- a/crates/engine/src/cmd/mod.rs +++ b/crates/engine/src/cmd/mod.rs @@ -51,6 +51,7 @@ pub mod sharing; pub mod sources; pub mod sql; pub mod stac; +pub mod tile_export; pub mod tiles; pub mod time; pub mod view; diff --git a/crates/engine/src/cmd/raster.rs b/crates/engine/src/cmd/raster.rs index 0c670b2184..63177506e7 100644 --- a/crates/engine/src/cmd/raster.rs +++ b/crates/engine/src/cmd/raster.rs @@ -87,12 +87,29 @@ pub fn commands() -> Vec { r#"{"fromLayer"?: bool (the active or named layer's colour ramp stretch; the settings apply to every raster added later that would be a grey stretch, opened files and analysis results alike), "layer"?, "ramp"?, "invert"?: bool, "stretch"?: "none"|"minMax"|"percentClip"|"standardDeviation"|"histogramEqualize", "low"?, "high"?: %, "n"?: deviations, "resampling"?: "nearest"|"bilinear", "reset"?: bool (back to the black-to-white grey stretch)}"#, ), 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_raster).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)}"#, + 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"}"#, ), ] } +/// Export Raster writes the active (or named) raster layer, or a tile layer: one a script names +/// (`tiles`) while another layer is active, or the one picked in the Layers pane. Which, and +/// why none, is said by the export itself once tile layers are there. +fn need_exportable(s: &Session) -> Option<&'static str> { + need_raster(s).filter(|_| s.map().tiles.is_empty()) +} + +/// The export reads the tile layer it writes, with its route; else the raster layer. +fn export_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + match super::tile_export::target(input, p)? { + Some(i) if super::tile_export::named(p) => Ok(super::tiles::tile_inputs(input.map(), [i])), + // The tile layer picked in the Layers pane is a choice a plan can't see ahead of time. + Some(_) => Ok(super::Inputs::unresolved()), + None => super::Inputs::single_layer(input, p), + } +} + /// The raster layer named by `p` (default: active layer) and its data. fn get(s: &Session, p: &Value) -> Result<(u32, Arc), String> { let id = s.layer_id(p)?; @@ -947,7 +964,20 @@ pub fn geotiff_bytes(s: &Session, p: &Value) -> Result<(String, Vec), String /// `share.exportRaster`'s inputs, and the work that writes the raster as a GeoTIFF (a /// cloud-optimised one with `cog`). pub(crate) fn export_work(s: &Session, p: &Value) -> Result { + if let Some(i) = super::tile_export::target(s, p)? { + return super::tile_export::export_work(s, p, i); + } let path = ps(p, "path").ok_or("path is required")?.to_string(); + // A raster layer is written whole: a box or zoom asked for would be dropped. + if [p.get("bbox"), p.get("zoom")].into_iter().flatten().any(|v| !v.is_null()) { + return Err("bbox and zoom are for a tile layer: name it with \"tiles\", or pick it in the Layers pane".into()); + } + // No layer named or active, nor a tile layer picked: what to pick. + if matches!(p.get("layer"), None | Some(Value::Null)) + && let Some(why) = need_raster(s) + { + return Err(why.replace("a raster layer", "a raster or tile layer")); + } let (_, r) = get(s, p)?; let bi = band(p, "band", &r)?.unwrap_or(0); let cog = b(p, "cog") == Some(true); @@ -955,7 +985,7 @@ pub(crate) fn export_work(s: &Session, p: &Value) -> Result Result { +pub(super) fn write_geotiff(r: &Raster, bi: usize, cog: bool, path: &str) -> Result { let cog = if cog { // Tiles go to the file as they are made: the whole file is never held in memory. Some(raster::cogwrite::write_file(r, bi, Path::new(path)).map_err(|e| format!("{path}: {e}"))?) diff --git a/crates/engine/src/cmd/tile_export.rs b/crates/engine/src/cmd/tile_export.rs new file mode 100644 index 0000000000..0fee38a644 --- /dev/null +++ b/crates/engine/src/cmd/tile_export.rs @@ -0,0 +1,501 @@ +//! Export Raster of a tile layer (#1561): what a WMS, WMTS or XYZ layer (or a raster tile +//! archive) shows, as a GeoTIFF placed on the ground. The layer's own tiles at one zoom are put +//! together on their Web Mercator grid, a pixel of the file for a pixel of a tile, without +//! warping, alone (no other layer, no basemap) and with their alpha. + +use std::time::Duration; + +use rustgis_data::raster::{ColorHint, Crs, GeoTransform, Raster}; +use rustgis_geom::{Coord, Extent}; +use rustgis_map::tiles::{TileKind, TileLayer}; +use rustgis_render::rustgis_tiles::{self, Frame, cache, source, style, template::HALF_WORLD}; +use serde_json::{Value, json}; + +use super::exports::{Work, Wrote}; +use super::ps; +use crate::Session; + +/// The most tiles an export fetches. A box at a high zoom is thousands of requests, and many +/// tile services' terms forbid bulk downloads (OpenStreetMap's tile usage policy, for one). +pub(crate) const MOST_TILES: u64 = 1024; + +/// The most pixels a side the file holds: four bands of 32-bit samples are held while it is +/// written, 1 GiB at this size. +const MOST_SIDE: u64 = rustgis_render::MAX_SIDE as u64; + +/// The side of the blocks the picture is drawn in, each with at most a few dozen tiles of 256 +/// pixels ([`block_side`] makes it smaller for smaller tiles). +const BLOCK: u32 = 2048; + +/// The side of the blocks for `tile_size`-pixel tiles: a block touches at most 20 tiles a side, +/// 400 in all, the most a drawing places ([`rustgis_tiles::template::MAX_TILES`]); more would be +/// left out. +fn block_side(tile_size: u32) -> u32 { + BLOCK.min(tile_size.max(1).saturating_mul(19)) +} + +/// How long a block waits for its tiles, and the layer's description (a TileJSON, an archive's +/// header) for its tile grid. +const WAIT: Duration = Duration::from_secs(60); + +/// The most different drawing errors an export names in full. +const SHOWN_ERRORS: usize = 3; + +/// Waits for tiles in a row that bring nothing new before a block is written with what came. +const IDLE_PASSES: u32 = 3; + +/// The tile layer `share.exportRaster` writes, if it writes one: the one `tiles` names (by name, +/// or index from the top), else the one picked in the Layers pane while no map layer is +/// active and no `layer` is given. +pub(crate) fn target(input: &impl super::InputSource, p: &Value) -> Result, String> { + let context = input.input_context(); + let tiles = &context.map().tiles; + match p.get("tiles") { + None | Some(Value::Null) => {} + Some(Value::String(n)) => { + return tiles + .iter() + .position(|t| t.name.eq_ignore_ascii_case(n.trim())) + .map(Some) + .ok_or_else(|| format!("no tile layer '{n}'")); + } + Some(v) => { + let i = v.as_u64().and_then(|i| usize::try_from(i).ok()).filter(|i| *i < tiles.len()); + return i.map(Some).ok_or_else(|| "tiles is a tile layer's name or its index in the Layers pane (0 = top)".to_string()); + } + } + let unnamed = matches!(p.get("layer"), None | Some(Value::Null)); + Ok(if unnamed && context.active_layer.is_none() { tiles.iter().position(|t| t.selected) } else { None }) +} + +/// Whether `p` names its tile layer (`tiles`), rather than taking the one picked. +pub(crate) fn named(p: &Value) -> bool { + p.get("tiles").is_some_and(|t| !t.is_null()) +} + +/// `share.exportRaster` of tile layer `i`: reads what it needs of the session (the layer, the +/// box, the zoom the view shows) and returns the work that fetches the tiles and writes the +/// file. +pub(crate) fn export_work(s: &Session, p: &Value, i: usize) -> Result { + // The browser can't wait for tiles on a thread of its own. + if cfg!(target_arch = "wasm32") { + return Err("a tile layer is exported as a raster in the desktop app: here, Export Map writes the view".into()); + } + let path = ps(p, "path").ok_or("path is required")?.to_string(); + let t = s.map().tiles.get(i).ok_or("no tile layer at that index")?.clone(); + // One layer is written: a raster named beside it would be dropped. + if !matches!(p.get("layer"), None | Some(Value::Null)) { + return Err(format!("{}: name one layer: \"tiles\" for a tile layer, or \"layer\" for a raster", t.name)); + } + if p.get("band").is_some_and(|b| !b.is_null()) { + return Err(format!("{}: a tile layer is written in colour; band is for a raster layer", t.name)); + } + let cog = match p.get("cog") { + None | Some(Value::Null) => false, + Some(v) => v.as_bool().ok_or("cog must be true or false")?, + }; + if t.kind == TileKind::Cog { + return Err(format!("{}: a cloud-optimised GeoTIFF is already a raster: add it as a layer (Add Data) and export that", t.name)); + } + if let Some(e) = &t.error { + return Err(format!("{}: {e}", t.name)); + } + if t.time_hidden { + return Err(format!("{}: none of its dates is in the time slider's span, so it shows nothing to export", t.name)); + } + let zoom = match p.get("zoom") { + None | Some(Value::Null) => None, + Some(v) => Some(v.as_u64().filter(|z| *z <= 30).ok_or("zoom is a whole number from 0 to 30")? as u8), + }; + let area = match p.get("bbox") { + None | Some(Value::Null) => { + if s.map().projection.is_globe() { + return Err("give a bbox [west, south, east, north]: the globe shows no box to export".into()); + } + let e = super::workspace::view_extent(s); + if e.is_empty() { + return Err("the view shows no part of the Earth: give a bbox [west, south, east, north]".into()); + } + e + } + Some(v) => bbox(v)?, + }; + // The resolution the view shows, as Web Mercator metres a pixel at its centre: the zoom of + // tiles the map draws there, when none is asked. + let shown = super::navigate::view_of(s.map()).map(|(_, z)| 2.0 * HALF_WORLD / (512.0 * 2f64.powf(z))); + if zoom.is_none() && shown.is_none() { + return Err("the map's centre is no place on Earth: give a zoom".into()); + } + Ok(Box::new(move || write(&t, &path, area, zoom, shown, cog))) +} + +/// A `bbox` of `[west, south, east, north]` in degrees. +fn bbox(v: &Value) -> Result { + // Every element a number: one that isn't is refused, not skipped. + let n: Vec = v.as_array().and_then(|a| a.iter().map(Value::as_f64).collect()).unwrap_or_default(); + let [w, s, e, n] = n[..] else { return Err("bbox is [west, south, east, north] in degrees".into()) }; + if !(w < e && s < n && (-90.0..=90.0).contains(&s) && (-90.0..=90.0).contains(&n) && w >= -540.0 && e <= 540.0) { + return Err("bbox is [west, south, east, north] in degrees, west below east and south below north".into()); + } + // Its west side within −180..180°, the box moved by whole turns. + let turn = ((w + 180.0) / 360.0).floor() * 360.0; + Ok(Extent::new(w - turn, s, e - turn, n)) +} + +/// The tile grid a layer's tiles are on: their pixels a side and the zooms they come at, and +/// their addresses (templates the tiles' drawing fills in). +#[derive(Clone, Debug, PartialEq)] +struct Grid { + tile_size: u32, + min_zoom: u8, + max_zoom: u8, + tiles: Vec, +} + +/// The tile grid of a resolved layer that draws raster tiles only; why it can't be exported +/// otherwise. +fn grid_of(name: &str, r: &source::Resolved) -> Result { + let shown: Vec<&style::StyleLayer> = r.style.layers.iter().filter(|l| l.visible && l.kind != style::LayerKind::Other).collect(); + if shown.iter().any(|l| l.kind != style::LayerKind::Raster) { + return Err(format!( + "{name} draws vector tiles (shapes and labels in a style), not pictures: Export Map (share.exportMap) to a .tif writes what the map draws" + )); + } + // One source: the tiles of several, each on its own grid, aren't one grid of pixels. + let mut ids: Vec<&String> = shown.iter().filter_map(|l| l.source.as_ref()).collect(); + ids.sort(); + ids.dedup(); + if ids.len() > 1 { + return Err(format!( + "{name} draws tiles from {} sources: Export Raster writes one source's tiles; Export Map (share.exportMap) to a .tif writes what the map draws", + ids.len() + )); + } + ids.first() + .and_then(|id| r.style.sources.iter().find(|(s, _)| s == *id)) + .and_then(|(_, s)| match s { + style::Source::Raster { tiles, tile_size, min_zoom, max_zoom, .. } => { + Some(Grid { tile_size: (*tile_size).max(1), min_zoom: *min_zoom, max_zoom: *max_zoom, tiles: tiles.clone() }) + } + _ => None, + }) + .ok_or_else(|| format!("{name} has no raster tiles to export")) +} + +/// The box to write: `area` (degrees) on the pixel grid of `zoom`'s tiles, whole pixels out to +/// its edges and within the world's rows; its corner (Web Mercator) and size in pixels. +fn placed(area: &Extent, res: f64) -> (f64, f64, u64, u64) { + let lo = rustgis_geom::lonlat_to_merc(Coord::new(area.min_x, area.min_y)); + let hi = rustgis_geom::lonlat_to_merc(Coord::new(area.max_x, area.max_y)); + let col = |x: f64, up: bool| { + let c = (x + HALF_WORLD) / res; + if up { c.ceil() } else { c.floor() } + }; + let row = |y: f64, up: bool| { + let r = ((HALF_WORLD - y) / res).clamp(0.0, 2.0 * HALF_WORLD / res); + if up { r.ceil() } else { r.floor() } + }; + let (c0, c1) = (col(lo.x, false), col(hi.x, true)); + // At most one world wide. + let c1 = c1.min(c0 + (2.0 * HALF_WORLD / res).round()); + let (r0, r1) = (row(hi.y, false), row(lo.y, true)); + let left = c0 * res - HALF_WORLD; + let top = HALF_WORLD - r0 * res; + (left, top, (c1 - c0).max(0.0) as u64, (r1 - r0).max(0.0) as u64) +} + +/// The tiles at zoom `z` that a `w × h` box at (`left`, `top`) of `res` metres a pixel touches. +fn tiles_over(left: f64, top: f64, w: u64, h: u64, res: f64, z: u8) -> u64 { + let span = 2.0 * HALF_WORLD / 2f64.powi(i32::from(z)); + let eps = res * 1e-6; + let cols = ((left + w as f64 * res - eps + HALF_WORLD) / span).floor() - ((left + eps + HALF_WORLD) / span).floor() + 1.0; + let rows = ((HALF_WORLD - top + h as f64 * res - eps) / span).floor() - ((HALF_WORLD - top + eps) / span).floor() + 1.0; + (cols.max(0.0) * rows.max(0.0)) as u64 +} + +/// The first zoom whose tiles the layer draws: its first, unless its tiles are smaller than the +/// 512 pixels a map's zoom counts in, as the tiles' drawing checks that zoom against the first +/// (256-pixel tiles of zoom 10 are drawn at the map's zoom 9: from zoom 11). +fn first_zoom(g: &Grid) -> u8 { + (g.min_zoom..=g.max_zoom.max(g.min_zoom)) + .find(|z| f64::from(rustgis_tiles::template::style_zoom(res_at(*z, g.tile_size))) + 0.5 >= f64::from(g.min_zoom)) + .unwrap_or(g.min_zoom) +} + +/// The tiles at zoom `z` over a `w × h` block at `frame` that failed (fetched, or read, without +/// a picture), as the cache keeps them: each address with its error. A tile the server hasn't (a 404 past its +/// coverage) is one too: on the ground its gap passes for data. +fn failed_tiles(templates: &[String], frame: &Frame, w: u32, h: u32, z: u8) -> Vec<(String, String)> { + if templates.is_empty() { + return Vec::new(); + } + let (right, bottom) = (frame.left + f64::from(w) * frame.m_per_px, frame.top - f64::from(h) * frame.m_per_px); + let view = Extent::new(frame.left, bottom, right, frame.top); + // Each tile once: at the world's edge a tile is placed again a world over. + let mut urls: Vec = rustgis_tiles::template::covering(&view, z) + .into_iter() + .filter_map(|p| { + let i = (p.id.x as usize).wrapping_add(p.id.y as usize) % templates.len(); + Some(rustgis_tiles::template::expand(templates.get(i)?, p.id, 256, false, &[])) + }) + .collect(); + urls.sort(); + urls.dedup(); + urls.into_iter() + // Only what the drawing asked for: a tile it didn't (a style layer out of its zooms) isn't + // fetched now. + .filter_map(|url| cache::peek_failure(&url).map(|e| (url, e))) + .collect() +} + +/// Metres a pixel of zoom `z`'s `tile_size`-pixel tiles. +fn res_at(z: u8, tile_size: u32) -> f64 { + 2.0 * HALF_WORLD / (f64::from(tile_size) * 2f64.powi(i32::from(z))) +} + +/// The layer's description resolved: its tile grid, or why it has none. Waits while it is on +/// its way (an archive's header, a TileJSON document), as long as [`WAIT`]. +fn resolved(spec: &rustgis_tiles::Spec, name: &str) -> Result, String> { + let start = web_time::Instant::now(); + loop { + let since = cache::generation(); + match source::resolve(spec) { + source::Resolve::Ready(r) => return Ok(r), + source::Resolve::Failed(e) => return Err(format!("{name}: {e}")), + source::Resolve::Pending => {} + } + rustgis_data::progress::check().map_err(|e| e.to_string())?; + let left = WAIT.saturating_sub(start.elapsed()); + if left.is_zero() { + return Err(format!("{name}: its description didn't come within {} s: export again later", WAIT.as_secs())); + } + cache::wait_for_news_unless(since, left.min(Duration::from_secs(5)), || rustgis_data::progress::check().is_err()); + if cache::generation() == since && cache::in_flight() == 0 { + // Nothing on its way and nothing new: ask again after a moment. + std::thread::sleep(Duration::from_millis(50)); + } + } +} + +/// Draws block `frame` (`w × h` pixels) of the layer, waiting for its tiles; what it drew, its +/// credits, and why it may have gaps. +fn block(spec: &rustgis_tiles::Spec, frame: &Frame, w: u32, h: u32) -> Result<(tiny_skia::Pixmap, rustgis_tiles::Drawn), String> { + let mut pm = tiny_skia::Pixmap::new(w, h).ok_or("could not allocate the image")?; + let start = web_time::Instant::now(); + let cancelled = || rustgis_data::progress::check().is_err(); + let mut idle = 0; + loop { + let since = cache::generation(); + pm.fill(tiny_skia::Color::TRANSPARENT); + let d = rustgis_tiles::draw(&mut pm, spec, 1.0, frame, false); + let left = WAIT.saturating_sub(start.elapsed()); + // Waits that bring nothing new end it: a tile that failed is tried again later, not now. + if !d.pending || left.is_zero() || idle >= IDLE_PASSES || cancelled() { + rustgis_data::progress::check().map_err(|e| e.to_string())?; + return Ok((pm, d)); + } + cache::wait_for_news_unless(since, left.min(Duration::from_secs(30)), cancelled); + if cache::generation() == since { + idle += 1; + } else { + idle = 0; + } + // Nothing on its way: news then is other layers' (a retry's alarm), not this block's. + if cache::in_flight() == 0 { + std::thread::sleep(Duration::from_millis(50)); + } + } +} + +/// Fetches tile layer `t`'s tiles over `area` (degrees) at `zoom` (else the zoom of its tiles +/// the view shows, at `shown` metres a pixel), and writes them to `path` as an RGBA GeoTIFF in +/// Web Mercator (a cloud-optimised one with `cog`). +fn write(t: &TileLayer, path: &str, area: Extent, zoom: Option, shown: Option, cog: bool) -> Result { + let name = t.name.as_str(); + let spec = rustgis_render::tile_spec(t); + let r = resolved(&spec, name)?; + let g = grid_of(name, &r)?; + let z = match zoom { + Some(z) => z, + None => rustgis_tiles::template::zoom_for(shown.unwrap_or(f64::NAN), g.tile_size, g.max_zoom), + }; + let first = first_zoom(&g); + if z < first { + return Err(format!("{name} is drawn from zoom {first}: give a zoom of at least {first}")); + } + let res = res_at(z, g.tile_size); + let (left, top, w, h) = placed(&area, res); + if w == 0 || h == 0 { + return Err(format!("{name}: the box holds no pixel at zoom {z}")); + } + // A zoom past the service's takes its deepest tiles, each drawn larger. + let fetched = z.min(g.max_zoom); + let n = tiles_over(left, top, w, h, res, fetched); + let fits = |z: u8| { + let res = res_at(z, g.tile_size); + let (l, t, w, h) = placed(&area, res); + w <= MOST_SIDE && h <= MOST_SIDE && tiles_over(l, t, w, h, res, z.min(g.max_zoom)) <= MOST_TILES + }; + let lower = || (first..z).rev().find(|z| fits(*z)).map_or_else(String::new, |z| format!(" (zoom {z} fits)")); + if n > MOST_TILES { + return Err(format!( + "{name}: the box at zoom {z} takes {n} tiles, more than the {MOST_TILES} an export fetches, as tile services' terms forbid bulk downloads (OpenStreetMap's tile usage policy, for one): give a smaller bbox or a lower zoom{}", + lower() + )); + } + if w > MOST_SIDE || h > MOST_SIDE { + return Err(format!( + "{name}: the box at zoom {z} is {w} × {h} pixels, more than the {MOST_SIDE} a side an export holds: give a smaller bbox or a lower zoom{}", + lower() + )); + } + let (w, h) = (w as u32, h as u32); + let mut bands: Vec> = (0..4) + .map(|_| rustgis_data::raster::filled::(w as usize, h as usize, 0.0)) + .collect::>() + .map_err(|e| format!("{name}: {e}"))?; + let side = block_side(g.tile_size); + let (bx, by) = (w.div_ceil(side), h.div_ceil(side)); + rustgis_data::progress::begin((bx * by) as usize); + let (mut credits, mut errors, mut drew) = (Vec::::new(), Vec::::new(), false); + // The blocks with tiles still on their way when the wait ended, and those with tiles that + // couldn't be drawn. + let (mut pending, mut failed, mut lost) = (0u32, 0u32, Vec::::new()); + for j in 0..by { + for i in 0..bx { + let (x0, y0) = (i * side, j * side); + let (bw, bh) = (side.min(w - x0), side.min(h - y0)); + let frame = Frame { left: left + f64::from(x0) * res, top: top - f64::from(y0) * res, m_per_px: res, ppp: 1.0 }; + let (pm, d) = block(&spec, &frame, bw, bh)?; + for c in d.attribution { + if !credits.contains(&c) { + credits.push(c); + } + } + pending += u32::from(d.pending); + // The drawing leaves out a tile that failed without saying so: the cache does. + let missing = failed_tiles(&g.tiles, &frame, bw, bh, fetched); + if d.error.is_some() || !missing.is_empty() { + failed += 1; + } + // A tile on the edge of two blocks is in both: counted once. + let mut missing_errors = Vec::new(); + for (url, e) in missing { + if !lost.contains(&url) { + lost.push(url); + } + missing_errors.push(e); + } + for e in d.error.into_iter().chain(missing_errors) { + if !errors.contains(&e) { + errors.push(e); + } + } + for (k, px) in pm.pixels().iter().enumerate() { + let (col, row) = (x0 as usize + k % bw as usize, y0 as usize + k / bw as usize); + let at = row * w as usize + col; + // tiny-skia keeps colours premultiplied; a file keeps them straight. + let c = px.demultiply(); + drew |= c.alpha() > 0; + for (band, v) in bands.iter_mut().zip([c.red(), c.green(), c.blue(), c.alpha()]) { + band[at] = f32::from(v); + } + } + rustgis_data::progress::step(1).map_err(|e| e.to_string())?; + } + } + let transform = GeoTransform { origin_x: left, origin_y: top, pixel_width: res, pixel_height: -res }; + let color = ColorHint::Rgb { alpha: true }; + let mut raster = + Raster::new(name, w, h, bands, transform, Crs::WebMercator, None, color, "UInt8").map_err(|e| format!("{name}: {e}"))?; + raster.copyright = (!credits.is_empty()).then(|| credits.join(" | ")); + let mut wrote = super::raster::write_geotiff(&raster, 0, cog, path)?; + // What it holds, after what Export Raster says of every raster. + let mut said = format!("{path}: {name} at zoom {z}, {w} × {h} pixels in Web Mercator (EPSG:3857), from {n} tile(s)"); + if z > g.max_zoom { + said.push_str(&format!("; the service has tiles to zoom {} only, so they are enlarged", g.max_zoom)); + } + wrote.notes.insert(0, said); + if let Some(o) = wrote.answer.as_object_mut() { + o.insert("width".into(), json!(w)); + o.insert("height".into(), json!(h)); + o.insert("zoom".into(), json!(z)); + o.insert("tiles".into(), json!(n)); + o.insert("crs".into(), json!(Crs::WebMercator.label())); + if let Some(c) = &raster.copyright { + o.insert("copyright".into(), json!(c)); + } + } + // On the ground a gap passes for data: said, as a warning. + let mut gaps = Vec::new(); + let blocks = bx * by; + if pending > 0 { + gaps.push(format!( + "{path}: in {pending} of its {blocks} block(s) of up to {side} × {side} pixels, some of {name}'s tiles hadn't come after the wait, so parts of it are empty: export again later" + )); + } + if !drew && pending == 0 && errors.is_empty() { + gaps.push(format!("{path}: {name} drew nothing in the box at zoom {z}: its service may have no tiles there")); + } + if !errors.is_empty() { + // Each different error, a few in full. + let shown: Vec<&str> = errors.iter().take(SHOWN_ERRORS).map(String::as_str).collect(); + let more = errors.len().saturating_sub(SHOWN_ERRORS); + let more = if more > 0 { format!(" (and {more} more)") } else { String::new() }; + let tiles = if lost.is_empty() { String::new() } else { format!(" ({} tile(s) couldn't be fetched or read)", lost.len()) }; + gaps.push(format!( + "{path}: in {failed} of its {blocks} block(s), {name} couldn't be drawn whole{tiles}, so parts of it are empty; {} different error(s): {}{more}", + errors.len(), + shown.join(" | ") + )); + } + if !gaps.is_empty() { + wrote.then = Some(Box::new(move |s: &mut Session, _: &mut Value| { + for g in gaps { + s.warn(g); + } + Ok(()) + })); + } + Ok(wrote) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A box is put on the zoom's pixel grid, whole pixels out to its edges, within the world's + /// rows and at most one world wide. + #[test] + fn a_box_is_placed_on_the_tiles_pixel_grid() { + let res = res_at(1, 256); + // The world at zoom 1: 512 pixels a side. + let (l, t, w, h) = placed(&Extent::new(-180.0, -85.06, 180.0, 85.06), res); + assert_eq!((w, h), (512, 512)); + assert!((l + HALF_WORLD).abs() < 1e-6 && (t - HALF_WORLD).abs() < 1e-6); + assert_eq!(tiles_over(l, t, w, h, res, 1), 4); + // Wider than the world: one world. + assert_eq!(placed(&Extent::new(-300.0, -10.0, 300.0, 10.0), res).2, 512); + // A small box takes the pixels it touches. + let (l, _, w, _) = placed(&Extent::new(0.1, 0.1, 0.2, 0.2), res_at(2, 256)); + assert_eq!(w, 1); + assert!(l >= 0.0); + assert_eq!(bbox(&json!([1, 2, 3])).unwrap_err(), "bbox is [west, south, east, north] in degrees"); + assert!(bbox(&json!([10, 0, 5, 1])).is_err()); + // A box past the antimeridian is moved into the world by whole turns. + let e = bbox(&json!([190, 0, 200, 1])).unwrap(); + assert_eq!((e.min_x, e.max_x), (-170.0, -160.0)); + } + + /// 256-pixel tiles are drawn from the zoom after their first (the drawing counts the map's + /// 512-pixel zoom against it); 512-pixel ones from their first. + #[test] + fn the_first_zoom_is_the_first_drawn() { + let g = |tile_size, min_zoom| Grid { tile_size, min_zoom, max_zoom: 18, tiles: Vec::new() }; + assert_eq!(first_zoom(&g(256, 10)), 11); + assert_eq!(first_zoom(&g(512, 10)), 10); + assert_eq!(first_zoom(&g(256, 0)), 0); + // Blocks touch at most 400 tiles, however small the tiles. + assert_eq!((block_side(256), block_side(64), block_side(0)), (2048, 1216, 19)); + } +} diff --git a/crates/engine/src/cmd/tiles.rs b/crates/engine/src/cmd/tiles.rs index 10b0dc5899..cdd44e412f 100644 --- a/crates/engine/src/cmd/tiles.rs +++ b/crates/engine/src/cmd/tiles.rs @@ -606,7 +606,7 @@ fn set_inputs(input: &super::InputContext<'_>, p: &Value) -> Result crate::policy::Effects { +pub(crate) fn tile_source_reach(t: &TileLayer) -> crate::policy::Effects { use crate::policy::{Effect, Effects}; use rustgis_render::rustgis_tiles::{mbtiles, s3}; if t.kind == TileKind::MbTiles && mbtiles::is_upload(&t.url) { diff --git a/crates/engine/src/policy/effects.rs b/crates/engine/src/policy/effects.rs index 71592896a3..c4b5a17be4 100644 --- a/crates/engine/src/policy/effects.rs +++ b/crates/engine/src/policy/effects.rs @@ -90,6 +90,12 @@ pub(super) fn command(s: &crate::cmd::InputContext<'_>, spec: &CommandSpec, para "map.tiles" | "info.project" => crate::cmd::tiles::listing_reach(s.map()), "map.setTiles" => crate::cmd::tiles::set_reach(s, params), "time.export" => crate::cmd::time::export_reach(s, params), + // A tile layer's export fetches its tiles (an archive, from its file). + "share.exportRaster" => crate::cmd::tile_export::target(s, params) + .ok() + .flatten() + .and_then(|i| s.map().tiles.get(i)) + .map_or(Effects::NONE, crate::cmd::tiles::tile_source_reach), _ => Effects::NONE, }); if matches!( diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index cd31710330..8d4b7ea3b0 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -33403,3 +33403,178 @@ fn export_map_to_geotiff_draws_tile_layers_and_keeps_their_credits() { ); std::fs::remove_dir_all(&dir).ok(); } + +/// Export Raster writes a tile layer alone: its tiles at the zoom asked, a pixel of the file for a +/// pixel of a tile, on their Web Mercator grid, with their alpha and credits; a zoom past the +/// service's enlarges its deepest tiles; too many tiles are refused, saying why (#1561). +#[test] +fn export_raster_puts_a_tile_layer_s_tiles_together() { + use rustgis_data::raster::{ColorHint, Crs, geotiff}; + let dir = std::env::temp_dir().join(format!("rustgis-tile-raster-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let archive = dir.join("quarters.mbtiles"); + let tile = |c: Option<(u8, u8, u8)>| { + let mut pm = tiny_skia::Pixmap::new(256, 256).unwrap(); + if let Some((r, g, b)) = c { + pm.fill(tiny_skia::Color::from_rgba8(r, g, b, 255)); + } + pm.encode_png().unwrap() + }; + let c = rusqlite::Connection::open(&archive).unwrap(); + c.execute_batch( + "CREATE TABLE metadata (name text, value text); CREATE TABLE tiles (zoom_level integer, tile_column integer, tile_row integer, tile_data blob); + INSERT INTO metadata VALUES ('name', 'Quarters'), ('format', 'png'), ('minzoom', '0'), ('maxzoom', '1'), ('attribution', 'Quarter Co');", + ) + .unwrap(); + // Zoom 1, rows counted from the south: red north-west, green north-east, blue south-west and + // nothing (see-through) south-east. + for (x, y, colour) in [(0, 1, Some((255, 0, 0))), (1, 1, Some((0, 255, 0))), (0, 0, Some((0, 0, 255))), (1, 0, None)] { + c.execute("INSERT INTO tiles VALUES (1, ?1, ?2, ?3)", rusqlite::params![x, y, tile(colour)]).unwrap(); + } + c.execute("INSERT INTO tiles VALUES (0, 0, 0, ?1)", [tile(Some((255, 255, 0)))]).unwrap(); + drop(c); + let mut s = Session::new(Project::default()); + s.view_size = (300.0, 200.0); + s.run("layer.addData", &json!({"path": archive.display().to_string()})).unwrap(); + s.run("map.goToXY", &json!({"lon": 10.0, "lat": 20.0, "scale": 50_000_000})).unwrap(); + let path = dir.join("world.tif"); + let at = |p: &std::path::Path| p.display().to_string(); + let world = json!([-180, -85.06, 180, 85.06]); + let out = s.run("share.exportRaster", &json!({"tiles": 0, "path": at(&path), "bbox": world, "zoom": 1})).unwrap(); + assert_eq!( + (out["width"].as_u64(), out["height"].as_u64(), out["zoom"].as_u64(), out["tiles"].as_u64()), + (Some(512), Some(512), Some(1), Some(4)) + ); + assert_eq!(out["copyright"], "Quarter Co"); + let r = geotiff::read(std::io::Cursor::new(std::fs::read(&path).unwrap()), "world").unwrap(); + assert_eq!((r.width, r.height, r.crs), (512, 512, Crs::WebMercator)); + assert_eq!(r.color, ColorHint::Rgb { alpha: true }); + assert_eq!(r.copyright.as_deref(), Some("Quarter Co")); + let half = rustgis_geom::MERCATOR_R * std::f64::consts::PI; + let t = r.transform; + assert!((t.origin_x + half).abs() < 1e-6 && (t.origin_y - half).abs() < 1e-6, "{t:?}"); + assert!((t.pixel_width - half / 256.0).abs() < 1e-6 && (t.pixel_height + half / 256.0).abs() < 1e-6, "{t:?}"); + let px = |col: usize, row: usize| -> Vec { r.bands.iter().map(|b| b.data[row * 512 + col]).collect() }; + assert_eq!(px(100, 100), [255.0, 0.0, 0.0, 255.0]); + assert_eq!(px(400, 100), [0.0, 255.0, 0.0, 255.0]); + assert_eq!(px(100, 400), [0.0, 0.0, 255.0, 255.0]); + assert_eq!(px(400, 400)[3], 0.0); + // The tiles had all come: nothing says it has gaps. + assert!(!s.notices.iter().any(|n| n.text.contains("parts of it")), "{:?}", s.notices); + // A box takes the pixels it touches, of the tile it is in. + let out = s.run("share.exportRaster", &json!({"tiles": "Quarters", "path": at(&path), "bbox": [10, 10, 20, 20], "zoom": 1})).unwrap(); + assert_eq!(out["tiles"], 1); + let r = geotiff::read(std::io::Cursor::new(std::fs::read(&path).unwrap()), "box").unwrap(); + assert!(r.bands[1].data.iter().all(|v| *v == 255.0) && r.bands[0].data.iter().all(|v| *v == 0.0)); + // Past the service's deepest zoom its tiles are enlarged, and it says so. + let n = s.notices.len(); + let out = s.run("share.exportRaster", &json!({"tiles": 0, "path": at(&path), "bbox": world, "zoom": 2})).unwrap(); + assert_eq!(out["width"], 1024); + assert!(s.notices[n..].iter().any(|n| n.text.contains("tiles to zoom 1 only")), "{:?}", &s.notices[n..]); + // Its zoom 0 is its own tile, 256 pixels for the world. + let out = s.run("share.exportRaster", &json!({"tiles": 0, "path": at(&path), "bbox": world, "zoom": 0})).unwrap(); + assert_eq!(out["width"], 256); + // Too many pixels or tiles are refused, saying which zoom fits. + let e = s.run("share.exportRaster", &json!({"tiles": 0, "path": at(&path), "bbox": [0, 0, 10, 10], "zoom": 12})).unwrap_err(); + assert!(e.contains("more than the 1024 an export fetches") || e.contains("8192"), "{e}"); + assert!(e.contains("fits)"), "{e}"); + // The tile layer picked in the Layers pane is the one written, at the zoom the view shows. + s.run("map.selectTiles", &json!({"index": 0})).unwrap(); + let out = s.run("share.exportRaster", &json!({"path": at(&path)})).unwrap(); + assert!(out["zoom"].as_u64().is_some_and(|z| z <= 1), "{out}"); + // A raster layer named beside the tile layer is refused, not dropped. + let e = s.run("share.exportRaster", &json!({"tiles": 0, "layer": "Cities", "path": at(&path)})).unwrap_err(); + assert!(e.contains("name one layer"), "{e}"); + // Named wrongly, or with a band, it is refused. + assert!(s.run("share.exportRaster", &json!({"tiles": "Nowhere", "path": at(&path)})).unwrap_err().contains("no tile layer 'Nowhere'")); + assert!(s.run("share.exportRaster", &json!({"tiles": 0, "path": at(&path), "band": 1})).is_err()); + assert!(s.run("share.exportRaster", &json!({"tiles": 0, "path": at(&path), "bbox": [5, 0, 1, 1]})).is_err()); + // Past 1024 tiles within 8192 pixels a side: refused for the tiles, with a zoom that fits. + let deep = dir.join("deep.mbtiles"); + let c = rusqlite::Connection::open(&deep).unwrap(); + c.execute_batch( + "CREATE TABLE metadata (name text, value text); CREATE TABLE tiles (zoom_level integer, tile_column integer, tile_row integer, tile_data blob); + INSERT INTO metadata VALUES ('name', 'Deep'), ('format', 'png'), ('minzoom', '0'), ('maxzoom', '20');", + ) + .unwrap(); + c.execute("INSERT INTO tiles VALUES (0, 0, 0, ?1)", [tile(Some((9, 9, 9)))]).unwrap(); + drop(c); + s.run("layer.addData", &json!({"path": at(&deep)})).unwrap(); + let d = s.map().tiles.iter().position(|t| t.name.eq_ignore_ascii_case("deep")).unwrap(); + let e = + s.run("share.exportRaster", &json!({"tiles": d, "path": at(&path), "bbox": [0.01, 0.01, 1.409, 1.409], "zoom": 13})).unwrap_err(); + assert!(e.contains("1089 tiles, more than the 1024"), "{e}"); + assert!(e.contains("(zoom 12 fits)"), "{e}"); + // A bbox with an element that isn't a number is refused, not read without it. + assert!(s.run("share.exportRaster", &json!({"tiles": d, "path": at(&path), "bbox": [10, null, 20, 30, 40]})).is_err()); + // A raster layer is written whole: a box or zoom for it is refused, not dropped. + s.run("layer.addData", &json!({"path": at(&path)})).unwrap(); + let raster = s.map().layers.iter().find(|l| l.is_raster()).map(|l| l.name.clone()).unwrap(); + let e = s.run("share.exportRaster", &json!({"layer": raster, "path": at(&dir.join("r.tif")), "bbox": [0, 0, 1, 1]})).unwrap_err(); + assert!(e.contains("for a tile layer"), "{e}"); + // Lossy both ways: a box where nothing is drawn (the see-through quarter) says so… + let warned = |s: &Session, n: usize, what: &str| { + s.notices[n..].iter().any(|x| x.severity == rustgis_data::diagnostics::Severity::Warning && x.text.contains(what)) + }; + let n = s.notices.len(); + s.run("share.exportRaster", &json!({"tiles": "Quarters", "path": at(&path), "bbox": [10, -20, 20, -10], "zoom": 1})).unwrap(); + assert!(warned(&s, n, "drew nothing in the box at zoom 1"), "{:?}", &s.notices[n..]); + // …and so does a tile that can't be drawn, with how many blocks and errors. + let broken = dir.join("broken.mbtiles"); + let c = rusqlite::Connection::open(&broken).unwrap(); + c.execute_batch( + "CREATE TABLE metadata (name text, value text); CREATE TABLE tiles (zoom_level integer, tile_column integer, tile_row integer, tile_data blob); + INSERT INTO metadata VALUES ('name', 'Broken'), ('format', 'png'), ('minzoom', '0'), ('maxzoom', '0'); + INSERT INTO tiles VALUES (0, 0, 0, x'00010203');", + ) + .unwrap(); + drop(c); + s.run("layer.addData", &json!({"path": at(&broken)})).unwrap(); + let i = s.map().tiles.iter().position(|t| t.name.eq_ignore_ascii_case("broken")).unwrap(); + // A tile it can't read is a gap, said with how many blocks and tiles, and why. + let n = s.notices.len(); + s.run("share.exportRaster", &json!({"tiles": i, "path": at(&path), "bbox": world, "zoom": 0})).unwrap(); + assert!( + warned(&s, n, "in 1 of its 1 block(s), broken couldn't be drawn whole (1 tile(s) couldn't be fetched or read)"), + "{:?}", + &s.notices[n..] + ); + // Over two blocks, each tile that fails is counted once, the one on their edge too: every + // tile of this archive fails, so as many fail as the export fetches. + let bad = dir.join("bad.mbtiles"); + let c = rusqlite::Connection::open(&bad).unwrap(); + c.execute_batch( + "CREATE TABLE metadata (name text, value text); CREATE TABLE tiles (zoom_level integer, tile_column integer, tile_row integer, tile_data blob); + INSERT INTO metadata VALUES ('name', 'Bad'), ('format', 'png'), ('minzoom', '0'), ('maxzoom', '4');", + ) + .unwrap(); + for x in 0..16 { + for y in 0..16 { + c.execute("INSERT INTO tiles VALUES (4, ?1, ?2, x'00010203')", [x, y]).unwrap(); + } + } + drop(c); + s.run("layer.addData", &json!({"path": at(&bad)})).unwrap(); + let b = s.map().tiles.iter().position(|t| t.name.eq_ignore_ascii_case("bad")).unwrap(); + let n = s.notices.len(); + let out = s.run("share.exportRaster", &json!({"tiles": b, "path": at(&path), "bbox": [-170, -60, 50, 60], "zoom": 4})).unwrap(); + assert!(out["width"].as_u64().is_some_and(|w| w > 2048), "{out}"); + // 11 columns (pixels 113 to 2617 of 4096) by 8 rows (1189 to 2907) of 256-pixel tiles. + let fetched = out["tiles"].as_u64().unwrap(); + assert_eq!(fetched, 88); + assert!( + warned(&s, n, &format!("in 2 of its 2 block(s), Bad couldn't be drawn whole ({fetched} tile(s)")) + || warned(&s, n, &format!("in 2 of its 2 block(s), bad couldn't be drawn whole ({fetched} tile(s)")), + "{:?}", + &s.notices[n..] + ); + // Its archive gone: refused, or written with the gap said, never quietly. + let i = s.map().tiles.iter().position(|t| t.name.eq_ignore_ascii_case("broken")).unwrap(); + let gone = s.map().tiles[i].url.replace("broken", "gone"); + s.map_mut().tiles[i].url = gone; + let n = s.notices.len(); + if s.run("share.exportRaster", &json!({"tiles": i, "path": at(&path), "bbox": world, "zoom": 0})).is_ok() { + assert!(warned(&s, n, "couldn't be drawn whole") || warned(&s, n, "hadn't come after the wait"), "{:?}", &s.notices[n..]); + } + std::fs::remove_dir_all(&dir).ok(); +} diff --git a/crates/tiles/src/cache.rs b/crates/tiles/src/cache.rs index 4397992b45..4560fca965 100644 --- a/crates/tiles/src/cache.rs +++ b/crates/tiles/src/cache.rs @@ -477,6 +477,23 @@ pub fn peek(url: &str) -> Option> { } } +/// Why the fetch of `url` failed, when the cache holds a failure for it, without asking for it +/// again: what a drawing asked for and couldn't have, for a caller that must say so (an export +/// of a tile layer). +pub fn peek_failure(url: &str) -> Option { + if let Err(e) = crate::logins::check(resource_of(url)) { + return Some(e.to_string()); + } + if let Some(base) = crate::warp::direct(url) { + return peek_failure(base); + } + let c = lock(); + match &c.slots.get(url)?.entry { + Entry::Failed(e, _) => Some(e.clone()), + _ => None, + } +} + /// Forgets everything (and failures, so they are tried again). pub fn clear() { let mut c = lock(); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index aba70cf030..85201cd3d0 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -3929,7 +3929,12 @@ impl RustGisApp { } } "ui.exportRaster" if self.services.download.is_some() => { - // The web app: encode once and download. + // The web app: encode once and download. A tile layer is fetched on a thread of + // its own, which the browser hasn't: the export says so. + if p.get("tiles").is_some_and(|t| !t.is_null()) || self.session.selected_tile().is_some() { + let tiles = p.get("tiles").cloned().unwrap_or(Value::Null); + return self.run("share.exportRaster", json!({"path": "tiles.tif", "tiles": tiles})); + } match rustgis_engine::cmd::raster::geotiff_bytes(&self.session, &Value::Null) { Ok((name, bytes)) => { self.download(&format!("{name}.tif"), bytes); @@ -3941,18 +3946,51 @@ impl RustGisApp { } } "ui.exportRaster" => { - let name = self - .session - .active_layer - .and_then(|id| self.session.map().layer(id)) - .map(|l| format!("{}.tif", l.name)) - .unwrap_or_default(); + // A tile layer (the one its menu names, else the one picked) is written over the + // view at the zoom it shows. + // A tile layer asked for by name or index is that one or none, never another. + let tile = match p.get("tiles").filter(|t| !t.is_null()) { + Some(v) => { + let all = &self.session.map().tiles; + let named = v.as_str().and_then(|n| all.iter().position(|t| t.name.eq_ignore_ascii_case(n.trim()))); + match v.as_u64().and_then(|i| usize::try_from(i).ok()).or(named).filter(|i| *i < all.len()) { + Some(i) => Some(i), + None => { + let why = tl!("No such tile layer").to_string(); + self.session.notify(why.clone(), true); + return Err(why); + } + } + } + None => self.session.selected_tile(), + }; + let tiles = self.session.map().tiles.get(tile.unwrap_or(usize::MAX)).map(|t| t.name.clone()); + let name = match &tiles { + Some(t) => format!("{t}.tif"), + None => self + .session + .active_layer + .and_then(|id| self.session.map().layer(id)) + .map(|l| format!("{}.tif", l.name)) + .unwrap_or_default(), + }; + // Neither a tile layer nor a raster: said before the save dialog, not after it. + let raster = self.session.active_layer.and_then(|id| self.session.map().layer(id)).is_some_and(|l| l.is_raster()); + if tiles.is_none() && !raster { + let why = tl!("Select a raster or tile layer in the Layers pane first").to_string(); + self.session.notify(why.clone(), true); + return Err(why); + } if let Some(path) = dialogs::pick_save(&name, &["tif", "tiff"], tl!("GeoTIFF")) { + let params = match tile.filter(|_| tiles.is_some()) { + Some(i) => json!({"path": path, "tiles": i}), + None => json!({"path": path}), + }; // Written on a thread of its own, with progress and Cancel in the status bar. #[cfg(not(target_arch = "wasm32"))] - return exportjob::start(self, "share.exportRaster", json!({"path": path})); + return exportjob::start(self, "share.exportRaster", params); #[cfg(target_arch = "wasm32")] - return self.run("share.exportRaster", json!({"path": path})); + return self.run("share.exportRaster", params); } } "ui.exportMap" => { diff --git a/crates/ui-egui/src/panes.rs b/crates/ui-egui/src/panes.rs index 6f30d91a25..e4dba4cfa8 100644 --- a/crates/ui-egui/src/panes.rs +++ b/crates/ui-egui/src/panes.rs @@ -1747,6 +1747,15 @@ fn tile_row(app: &mut RustGisApp, ui: &mut Ui, i: usize, moves: [bool; 2], reord action = Some(("map.setTiles", json!({"index": i, "move": "down"}))); ui.close(); } + // Its raster tiles over the view, as a GeoTIFF placed on the ground. + // Not a vector archive (one whose source layers its header names). + if matches!(tile.kind, TileKind::Raster | TileKind::PmTiles | TileKind::MbTiles) + && sources.is_none() + && ui.button(tl!("Export Raster…")).clicked() + { + action = Some(("ui.exportRaster", json!({"tiles": i}))); + ui.close(); + } if tile.archive_file().is_some() && ui.button(tl!("Repair Data Source…")).clicked() { action = Some(("ui.dialog", json!({"name": "repairTiles", "index": i}))); ui.close(); diff --git a/docs/user-guide/data-formats.md b/docs/user-guide/data-formats.md index cf41c86c1f..6aee9fc433 100644 --- a/docs/user-guide/data-formats.md +++ b/docs/user-guide/data-formats.md @@ -579,7 +579,7 @@ were rounded, and Export Raster, which writes the rounded values, says so again. | FlatGeobuf | Data › Export to `.fgb` | In the layer's coordinate system (see below), with a spatial index; features without geometry are left out (and reported). Desktop only: the writer needs a temporary file, which the browser doesn't have. | | KML, KMZ, GPX | Data › Export to `.kml`, `.kmz` or `.gpx` | Placemarks named by a `name` field, if there is one; features without geometry are left out. | | GeoJSON, KML, GPX, CSV, CSV with WKT | Analyze › Workflows › Data Interop (`data.convert`; `"format": "geojson"`, `"kml"`, `"gpx"`, `"csv"` or `"wkt"`) | Returns text when `"path"` is omitted; writes a file on desktop when it is given. | -| GeoTIFF / Cloud-optimised GeoTIFF | Data › Export Raster (`share.exportRaster`) | Untiled GeoTIFF by default; `"cog": true` writes 512-pixel tiles, Deflate compression and overviews. Writes the loaded raster's resolution (an overview when that is what was opened), the selected band or its colour bands. The web app's Export Raster downloads an ordinary GeoTIFF. | +| GeoTIFF / Cloud-optimised GeoTIFF | Data › Export Raster (`share.exportRaster`) | Untiled GeoTIFF by default; `"cog": true` writes 512-pixel tiles, Deflate compression and overviews. Writes the loaded raster's resolution (an overview when that is what was opened), the selected band or its colour bands. A WMS, WMTS or XYZ tile layer (`"tiles"`) is written as its tiles at one zoom, RGBA in Web Mercator (see [Sharing](sharing.md#exporting)). The web app's Export Raster downloads an ordinary GeoTIFF. | | PNG | Publish › Export Map (`share.exportMap`) | Configurable DPI, with labels, scale bar and tile credits. | | GeoTIFF of the map view | Publish › Export Map to `.tif` (`share.exportMap`) | The view as an RGB GeoTIFF in the map's projection, with labels but no scale bar; tile credits in the Copyright tag; `"cog": true` for a cloud-optimised one. Flat, untilted maps of the Earth only (see [Exporting](sharing.md#exporting)). | | PDF | Layouts, reports, presentations and the map view (Print Map), as vectors | | diff --git a/docs/user-guide/sharing.md b/docs/user-guide/sharing.md index 1d4b3c3e5b..be4d03c59e 100644 --- a/docs/user-guide/sharing.md +++ b/docs/user-guide/sharing.md @@ -30,6 +30,23 @@ about them. The web app's Export Map downloads a PNG. A projection whose parameters GeoTIFF's keys can't hold (Mollweide, for example) is refused too: switch the map to an EPSG projection first. +**Export Raster** writes a tile layer alone too: right-click a WMS, WMTS or XYZ layer (or a +raster tile archive) in the Layers pane and choose **Export Raster…**, or pick it and use +**Data › Export Raster**. Its own tiles over the view, at the zoom of the view's scale, are put together +as an RGBA GeoTIFF in Web Mercator (EPSG:3857): a pixel of the file for a pixel of a tile, not +resampled, with the layer's transparency and its credits in the Copyright tag. In a script, +`share.exportRaster` takes `"tiles"` (the tile layer's name, or its index in the Layers pane, 0 +at the top), `"bbox"` (`[west, south, east, north]` in degrees, the view's by default), `"zoom"` +(the tiles' zoom, 0 to 30) and `"cog"`. A zoom past the service's deepest enlarges its tiles, and +says so. An export fetches at most 1024 tiles and holds at most 8192 pixels a side: past either it +is refused, naming a zoom that fits, as many tile services' terms forbid bulk downloads +(OpenStreetMap's tile usage policy, for one). Tiles that haven't come after a wait (up to a minute for +each block of up to 2048 pixels), or can't be fetched or drawn (a server's "no tile here" +included), leave gaps, and the export warns with how many; so does a box where it drew nothing. +The layer's opacity is a display setting and isn't applied: the file keeps the tiles' own alpha. Vector tiles, and styles that draw them or draw tiles from more +than one source, are refused: Export Map to a `.tif` writes what they draw. A style drawing only +one source's raster tiles is written as those tiles. The web app exports the view with Export Map instead. + **Print Map** opens a PDF of the map view, kept as vectors as a layout's PDF is (see Layouts), in the system viewer on desktop; **Capture To Clipboard** copies a map image. **Export To 3D** writes a layer as a Wavefront OBJ or binary STL model