diff --git a/crates/engine/src/cmd/gp.rs b/crates/engine/src/cmd/gp.rs index 9bba22869d..c18cf994ed 100644 --- a/crates/engine/src/cmd/gp.rs +++ b/crates/engine/src/cmd/gp.rs @@ -275,7 +275,8 @@ impl PluginRun { } } -/// A plugin tool's run started and not yet complete. +/// A tool's run on another thread, started and not yet complete: a plugin tool's +/// ([`plugin_start`]) or a built-in one's ([`tool_start`]). pub(crate) struct StartedRun { id: u64, name: String, @@ -284,10 +285,13 @@ pub(crate) struct StartedRun { /// The parameters, as the history records them. params: Value, t0: u64, - /// The feature ids of the streamed layers it reads whole, by lowercase name. + /// The feature ids of the streamed layers it reads whole, by lowercase name (a built-in tool + /// reads them on its thread, and hands them back with its result). full_ids: FullIds, - /// The other layers it reads, as it read them, by lowercase name. + /// The layers it reads, as it read them, by lowercase name. read: std::collections::HashMap, + /// The map it ran on: its identity ([`rustgis_map::Map::instance_id`]), and its name. + map: (u64, String), } /// The feature ids of the streamed layers a tool read whole, by lowercase name: what its @@ -310,16 +314,21 @@ pub fn plugin_start(s: &mut Session, tool: &str, params: &Value) -> Result MAX_ASYNC_RUNS { s.plugin_runs.remove(0); @@ -341,6 +350,18 @@ pub fn plugin_abandon(s: &mut Session, id: u64) { /// Finishes a run from [`plugin_start`] with what [`PluginRun::run`] returned: `gp.finish /// {"id"}` then applies it like `gp.run` would, with undo and history. pub fn plugin_complete(s: &mut Session, id: u64, result: Result) -> Result<(), String> { + complete(s, id, result, None) +} + +/// Completes a started run: fits its result to the edits made while it ran and to the windows of +/// the streamed layers it read whole (`full_ids` when it read them on its thread), and leaves it +/// for `gp.finish {"id"}`. +fn complete( + s: &mut Session, + id: u64, + result: Result, + full_ids: Option, +) -> Result<(), String> { let i = s .plugin_runs .iter() @@ -348,8 +369,33 @@ pub fn plugin_complete(s: &mut Session, id: u64, result: Result true, + Output::Layers(out) => out.iter().any(|o| !matches!(o, rustgis_analysis::OutputLayer::Table(_))), + _ => false, + }); + let mut same = (0..s.project.maps.len()).filter(|&i| s.project.maps.get(i).is_some_and(|m| m.instance_id() == run.map.0)); + let ran_on = same.next().filter(|_| same.next().is_none()); + if on_map && ran_on != Some(s.project.active_map) { + result = Err(format!( + "{} ran on the map '{}', which was deleted or is no longer the active one: open it and run the tool again", + run.name, run.map.1 + )); + } + // A selection's rows of a streamed layer read whole are found by the name it was read under, + // whatever the layer is called now. + let read_as = output_layer(&result); + let mut full_ids = full_ids.unwrap_or(run.full_ids); + fit_to_changes(s, &mut result, &run.name, &run.read, &full_ids); + if let (Some(was), Some(now)) = (read_as, output_layer(&result)) + && was != now + && let Some(ids) = full_ids.remove(&was) + { + full_ids.insert(now, ids); + } + fit_to_windows(s, &mut result, &full_ids); s.pending.push(crate::PendingRun { id, tool: run.name, @@ -365,69 +411,216 @@ pub fn plugin_complete(s: &mut Session, id: u64, result: Result, + params: Value, +} + +#[cfg(not(target_arch = "wasm32"))] +/// What a [`ToolRun`] made, for [`tool_complete`]. +pub struct ToolDone { + result: Result, + full_ids: FullIds, +} + +#[cfg(not(target_arch = "wasm32"))] +impl ToolRun { + /// Reads the tool's inputs and runs it, blocking until it ends; `control` stops it and counts + /// its progress (see [`rustgis_analysis::control`]). + pub fn run(self, control: &std::sync::Arc) -> ToolDone { + let ToolRun { tool, snap, params, .. } = self; + let name = rustgis_analysis::tool(tool).map_or(tool, |t| t.name); + if control.is_cancelled() { + return ToolDone { result: Err(format!("{name} was cancelled before it started")), full_ids: FullIds::new() }; + } + // A bug in reading the inputs, like one in a tool, must not take the app down. + std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + let inputs = match inputs_of(snap, rustgis_analysis::tool(tool), ¶ms) { + Ok(inputs) => inputs, + Err(e) => return ToolDone { result: Err(e), full_ids: FullIds::new() }, + }; + let mut result = rustgis_analysis::run_with(tool, &inputs, ¶ms, control); + if let Ok(r) = &mut result { + r.messages.extend(inputs.messages.iter().cloned()); + } + ToolDone { result, full_ids: full_ids(&inputs.loaded) } + })) + .unwrap_or_else(|_| ToolDone { result: Err(format!("{name} failed unexpectedly")), full_ids: FullIds::new() }) + } +} + +#[cfg(not(target_arch = "wasm32"))] +/// Starts a run of a built-in tool on the active map, as `gp.run` would run it, to go on another +/// thread: its parameters checked and what it reads of the session taken now (cheaply: the +/// features and pixels are shared), its files and streamed layers read on that thread. The run's +/// result goes to [`tool_complete`], then `gp.finish {"id"}` applies it with undo and history; a +/// layer edited meanwhile is handled as for a plugin tool. Plugin tools start with +/// [`plugin_start`]. +pub fn tool_start(s: &mut Session, tool: &str, params: &Value) -> Result { + let spec = rustgis_analysis::tool(tool_id(tool).unwrap_or(tool)) + .filter(|t| !t.id.starts_with(rustgis_plugins::PREFIX)) + .ok_or_else(|| format!("no tool '{tool}'"))?; + let resolved = rustgis_analysis::resolve_params(spec, params)?; + let read = read_layers(s, spec, &resolved); + s.next_pending = s.next_pending.wrapping_add(1); + let id = s.next_pending; + let recorded_params = recorded(Some(spec), params.clone()); + let full_ids = FullIds::new(); + s.plugin_runs.push(StartedRun { + id, + name: spec.name.to_string(), + tool: spec.id.to_string(), + params: recorded_params, + t0: now_ms(), + full_ids, + read, + map: (s.map().instance_id(), s.map().name.clone()), + }); + // A run that never completes is dropped eventually. + if s.plugin_runs.len() > MAX_ASYNC_RUNS { + s.plugin_runs.remove(0); + } + Ok(ToolRun { id, tool: spec.id, snap: Snapshot::owned(s), params: params.clone() }) +} + +#[cfg(not(target_arch = "wasm32"))] +/// Whether a run from [`tool_start`] still waits for its result: opening or creating a project +/// drops the runs of the previous one. +pub fn tool_waiting(s: &Session, id: u64) -> bool { + plugin_waiting(s, id) +} + +#[cfg(not(target_arch = "wasm32"))] +/// Forgets a run from [`tool_start`] that won't complete (its thread couldn't start). +pub fn tool_abandon(s: &mut Session, id: u64) { + plugin_abandon(s, id); +} + +#[cfg(not(target_arch = "wasm32"))] +/// Finishes a run from [`tool_start`] with what [`ToolRun::run`] made: `gp.finish {"id"}` then +/// applies it like `gp.run` would, with undo and history. +pub fn tool_complete(s: &mut Session, id: u64, done: ToolDone) -> Result<(), String> { + complete(s, id, done.result, Some(done.full_ids)) +} + /// Fits a background run's result to edits made while it ran: its layer is the one it read, -/// whatever its name now; a selection of a layer that changed selects the same features (by id) -/// where they now are; and an update of a layer that changed, which would discard those edits, -/// or of one removed, is refused. +/// whatever its name now; a selection of a layer that changed, or of a streamed layer whose +/// window moved, selects the same features (by id) where they now are; and an update of a layer +/// that changed, which would discard those edits, or of one removed, is refused. A streamed +/// layer it read whole is fitted by [`fit_to_windows`]. fn fit_to_changes( s: &Session, res: &mut Result, tool: &str, read: &std::collections::HashMap, + full_ids: &FullIds, ) { let Ok(r) = res else { return }; let (Output::Selection { layer, .. } | Output::Update { layer, .. }) = &mut r.output else { return }; - let Some(was) = read.get(&layer.to_ascii_lowercase()) else { return }; + let key = layer.to_ascii_lowercase(); + let Some(was) = read.get(&key) else { return }; // The layer it read, by id: renamed perhaps, but not another of the same name. let Some(now) = s.map().layer(was.id) else { *res = Err(format!("{layer} was removed while {tool} ran")); return; }; + // Another layer with its id (on a map that took the name of the one it ran on), or the layer + // relinked to another table of its file, isn't it. + if now.dataset.source != was.source || now.linked_table() != was.table.as_deref() { + *res = Err(format!("{layer} was removed or replaced while {tool} ran")); + return; + } layer.clone_from(&now.name); - if was.fingerprint.is_some() && fingerprint(&now.dataset) == was.fingerprint { + if Read::unchanged(&was.dataset, &now.dataset) { return; } + // A selection of what it read whole (rows of the whole source) is fitted to the layer later, + // loaded since or not. A layer still streamed has rows that follow the view: an update of it + // is checked there too. + let streamed = was.streamed && now.is_streamed(); match &mut r.output { + Output::Selection { .. } if was.streamed && full_ids.contains_key(&key) => {} Output::Selection { rows, .. } => { - let ids: std::collections::HashSet = rows.iter().filter_map(|r| was.ids.get(*r)).copied().collect(); + let ids: std::collections::HashSet = rows.iter().filter_map(|r| was.dataset.features.get(*r)).map(|f| f.id).collect(); *rows = now.dataset.features.iter().enumerate().filter(|(_, f)| ids.contains(&f.id)).map(|(i, _)| i).collect(); + let gone = ids.len().saturating_sub(rows.len()); + if gone > 0 { + let place = if now.is_streamed() { "outside the part of it read for the view" } else { "deleted" }; + r.messages.push(format!( + "{gone} of the features {tool} selected in {} are no longer there ({place} while it ran) and aren't selected", + now.name + )); + } } + _ if streamed => {} + // Its rows since then can't be told from edits: they are another copy of the source. + _ if was.streamed => *res = Err(format!("{} was loaded whole or replaced while {tool} ran; run it again", now.name)), _ => *res = Err(format!("{} changed while {tool} ran; run it again", now.name)), } } +/// The layer a result selects in or updates, by lowercase name. +fn output_layer(res: &Result) -> Option { + match &res.as_ref().ok()?.output { + Output::Selection { layer, .. } | Output::Update { layer, .. } => Some(layer.to_ascii_lowercase()), + _ => None, + } +} + /// A layer a background run read, to tell whether it changed while the run went on. pub(crate) struct Read { + /// The layer, by id: renamed perhaps, but not another of the same name. id: u32, - /// Its features' ids, in order: what its rows were. - ids: Vec, - /// None when it couldn't be taken: the layer then counts as changed. - fingerprint: Option, + /// Its dataset as the run read it: a copy that shares the rows, so taking it costs nothing + /// however large the layer, and any edit since (which copies the rows held here too) shows. + /// For a streamed layer, the window in view then. + dataset: Dataset, + /// Whether it was read in place from its file, its rows following the view. + streamed: bool, + /// Where its data comes from: a layer with the same id and another source is another layer. + source: Option, + /// The table of that file it reads: a repair can relink it to another. + table: Option, } impl Read { fn of(l: &rustgis_map::Layer) -> Self { - Read { id: l.id, ids: l.dataset.features.iter().map(|f| f.id).collect(), fingerprint: fingerprint(&l.dataset) } + Read { + id: l.id, + dataset: l.dataset.clone(), + streamed: l.is_streamed(), + source: l.dataset.source.clone(), + table: l.linked_table().map(String::from), + } } -} -/// A hash of a dataset's contents, without keeping a copy of them. -fn fingerprint(d: &Dataset) -> Option { - use std::hash::Hasher; - struct W(std::hash::DefaultHasher); - impl std::io::Write for W { - fn write(&mut self, b: &[u8]) -> std::io::Result { - self.0.write(b); - Ok(b.len()) - } - fn flush(&mut self) -> std::io::Result<()> { - Ok(()) - } + /// Whether `now` is the dataset `was` as it was read: the same rows in memory, and the same + /// fields, geometry type, coordinate system and source. + fn unchanged(was: &Dataset, now: &Dataset) -> bool { + now.features.shares(&was.features) + && now.fields == was.fields + && now.geometry_type == was.geometry_type + && now.crs == was.crs + && now.crs_incomplete == was.crs_incomplete + && now.zm_dropped == was.zm_dropped + && now.source == was.source } - let mut w = W(std::hash::DefaultHasher::new()); - // A hash of part of it would say too little. - serde_json::to_writer(&mut w, d).ok()?; - Some(w.0.finish()) +} + +/// The layers a tool names, as it reads them now, by lowercase name. +fn read_layers(s: &Session, spec: &rustgis_analysis::ToolSpec, resolved: &Value) -> std::collections::HashMap { + layer_names(Some(spec), resolved) + .into_iter() + .filter_map(|n| s.map().layer_by_name(n)) + .map(|l| (l.name.to_ascii_lowercase(), Read::of(l))) + .collect() } /// Fits a tool's result to the map: a selection made on the complete streamed source becomes @@ -441,6 +634,12 @@ fn fit_to_windows(s: &Session, res: &mut Result = rows.iter().filter_map(|r| full.get(*r)).copied().collect(); *rows = l.dataset.features.iter().enumerate().filter(|(_, f)| ids.contains(&f.id)).map(|(i, _)| i).collect(); + let gone = ids.len().saturating_sub(rows.len()); + if gone > 0 { + // Still streamed: outside the window. Loaded whole since: deleted meanwhile. + let place = if l.is_streamed() { "are outside the part of it read for the view" } else { "were deleted while it ran" }; + r.messages.push(format!("{gone} of the features selected in {layer} {place}, and aren't selected")); + } } if let Ok(r) = res && let Output::Update { layer, .. } = &r.output @@ -460,10 +659,42 @@ fn finish(s: &mut Session, p: &Value) -> CmdResult { apply(s, Ran { name: r.tool, id: r.tool_id, command: None, started: r.started }, r.params, r.result, r.millis) } +/// What a tool reads of the session: the active map, the standalone tables, the geoprocessing +/// environment and the view's size. A run on the session's own thread borrows them; one on +/// another thread owns a copy taken when it starts, which shares the features and pixels. +struct Snapshot<'a> { + map: std::borrow::Cow<'a, rustgis_map::Map>, + tables: std::borrow::Cow<'a, [crate::project::Table]>, + env: std::borrow::Cow<'a, crate::project::GpEnvironment>, + /// The map view's size, for its extent. + view_size: (f64, f64), +} + +impl<'a> Snapshot<'a> { + /// The session itself, for a run on its thread. + fn of(s: &'a Session) -> Snapshot<'a> { + use std::borrow::Cow::Borrowed; + Snapshot { map: Borrowed(s.map()), tables: Borrowed(&s.project.tables), env: Borrowed(&s.project.gp_env), view_size: s.view_size } + } + + /// A copy of the session for a run on another thread: cheap, as the features and pixels are + /// shared. + #[cfg(not(target_arch = "wasm32"))] + fn owned(s: &Session) -> Snapshot<'static> { + use std::borrow::Cow::Owned; + Snapshot { + map: Owned(s.map().clone()), + tables: Owned(s.project.tables.clone()), + env: Owned(s.project.gp_env.clone()), + view_size: s.view_size, + } + } +} + /// A tool's inputs on the active map. struct MapInputs<'a> { - /// The session: its active map and its standalone tables. - session: &'a Session, + /// The session as the tool started: its active map and its standalone tables. + snap: Snapshot<'a>, /// The rows of each layer (by lowercase name) the tool runs on: the selection or the /// environment's extent. rows: std::collections::HashMap>, @@ -483,7 +714,7 @@ struct MapInputs<'a> { impl MapInputs<'_> { /// The flat map's projection, built on first use. fn flat(&self) -> Option<&(String, rustgis_map::projection::Flat, f64)> { - let map = self.session.map(); + let map = &self.snap.map; self.flat .get_or_init(|| { (map.projection != rustgis_map::Projection::Globe).then(|| { @@ -500,7 +731,14 @@ impl MapInputs<'_> { } fn map_inputs<'a>(s: &'a Session, spec: Option<&rustgis_analysis::ToolSpec>, params: &Value) -> Result, String> { - let full = streamed_inputs(s.map(), spec, params)?; + inputs_of(Snapshot::of(s), spec, params) +} + +/// A tool's inputs from what it read of the session: the streamed layers and files it names read +/// whole, and the rows of each layer it runs on. It reads, so a background run does this on its +/// thread. +fn inputs_of<'a>(snap: Snapshot<'a>, spec: Option<&rustgis_analysis::ToolSpec>, params: &Value) -> Result, String> { + let full = streamed_inputs(&snap.map, spec, params)?; let messages = Vec::new(); #[cfg(not(target_arch = "wasm32"))] let mut messages = messages; @@ -510,7 +748,7 @@ fn map_inputs<'a>(s: &'a Session, spec: Option<&rustgis_analysis::ToolSpec>, par if let Some(spec) = spec.filter(|t| rustgis_analysis::external_source(t).is_none()) { for parameter in spec.params.iter().filter(|p| p.kind == rustgis_analysis::ParamKind::VectorLayers) { for name in rustgis_analysis::layer_list(params.get(parameter.name).unwrap_or(&Value::Null))? { - if s.map().layer_by_name(name).is_none() && !full.contains_key(name) { + if snap.map.layer_by_name(name).is_none() && !full.contains_key(name) { let (dataset, report) = rustgis_data::io::read_path(std::path::Path::new(name)).map_err(|e| format!("{name}: {e}"))?; messages.extend(report.messages.into_iter().map(|message| format!("{name}: {message}"))); full.insert(name.to_string(), dataset); @@ -518,9 +756,9 @@ fn map_inputs<'a>(s: &'a Session, spec: Option<&rustgis_analysis::ToolSpec>, par } } } - let mut rows = env_inputs(s.map(), &full, &s.project.gp_env, params)?; + let mut rows = env_inputs(&snap.map, &full, &snap.env, params)?; for (name, dataset) in &full { - let Some(layer) = s.map().layer_by_name(name).filter(|l| !l.selection.is_empty()) else { continue }; + let Some(layer) = snap.map.layer_by_name(name).filter(|l| !l.selection.is_empty()) else { continue }; let missing = || format!("{}: a selected feature is no longer in the source", layer.name); let mut by_id = layer .selection @@ -539,7 +777,7 @@ fn map_inputs<'a>(s: &'a Session, spec: Option<&rustgis_analysis::ToolSpec>, par .collect::, _>>()?; rows.insert(name.clone(), selected); } - Ok(MapInputs { session: s, rows, loaded: full, messages, flat: std::cell::OnceCell::new(), view: std::cell::OnceCell::new() }) + Ok(MapInputs { snap, rows, loaded: full, messages, flat: std::cell::OnceCell::new(), view: std::cell::OnceCell::new() }) } /// Complete streamed inputs without selections, plus selected PMTiles inputs: that exporter @@ -656,7 +894,7 @@ impl Inputs for MapInputs<'_> { Ok((out.bytes, out.messages)) } fn raster_to_pmtiles(&self, layer: &str, min_zoom: u8, max_zoom: u8) -> Result, String> { - let map = self.session.map(); + let map = &self.snap.map; let layer = map.layer_by_name(layer).ok_or_else(|| format!("no raster layer named '{layer}'"))?; let raster = layer.raster.as_ref().ok_or_else(|| format!("no raster layer named '{}'", layer.name))?; let opacity = map.layer_opacity(layer); @@ -666,19 +904,15 @@ impl Inputs for MapInputs<'_> { self.loaded .get(layer) .or_else(|| self.loaded.get(&layer.to_ascii_lowercase())) - .or_else(|| self.session.map().layer_by_name(layer).map(|l| &l.dataset)) + .or_else(|| self.snap.map.layer_by_name(layer).map(|l| &l.dataset)) // A table as commands find one: by its name, else (names can repeat) by its id. - .or_else(|| { - crate::project::find_table(&self.session.project.tables, layer) - .and_then(|i| self.session.project.tables.get(i)) - .map(|t| &t.dataset) - }) + .or_else(|| crate::project::find_table(&self.snap.tables, layer).and_then(|i| self.snap.tables.get(i)).map(|t| &t.dataset)) } fn table(&self, id: u32) -> Option<&Dataset> { - self.session.project.tables.iter().find(|t| t.id == id).map(|t| &t.dataset) + self.snap.tables.iter().find(|t| t.id == id).map(|t| &t.dataset) } fn selection(&self, layer: &str) -> &[usize] { - let Some(l) = self.session.map().layer_by_name(layer) else { return &[] }; + let Some(l) = self.snap.map.layer_by_name(layer) else { return &[] }; // Loaded selections and environment extents are already rows of the dataset returned // above. An extent containing none is refused before the run by `env_inputs`. self.rows.get(&l.name.to_ascii_lowercase()).map(Vec::as_slice).unwrap_or(&l.selection) @@ -693,14 +927,14 @@ impl Inputs for MapInputs<'_> { fn view_extent(&self) -> Option { *self.view.get_or_init(|| { // The globe shows the whole Earth (the camera's flat extent means nothing there). - if self.session.map().projection.is_globe() { + if self.snap.map.projection.is_globe() { return Some(rustgis_geom::Extent::new(-180.0, -90.0, 180.0, 90.0)); } - Some(super::workspace::view_extent(self.session)).filter(|e| !e.is_empty()) + Some(super::workspace::view_extent_of(&self.snap.map, self.snap.view_size)).filter(|e| !e.is_empty()) }) } fn raster(&self, layer: &str) -> Option> { - let rl = self.session.map().layer_by_name(layer)?.raster.as_ref()?; + let rl = self.snap.map.layer_by_name(layer)?.raster.as_ref()?; Some(RasterInput { raster: rl.data.as_deref()?, path: rl.path.as_deref(), @@ -1027,6 +1261,8 @@ fn apply(s: &mut Session, ran: Ran, params: Value, res: Result { let l = s.map_mut().layers.iter_mut().find(|l| l.name.eq_ignore_ascii_case(&layer)).ok_or("the input layer disappeared")?; l.dataset = dataset; + // Under the layer's name now: a background run's layer may have been renamed meanwhile. + l.dataset.name.clone_from(&l.name); // A streamed layer updated by a tool holds the whole result in memory from now on. l.dataset.windowed = false; l.source = None; diff --git a/crates/engine/src/cmd/items.rs b/crates/engine/src/cmd/items.rs index ad7d6794e1..aeda61248f 100644 --- a/crates/engine/src/cmd/items.rs +++ b/crates/engine/src/cmd/items.rs @@ -460,6 +460,7 @@ fn import_maps(s: &mut Session, p: &Value) -> CmdResult { m.name = format!("{base} ({i})"); } names.push(m.name.clone()); + m.renew_instance_id(); s.project.maps.push(m); } // Repair what was read (rasters reopen, cameras clamp); the sources that read S3 wait for diff --git a/crates/engine/src/cmd/navigate.rs b/crates/engine/src/cmd/navigate.rs index 79de67e9e5..8d6041c1e0 100644 --- a/crates/engine/src/cmd/navigate.rs +++ b/crates/engine/src/cmd/navigate.rs @@ -269,6 +269,7 @@ fn duplicate_map(s: &mut Session, p: &Value) -> CmdResult { let mut m = s.project.maps.get(i).ok_or("no map")?.clone(); m.name = super::copy_name(ps(p, "name"), &m.name, |n| s.project.maps.iter().any(|x| x.name.eq_ignore_ascii_case(n))); m.label_lock = None; + m.renew_instance_id(); for l in &mut m.layers { l.renew_instance_id(); l.selection.clear(); diff --git a/crates/engine/src/cmd/workspace.rs b/crates/engine/src/cmd/workspace.rs index 7ac15a104a..5b707550d2 100644 --- a/crates/engine/src/cmd/workspace.rs +++ b/crates/engine/src/cmd/workspace.rs @@ -268,6 +268,7 @@ pub fn commands() -> Vec { } else { let mut m = a.data.clone(); m.name = map_name; + m.renew_instance_id(); // Reading a project checks only maps' layers, not offline snapshots', so their // selections are put right here. for l in &mut m.layers { @@ -340,8 +341,11 @@ pub(crate) fn fill_cache(s: &mut Session) -> usize { /// The longitude/latitude extent the flat map's view shows. pub fn view_extent(s: &Session) -> Extent { - let m = s.map(); - let (w, h) = s.view_size; + view_extent_of(s.map(), s.view_size) +} + +/// The longitude/latitude extent a `size` view of map `m` shows. +pub fn view_extent_of(m: &rustgis_map::Map, (w, h): (f64, f64)) -> Extent { let e = m.camera.extent(w, h); let mut ll = Extent::EMPTY; for i in 0..=8 { @@ -727,6 +731,11 @@ fn offline_sync(s: &mut Session, p: &Value) -> CmdResult { let name = m.name.clone(); *m = fresh.clone(); m.name = name; + // A copy of the source map: a map, and layers, of its own. + m.renew_instance_id(); + for l in &mut m.layers { + l.renew_instance_id(); + } } if let Some(a) = s.project.offline_areas.get_mut(ai) { a.data = fresh; diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 06dacf0ba6..c7a057fb2b 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -6917,6 +6917,15 @@ fn offline_sync_rejects_incompatible_fields_and_preserves_safe_refreshes() { let offline = &s.project.maps.iter().find(|m| m.name == "Fieldwork (offline)").unwrap().layer_by_name("Assets").unwrap().dataset; assert_eq!(offline.fields, source.fields, "{mode}"); assert_eq!(offline.features, source.features, "{mode}"); + // The synced copy is a map, with layers, of its own (a background run finds its map by it). + let ids: Vec = s.project.maps.iter().map(|m| m.instance_id()).collect(); + assert!(ids.iter().enumerate().all(|(i, a)| ids[..i].iter().all(|b| a != b)), "{mode}: {ids:?}"); + let offline_map = s.project.maps.iter().find(|m| m.name == "Fieldwork (offline)").unwrap(); + assert_ne!( + offline_map.layer_by_name("Assets").unwrap().instance_id(), + s.map().layer_by_name("Assets").unwrap().instance_id(), + "{mode}" + ); } for mode in ["source_deleted", "geometry_only", "offline_added", "source_reordered", "source_type_changed", "empty_offline_schema"] { @@ -23730,3 +23739,277 @@ fn a_cancelled_tool_leaves_the_map_as_it_was_and_says_so_in_the_history() { crate::cmd::gp::run_tool(&mut s, "idwInterpolation", ¶ms).unwrap_or_else(|e| panic!("{e}")); assert_eq!(s.map().layers.len(), layers + 1); } + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn built_in_tools_run_off_the_session() { + use crate::cmd::gp::{tool_abandon, tool_complete, tool_start, tool_waiting}; + use rustgis_analysis::control::Control; + fn sends(_: &T) {} + let mut s = world(); + // Started: what it reads is taken now; it runs elsewhere while the project goes on. + let run = tool_start(&mut s, "buffer", &json!({"input": "Cities", "distance": 50})).unwrap(); + assert_eq!(run.tool, "buffer"); + sends(&run); + let id = run.id; + assert!(tool_waiting(&s, id)); + let made = std::thread::spawn(move || run.run(&Control::new())); + s.run("layer.remove", &json!({"layer": "Cities"})).unwrap(); + let done = made.join().unwrap(); + tool_complete(&mut s, id, done).unwrap(); + // Applied as gp.run applies a run: its layer, its history, undo. + let before = s.map().layers.len(); + s.run("gp.finish", &json!({"id": id})).unwrap(); + assert_eq!(s.map().layers.len(), before + 1); + let h = s.project.gp_history.last().unwrap(); + assert!(h.ok && h.tool == "Buffer" && h.params["input"] == "Cities", "{h:?}"); + s.run("edit.undo", &Value::Null).unwrap(); + assert_eq!(s.map().layers.len(), before); + // Cancelled before it starts (a cancel while it runs is the analysis crate's test): it fails, + // saying so, and adds nothing (a failed command leaves the project as it was, as a failed + // gp.run does). + let mut s = world(); + let layers = s.map().layers.len(); + let run = tool_start(&mut s, "idwInterpolation", &json!({"input": "Cities", "field": "pop_max", "pixelSize": 5.0})).unwrap(); + let control = Control::new(); + control.cancel(); + let id = run.id; + let done = run.run(&control); + tool_complete(&mut s, id, done).unwrap(); + let e = s.run("gp.finish", &json!({"id": id})).unwrap_err(); + assert_eq!(e, "IDW Interpolation was cancelled before it started"); + assert_eq!(s.map().layers.len(), layers); + // Inputs it can't read fail its run, in the history, rather than its start. + let missing = std::env::temp_dir().join(format!("rustgis-no-such-bg-{}.geojson", std::process::id())); + let run = tool_start(&mut s, "merge", &json!({"inputs": [missing.display().to_string(), "Cities"]})).unwrap(); + let id = run.id; + let done = run.run(&Control::new()); + tool_complete(&mut s, id, done).unwrap(); + let e = s.run("gp.finish", &json!({"id": id})).unwrap_err(); + assert!(e.starts_with(&missing.display().to_string()), "{e}"); + // Parameters are checked when it starts; plugin tools start their own way; a run completes once. + assert!(tool_start(&mut s, "buffer", &json!({})).err().unwrap().contains("required")); + assert!(tool_start(&mut s, "plugin:none/x", &json!({})).err().unwrap().contains("no tool")); + let again = tool_start(&mut s, "buffer", &json!({"input": "Cities", "distance": 50})).unwrap(); + let id = again.id; + tool_complete(&mut s, id, again.run(&Control::new())).unwrap(); + let late = tool_start(&mut s, "buffer", &json!({"input": "Cities", "distance": 50})).unwrap(); + let done = late.run(&Control::new()); + assert!(tool_complete(&mut s, id, done).unwrap_err().contains("dropped"), "completed once"); + s.run("gp.finish", &json!({"id": id})).unwrap(); + // A run whose map is no longer the active one adds nothing to the one that is, and says so. + s.run("map.new", &json!({"name": "Second"})).unwrap(); + s.project.active_map = 0; + let run = tool_start(&mut s, "buffer", &json!({"input": "Cities", "distance": 50})).unwrap(); + s.project.active_map = 1; + let layers = s.map().layers.len(); + let (id, done) = (run.id, run.run(&Control::new())); + tool_complete(&mut s, id, done).unwrap(); + let e = s.run("gp.finish", &json!({"id": id})).unwrap_err(); + assert!(e.contains("no longer the active one"), "{e}"); + assert_eq!(s.map().layers.len(), layers); + // A tool that rewrites its layer applies when the layer is as it read it, and is refused when + // it was edited meanwhile, even only in its fields (the rows, however many, aren't compared). + let mut s = world(); + let run = tool_start(&mut s, "calculateGeometry", &json!({"input": "Countries"})).unwrap(); + let (id, done) = (run.id, run.run(&Control::new())); + tool_complete(&mut s, id, done).unwrap(); + s.run("gp.finish", &json!({"id": id})).unwrap(); + // Renamed meanwhile, the layer it read is updated, under its new name. + let run = tool_start(&mut s, "calculateGeometry", &json!({"input": "Countries"})).unwrap(); + s.run("layer.rename", &json!({"layer": "Countries", "name": "Nations"})).unwrap(); + let (id, done) = (run.id, run.run(&Control::new())); + tool_complete(&mut s, id, done).unwrap(); + s.run("gp.finish", &json!({"id": id})).unwrap(); + let nations = s.map().layer_by_name("Nations").unwrap(); + assert_eq!(nations.dataset.name, "Nations"); + s.run("layer.rename", &json!({"layer": "Nations", "name": "Countries"})).unwrap(); + let run = tool_start(&mut s, "calculateGeometry", &json!({"input": "Countries"})).unwrap(); + let countries = s.map_mut().layers.iter_mut().find(|l| l.name == "Countries").unwrap(); + countries.dataset.fields[0].name = "renamed".into(); + let (id, done) = (run.id, run.run(&Control::new())); + tool_complete(&mut s, id, done).unwrap(); + let e = s.run("gp.finish", &json!({"id": id})).unwrap_err(); + assert!(e.contains("changed while"), "{e}"); + // Opening another project drops its runs; one whose thread couldn't start is forgotten. + let run = tool_start(&mut s, "buffer", &json!({"input": "Cities", "distance": 50})).unwrap(); + s.run("project.new", &Value::Null).unwrap(); + assert!(!tool_waiting(&s, run.id)); + let mut s = world(); + let run = tool_start(&mut s, "buffer", &json!({"input": "Cities", "distance": 50})).unwrap(); + tool_abandon(&mut s, run.id); + assert!(!tool_waiting(&s, run.id)); +} + +/// A background selection on a streamed layer renamed while it ran selects the features it chose +/// where they are in the layer's window; a run whose map moved (one before it was deleted) still +/// lands on it. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn background_runs_follow_renamed_streamed_layers_and_moved_maps() { + use crate::cmd::gp::{tool_complete, tool_start}; + use rustgis_analysis::control::Control; + let path = grid_fgb("bgrenamed", 50); + let mut s = Session::new(Project::default()); + s.run("layer.addData", &json!({"path": path.display().to_string(), "stream": true})).unwrap(); + // Only a corner is in view: the window holds part of the source, in its own order. + let v = view_of(&mut s, rustgis_geom::Extent::new(0.0, 0.0, 20.0, 20.0)); + crate::render::render_png(s.map_mut(), &v).unwrap(); + assert!(s.map().layers[0].dataset.features.len() < 2500, "only part of the source is in the window"); + let run = tool_start(&mut s, "selectByAttributes", &json!({"input": "bgrenamed", "where": "x > 0 AND x < 20"})).unwrap(); + s.run("layer.rename", &json!({"layer": "bgrenamed", "name": "Renamed"})).unwrap(); + let (id, done) = (run.id, run.run(&Control::new())); + tool_complete(&mut s, id, done).unwrap(); + s.run("gp.finish", &json!({"id": id})).unwrap(); + let l = s.map().layer_by_name("Renamed").unwrap(); + let x = |f: &rustgis_data::Feature| f.attrs.get(1).and_then(|v| v.as_f64()).unwrap_or(f64::NAN); + let mut selected: Vec = l.selection.iter().map(|&r| l.dataset.features[r].id).collect(); + let mut expected: Vec = l.dataset.features.iter().filter(|f| x(f) > 0.0 && x(f) < 20.0).map(|f| f.id).collect(); + selected.sort_unstable(); + expected.sort_unstable(); + assert!(!expected.is_empty() && selected == expected, "{selected:?} vs {expected:?}"); + // What it selected outside the window isn't selected, and that is said. + let said = &s.project.gp_history.last().unwrap().messages; + assert!(said.iter().any(|m| m.contains("outside the part of it read for the view")), "{said:?}"); + let _ = std::fs::remove_file(path); + // A map deleted before the run's own map moves it up: the result still lands there. + let mut s = world(); + s.project.maps.insert(0, rustgis_map::Map::new("Before")); + s.project.active_map = 1; + let layers = s.map().layers.len(); + let run = tool_start(&mut s, "buffer", &json!({"input": "Cities", "distance": 50})).unwrap(); + s.run("map.remove", &json!({"name": "Before"})).unwrap(); + assert_eq!(s.project.active_map, 0); + let (id, done) = (run.id, run.run(&Control::new())); + tool_complete(&mut s, id, done).unwrap(); + s.run("gp.finish", &json!({"id": id})).unwrap(); + assert_eq!(s.map().layers.len(), layers + 1); + // Its map deleted and another given its name, holding a layer of the same id from another + // source: that isn't the layer it read, and its selection doesn't land there. + let path = grid_fgb("bgother", 5); + let mut s = world(); + s.run("map.new", &json!({"name": "Ran"})).unwrap(); + s.project.active_map = s.project.maps.iter().position(|m| m.name == "Ran").unwrap(); + s.run("layer.addData", &json!({"path": path.display().to_string(), "stream": true, "name": "Points"})).unwrap(); + let read_id = s.map().layer_by_name("Points").unwrap().id; + let run = tool_start(&mut s, "selectByAttributes", &json!({"input": "Points", "where": "i >= 0"})).unwrap(); + s.run("map.remove", &json!({"name": "Ran"})).unwrap(); + s.run("map.new", &json!({"name": "Ran"})).unwrap(); + s.project.active_map = s.project.maps.iter().position(|m| m.name == "Ran").unwrap(); + let other = grid_fgb("bgother2", 5); + s.run("layer.addData", &json!({"path": other.display().to_string(), "stream": true, "name": "Points"})).unwrap(); + assert_eq!(s.map().layer_by_name("Points").unwrap().id, read_id, "the same id, on a map of the same name"); + let (id, done) = (run.id, run.run(&Control::new())); + tool_complete(&mut s, id, done).unwrap(); + let e = s.run("gp.finish", &json!({"id": id})).unwrap_err(); + assert!(e.contains("was deleted or is no longer the active one"), "{e}"); + assert!(s.map().layer_by_name("Points").unwrap().selection.is_empty()); + let _ = (std::fs::remove_file(path), std::fs::remove_file(other)); +} + +/// A background run's result lands on its map renamed while it ran; its selection of a streamed +/// layer whose window was read again meanwhile selects the same features, saying how many left +/// it; and its update of a streamed layer loaded whole and edited meanwhile is refused. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn background_runs_follow_a_renamed_map_a_moved_window_and_a_loaded_layer() { + use crate::cmd::gp::{tool_complete, tool_start}; + use rustgis_analysis::control::Control; + let mut s = world(); + let layers = s.map().layers.len(); + let run = tool_start(&mut s, "buffer", &json!({"input": "Cities", "distance": 50})).unwrap(); + s.run("map.rename", &json!({"name": "Renamed while it ran"})).unwrap(); + let (id, done) = (run.id, run.run(&Control::new())); + tool_complete(&mut s, id, done).unwrap(); + s.run("gp.finish", &json!({"id": id})).unwrap(); + assert_eq!(s.map().layers.len(), layers + 1); + // Renamed and the rename undone: the same map. + let layers = s.map().layers.len(); + let run = tool_start(&mut s, "buffer", &json!({"input": "Cities", "distance": 50})).unwrap(); + s.run("map.rename", &json!({"name": "Renamed again"})).unwrap(); + s.run("edit.undo", &Value::Null).unwrap(); + let (id, done) = (run.id, run.run(&Control::new())); + tool_complete(&mut s, id, done).unwrap(); + s.run("gp.finish", &json!({"id": id})).unwrap(); + assert_eq!(s.map().layers.len(), layers + 1); + // Deleted, and another map made with its name: the new layer doesn't land there. + s.run("map.new", &json!({"name": "Other"})).unwrap(); + let ran = s.project.maps[0].name.clone(); + s.project.active_map = 0; + let run = tool_start(&mut s, "buffer", &json!({"input": "Cities", "distance": 50})).unwrap(); + s.run("map.remove", &json!({"name": ran})).unwrap(); + s.run("map.new", &json!({"name": ran})).unwrap(); + s.project.active_map = s.project.maps.iter().position(|m| m.name == ran).unwrap(); + let (id, done) = (run.id, run.run(&Control::new())); + tool_complete(&mut s, id, done).unwrap(); + let e = s.run("gp.finish", &json!({"id": id})).unwrap_err(); + assert!(e.contains("was deleted or is no longer the active one"), "{e}"); + assert!(s.map().layers.is_empty()); + // A selection in the window (so the tool reads the window), then the view moves. + let path = grid_fgb("bgwindow", 50); + let mut s = Session::new(Project::default()); + s.run("layer.addData", &json!({"path": path.display().to_string(), "stream": true})).unwrap(); + let v = view_of(&mut s, rustgis_geom::Extent::new(0.0, 0.0, 20.0, 20.0)); + crate::render::render_png(s.map_mut(), &v).unwrap(); + let ids = |s: &Session| -> Vec { s.map().layers[0].dataset.features.iter().map(|f| f.id).collect() }; + let x = |f: &rustgis_data::Feature| f.attrs.get(1).and_then(|v| v.as_f64()).unwrap_or(f64::NAN); + let picked: std::collections::HashSet = + s.map().layers[0].dataset.features.iter().filter(|f| x(f) > 0.0 && x(f) < 20.0).map(|f| f.id).collect(); + let before = ids(&s); + let n = before.len(); + s.map_mut().layers[0].selection = (0..n).collect(); + let run = tool_start(&mut s, "selectByAttributes", &json!({"input": "bgwindow", "where": "x > 0 AND x < 20"})).unwrap(); + // The view moved: the window, read again, holds part of what it did, in another order. + let l = &mut s.map_mut().layers[0]; + l.dataset.features = l.dataset.features.iter().rev().filter(|f| f.id % 3 != 0).cloned().collect(); + l.selection.clear(); + let after = ids(&s); + let (id, done) = (run.id, run.run(&Control::new())); + tool_complete(&mut s, id, done).unwrap(); + s.run("gp.finish", &json!({"id": id})).unwrap(); + let l = &s.map().layers[0]; + let mut selected: Vec = l.selection.iter().map(|&r| l.dataset.features[r].id).collect(); + let mut expected: Vec = after.iter().copied().filter(|i| picked.contains(i)).collect(); + selected.sort_unstable(); + expected.sort_unstable(); + assert!(!expected.is_empty() && expected.len() < picked.len(), "{} of {}", expected.len(), picked.len()); + assert_eq!(selected, expected); + let said = &s.project.gp_history.last().unwrap().messages; + assert!(said.iter().any(|m| m.contains("no longer there")), "{said:?}"); + // Streamed and read whole, then loaded whole while the tool ran, and one of the features it + // selects deleted: its selection, rows of the whole source, lands on the others, and says so. + s.map_mut().layers[0].selection.clear(); + let mut t = Session::new(Project::default()); + t.run("layer.addData", &json!({"path": path.display().to_string(), "stream": true})).unwrap(); + let v = view_of(&mut t, rustgis_geom::Extent::new(0.0, 0.0, 20.0, 20.0)); + crate::render::render_png(t.map_mut(), &v).unwrap(); + let run = tool_start(&mut t, "selectByAttributes", &json!({"input": "bgwindow", "where": "i < 100"})).unwrap(); + t.run("layer.loadAll", &json!({"layer": t.map().layers[0].id})).unwrap(); + // One of the features it selects is deleted meanwhile. + let i = |f: &rustgis_data::Feature| f.attrs.first().and_then(|v| v.as_f64()).unwrap_or(f64::NAN); + let first = t.map().layers[0].dataset.features.iter().position(|f| i(f) == 0.0).unwrap(); + t.map_mut().layers[0].selection = vec![first]; + t.run("edit.enable", &json!({"on": true})).unwrap(); + t.run("edit.delete", &Value::Null).unwrap(); + let (id, done) = (run.id, run.run(&Control::new())); + tool_complete(&mut t, id, done).unwrap(); + t.run("gp.finish", &json!({"id": id})).unwrap(); + let l = &t.map().layers[0]; + assert!(!l.is_streamed()); + assert_eq!(l.selection.len(), 99); + assert!(l.selection.iter().all(|&r| i(&l.dataset.features[r]) < 100.0 && i(&l.dataset.features[r]) != 0.0)); + let said = &t.project.gp_history.last().unwrap().messages; + assert!(said.iter().any(|m| m.contains("1 of the features selected in bgwindow were deleted while it ran")), "{said:?}"); + // Streamed and read whole, then loaded whole and edited while the tool ran: its update + // would discard the edit. + let run = tool_start(&mut s, "calculateGeometry", &json!({"input": "bgwindow"})).unwrap(); + s.run("layer.loadAll", &json!({"layer": s.map().layers[0].id})).unwrap(); + s.run("edit.enable", &json!({"on": true})).unwrap(); + s.run("edit.setValue", &json!({"layer": "bgwindow", "row": 0, "field": "i", "value": -1})).unwrap(); + let (id, done) = (run.id, run.run(&Control::new())); + tool_complete(&mut s, id, done).unwrap(); + let e = s.run("gp.finish", &json!({"id": id})).unwrap_err(); + assert!(e.contains("loaded whole or replaced while"), "{e}"); + assert!(s.map().layers[0].dataset.features.iter().any(|f| f.attrs.first().and_then(|v| v.as_f64()) == Some(-1.0))); + let _ = std::fs::remove_file(path); +} diff --git a/crates/map/src/lib.rs b/crates/map/src/lib.rs index 44454f706b..4ff1a98475 100644 --- a/crates/map/src/lib.rs +++ b/crates/map/src/lib.rs @@ -1244,6 +1244,9 @@ pub struct Map { pub body: String, #[serde(default)] next_layer_id: u32, + /// Process-local identity, kept by undo snapshots but never stored in a project. + #[serde(skip, default = "next_layer_instance")] + instance_id: u64, } fn earth() -> String { @@ -1350,9 +1353,20 @@ impl Map { time: None, body: earth(), next_layer_id: 1, + instance_id: next_layer_instance(), } } + /// Identifies this live map, whatever its name or place among the project's maps. + pub fn instance_id(&self) -> u64 { + self.instance_id + } + + /// Starts a distinct live map after copying one. Undo snapshots keep the original identity. + pub fn renew_instance_id(&mut self) { + self.instance_id = next_layer_instance(); + } + /// Every address the map keeps to read its data again (service groups and their layers' /// sources, tile layers and their dated addresses, terrain tiles, layers' dataset, streamed and /// raster sources, time-following Zarr stores). Local paths come too: callers judge each one. diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index d1801985a9..e223f2129a 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -1883,6 +1883,20 @@ impl RustGisApp { /// made. /// `active` is the active layer's name from [`Self::layer_acted_on`], read before it ran. fn record_step(&mut self, id: &str, params: &Value, active: Option<(String, bool)>) { + if self.ws.recording.is_none() { + return; + } + // A tool run in the background is applied by `gp.finish`, whose run id means nothing to a + // replay: the step is the run itself, `gp.run` with its tool and parameters, from the + // history entry it has just added. + let finished; + let (id, params) = if id == "gp.finish" { + let Some(h) = self.session.project.gp_history.last().filter(|h| !h.id.is_empty()) else { return }; + finished = json!({"tool": h.id, "params": h.params}); + ("gp.run", &finished) + } else { + (id, params) + }; let Some(steps) = self.ws.recording.as_mut() else { return }; // Undo doesn't take back a recorded step (the undo history and the recording don't line // up step for step): say how to. @@ -3764,6 +3778,8 @@ impl RustGisApp { rustgis_engine::plugins::set_language(self.lang().code()); panes::poll_geolibre(self, ctx); panes::poll_plugin_run(self, ctx); + #[cfg(not(target_arch = "wasm32"))] + panes::poll_tool_run(self, ctx); self.poll_downloads(); let _ = self.deliver_gp_files(Ok(Value::Null)); // A value picked under Dynamic Text lasts while the text tool does, however the tool @@ -4273,6 +4289,30 @@ mod record_tests { assert_eq!(app.ws.recording.as_ref().map(Vec::len), Some(1)); } + /// A background run's `gp.finish` is recorded as the `gp.run` it finished. + #[test] + fn a_tool_finished_in_the_background_is_recorded_as_its_run() { + let mut app = RustGisApp::with_sample(); + app.ws.recording = Some(Vec::new()); + app.session.project.gp_history.push(rustgis_engine::GpRecord { + tool: "Buffer".into(), + id: "buffer".into(), + params: json!({"input": "Cities", "distance": 50}), + command: None, + at: 0, + ok: true, + messages: Vec::new(), + millis: 0, + outputs: Vec::new(), + }); + app.record_step("gp.finish", &json!({"id": 7}), None); + let steps = app.ws.recording.as_deref().unwrap_or_default(); + assert_eq!( + steps.iter().map(|s| (s.command.as_str(), s.params.clone())).collect::>(), + [("gp.run", json!({"tool": "buffer", "params": {"input": "Cities", "distance": 50}}))] + ); + } + /// A step keeping a plain `key` is recorded, and that is said once a recording. #[test] fn a_kept_key_is_said_once() { diff --git a/crates/ui-egui/src/panes.rs b/crates/ui-egui/src/panes.rs index 0ce6bdb04b..3f24ee2a2f 100644 --- a/crates/ui-egui/src/panes.rs +++ b/crates/ui-egui/src/panes.rs @@ -2648,6 +2648,9 @@ pub struct GpForm { pub web_run: Option<(u64, &'static str, crate::Slot)>, /// A plugin tool running on a thread of its own, so the window stays responsive. pub plugin_run: Option, + /// A built-in tool running on a thread of its own, likewise (the browser runs them at once). + #[cfg(not(target_arch = "wasm32"))] + pub tool_run: Option, /// Files picked in the browser for file parameters, which have no paths there: (name, /// contents), sent with the run while the parameter still shows that name. pub files: HashMap<&'static str, (String, Vec)>, @@ -2671,6 +2674,69 @@ pub struct PluginJob { pub control: std::sync::Arc, } +/// A built-in tool running on a thread of its own. +#[cfg(not(target_arch = "wasm32"))] +pub struct ToolJob { + pub id: u64, + pub tool: &'static str, + /// What the run made, once it has. + pub slot: crate::Slot, + /// Stops the run, and counts its progress. + pub control: std::sync::Arc, +} + +/// Starts a built-in tool on a thread of its own; [`poll_tool_run`] applies what it made. +#[cfg(not(target_arch = "wasm32"))] +fn start_tool_run(app: &mut RustGisApp, tool: &'static str, params: &Value, ctx: &egui::Context) { + match rustgis_engine::cmd::gp::tool_start(&mut app.session, tool, params) { + Ok(run) => { + let slot = crate::Slot::default(); + let control = rustgis_engine::analysis::control::Control::new(); + let (out, ctl, ctx) = (slot.clone(), control.clone(), ctx.clone()); + let id = run.id; + let spawned = std::thread::Builder::new().name(tool.into()).spawn(move || { + let done = run.run(&ctl); + // Filled even when poisoned, or the pane would wait for it forever. + *out.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = Some(done); + ctx.request_repaint(); + }); + match spawned { + Ok(_) => { + app.gp.tool_run = Some(ToolJob { id, tool, slot, control }); + app.gp.last = None; + } + Err(e) => { + rustgis_engine::cmd::gp::tool_abandon(&mut app.session, id); + app.gp.last = Some((false, vec![e.to_string()])); + } + } + } + Err(e) => app.gp.last = Some((false, vec![e])), + } +} + +/// Applies a built-in tool run that finished on its thread. +#[cfg(not(target_arch = "wasm32"))] +pub fn poll_tool_run(app: &mut RustGisApp, ctx: &egui::Context) { + let Some(job) = &app.gp.tool_run else { return }; + // Its project was closed: stop it, as its result has nowhere to go, and say so. + if !rustgis_engine::cmd::gp::tool_waiting(&app.session, job.id) { + job.control.cancel(); + dropped(app, job.tool); + app.gp.tool_run = None; + return; + } + let Some(done) = take(&job.slot) else { + // Its progress moves as it works. + ctx.request_repaint_after(std::time::Duration::from_millis(100)); + return; + }; + let (id, tool) = (job.id, job.tool); + app.gp.tool_run = None; + let r = rustgis_engine::cmd::gp::tool_complete(&mut app.session, id, done).and_then(|_| app.run("gp.finish", json!({"id": id}))); + finished(app, tool, r); +} + /// Starts a plugin tool on a thread of its own; [`poll_plugin_run`] applies what it returns. #[cfg(not(target_arch = "wasm32"))] fn start_plugin_run(app: &mut RustGisApp, tool: &'static str, params: &Value, ctx: &egui::Context) { @@ -2704,9 +2770,10 @@ fn start_plugin_run(app: &mut RustGisApp, tool: &'static str, params: &Value, ct /// Applies a plugin tool run that finished on its thread. pub fn poll_plugin_run(app: &mut RustGisApp, ctx: &egui::Context) { let Some(job) = &app.gp.plugin_run else { return }; - // Its project was closed: stop it, as its result has nowhere to go. + // Its project was closed: stop it, as its result has nowhere to go, and say so. if !rustgis_engine::cmd::gp::plugin_waiting(&app.session, job.id) { job.control.cancel(); + dropped(app, job.tool); app.gp.plugin_run = None; return; } @@ -2721,6 +2788,12 @@ pub fn poll_plugin_run(app: &mut RustGisApp, ctx: &egui::Context) { finished(app, tool, r); } +/// Says that a tool's run was dropped: its project closed while it ran. +fn dropped(app: &mut RustGisApp, tool: &'static str) { + let name = rustgis_engine::analysis::tool(tool).map(|t| t.name).unwrap_or(tool); + app.session.notify(crate::i18n::fmt("{tool} was stopped: its project was closed while it ran.", &[("tool", tl_raw!(name))]), false); +} + /// Shows how an asynchronous tool run went: under its form when it's open, else as a notice. fn finished(app: &mut RustGisApp, tool: &'static str, r: Result) { let last = match r { @@ -3249,7 +3322,14 @@ pub(crate) fn input_table<'a>(session: &'a Session, name: &str) -> Option<&'a ru fn geoprocessing(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { let Some(spec) = app.gp.tool.and_then(rustgis_engine::analysis::tool) else { - // Tool finder. + // Tool finder, under the tool running, if one is. + let running = app.gp.web_run.is_some() || app.gp.plugin_run.is_some(); + #[cfg(not(target_arch = "wasm32"))] + let running = running || app.gp.tool_run.is_some(); + if running { + run_status(app, ui, t); + ui.add_space(8.0); + } crate::widgets::search_field(ui, &mut app.gp.search, tl!("Find Tools")); ui.separator(); ui.add_space(4.0); @@ -3673,8 +3753,10 @@ fn geoprocessing(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { } ui.add_space(8.0); ui.with_layout(egui::Layout::right_to_left(egui::Align::Min), |ui| { - // One browser run at a time, and one plugin run. + // One browser run at a time, and one background run. let idle = app.gp.web_run.is_none() && app.gp.plugin_run.is_none(); + #[cfg(not(target_arch = "wasm32"))] + let idle = idle && app.gp.tool_run.is_none(); if crate::widgets::primary_button(ui, tl!("Run"), idle, vec2(96.0, 30.0)).clicked() { // A blank output opens Save As before any work starts. Cancelling writes nothing. #[cfg(not(target_arch = "wasm32"))] @@ -3737,6 +3819,10 @@ fn geoprocessing(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { // On a thread of its own: a plugin tool may run for minutes. #[cfg(not(target_arch = "wasm32"))] start_plugin_run(app, spec.id, ¶ms, ui.ctx()); + } else if !cfg!(target_arch = "wasm32") { + // A built-in tool too, with its progress and Cancel; the browser has no threads. + #[cfg(not(target_arch = "wasm32"))] + start_tool_run(app, spec.id, ¶ms, ui.ctx()); } else { let r = app.run("gp.run", json!({"tool": spec.id, "params": params})); app.gp.last = Some(match r { @@ -3747,6 +3833,29 @@ fn geoprocessing(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { } } }); + run_status(app, ui, t); + if let Some((ok, msgs)) = &app.gp.last { + ui.add_space(8.0); + let (fill, stroke, head) = + if *ok { (t.green.gamma_multiply(0.12), t.green, "Completed") } else { (t.red.gamma_multiply(0.12), t.red, "Failed") }; + egui::Frame::NONE + .fill(fill) + .stroke(Stroke::new(1.0, stroke)) + .corner_radius(CornerRadius::same(6)) + .inner_margin(egui::Margin::same(8)) + .show(ui, |ui| { + ui.set_width(ui.available_width()); + ui.label(egui::RichText::new(tl!(head)).font(semibold(12.5)).color(stroke)); + for m in msgs { + ui.label(egui::RichText::new(m).size(11.5)); + } + }); + } +} + +/// The tool running, if one is: in the tool's form and in the finder, so that it can be cancelled +/// from either. +fn run_status(app: &RustGisApp, ui: &mut Ui, t: &Tokens) { if app.gp.web_run.is_some() { ui.add_space(8.0); ui.horizontal(|ui| { @@ -3775,22 +3884,27 @@ fn geoprocessing(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { job.control.cancel(); } } - if let Some((ok, msgs)) = &app.gp.last { + #[cfg(not(target_arch = "wasm32"))] + if let Some(job) = &app.gp.tool_run { ui.add_space(8.0); - let (fill, stroke, head) = - if *ok { (t.green.gamma_multiply(0.12), t.green, "Completed") } else { (t.red.gamma_multiply(0.12), t.red, "Failed") }; - egui::Frame::NONE - .fill(fill) - .stroke(Stroke::new(1.0, stroke)) - .corner_radius(CornerRadius::same(6)) - .inner_margin(egui::Margin::same(8)) - .show(ui, |ui| { - ui.set_width(ui.available_width()); - ui.label(egui::RichText::new(tl!(head)).font(semibold(12.5)).color(stroke)); - for m in msgs { - ui.label(egui::RichText::new(m).size(11.5)); - } + let mut cancel = false; + ui.horizontal(|ui| { + ui.spinner(); + let name = rustgis_engine::analysis::tool(job.tool).map(|t| t.name).unwrap_or(job.tool); + // Two labels, not one sentence put together from translated pieces. + ui.label(egui::RichText::new(tl!("Running…")).size(12.0)); + ui.label(egui::RichText::new(tl!(name)).size(12.0).strong()); + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + cancel = ui.add_enabled(!job.control.is_cancelled(), egui::Button::new(tl!("Cancel"))).clicked(); }); + }); + // How far its current step has come, when it counts its work. + if let Some((done, total)) = job.control.progress() { + ui.add(egui::ProgressBar::new(done as f32 / total.max(1) as f32).show_percentage().desired_height(14.0)); + } + if cancel { + job.control.cancel(); + } } } @@ -6333,6 +6447,81 @@ pub(crate) fn layout_menu(app: &mut RustGisApp, ui: &mut Ui, n: &str) { mod tests { use super::*; + /// Run starts a built-in tool on a thread of its own: the pane shows it running, with Cancel, + /// and its result lands as gp.run's would, with its layer and history. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn the_geoprocessing_pane_runs_a_tool_in_the_background() { + use egui_kittest::kittest::Queryable; + let mut app = RustGisApp::with_sample(); + let _ = app.run("ui.gp.open", json!({"tool": "buffer"})); + app.gp.values.insert("input", "Cities".into()); + app.gp.values.insert("distance", "50".into()); + app.ws.recording = Some(Vec::new()); + let layers = app.session.map().layers.len(); + let mut fonts = false; + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(420.0, 1000.0)).build_ui_state( + move |ui, app: &mut RustGisApp| { + if !fonts { + crate::theme::install_fonts(ui.ctx(), &[], None); + fonts = true; + return; + } + poll_tool_run(app, ui.ctx()); + geoprocessing(app, ui, &Tokens::light()); + }, + app, + ); + h.run(); + h.get_by_label("Run").click(); + h.step(); + assert!(h.state().gp.tool_run.is_some(), "it runs in the background"); + let end = std::time::Instant::now() + std::time::Duration::from_secs(60); + while h.state().gp.tool_run.is_some() { + assert!(std::time::Instant::now() < end, "the run never landed"); + std::thread::sleep(std::time::Duration::from_millis(10)); + h.step(); + } + let app = h.state(); + assert_eq!(app.gp.last.as_ref().map(|l| l.0), Some(true), "{:?}", app.gp.last); + assert_eq!(app.session.map().layers.len(), layers + 1); + assert_eq!(app.session.project.gp_history.last().map(|r| (r.ok, r.tool.as_str())), Some((true, "Buffer"))); + // Recorded once, as its run. + let steps = app.ws.recording.as_deref().unwrap_or_default(); + assert_eq!(steps.iter().map(|s| s.command.as_str()).collect::>(), ["gp.run"]); + assert_eq!((&steps[0].params["tool"], &steps[0].params["params"]["input"]), (&json!("buffer"), &json!("Cities"))); + } + + /// The tool running stays in view, with its Cancel button, when the pane goes back to the + /// list of tools. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn a_tool_running_in_the_background_can_be_cancelled_from_the_tool_list() { + use egui_kittest::kittest::Queryable; + let mut app = RustGisApp::with_sample(); + let run = rustgis_engine::cmd::gp::tool_start(&mut app.session, "buffer", &json!({"input": "Cities", "distance": 50})).unwrap(); + let control = rustgis_engine::analysis::control::Control::new(); + app.gp.tool_run = Some(ToolJob { id: run.id, tool: run.tool, slot: crate::Slot::default(), control: control.clone() }); + app.gp.tool = None; + let mut fonts = false; + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(420.0, 1000.0)).build_ui_state( + move |ui, app: &mut RustGisApp| { + if !fonts { + crate::theme::install_fonts(ui.ctx(), &[], None); + fonts = true; + return; + } + geoprocessing(app, ui, &Tokens::light()); + }, + app, + ); + // Its spinner keeps repainting: steps, not run. + h.run_steps(3); + h.get_by_label("Cancel").click(); + h.run_steps(2); + assert!(control.is_cancelled()); + } + #[test] fn field_source_uses_the_longest_input_prefix() { use rustgis_engine::analysis::{ParamSpec, ToolSpec}; diff --git a/crates/ui-egui/tests/tool_field_lists.rs b/crates/ui-egui/tests/tool_field_lists.rs index 086f236513..a4a30fde2f 100644 --- a/crates/ui-egui/tests/tool_field_lists.rs +++ b/crates/ui-egui/tests/tool_field_lists.rs @@ -33,6 +33,19 @@ fn harness(tool: &str, values: serde_json::Value) -> Harness<'static, RustGisApp h } +/// Clicks Run and waits for the tool to land: on the desktop it runs on a thread of its own. +fn click_run(h: &mut Harness<'_, RustGisApp>) { + h.get_by_role_and_label(egui::accesskit::Role::Button, "Run").click(); + h.run_steps(1); + let end = std::time::Instant::now() + std::time::Duration::from_secs(60); + while h.state().gp.tool_run.is_some() { + assert!(std::time::Instant::now() < end, "the run never landed"); + std::thread::sleep(std::time::Duration::from_millis(5)); + h.run_steps(1); + } + h.run_steps(2); +} + /// AccessKit labels distinguish the list's field and statistic selectors from other tool inputs. fn choose(h: &mut Harness<'_, RustGisApp>, label: &str, value: &str) { h.get_by_role_and_label(egui::accesskit::Role::ComboBox, label).click(); @@ -52,8 +65,7 @@ fn summary_list_runs_with_ordered_case_fields_and_reopens_from_history() { h.get_by_label("amount").click(); h.run_steps(2); choose(&mut h, "Statistic", "SUM"); - h.get_by_role_and_label(egui::accesskit::Role::Button, "Run").click(); - h.run_steps(3); + click_run(&mut h); let history = h.state().session.project.gp_history.last().unwrap(); assert_eq!(history.params["statistics"], json!([{"field":"amount","statistic":"SUM"}])); assert_eq!(history.params["case"], json!(["region", "category"])); @@ -67,8 +79,7 @@ fn summary_list_runs_with_ordered_case_fields_and_reopens_from_history() { reversed["case"] = json!(["category", "region"]); h.state_mut().run("ui.gp.open", json!({"tool":"summaryStatistics", "values":reversed})).unwrap(); h.run_steps(3); - h.get_by_role_and_label(egui::accesskit::Role::Button, "Run").click(); - h.run_steps(3); + click_run(&mut h); let output = &h.state().session.project.tables.last().unwrap().dataset; assert_eq!(output.fields.iter().map(|f| f.name.as_str()).collect::>(), ["category", "region", "FREQUENCY", "SUM_amount"]); assert_eq!(output.value(0, 3).as_f64(), Some(16.3)); @@ -92,8 +103,7 @@ fn merge_rules_offer_join_fields_and_text_rules_and_preserve_delimiter() { h.run_steps(2); let rules: serde_json::Value = serde_json::from_str(&h.state().gp.values["merge"]).unwrap(); assert_eq!(rules, json!([{"field":"category","rule":"join","delimiter":"; "}])); - h.get_by_role_and_label(egui::accesskit::Role::Button, "Run").click(); - h.run_steps(3); + click_run(&mut h); let history = h.state().session.project.gp_history.last().unwrap(); assert_eq!(history.params["merge"], json!([{"field":"category","rule":"join","delimiter":"; "}])); } @@ -223,8 +233,7 @@ fn model_merge_fields_follow_the_wired_join_input() { fn invalid_statistics_text_is_not_changed_into_an_empty_list() { let mut h = harness("summaryStatistics", json!({"input":"Records", "field":"amount", "statistics":"null"})); assert!(h.query_by_label("JSON value").is_some()); - h.get_by_role_and_label(egui::accesskit::Role::Button, "Run").click(); - h.run_steps(3); + click_run(&mut h); let result = h.state().gp.last.as_ref().unwrap(); assert!(!result.0); assert!(result.1.iter().any(|message| message.contains("Statistics must be a list"))); @@ -421,8 +430,7 @@ fn summary_picker_excludes_case_variant_output_collisions() { assert!(h.query_by_role_and_label(egui::accesskit::Role::Button, "COUNT").is_none()); h.get_by_role_and_label(egui::accesskit::Role::Button, "SUM").click_accesskit(); h.run_steps(2); - h.get_by_role_and_label(egui::accesskit::Role::Button, "Run").click(); - h.run_steps(3); + click_run(&mut h); assert!(h.state().gp.last.as_ref().unwrap().0); let output = &h.state().session.project.tables.last().unwrap().dataset; assert_eq!(output.fields.iter().map(|f| f.name.as_str()).collect::>(), ["FREQUENCY", "COUNT_region", "SUM_REGION"]); @@ -448,8 +456,7 @@ fn saved_duplicate_statistics_explain_how_to_repair_them() { h.get_all_by_role_and_label(egui::accesskit::Role::Button, "Remove").nth(1).unwrap().click(); h.run_steps(3); assert!(h.query_by_label("Statistic is already listed for this field.").is_none()); - h.get_by_role_and_label(egui::accesskit::Role::Button, "Run").click(); - h.run_steps(3); + click_run(&mut h); assert!(h.state().gp.last.as_ref().unwrap().0); let output = &h.state().session.project.tables.last().unwrap().dataset; assert_eq!(output.value(0, 1).as_f64(), Some(16.3)); diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index 71c822e301..3501110ba0 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -57,6 +57,19 @@ Open a tool from **Analyze › Portal › Feature Analysis** or **Raster Analysi which searches every tool) or Command Search. Fill in its parameters and click **Run**: new layers or tables are added to the project; selection tools select existing rows and Calculate Geometry Attributes updates the input layer. Each run is listed under **Analyze › History**. + +In the desktop app a tool runs in the background, so the map and the rest of the window stay in +use. While it runs, the Tools pane shows its name, a **Cancel** button and, for the tools that +count their work (Viewshed, IDW Interpolation, Kriging, Kernel Density, Buffer, Select Layer By +Location and others), a progress bar. One tool runs at a time. A cancelled run adds nothing, and +for a tool that counts its work, its message says how far through its current step it got. Edits +made to its input layers while it runs are kept: a selection it makes selects the same features +where they now are (and says how many are gone), and a result that would replace an edited +layer, or one loaded whole meanwhile, is refused. Renaming the map while it runs is fine; after +switching to another map, a result that adds to or changes the map is refused (one made only of +tables or a file is still made). Opening another project drops the run. Scripts, models, the AI +Assistant, the command line and the web app wait for each tool to finish. + From the command line or a script (`gp.run`), a built-in tool's parameter with a list of choices, such as Buffer's units, takes one of them in any letter case (`"meters"` for `Meters`); any other value is an error that names the choices (Buffer's units no longer take the abbreviations `m`, `mi` or `metres`). @@ -766,8 +779,9 @@ next visit. The runner's code comes from jsDelivr, pinned to one geolibre-wasm r same trust as installing that package. Tools that read files by path (LiDAR files, for instance) need the desktop app, since a web page has no file system. -In the desktop app and on the command line, RustGIS waits while a tool runs, so a long run on -a large raster or point cloud keeps the window busy until it finishes. In the web app the run +In the desktop app the Tools pane runs a GeoLibre tool in the background like any other; its +**Cancel** takes effect when the GeoLibre program ends, and the result is then dropped. On the +command line RustGIS waits while a tool runs. In the web app the run is started in the background and the result is added when it ends, but the tool itself runs in the page, so the page is busy while it computes.