diff --git a/crates/data/src/progress.rs b/crates/data/src/progress.rs index 8602a7483a..c10b3a3a08 100644 --- a/crates/data/src/progress.rs +++ b/crates/data/src/progress.rs @@ -1,7 +1,8 @@ //! Cancelling a long read or write and following its progress (#1291 step 3). Whoever runs it //! (the app's export on a thread of its own) installs a [`Probe`] on that thread; the writers' -//! loops say how far they have come with [`begin`] and [`step`], and `step` stops them once the -//! probe was cancelled. With none installed, as for a script or the web app, they only run. +//! loops (and a layout export's pages) say how far they have come with [`begin`] and [`step`], +//! and `step` stops them once the probe was cancelled; [`check`] only stops (the map's wait for +//! its tiles). With none installed, as for a script or the web app, they only run. //! //! The analysis crate's run control is a probe, so the same Cancel and progress reach a tool and //! an export. @@ -43,7 +44,7 @@ impl Drop for Installed { } /// Starts counting a step of `total` items for the progress. -pub(crate) fn begin(total: usize) { +pub fn begin(total: usize) { CURRENT.with(|c| { if let Some(p) = c.borrow().as_ref() { p.begin(total as u64); @@ -53,7 +54,7 @@ pub(crate) fn begin(total: usize) { /// Counts `n` more items done, and stops the write once its run was asked to: call it for each /// feature, row or tile of a long loop. -pub(crate) fn step(n: usize) -> Result<()> { +pub fn step(n: usize) -> Result<()> { CURRENT.with(|c| match c.borrow().as_ref() { Some(p) if p.cancelled() => Err(DataError::Cancelled), Some(p) => { @@ -66,7 +67,7 @@ pub(crate) fn step(n: usize) -> Result<()> { /// Stops the write once its run was asked to, counting nothing: for a check after a loop that /// can't stop by itself. -pub(crate) fn check() -> Result<()> { +pub fn check() -> Result<()> { CURRENT.with(|c| match c.borrow().as_ref() { Some(p) if p.cancelled() => Err(DataError::Cancelled), _ => Ok(()), diff --git a/crates/engine/src/cmd/exports.rs b/crates/engine/src/cmd/exports.rs index 8ed6f7110d..52f3e8365e 100644 --- a/crates/engine/src/cmd/exports.rs +++ b/crates/engine/src/cmd/exports.rs @@ -1,16 +1,17 @@ //! Exports that write a file from what the session shares (a layer's features, a raster's -//! pixels). A script or the command line writes it where the command is called; the app writes it -//! on a thread of its own with progress and Cancel (#1291 step 3): [`export_start`] reads the -//! session, [`ExportRun::run`] writes the file, and [`export_land`] says what was written, as the -//! command would have. It lands whatever layer is active or project is open by then: the file is -//! written either way, and an export changes nothing in the project. +//! pixels, a copy of the map or project to draw). A script or the command line writes it where +//! the command is called; the app writes it on a thread of its own with progress and Cancel +//! (#1291 step 3): [`export_start`] reads the session, [`ExportRun::run`] writes the file, and +//! [`export_land`] says what was written, as the command would have. It lands whatever layer is +//! active or project is open by then: the file is written either way, and an export changes +//! nothing in the project. use serde_json::Value; use crate::{CmdResult, Session}; /// What an export wrote: the command's answer, and what it says of the file (the last says it -/// was exported). +/// was exported, unless `then` says it). pub struct Wrote { pub(crate) answer: Value, pub(crate) notes: Vec, @@ -19,8 +20,9 @@ pub struct Wrote { pub(crate) then: Option, } -/// What an export does to the session once its file is written, and adds to its answer. -pub(crate) type Then = Box; +/// What an export does to the session once its file is written, and adds to its answer; or the +/// failure it says once what came before it was said (a layout's warnings, then its error). +pub(crate) type Then = Box Result<(), String> + Send>; /// The writing of an export, its inputs read: it needs no session. pub(crate) type Work = Box Result + Send>; @@ -33,6 +35,8 @@ type Start = fn(&Session, &Value) -> Result; const IN_BACKGROUND: &[(&str, &str, Start)] = &[ ("share.exportLayer", "Export Features", super::share::layer_work), ("share.exportRaster", "Export Raster", super::raster::export_work), + ("share.exportMap", "Export Map", super::share::map_work), + ("layout.export", "Export Layout", super::layout::export_work), ]; /// `command` and what it is called, when the app writes its file on another thread (see @@ -47,7 +51,7 @@ pub(crate) fn write_here(s: &mut Session, p: &Value, command: &str) -> CmdResult let work = start(s, p)?; // Where a person at a terminal can stop it with Ctrl-C, as on the app's thread. let wrote = if s.stoppable { written_under(name, &rustgis_analysis::control::Control::stoppable(), work)? } else { work()? }; - Ok(said(s, wrote)) + said(s, wrote) } /// `work` under `control`, as [`rustgis_analysis::run_under`] runs it, except that a file once @@ -61,16 +65,77 @@ fn written_under(name: &str, control: &std::sync::Arc Value { +/// Says what an export wrote, and gives its answer (or the failure its `then` says). +fn said(s: &mut Session, wrote: Wrote) -> CmdResult { let Wrote { mut answer, notes, then } = wrote; for note in notes { s.notify(note, false); } if let Some(then) = then { - then(s, &mut answer); + then(s, &mut answer)?; + } + Ok(answer) +} + +/// Lands an export drawn on a copy of the map or project (Export Map, Export Layout): what +/// drawing it said, with each notice's severity; the streamed layers whose source failed on the +/// copy, marked on their live layers as drawing them there would have (or, where that layer is +/// gone or opened afresh, said in one notice to be missing from the file); then that it was +/// written to `path`, or why not. A cancelled one says only that it was cancelled. +pub(crate) fn drawn( + maps: &[rustgis_map::Map], + notices: Vec<(rustgis_data::diagnostics::Severity, String)>, + written: Result, + path: String, +) -> Result { + if let (Err(e), Err(_)) = (&written, rustgis_data::progress::check()) { + return Err(e.clone()); } - answer + // Each by its live layer, with the source it failed on: one the user removed, or opened + // afresh meanwhile (`SourceLayer::same_opening`), is left as it is. + let failed: Vec<_> = maps + .iter() + .flat_map(|m| &m.layers) + .filter_map(|l| { + let src = l.source.as_ref()?; + Some((l.instance_id(), l.name.clone(), src.error.clone()?, src.clone())) + }) + .collect(); + let answer = written.as_ref().map_or(Value::Null, Value::clone); + let then: Then = Box::new(move |s: &mut Session, _: &mut Value| { + use rustgis_data::diagnostics::Severity; + for (severity, text) in notices { + match severity { + Severity::Error => s.notify(text, true), + Severity::Warning => s.warn(text), + Severity::Info => s.notify(text, false), + } + } + let mut missing = Vec::new(); + for (instance, name, error, copied) in failed { + let live = s.project.maps.iter_mut().flat_map(|m| m.layers.iter_mut()).find(|l| l.instance_id() == instance); + match live.and_then(|l| l.source.as_mut()) { + // The same open: its failure shows on the layer (it may already). + Some(src) if src.same_opening(&copied) => { + src.error.get_or_insert(error); + } + _ => missing.push((name, error)), + } + } + // One notice for them all, with a count, naming the first. + if let Some((name, error)) = missing.first() { + let n = missing.len(); + s.warn(if n == 1 { + format!("{path}: {name} isn't in the file, as its source couldn't be read: {error}") + } else { + format!("{path}: {n} layers aren't in the file, as their sources couldn't be read; the first, {name}: {error}") + }); + } + written?; + s.notify(format!("Exported {path}"), false); + Ok(()) + }); + Ok(Wrote { answer, notes: Vec::new(), then: Some(then) }) } #[cfg(not(target_arch = "wasm32"))] @@ -81,16 +146,33 @@ fn said(s: &mut Session, wrote: Wrote) -> Value { pub struct ExportRun { /// What the export is called, for its progress and a cancelled run's error. pub name: &'static str, + started: Started, work: Work, } +#[cfg(not(target_arch = "wasm32"))] +/// The command an export started on another thread runs, with what and when: [`export_land`] +/// says what it wrote as that command, and puts it in the session's log as `Session::run` does. +#[derive(Clone)] +pub struct Started { + pub(crate) command: &'static str, + pub(crate) params: Value, + pub(crate) at: u64, + pub(crate) start: web_time::Instant, +} + #[cfg(not(target_arch = "wasm32"))] impl ExportRun { + /// What [`export_land`] needs of it once it has run. + pub fn started(&self) -> Started { + self.started.clone() + } + /// Writes the file, blocking until it's written; `control` stops it and counts its progress /// (see [`rustgis_analysis::control`]). A cancelled export leaves no file, or the one there /// before. pub fn run(self, control: &std::sync::Arc) -> Result { - let ExportRun { name, work } = self; + let ExportRun { name, work, .. } = self; // A bug in a writer must not take the app down. std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| written_under(name, control, work))) .unwrap_or_else(|_| Err(format!("{name} failed unexpectedly"))) @@ -101,15 +183,34 @@ impl ExportRun { /// Starts `command` (one of [`IN_BACKGROUND`]'s) with `p`, to write its file on another thread: /// its parameters checked and its inputs taken now. pub fn export_start(s: &mut Session, command: &str, p: &Value) -> Result { - let &(_, name, start) = + let &(command, name, start) = IN_BACKGROUND.iter().find(|(c, ..)| *c == command).ok_or_else(|| format!("{command} doesn't run in the background"))?; - Ok(ExportRun { name, work: start(s, p)? }) + let started = Started { command, params: p.clone(), at: crate::project_log::now_ms(), start: web_time::Instant::now() }; + Ok(ExportRun { name, started, work: start(s, p)? }) } #[cfg(not(target_arch = "wasm32"))] -/// Says what [`ExportRun::run`] wrote, as its command would have, and gives its answer. -pub fn export_land(s: &mut Session, wrote: Wrote) -> Value { - said(s, wrote) +/// Says what [`ExportRun::run`] wrote (or why it failed), as its command would have, and gives +/// its answer: the troubleshooting history files what it says (its failure too) under that +/// command, and the session's log has its entry, as when it runs here. +pub fn export_land(s: &mut Session, started: Started, done: Result) -> CmdResult { + let Started { command, params, at, start } = started; + let before = s.diagnostic_source.replace(command); + let mark = s.notice_mark(); + let landed = done.and_then(|wrote| said(s, wrote)); + // Its failure said once, under the command, as `Session::run` says one. + if let Err(e) = &landed { + let text = Session::redacted(e); + if !s.notices_since(mark).iter().any(|n| n.error && n.text == text.as_str()) { + s.notify_redacted(text, true); + } + } + s.diagnostic_source = before; + if s.log.on() && crate::project_log::is_logged(s, command, ¶ms) { + let entry = crate::project_log::entry(command, crate::project_log::kept_params(¶ms), &landed, at, start.elapsed()); + s.log.push(entry); + } + landed } #[cfg(test)] diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index b53ea41c09..024758cd60 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -121,7 +121,7 @@ pub fn commands() -> Vec { C::new("layout.seriesPage", "Map Series Page", "Publish › Map Frames", series_page).params(r#"{"layout"?, "page": number|"next"|"previous"}"#), C::new("layout.seriesRefresh", "Refresh Map Series", "Publish › Map Frames", |s, p| series_page(s, &json!({"layout": p.get("layout"), "page": "current"}))) .params(r#"{"layout"?}"#), - C::new("layout.export", "Export Layout", "Publish › Output", export) + C::new("layout.export", "Export Layout", "Publish › Output", |s, p| super::exports::write_here(s, p, "layout.export")) .view() .params(r#"{"layout"?, "path": "*.png|*.pdf|*.svg" (PNG/SVG: current page; SVG: each element a group of its own; text and shapes as SVG, map frames and surrounds as vectors, rasters and tiles in them as pictures at dpi), "dpi"?: 72..600, "allPages"?: bool (PDF only; default true: every map series page, up to 500; false: the current page)}"#), C::new("layout.save", "Save Layout File", "Publish › Save As", |s, p| { @@ -3338,8 +3338,8 @@ fn series_page(s: &mut Session, p: &Value) -> CmdResult { /// Renders a layout to PNG or PDF bytes. `all` renders every map series page (PDF only). /// What an export of a layout whose texts name installed families is told while the app is still -/// indexing the fonts (its first seconds; an export runs on its interface thread, which mustn't -/// wait): those texts are drawn in their fallbacks. A script or the command line indexes them at +/// indexing the fonts (its first seconds; the app's exports don't wait for them): those texts +/// are drawn in their fallbacks. A script or the command line indexes them at /// once. fn named_fonts_unread(layout: &Layout) -> Option<&'static str> { let named = layout.elements.iter().any(|e| matches!(&e.kind, ElementKind::Text { font_name: Some(_), .. })); @@ -3379,6 +3379,10 @@ pub fn layout_bytes(s: &mut Session, li: usize, pdf: bool, dpi: f64, all: bool) let mut out = Vec::new(); let mut png = None; let mut held = 0usize; + // How far a series' pages have come, when the export runs with progress. + if pages.len() > 1 { + rustgis_data::progress::begin(pages.len()); + } for page in &pages { if stepping { series_page(s, &json!({"layout": li, "page": page}))?; @@ -3420,6 +3424,8 @@ pub fn layout_bytes(s: &mut Session, li: usize, pdf: bool, dpi: f64, all: bool) let img = rustgis_render::layout::render(layout, &mut s.project.maps, dpi, &info).ok_or("the page is too large to draw")?; png = Some(img.encode_png().map_err(|e| e.to_string())?); } + // A cancelled export stops here, its layout put back below. + rustgis_data::progress::step(1).map_err(|e| e.to_string())?; } Ok((out, png)) }; @@ -3471,24 +3477,44 @@ pub(crate) fn pictured_notices(s: &mut Session, frames: &[(u32, String)], pages: } } -fn export(s: &mut Session, p: &Value) -> CmdResult { +/// `layout.export`: a layout to a PNG, PDF or SVG. It draws a copy of the project, so the project +/// can change meanwhile (a map series steps through its pages on the copy); a cancelled export +/// stops at the next page, or its map's wait for tiles, and writes nothing. +pub(crate) fn export_work(s: &Session, p: &Value) -> Result { let path = ps(p, "path").ok_or("path is required")?.to_string(); let li = index(s, p)?; let lower = path.to_ascii_lowercase(); - let pdf = lower.ends_with(".pdf"); - if lower.ends_with(".svg") { - let text = layout_svg(s, li, f(p, "dpi").unwrap_or(150.0))?; - rustgis_data::io::write_atomic(Path::new(&path), text.as_bytes()).map_err(|e| format!("{path}: {e}"))?; - s.notify(format!("Exported {path}"), false); - return Ok(json!({"path": path, "bytes": text.len()})); - } - if !pdf && !lower.ends_with(".png") { + let (pdf, svg) = (lower.ends_with(".pdf"), lower.ends_with(".svg")); + if !pdf && !svg && !lower.ends_with(".png") { return Err("export to a .png, .pdf or .svg file".into()); } - let bytes = layout_bytes(s, li, pdf, f(p, "dpi").unwrap_or(150.0), b(p, "allPages").unwrap_or(true))?; - rustgis_data::io::write_atomic(Path::new(&path), &bytes).map_err(|e| format!("{path}: {e}"))?; - s.notify(format!("Exported {path}"), false); - Ok(json!({"path": path, "bytes": bytes.len()})) + let (dpi, all) = (f(p, "dpi").unwrap_or(150.0), b(p, "allPages").unwrap_or(true)); + let mut project = s.project.clone(); + for m in &mut project.maps { + m.detach_reads(); + } + Ok(Box::new(move || { + let mut copy = Session::new(project); + let written = (|| { + let bytes = if svg { layout_svg(&mut copy, li, dpi)?.into_bytes() } else { layout_bytes(&mut copy, li, pdf, dpi, all)? }; + rustgis_data::progress::check().map_err(|e| e.to_string())?; + rustgis_data::io::write_atomic(Path::new(&path), &bytes).map_err(|e| format!("{path}: {e}"))?; + Ok(json!({"path": path, "bytes": bytes.len()})) + })(); + // What drawing it said (frames drawn as pictures, fonts it lacks), as the command says it + // here; past the notices a session keeps, how many earlier ones there were. + let kept = std::mem::take(&mut copy.notices); + let earlier = copy.notices_given().saturating_sub(kept.len() as u64); + let mut said: Vec<_> = Vec::new(); + if earlier > 0 { + said.push(( + rustgis_data::diagnostics::Severity::Warning, + format!("{path}: {earlier} earlier notices from drawing it aren't shown"), + )); + } + said.extend(kept.into_iter().map(|n| (n.severity, n.text))); + super::exports::drawn(©.project.maps, said, written, path) + })) } /// The layout `li` as it is now (a map series at its current page) as SVG, kept as vectors with diff --git a/crates/engine/src/cmd/share.rs b/crates/engine/src/cmd/share.rs index ea126b233c..efa96122fe 100644 --- a/crates/engine/src/cmd/share.rs +++ b/crates/engine/src/cmd/share.rs @@ -10,12 +10,17 @@ use serde_json::{Value, json}; use super::exports::{Work, Wrote}; use super::{f, need_layer, ps}; -use crate::{CmdResult, CommandSpec as C, Session}; +use crate::{CommandSpec as C, Session}; pub fn commands() -> Vec { vec![ - C::new("share.exportMap", "Export Map", "Publish › Output", export_map) - .view() + C::new("share.exportMap", "Export Map", "Publish › Output", |s, p| { + // A style added with its own view: the export waits for it to load, as it does for + // tiles. The app runs it here, not on a thread, while one loads. + super::tiles::follow_style_view(s, web_time::Duration::from_secs(30)); + super::exports::write_here(s, p, "share.exportMap") + }) + .view() .params(r#"{"path": "*.png", "width"?: px, "height"?: px, "dpi"?: 96..600}"#), C::new("share.exportLayer", "Export Features", "Publish › Output", |s, p| super::exports::write_here(s, p, "share.exportLayer")) .view() @@ -136,21 +141,32 @@ fn report_table(s: &mut Session, path: &str, gpkg: &GpkgTable, table: Option, height: Option, dpi: Option) -> Result, String> { - let (vw, vh) = s.view_size; + let view_size = s.view_size; + png_of(s.map_mut(), view_size, width, height, dpi) +} + +/// Renders `map` at its camera in a view of `(vw, vh)` points (or a requested size) to PNG bytes. +fn png_of( + map: &mut rustgis_map::Map, + (vw, vh): (f64, f64), + width: Option, + height: Option, + dpi: Option, +) -> Result, String> { // NaN would pass through `clamp`; non-finite values fall back to the defaults. let fin = |v: Option| v.filter(|x| x.is_finite()); let (width, height, dpi) = (fin(width), fin(height), fin(dpi)); let w = width.unwrap_or(vw).clamp(16.0, 8000.0); let h = height.unwrap_or(vh).clamp(16.0, 8000.0); let ppp = (dpi.unwrap_or(96.0) / 96.0).clamp(0.5, 6.0); - let mut camera = s.map().camera; + let mut camera = map.camera; // Keep the same ground extent when the size differs from the view. if width.is_some() || height.is_some() { camera.res *= (vw / w).max(vh / h); camera.clamp(); } let view = View { width: w, height: h, pixels_per_point: ppp, camera, sky: false }; - rustgis_render::render_png(s.map_mut(), &view) + rustgis_render::render_png(map, &view) } /// The map view on one PDF page (for printing): the page is the view's shape at 96 points per @@ -175,14 +191,21 @@ pub fn map_pdf(s: &mut Session, dpi: f64) -> Result, String> { Ok(rustgis_render::pdf::write(&[page], &s.map().name)) } -fn export_map(s: &mut Session, p: &Value) -> CmdResult { +/// `share.exportMap`: draws the active map as it is now to a PNG. It draws a copy of the map, so +/// the map can move meanwhile; a cancelled export stops waiting for tiles and writes nothing. +pub(crate) fn map_work(s: &Session, p: &Value) -> Result { let path = ps(p, "path").ok_or("path is required")?.to_string(); - // A style added with its own view: the export waits for it to load, as it does for tiles. - super::tiles::follow_style_view(s, web_time::Duration::from_secs(30)); - let png = map_png(s, f(p, "width"), f(p, "height"), f(p, "dpi"))?; - rustgis_data::io::write_atomic(std::path::Path::new(&path), &png).map_err(|e| format!("{path}: {e}"))?; - s.notify(format!("Exported {path}"), false); - Ok(json!({"path": path, "bytes": png.len()})) + let (width, height, dpi) = (f(p, "width"), f(p, "height"), f(p, "dpi")); + let (mut map, view_size) = (s.map().detached(), s.view_size); + Ok(Box::new(move || { + let written = (|| { + let png = png_of(&mut map, view_size, width, height, dpi)?; + rustgis_data::progress::check().map_err(|e| e.to_string())?; + rustgis_data::io::write_atomic(Path::new(&path), &png).map_err(|e| format!("{path}: {e}"))?; + Ok(json!({"path": path, "bytes": png.len()})) + })(); + super::exports::drawn(std::slice::from_ref(&map), Vec::new(), written, path) + })) } /// `share.exportLayer`: writes a layer to a file by its extension. A streamed layer exports its @@ -258,8 +281,12 @@ fn write_layer(l: &rustgis_map::Layer, path: &str, asked: Option, gpkg: Gpk // The table written, said and put in the answer once the export lands, where the session // (layers reading a replaced table) is at hand. let path = path.to_string(); - let then: Option = table - .map(|t| Box::new(move |s: &mut Session, out: &mut Value| report_table(s, &path, &gpkg, Some(t), out)) as super::exports::Then); + let then: Option = table.map(|t| { + Box::new(move |s: &mut Session, out: &mut Value| { + report_table(s, &path, &gpkg, Some(t), out); + Ok(()) + }) as super::exports::Then + }); Ok(Wrote { answer, notes: said, then }) } diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 574c4733ed..688bd9fd02 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -451,6 +451,8 @@ pub struct TopologyIssue { pub struct Notice { pub text: String, pub error: bool, + /// How the troubleshooting history files it (an error, a warning or information). + pub(crate) severity: rustgis_data::diagnostics::Severity, } const UNDO_LIMIT: usize = 40; @@ -665,7 +667,7 @@ pub struct Session { put_aside: Option>, depth: u32, /// Only the command's registered id, never its parameters. Nested commands restore this. - diagnostic_source: Option<&'static str>, + pub(crate) diagnostic_source: Option<&'static str>, /// How many of the commands running now restore the project if they fail: a replacement is /// put aside only under one of them, as only then can it be rolled back. restorable: u32, @@ -1650,13 +1652,18 @@ impl Session { fn notice_redacted(&mut self, text: rustgis_data::diagnostics::Redacted, severity: rustgis_data::diagnostics::Severity) { self.diagnostics.push_redacted(severity, self.diagnostic_source.unwrap_or("Application"), &text); - self.notices.push(Notice { text: text.into_string(), error: severity == rustgis_data::diagnostics::Severity::Error }); + self.notices.push(Notice { text: text.into_string(), error: severity == rustgis_data::diagnostics::Severity::Error, severity }); self.notices_given += 1; if self.notices.len() > 200 { self.notices.remove(0); } } + /// How many notices were given in this session, including ones no longer kept. + pub(crate) fn notices_given(&self) -> u64 { + self.notices_given + } + /// A mark to pass to [`Session::notices_since`]. pub fn notice_mark(&self) -> u64 { self.notices_given diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 606d28eea8..c90910a191 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -28040,6 +28040,11 @@ fn raster_tools_run_on_another_thread() { #[test] fn exports_run_on_another_thread() { use crate::cmd::exports::{export_land, export_start, runs_in_background}; + // A path as it appears in JSON, as `PATH`: on Windows its backslashes are escaped there. + let unpathed_json = |v: &Value, path: &str| { + let escaped = json!(path).to_string(); + v.to_string().replace(escaped.trim_matches('"'), "PATH") + }; use rustgis_analysis::control::Control; let mut s = world(); s.run("layer.addDataBytes", &json!({"fileName": "dem.tif", "data": dem_b64()})).unwrap(); @@ -28047,7 +28052,12 @@ fn exports_run_on_another_thread() { let dir = std::env::temp_dir().join(format!("rustgis-exports-thread-{}", std::process::id())); std::fs::create_dir_all(&dir).unwrap(); let at = |name: &str| dir.join(name).display().to_string(); + s.run("layout.new", &json!({"page": "a4", "orientation": "landscape", "template": "map"})).unwrap(); for (command, file, p) in [ + ("share.exportMap", "map.png", json!({"width": 64, "height": 48})), + ("layout.export", "layout.png", json!({"dpi": 48})), + ("layout.export", "layout.pdf", json!({"dpi": 48})), + ("layout.export", "layout.svg", json!({"dpi": 48})), ("share.exportLayer", "countries.geojson", json!({"layer": "Countries"})), ("share.exportLayer", "countries.shp", json!({"layer": "Countries"})), ("share.exportLayer", "countries.gpkg", json!({"layer": "Countries", "crs": "EPSG:3857"})), @@ -28067,11 +28077,19 @@ fn exports_run_on_another_thread() { let mark = s.notice_mark(); let answer_here = s.run(command, &with(&here)).unwrap(); let said_here = said(&s, mark, &here); + let logged = |s: &Session| s.log.entries().last().map(|e| (e.command.clone(), e.params.clone(), e.ok)); + let logged_here = logged(&s); + assert!(logged_here.as_ref().is_some_and(|(c, ..)| c == command), "{command}: logged where it's called"); let run = export_start(&mut s, command, &with(&there)).unwrap(); - let mark = s.notice_mark(); - let wrote = std::thread::spawn(move || run.run(&Control::new())).join().unwrap().unwrap(); + let (mark, started) = (s.notice_mark(), run.started()); + let wrote = std::thread::spawn(move || run.run(&Control::new())).join().unwrap(); assert!(s.notices_since(mark).is_empty(), "{command}: nothing is said before it lands"); - let answer_there = export_land(&mut s, wrote); + let answer_there = export_land(&mut s, started, wrote).unwrap(); + // The session's log has it as it has the export run here. + let there_params = logged(&s).map(|(c, p, ok)| (c, unpathed_json(&p, &there), ok)); + assert_eq!(there_params, logged_here.map(|(c, p, ok)| (c, unpathed_json(&p, &here), ok)), "{command} {file}"); + let filed = s.diagnostics.records().last().map(|r| r.source.clone()); + assert_eq!(filed.as_deref(), Some(command), "the history files what it said under the command"); assert_eq!(said(&s, mark, &there), said_here, "{command} {file}"); // The path as it appears in JSON: on Windows its backslashes are escaped there. let unpathed = |v: &Value, path: &str| { @@ -28096,8 +28114,75 @@ fn exports_run_on_another_thread() { assert!(export_start(&mut s, "share.exportLayer", &json!({"layer": "nowhere", "path": at("x.geojson")})).is_err()); assert!(export_start(&mut s, "share.exportLayer", &json!({"layer": "Countries", "path": at("x.gpkg"), "crs": "EPSG:nope"})).is_err()); assert!(export_start(&mut s, "share.exportRaster", &json!({"layer": dem, "path": at("x.tif"), "band": 99})).is_err()); - // Map and layout exports draw the map: they still run where they're called. - assert!(runs_in_background("share.exportMap").is_none() && runs_in_background("layout.export").is_none()); + assert!(export_start(&mut s, "layout.export", &json!({"path": at("x.jpg")})).is_err(), "a format it can't write"); + // Map and layout exports draw a copy of what was there when they started: moving the map or + // removing the layout meanwhile changes nothing in the file. + let (here, there) = (at("still_here.png"), at("still_there.png")); + s.run("share.exportMap", &json!({"path": here, "width": 64, "height": 48})).unwrap(); + let run = export_start(&mut s, "share.exportMap", &json!({"path": there, "width": 64, "height": 48})).unwrap(); + s.map_mut().camera.res *= 4.0; + run.run(&Control::new()).unwrap(); + assert_eq!(std::fs::read(&there).unwrap(), std::fs::read(&here).unwrap()); + let (here, there) = (at("still_here.svg"), at("still_there.svg")); + s.map_mut().camera.res /= 4.0; + s.run("layout.export", &json!({"path": here, "dpi": 48})).unwrap(); + let run = export_start(&mut s, "layout.export", &json!({"path": there, "dpi": 48})).unwrap(); + let layouts = std::mem::take(&mut s.project.layouts); + run.run(&Control::new()).unwrap(); + assert_eq!(std::fs::read(&there).unwrap(), std::fs::read(&here).unwrap()); + s.project.layouts = layouts; + // A layout export that fails says why when it lands, as it does here. + let nowhere = dir.join("no").join("such.png").display().to_string(); + let here = s.run("layout.export", &json!({"path": nowhere, "dpi": 48})).unwrap_err(); + let run = export_start(&mut s, "layout.export", &json!({"path": nowhere, "dpi": 48})).unwrap(); + let started = run.started(); + let mark = s.notice_mark(); + assert_eq!(export_land(&mut s, started, run.run(&Control::new())).unwrap_err(), here); + let errors: Vec<_> = s.notices_since(mark).iter().filter(|n| n.error).map(|n| n.text.clone()).collect(); + assert_eq!(errors, std::slice::from_ref(&here), "its failure is said once"); + let filed = s.diagnostics.records().last().map(|r| (r.source.clone(), r.severity)); + assert_eq!(filed, Some(("layout.export".to_string(), rustgis_data::diagnostics::Severity::Error)), "under the command"); + assert!(s.log.entries().last().is_some_and(|e| e.command == "layout.export" && !e.ok), "a failed one is logged as failed"); + // A streamed layer's source that failed on the copy is marked on its live layer, unless that + // was opened from somewhere else meanwhile. + let li = s.map().layers.iter().position(|l| l.name == "Countries").unwrap(); + let mut src = rustgis_map::source::SourceLayer::default(); + src.path = Some("gone.fgb".into()); + s.map_mut().layers[li].source = Some(src.clone()); + let mut copy = s.map().detached(); + let mut failed = src.clone(); + failed.error = Some("gone.fgb: not found".into()); + copy.layers[li].source = Some(failed); + let land = |s: &mut Session, copy: &rustgis_map::Map| { + let wrote = crate::cmd::exports::drawn(std::slice::from_ref(copy), Vec::new(), Ok(json!({})), at("drawn.png")); + let started = export_start(s, "share.exportMap", &json!({"path": at("drawn.png")})).unwrap().started(); + export_land(s, started, wrote).unwrap(); + s.map().layers[li].source.as_ref().and_then(|src| src.error.clone()) + }; + assert_eq!(land(&mut s, ©).as_deref(), Some("gone.fgb: not found")); + s.map_mut().layers[li].source.as_mut().unwrap().error = None; + s.map_mut().layers[li].source.as_mut().unwrap().path = Some("found.fgb".into()); + let mark = s.notice_mark(); + assert_eq!(land(&mut s, ©), None, "a source opened afresh keeps its own state"); + let said = s.notices_since(mark).iter().map(|n| n.text.clone()).collect::>().join("\n"); + assert!(said.contains("Countries isn't in the file") && said.contains("gone.fgb: not found"), "and the file's gap is said: {said}"); + // Opened afresh and failing again: still said, as the file lacks the layer either way. + s.map_mut().layers[li].source.as_mut().unwrap().error = Some("found.fgb: not found".into()); + let mark = s.notice_mark(); + land(&mut s, ©); + assert!(s.notices_since(mark).iter().any(|n| n.text.contains("Countries isn't in the file"))); + // Several left out: one notice, with a count. + let mut two = copy.clone(); + let other = two.layers.iter().position(|l| l.name != "Countries").unwrap(); + let mut gone = rustgis_map::source::SourceLayer::default(); + gone.path = Some("gone.fgb".into()); + gone.error = Some("gone.fgb: not found".into()); + two.layers[other].source = Some(gone); + let mark = s.notice_mark(); + land(&mut s, &two); + let missing: Vec<_> = s.notices_since(mark).iter().filter(|n| n.text.contains("in the file")).map(|n| n.text.clone()).collect(); + assert!(missing.len() == 1 && missing[0].contains("2 layers aren't in the file"), "{missing:?}"); + s.map_mut().layers[li].source = None; let _ = std::fs::remove_dir_all(&dir); } diff --git a/crates/map/src/lib.rs b/crates/map/src/lib.rs index 487c84e68c..af9910a273 100644 --- a/crates/map/src/lib.rs +++ b/crates/map/src/lib.rs @@ -1406,6 +1406,22 @@ impl Map { } } + /// A copy to draw elsewhere (an export on a thread of its own): its layers keep this map's + /// identities, and its streamed layers read for themselves (see [`source::SourceLayer::detach_read`]). + pub fn detached(&self) -> Map { + let mut copy = self.clone(); + copy.detach_reads(); + copy + } + + /// [`Map::detached`] for a map that is already a copy: its streamed layers leave a read under + /// way to the map it was copied from. + pub fn detach_reads(&mut self) { + for source in self.layers.iter_mut().filter_map(|l| l.source.as_mut()) { + source.detach_read(); + } + } + /// Identifies this live map, whatever its name or place among the project's maps. pub fn instance_id(&self) -> u64 { self.instance_id diff --git a/crates/map/src/source.rs b/crates/map/src/source.rs index 83223919a5..3882c81319 100644 --- a/crates/map/src/source.rs +++ b/crates/map/src/source.rs @@ -915,6 +915,34 @@ impl SourceLayer { self.open() } + /// Leaves a read under way to the layer this one was copied from, which takes it when it + /// lands: a copy drawn elsewhere (an export on a thread of its own) reads what it needs for + /// itself. The two would otherwise share the read, and the one that didn't take it would wait + /// for it forever. The copy decides its thinning by itself too, so the map's redraws never + /// wait for the export's. + pub fn detach_read(&mut self) { + self.pending = None; + self.render_lod = Default::default(); + } + + /// Whether `other` (a copy of this one) reads the same source the same way: the same file and + /// table, opened by the same open. One opened afresh since (`reopen`, a table replaced) is + /// another, whatever its path. + pub fn same_opening(&self, other: &SourceLayer) -> bool { + let handles = match (&self.handle, &other.handle) { + (Some(a), Some(b)) => Arc::ptr_eq(a, b), + (None, None) => true, + _ => false, + }; + // A source by its web address still opening: the same open shares its slot. + let openings = match (&self.opening, &other.opening) { + (Some(a), Some(b)) => Arc::ptr_eq(a, b), + (None, None) => true, + _ => false, + }; + handles && openings && self.path == other.path && self.table == other.table + } + /// Forgets the window, so the next view reads again (after the source changes). pub fn reset(&mut self) { self.render_lod = Default::default(); diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index 1899969445..7e7aaa3ef3 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -5589,11 +5589,16 @@ fn complete(map: &mut Map, view: &View, record: bool) -> Option { let start = web_time::Instant::now(); let budget = web_time::Duration::from_secs(60); let mut idle = 0; - while r.tiles_pending && idle < IDLE_PASSES && start.elapsed() < budget && cfg!(not(target_arch = "wasm32")) { + // An export cancelled meanwhile (see `rustgis_data::progress`) stops waiting: it writes nothing. + let cancelled = || rustgis_data::progress::check().is_err(); + while r.tiles_pending && idle < IDLE_PASSES && start.elapsed() < budget && cfg!(not(target_arch = "wasm32")) && !cancelled() { // Each wait within what is left of the minute, until something has come: a fetch of // another's (a map open beside it, a slow server) doesn't hold it up. let left = budget.saturating_sub(start.elapsed()); - rustgis_tiles::cache::wait_for_news(drawn_at, left.min(web_time::Duration::from_secs(30))); + rustgis_tiles::cache::wait_for_news_unless(drawn_at, left.min(web_time::Duration::from_secs(30)), cancelled); + if cancelled() { + break; + } let now = rustgis_tiles::cache::generation(); if now == drawn_at { idle += 1; diff --git a/crates/tiles/src/cache.rs b/crates/tiles/src/cache.rs index 07ecdf8ac7..31eec53255 100644 --- a/crates/tiles/src/cache.rs +++ b/crates/tiles/src/cache.rs @@ -349,11 +349,16 @@ const SETTLE: Duration = Duration::from_secs(1); /// a moment, or for a second at most: one drawing again with what came needn't wait for every /// fetch in the process (another map's, a slow server's, one never answered). Native only. pub fn wait_for_news(since: u64, timeout: Duration) { + wait_for_news_unless(since, timeout, || false); +} + +/// [`wait_for_news`], given up as soon as `stop` says so (an export cancelled meanwhile). +pub fn wait_for_news_unless(since: u64, timeout: Duration, stop: impl Fn() -> bool) { #[cfg(not(target_arch = "wasm32"))] { let start = Instant::now(); let (mut seen, mut changed, mut news) = (since, Instant::now(), None); - while in_flight() > 0 && start.elapsed() < timeout { + while in_flight() > 0 && start.elapsed() < timeout && !stop() { let now = generation(); if now != seen { (seen, changed) = (now, Instant::now()); @@ -366,7 +371,7 @@ pub fn wait_for_news(since: u64, timeout: Duration) { } } #[cfg(target_arch = "wasm32")] - let _ = (since, timeout); + let _ = (since, timeout, stop); } /// A waiting fetch: what to decode it as, whose diagnostics it goes to, and its expected statuses. diff --git a/crates/ui-egui/src/exportjob.rs b/crates/ui-egui/src/exportjob.rs index cd579745bd..7c6015eccb 100644 --- a/crates/ui-egui/src/exportjob.rs +++ b/crates/ui-egui/src/exportjob.rs @@ -1,7 +1,7 @@ -//! Export Features and Export Raster on the desktop write their file on a thread of their own -//! (#1291 step 3), so a large layer or raster doesn't hold the window: the status bar shows the -//! export under way, how far it has come and Cancel, and [`land`] says what it wrote, as the -//! command says when it runs here. +//! Export Features, Export Raster, Export Map and Export Layout on the desktop write their file +//! on a thread of their own (#1291 step 3), so a large layer, raster or map doesn't hold the +//! window: the status bar shows the export under way, how far it has come and Cancel, and +//! [`land`] says what it wrote, as the command says when it runs here. use std::sync::Arc; @@ -15,19 +15,26 @@ use crate::theme::Tokens; /// An export under way: what it is called, its Cancel and progress, and what it wrote once done. pub struct ExportJob { + started: exports::Started, name: &'static str, control: Arc, slot: crate::Slot>, } +/// Refuses another export while one runs, whichever way it would run (one at a time). +pub fn idle(app: &RustGisApp) -> Result<(), String> { + match &app.export_job { + Some(job) => Err(crate::i18n::fmt("{tool} is still running: wait for it, or cancel it.", &[("tool", tl_raw!(job.name))])), + None => Ok(()), + } +} + /// Starts `command` ([`exports::runs_in_background`]) with `p` on a thread of its own. One at a /// time: a second is refused while the first runs. pub fn start(app: &mut RustGisApp, command: &'static str, p: Value) -> Result { - if let Some(job) = &app.export_job { - return Err(crate::i18n::fmt("{tool} is still running: wait for it, or cancel it.", &[("tool", tl_raw!(job.name))])); - } + idle(app)?; let run = exports::export_start(&mut app.session, command, &p)?; - let name = run.name; + let (started, name) = (run.started(), run.name); // Cancel stops it. let control = Control::stoppable(); let (ctl, slot) = (control.clone(), crate::Slot::default()); @@ -43,7 +50,7 @@ pub fn start(app: &mut RustGisApp, command: &'static str, p: Value) -> Result bool { let Some(job) = &app.export_job else { return false }; let done = job.slot.lock().unwrap_or_else(std::sync::PoisonError::into_inner).take(); let Some(done) = done else { return true }; + let started = job.started.clone(); app.export_job = None; - match done { - // Its notices are the command's; the answer was for a script, which waits for its own. - Ok(wrote) => { - exports::export_land(&mut app.session, wrote); - } - Err(e) => app.session.notify(e, true), - } + // Its notices (a failure among them) are the command's; the answer was for a script, which + // waits for its own. + let _ = exports::export_land(&mut app.session, started, done); false } @@ -132,4 +136,26 @@ mod tests { assert!(left.iter().all(|n| !n.to_string_lossy().ends_with(".tmp")), "{left:?}"); let _ = std::fs::remove_dir_all(&dir); } + + #[test] + fn export_map_and_export_layout_draw_on_a_thread() { + let mut app = RustGisApp::with_sample(); + app.run("layout.new", json!({"page": "a4", "template": "map"})).unwrap(); + let dir = std::env::temp_dir().join(format!("rustgis-export-drawn-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + for (command, name, file) in [("ui.exportMap", "Export Map", "map.png"), ("ui.exportLayout", "Export Layout", "layout.png")] { + let path = dir.join(file); + crate::dialogs::PICKED.with(|c| *c.borrow_mut() = Some(path.display().to_string())); + assert_eq!(app.run(command, Value::Null).unwrap()["started"], name); + let end = std::time::Instant::now() + std::time::Duration::from_secs(120); + while land(&mut app) { + assert!(std::time::Instant::now() < end, "{name} never landed"); + std::thread::sleep(std::time::Duration::from_millis(5)); + } + let exported = format!("Exported {}", path.display()); + assert!(app.session.notices.iter().any(|n| n.text == exported && !n.error), "{:?}", app.session.notices.last()); + assert!(std::fs::metadata(&path).is_ok_and(|m| m.len() > 100), "{file}"); + } + let _ = std::fs::remove_dir_all(&dir); + } } diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 6b11cfc67f..859f8aaeae 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -3279,6 +3279,10 @@ impl RustGisApp { let (exts, kinds): (&[&str], _) = if pdf_only { (&["pdf"], tl!("PDF")) } else { (&["pdf", "png", "svg"], tl!("PDF, PNG or SVG")) }; if let Some(path) = dialogs::pick_save(&format!("{name}.pdf"), exts, kinds) { + // Drawn on a thread of its own, with progress and Cancel in the status bar. + #[cfg(not(target_arch = "wasm32"))] + return exportjob::start(self, "layout.export", json!({"layout": li, "path": path, "dpi": 200})); + #[cfg(target_arch = "wasm32")] return self.run("layout.export", json!({"layout": li, "path": path, "dpi": 200})); } } @@ -3789,6 +3793,15 @@ impl RustGisApp { "ui.exportMap" => { let name = format!("{}.png", self.session.map().name); if let Some(path) = dialogs::pick_save(&name, &["png"], tl!("PNG image")) { + // Drawn on a thread of its own, with progress and Cancel in the status bar; + // here while a style added with its own view loads, as the command waits for + // it to move the map there first. + #[cfg(not(target_arch = "wasm32"))] + if self.session.style_view.is_none() { + return exportjob::start(self, "share.exportMap", json!({"path": path, "dpi": 192})); + } + #[cfg(not(target_arch = "wasm32"))] + exportjob::idle(self)?; return self.run("share.exportMap", json!({"path": path, "dpi": 192})); } } diff --git a/docs/user-guide/sharing.md b/docs/user-guide/sharing.md index efdffa3637..803d228b0d 100644 --- a/docs/user-guide/sharing.md +++ b/docs/user-guide/sharing.md @@ -19,12 +19,16 @@ table. **Data › Export** also writes rasters to GeoTIFF, and **Data Interop** KML, GPX or CSV with WKT. Layouts, reports and presentations export from their own tabs and panes (see [Layouts and Reports](layouts.md)). -On the desktop, Export Features and Export Raster write their file in the background, so the map -and the rest of the window stay in use. While one runs, the status bar shows its name, a progress -bar (Export Raster's untiled GeoTIFF shows none) and **Cancel**; when it ends its messages appear -as before. One export runs at a time. A cancelled export writes nothing, and a file it was to -replace stays as it was. Scripts, models, the AI Assistant and the command line still wait for -the file. Export Map and layout exports still run before the window goes on. +On the desktop, Export Features, Export Raster, Export Map and Export Layout write their file in +the background, so the map and the rest of the window stay in use. While one runs, the status bar +shows its name, a progress bar (Export Raster's untiled GeoTIFF, Export Map and a one-page layout +show none) and **Cancel**; when it ends its messages appear as before. One export runs at a time. +Export Map and Export Layout draw the map and layout as they were when the export started, so +moving the map or editing the layout meanwhile doesn't change the file. A cancelled export writes +nothing, and a file it was to replace stays as it was. Scripts, models, the AI Assistant and the +command line still wait for the file. Printing a layout, and the exports of reports, presentations +and animations, still run before the window goes on. So does Export Map while a style added with +its own view is still loading: it waits for the map to move there, without progress or **Cancel**. A GeoPackage export writes a new file by default, and refuses an existing `.gpkg` to protect its tables, even if the save dialog offers to replace the file. To put the layer into an existing