diff --git a/crates/analysis/src/topology.rs b/crates/analysis/src/topology.rs index 06b02c813c..1f2b0643c7 100644 --- a/crates/analysis/src/topology.rs +++ b/crates/analysis/src/topology.rs @@ -35,9 +35,43 @@ impl Checked { /// Most errors reported per check. const MAX_ERRORS: usize = 10_000; -/// Most pair tests one check spends, so a huge layer can't stall the app. +/// Most pair tests one check spends, so a huge layer can't stall the app: a run that can't be +/// cancelled. The checks run on one thread, so the limit is the same on every machine; a run that +/// can be cancelled while it goes (the Error Inspector's, #1291) takes none ([`past_limit`]). const MAX_WORK: usize = 50_000_000; +/// Whether `work` passed the check's limit: [`MAX_WORK`], or none in a run that can be cancelled +/// while it goes ([`crate::control::uncapped`]), whose progress shows and whose Cancel stops it. +/// The run's control is read only once the limit is passed. Past it, such a run looks at Cancel +/// every [`LOOK_EVERY`] units, so the loops within one feature (one path's crossings, one end's +/// polar scan) that the limit bounded stop when it is cancelled, with what was found. +fn past_limit(work: usize) -> bool { + if work <= MAX_WORK { + return false; + } + if !crate::control::uncapped() { + return true; + } + // The units, not the calls: one charge can add many (a polar edge's pieces). + LOOKED_AT.with(|at| { + let last = at.get(); + let due = work < last || work - last >= LOOK_EVERY; + if due { + at.set(work); + } + due + }) && crate::control::cancelled() +} + +/// How many units of work past [`MAX_WORK`] go by between looks at Cancel ([`past_limit`]). +const LOOK_EVERY: usize = 4096; + +thread_local! { + /// The work a check on this thread had done when [`past_limit`] last looked at Cancel (a + /// smaller total is a new check's). + static LOOKED_AT: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + /// One geometry from several: polygons are unioned, lines and points gathered. pub fn merge(geoms: &[Geometry]) -> Option { match geoms.first()? { @@ -113,7 +147,7 @@ pub fn overlaps(ds: &Dataset, rows: &[usize]) -> Checked { break; } work += 1; - if work > MAX_WORK { + if past_limit(work) { return Checked::partial(out); } if !rects_touch(ba, bb) || !a.intersects(b) { @@ -133,7 +167,8 @@ pub fn overlaps(ds: &Dataset, rows: &[usize]) -> Checked { } /// Holes in the union of a layer's polygons that no polygon covers (the outer boundary isn't a -/// gap). +/// gap). The union of every polygon is one step: it looks at no Cancel and counts no progress +/// until it is done; the holes after it are checked a polygon at a time. pub fn gaps(ds: &Dataset, rows: &[usize]) -> Checked { let all: Vec> = polygons(ds, rows).into_iter().flat_map(|(_, m, _)| m.0).collect(); let u = geo::unary_union(all.iter()); @@ -166,8 +201,10 @@ fn segments_cross(a: (Coord, Coord), b: (Coord, Coord)) -> Option { /// Where a path crosses itself (rings and lines; ring closures are allowed). Segments are swept /// west to east, so only those overlapping in x are compared. A path too complex to finish -/// within the work budget is reported at its start, to be looked at, rather than passed. -fn self_crossing(path: &[Coord], closed: bool) -> Option { +/// within the work budget is reported at its start, to be looked at, rather than passed, and +/// says so (`Err`), so the check is said to be incomplete. A run cancelled past the limit stops +/// with `Err` too, which the caller tells apart by looking at Cancel. +fn self_crossing(path: &[Coord], closed: bool) -> Result, Coord> { let segs: Vec<(Coord, Coord)> = path.windows(2).filter_map(|w| Some((*w.first()?, *w.get(1)?))).collect(); let n = segs.len(); let minx = |s: &(Coord, Coord)| s.0.x.min(s.1.x); @@ -186,8 +223,8 @@ fn self_crossing(path: &[Coord], closed: bool) -> Option { break; } work += 1; - if work > MAX_WORK { - return path.first().copied(); + if past_limit(work) { + return Err(path.first().copied().unwrap_or(a.0)); } let (lo, hi) = (i.min(j), i.max(j)); // Neighbouring segments share a vertex, as do a ring's first and last. @@ -195,16 +232,18 @@ fn self_crossing(path: &[Coord], closed: bool) -> Option { continue; } if let Some(c) = segments_cross(*a, *b) { - return Some(c); + return Ok(Some(c)); } } } - None + Ok(None) } /// Lines and polygon rings that cross themselves. pub fn self_intersections(ds: &Dataset, rows: &[usize]) -> Checked { let mut out = Vec::new(); + // A path too complex to finish: reported at its start, and the check said incomplete. + let mut limited = false; crate::control::begin(rows.len()); for (i, r) in rows.iter().enumerate() { if crate::control::cancelled() { @@ -219,7 +258,16 @@ pub fn self_intersections(ds: &Dataset, rows: &[usize]) -> Checked { }; for (p, closed) in paths { let m: Vec = p.iter().map(|c| lonlat_to_plane(*c)).collect(); - if let Some(c) = self_crossing(&m, closed) { + let found = match self_crossing(&m, closed) { + Ok(c) => c, + // Stopped by Cancel, not by the limit: nothing to list (the run is dropped). + Err(_) if crate::control::cancelled() => return Checked::partial(out), + Err(start) => { + limited = true; + Some(start) + } + }; + if let Some(c) = found { out.push(TopologyError { rows: vec![*r], at: plane_to_lonlat(c) }); break; } @@ -228,7 +276,7 @@ pub fn self_intersections(ds: &Dataset, rows: &[usize]) -> Checked { return Checked::partial(out); } } - Checked::done(out) + if limited { Checked::partial(out) } else { Checked::done(out) } } /// Line ends that touch no other line (within `tol_m` metres). Line parts are indexed on a grid, @@ -269,7 +317,7 @@ pub fn dangles(ds: &Dataset, rows: &[usize], tol_m: f64) -> Checked { for gx in x0.max(0)..=x1.min(cells.last.0) { for gy in y0.max(0)..=y1.min(cells.last.1) { work += 1; - if work > MAX_WORK { + if past_limit(work) { return Checked::partial(Vec::new()); } let v = grid.entry((gx, gy)).or_default(); @@ -321,7 +369,7 @@ pub fn dangles(ds: &Dataset, rows: &[usize], tol_m: f64) -> Checked { .is_some_and(|op| op.len() >= 2 && rustgis_data::geometry::seg_dist2(op, *end) <= tol * tol) }) }; - if work > MAX_WORK { + if past_limit(work) { return Checked::partial(out); } if !touches { @@ -357,13 +405,13 @@ const PLACE_HALVINGS: usize = 60; /// at worst costs about a quarter of a piece measured in [`edge_from`]. Reads add up across ends. const READS_A_WORK: usize = 4; -/// The dangle check passed [`MAX_WORK`]: nothing more is compared. +/// The dangle check passed its limit ([`past_limit`]): nothing more is compared. struct WorkLimit; -/// Adds `n` to `work`, or says it passed [`MAX_WORK`]. +/// Adds `n` to `work`, or says it passed the limit ([`past_limit`]). fn charge(work: &mut usize, n: usize) -> Result<(), WorkLimit> { *work = work.saturating_add(n); - if *work > MAX_WORK { Err(WorkLimit) } else { Ok(()) } + if past_limit(*work) { Err(WorkLimit) } else { Ok(()) } } /// Counts a cheap read ([`READS_A_WORK`] to a unit of work). @@ -756,7 +804,8 @@ impl<'a> PolarEnds<'a> { *span = (left, cells.key(end.x + shift + reach.wx, end.y).0.min(cells.last.0)); } // A grid wider than two turns can contain many copies. Scan its occupied cells once; - // the edge tests use the nearest turn, and the existing work limit bounds the scan. + // the edge tests use the nearest turn, and the work limit bounds the scan (or, where there is + // none, Cancel stops it: see `past_limit`). if cells.size * (cells.last.0 as f64 + 1.0) > 2.0 * std::f64::consts::TAU * rustgis_geom::MERCATOR_R { columns = [(0, cells.last.0), (0, -1), (0, -1)]; } @@ -1442,6 +1491,30 @@ mod tests { assert!(Reach::new(c(0.0, 0.0), 89.999_99, 10.0).wx.is_infinite() && Reach::new(c(0.0, 0.0), 89.0, 10.0).wx.is_finite()); } + /// The work limit holds a run that can't be cancelled, and none that can (#1291). + #[test] + fn a_run_that_can_be_cancelled_takes_no_work_limit() { + assert!(past_limit(MAX_WORK + 1) && !past_limit(MAX_WORK)); + let control = crate::control::Control::stoppable(); + crate::run_under("Topology", &control, || { + assert!(!past_limit(MAX_WORK + 1) && !past_limit(usize::MAX)); + Ok(()) + }) + .unwrap(); + assert!(past_limit(MAX_WORK + 1), "the limit holds again after the run"); + // Past the limit, a run that can be cancelled looks at Cancel every LOOK_EVERY units, a + // charge of many units at once included, and stops. + let control = crate::control::Control::stoppable(); + crate::run_under("Topology", &control, || { + assert!(!past_limit(MAX_WORK + 1), "a look, not cancelled"); + control.cancel(); + assert!(!past_limit(MAX_WORK + 2), "no look until LOOK_EVERY units more"); + assert!(past_limit(MAX_WORK + 2 + 65_536), "one charge of many units past the interval looks, and stops"); + Ok(()) + }) + .unwrap_err(); + } + #[test] fn checks_find_problems() { let mut ds = Dataset::new("p", GeometryType::Polygon, vec![]); @@ -1493,6 +1566,23 @@ mod tests { assert!(not_covered(&pts, &[0], &ds).errors.is_empty()); } + /// A path too complex to check within the work limit is listed at its start, and the check + /// says it is incomplete; a run that can be cancelled takes no limit and checks it whole. + #[test] + fn a_path_past_the_work_limit_is_listed_and_the_check_is_incomplete() { + // 10,100 segments running north, each overlapping every other west to east: about 51 + // million pairs, none crossing. + let path: Vec = (0..10_101).map(|i| Coord::new(0.0, i as f64 * 0.001)).collect(); + let mut ds = Dataset::new("l", GeometryType::Polyline, vec![]); + ds.push(Some(Geometry::Line(vec![path])), vec![]); + let capped = self_intersections(&ds, &[0]); + assert_eq!(capped.errors.len(), 1, "listed at its start"); + assert!(capped.errors[0].at.y.abs() < 1e-9, "{:?}", capped.errors[0].at); + assert!(!capped.complete, "and said to be incomplete"); + let whole = crate::run_under("Topology", &crate::control::Control::stoppable(), || Ok(self_intersections(&ds, &[0]))).unwrap(); + assert!(whole.errors.is_empty() && whole.complete, "{whole:?}"); + } + #[test] fn long_paths_are_checked_not_skipped() { // 10,000 segments zigzagging east without crossing... diff --git a/crates/engine/src/cmd/design.rs b/crates/engine/src/cmd/design.rs index 0504c37c74..497b759364 100644 --- a/crates/engine/src/cmd/design.rs +++ b/crates/engine/src/cmd/design.rs @@ -2054,47 +2054,167 @@ fn topology_rules(s: &mut Session, p: &Json) -> CmdResult { Ok(Json::Array(rules)) } -/// Checks every topology rule; the errors are kept for the Error Inspector. -fn topology_validate(s: &mut Session, p: &Json) -> CmdResult { +/// One topology rule as `topology.validate` checks it: the layer's rows shown (their data shared, +/// not copied) and, for a rule against another layer, that layer's. +struct TopologyInput { + rule: TopologyRule, + layer: String, + data: rustgis_data::Dataset, + rows: Vec, + by: Option, +} + +/// The rules as the map has them now, each with what it reads; a rule whose layer is gone is left +/// out, as it always was. +fn topology_checks(s: &Session) -> Vec { + let mut inputs = Vec::new(); + for rule in s.map().editing.topology.clone() { + let Some(l) = s.map().layer_by_name(rule.layer()) else { continue }; + let rows: Vec = l.visible_rows().iter().enumerate().filter(|(_, v)| **v).map(|(i, _)| i).collect(); + // A covering layer that is gone leaves the rule with nothing to check (its layer's + // latitudes are still checked). + let by = match &rule { + TopologyRule::MustBeCoveredBy { by, .. } => s.map().layer_by_name(by).map(|b| b.dataset.clone()), + _ => None, + }; + inputs.push(TopologyInput { rule, layer: l.name.clone(), data: l.dataset.clone(), rows, by }); + } + inputs +} + +/// Whether two layers' data are the same: the same rows (shared, not compared one by one), fields +/// and geometry type. +fn same_data(a: &rustgis_data::Dataset, b: &rustgis_data::Dataset) -> bool { + a.features.shares(&b.features) && a.fields == b.fields && a.geometry_type == b.geometry_type +} + +/// Whether `was` (what a check read on another thread) is what the rules read now. +fn same_topology(was: &[TopologyInput], now: &[TopologyInput]) -> bool { + was.len() == now.len() + && was.iter().zip(now).all(|(a, b)| { + a.rule == b.rule + && a.layer == b.layer + && a.rows == b.rows + && same_data(&a.data, &b.data) + && match (&a.by, &b.by) { + (Some(x), Some(y)) => same_data(x, y), + (None, None) => true, + _ => false, + } + }) +} + +/// Checks each rule, in order, a result for each (a cancelled run is an error): a layer with a +/// latitude out of range is an error, as it always was. Each check looks at Cancel between features +/// and counts its progress, except the gaps check's union of every polygon, one step; in a run that +/// can be cancelled it takes no work limit (see `rustgis_analysis::topology`). +fn check_topology(inputs: &[TopologyInput], tol: f64) -> Result, String> { use rustgis_analysis::topology as t; - let tol = f(p, "toleranceM").unwrap_or(1.0).clamp(0.0, 10_000.0); + let mut found = Vec::with_capacity(inputs.len()); + for i in inputs { + // A cancelled run stops between rules too (a gaps rule's union doesn't look), as an error, + // so no result short of its rules is ever taken. + rustgis_data::progress::check().map_err(|_| "the topology check was cancelled".to_string())?; + rustgis_analysis::validate_latitudes(&i.data, i.rows.iter().copied())?; + found.push(match (&i.rule, &i.by) { + (TopologyRule::MustNotOverlap { .. }, _) => t::overlaps(&i.data, &i.rows), + (TopologyRule::MustNotHaveGaps { .. }, _) => t::gaps(&i.data, &i.rows), + (TopologyRule::MustNotSelfIntersect { .. }, _) => t::self_intersections(&i.data, &i.rows), + (TopologyRule::MustNotHaveDangles { .. }, _) => t::dangles(&i.data, &i.rows, tol), + (TopologyRule::MustBeCoveredBy { .. }, Some(by)) => { + rustgis_analysis::validate_latitudes(by, 0..by.features.len())?; + t::not_covered(&i.data, &i.rows, by) + } + // Its covering layer is gone: nothing checked, nothing said, as it always was. + (TopologyRule::MustBeCoveredBy { .. }, None) => t::Checked { errors: Vec::new(), complete: true, skipped: 0 }, + }); + } + Ok(found) +} + +/// The tolerance `topology.validate` checks dangles with, in metres. +fn topology_tolerance(p: &Json) -> f64 { + f(p, "toleranceM").unwrap_or(1.0).clamp(0.0, 10_000.0) +} + +/// The key a topology check read ahead is placed under. +const TOPOLOGY_AHEAD: &str = "topology.validate"; + +/// A topology check made on a thread of its own ([`topology_ahead`]), with what it read. +struct TopologyAhead { + inputs: Vec, + tol: f64, + found: Result, String>, +} + +/// The check `topology.validate` makes before it runs in the desktop app (#1291, #1562): every +/// rule on a thread of its own, with progress and Cancel and no work limit. The command takes it +/// as it lands when the rules and their layers are still the ones checked. +pub(crate) fn topology_ahead(s: &Session, p: &Json) -> Option { + let (inputs, tol) = (topology_checks(s), topology_tolerance(p)); + if inputs.is_empty() { + return None; + } + Some(super::reads::ahead("Error Inspector", TOPOLOGY_AHEAD.to_string(), move || { + let found = check_topology(&inputs, tol); + TopologyAhead { inputs, tol, found } + })) +} + +/// Checks every topology rule; the errors are kept for the Error Inspector. The work grows with +/// the layers, so the desktop app checks them on a thread of its own first ([`topology_ahead`]), +/// with the status bar's progress and **Cancel**, and the command takes that check as it lands; +/// when a rule or a layer changed meanwhile it says so and keeps the errors it had, rather than +/// check again here, which would hold the window. The assistant, tasks, scripts and the +/// command line call commands one at a time on the session and use each answer before the next, +/// so they check where they ask, with the work limit of a run that can't be cancelled. +fn topology_validate(s: &mut Session, p: &Json) -> CmdResult { + let tol = topology_tolerance(p); + let inputs = topology_checks(s); + let found = match super::reads::take::(s, TOPOLOGY_AHEAD) { + Some(ahead) if ahead.tol == tol && same_topology(&ahead.inputs, &inputs) => ahead.found?, + // Checking again here would hold the window: the check made on its thread is dropped. + Some(_) => return Err("the topology rules or their layers changed while they were checked: validate again".into()), + None => check_topology(&inputs, tol)?, + }; + // Rules whose layer, or covering layer, isn't on the map are checked for nothing: said. + let unchecked: Vec = s + .map() + .editing + .topology + .iter() + .filter(|r| !inputs.iter().any(|i| &i.rule == *r && i.by.is_some() == matches!(r, TopologyRule::MustBeCoveredBy { .. }))) + .map(|r| format!("{} on '{}'", r.label(), r.layer())) + .collect(); + if !unchecked.is_empty() { + s.warn(format!( + "{} topology rule(s) weren't checked: their layer or covering layer isn't on the map ({})", + unchecked.len(), + unchecked.join(", ") + )); + } let mut errors = Vec::new(); // Rules whose check stopped early (an error or work limit), or measured some part more coarsely // than it's meant to (a line winding round a pole many times): their counts may be off. let mut incomplete = Vec::new(); // Polygons a check had to leave out, by rule. let mut skipped = Vec::new(); - for rule in s.map().editing.topology.clone() { - let Some(l) = s.map().layer_by_name(rule.layer()) else { continue }; - let rows: Vec = l.visible_rows().iter().enumerate().filter(|(_, v)| **v).map(|(i, _)| i).collect(); - rustgis_analysis::validate_latitudes(&l.dataset, rows.iter().copied())?; - let found = match &rule { - TopologyRule::MustNotOverlap { .. } => t::overlaps(&l.dataset, &rows), - TopologyRule::MustNotHaveGaps { .. } => t::gaps(&l.dataset, &rows), - TopologyRule::MustNotSelfIntersect { .. } => t::self_intersections(&l.dataset, &rows), - TopologyRule::MustNotHaveDangles { .. } => t::dangles(&l.dataset, &rows, tol), - TopologyRule::MustBeCoveredBy { by, .. } => match s.map().layer_by_name(by) { - Some(b) => { - rustgis_analysis::validate_latitudes(&b.dataset, 0..b.dataset.features.len())?; - t::not_covered(&l.dataset, &rows, &b.dataset) - } - None => continue, - }, - }; + for (i, found) in inputs.iter().zip(found) { + let rule = &i.rule; if !found.complete || found.skipped > 0 { incomplete.push(rule.label().to_string()); } if found.skipped > 0 { // The features left out are the covering layer's. - let of = match &rule { + let of = match rule { TopologyRule::MustBeCoveredBy { by, .. } => by.as_str(), - _ => l.name.as_str(), + _ => i.layer.as_str(), }; - skipped.push(format!("{} on '{}': {} feature(s) of '{}'", rule.label(), l.name, found.skipped, of)); + skipped.push(format!("{} on '{}': {} feature(s) of '{}'", rule.label(), i.layer, found.skipped, of)); } errors.extend(found.errors.into_iter().map(|e| crate::TopologyIssue { rule: rule.label().to_string(), - layer: l.name.clone(), + layer: i.layer.clone(), rows: e.rows, lon: e.at.x, lat: e.at.y, diff --git a/crates/engine/src/cmd/reads.rs b/crates/engine/src/cmd/reads.rs index cdffc17828..91d06ff15e 100644 --- a/crates/engine/src/cmd/reads.rs +++ b/crates/engine/src/cmd/reads.rs @@ -1,6 +1,7 @@ //! Reads the desktop app makes on a thread of its own before a command adds what they read //! (#1291 step 3): Add Data's whole-file reads (a raster, a raster GDAL converts, a GeoJSON, -//! delimited text or spreadsheet read whole, a CityJSON or CityGML model's layers), Reload Data, Load All Features and native models or scripts. [`read_ahead`] +//! delimited text or spreadsheet read whole, a CityJSON or CityGML model's layers), Reload Data, Load All Features, native models or scripts and +//! the Error Inspector's topology check (#1291). [`read_ahead`] //! takes what the read needs of the session, [`ReadAhead::run`] reads on the thread, and [`read_land`] //! runs the admitted command under its initiating owner: the command takes //! it instead of reading again, so what it adds, says, logs and puts in Undo is the same as when @@ -128,7 +129,8 @@ pub(crate) fn prepare(s: &Session, op: Authorized, owner: Continuatio let name = if spec.id == "map.repairTiles" { "Repair Data Source" } else { "Reload Data" }; (name, work, None) } - _ => return Err("only Add Data, Reload Data, Load All, local tile archives, models and scripts can prepare background work".into()), + "topology.validate" => ("Error Inspector", super::design::topology_ahead(s, params), None), + _ => return Err("only Add Data, Reload Data, Load All, tile archives, models, scripts and topology prepare background work".into()), }; let name = work.as_ref().map_or(name, |work| work.name); Ok(ReadAhead { name, op, owner, source, work }) diff --git a/crates/engine/src/policy/dispatch.rs b/crates/engine/src/policy/dispatch.rs index 7c19f49309..518cf02c60 100644 --- a/crates/engine/src/policy/dispatch.rs +++ b/crates/engine/src/policy/dispatch.rs @@ -142,9 +142,16 @@ impl Session { fn capture_read(&mut self, command: &str, params: &Value) -> Result { if !matches!( command, - "layer.addData" | "layer.loadAll" | "layer.reloadData" | "model.run" | "script.run" | "map.reloadTiles" | "map.repairTiles" + "layer.addData" + | "layer.loadAll" + | "layer.reloadData" + | "model.run" + | "script.run" + | "map.reloadTiles" + | "map.repairTiles" + | "topology.validate" ) { - return Err("only Add Data, Load All, Reload Data, models, scripts and local tile archives can prepare background work".into()); + return Err("only Add Data, Load All, Reload Data, models, scripts, tile archives and topology prepare background work".into()); } let mut params = params.as_object().cloned().ok_or("read parameters must be an object")?; if matches!(command, "layer.loadAll" | "layer.reloadData") { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 6517ac8437..04187a87e4 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -38414,3 +38414,73 @@ fn a_scripts_spectral_zone_is_read_on_its_copy() { assert!(read_ahead(&mut s, "script.run", &script).unwrap().run(&control).is_err()); assert!(control.looked() > 0, "the zone's read looked at Cancel"); } + +/// The Error Inspector's topology check runs ahead on a thread of its own in the desktop app +/// (#1291): the command takes it as it lands, with the same answer and errors as checked here; a +/// layer changed meanwhile is checked again; a cancelled check looks at Cancel and lands nothing. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn a_topology_check_runs_ahead_and_is_taken() { + use crate::cmd::reads::{read_ahead, read_land}; + use rustgis_analysis::control::Control; + let square = |x: f64| { + json!({"type": "Feature", "properties": {}, "geometry": {"type": "Polygon", + "coordinates": [[[x, 0.0], [x + 1.0, 0.0], [x + 1.0, 1.0], [x, 1.0], [x, 0.0]]]}}) + }; + let parcels = + |xs: &[f64]| json!({"type": "FeatureCollection", "features": xs.iter().map(|x| square(*x)).collect::>()}).to_string(); + let session = || { + let mut s = Session::new(Project::default()); + s.run("layer.addDataText", &json!({"fileName": "parcels.geojson", "text": parcels(&[0.0, 0.5, 3.0])})).unwrap(); + s.run("topology.rules", &json!({"add": {"rule": "mustNotOverlap", "layer": "parcels"}})).unwrap(); + s + }; + let mut here = session(); + let expected = here.run("topology.validate", &Value::Null).unwrap(); + assert_eq!(expected["errors"], 1, "{expected}"); + + let mut s = session(); + let ahead = read_ahead(&mut s, "topology.validate", &json!({})).unwrap(); + assert!(ahead.reads()); + let read = ahead.run(&Control::stoppable()).unwrap(); + assert!(s.topology_errors.is_empty(), "nothing is kept before it lands"); + assert_eq!(read_land(&mut s, read).unwrap(), expected); + assert!(s.read_ahead.is_none(), "the check was taken"); + assert_eq!(s.topology_errors.len(), here.topology_errors.len()); + + // The layer changed while it was checked: said, not checked again here (which would hold the + // window), and the errors it had are kept. + let mut s = session(); + s.topology_errors = here.topology_errors.clone(); + let read = read_ahead(&mut s, "topology.validate", &json!({})).unwrap().run(&Control::stoppable()).unwrap(); + s.run("layer.remove", &json!({"layer": "parcels"})).unwrap(); + s.run("layer.addDataText", &json!({"fileName": "parcels.geojson", "text": parcels(&[0.0, 2.0, 3.0])})).unwrap(); + let e = read_land(&mut s, read).unwrap_err(); + assert!(e.contains("changed while they were checked"), "{e}"); + assert!(s.read_ahead.is_none()); + assert_eq!(s.topology_errors.len(), 1, "the errors it had are kept"); + // Checked where it is asked (a script, the assistant), it checks the layer as it is now. + assert_eq!(s.run("topology.validate", &Value::Null).unwrap()["errors"], 0); + + // A rule whose covering layer is gone still has its layer's latitudes checked. + let mut s = session(); + s.run("layer.addDataText", &json!({"fileName": "cover.geojson", "text": parcels(&[0.0])})).unwrap(); + let points = json!({"type": "FeatureCollection", "features": [{"type": "Feature", "properties": {}, "geometry": {"type": "Point", "coordinates": [0.5, 0.5]}}]}); + s.run("layer.addDataText", &json!({"fileName": "wells.geojson", "text": points.to_string()})).unwrap(); + s.run("topology.rules", &json!({"add": {"rule": "mustBeCoveredBy", "layer": "wells", "by": "cover"}})).unwrap(); + s.run("layer.remove", &json!({"layer": "cover"})).unwrap(); + let mark = s.notice_mark(); + assert_eq!(s.run("topology.validate", &Value::Null).unwrap()["errors"], 1, "the overlap rule's error, the other rule's none"); + let said: Vec = s.notices_since(mark).iter().map(|n| n.text.clone()).collect(); + assert!(said.iter().any(|t| t.contains("1 topology rule(s) weren't checked") && t.contains("'wells'")), "{said:?}"); + + // Cancelled as it starts: the check looks at it, and nothing lands. + let mut s = session(); + let control = Control::cancelled_once_started(); + assert!(read_ahead(&mut s, "topology.validate", &json!({})).unwrap().run(&control).is_err()); + assert!(control.looked() > 0, "the check looked at Cancel"); + + // No rules: nothing to read ahead. + let mut s = Session::new(Project::default()); + assert!(!read_ahead(&mut s, "topology.validate", &json!({})).unwrap().reads()); +} diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index e1294788c3..a870bb468e 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -2258,6 +2258,11 @@ impl RustGisApp { if let Some(command) = archive { return readjob::run(self, command, params); } + // The topology check's work grows with the layers: on a thread of its own (#1291). + if id == "topology.validate" { + let params = if params.is_null() { json!({}) } else { params }; + return readjob::run(self, "topology.validate", params); + } } self.run_with_prepared(id, params, AppDispatch::Direct) } diff --git a/crates/ui-egui/src/readjob.rs b/crates/ui-egui/src/readjob.rs index bce1107b97..6aa729e8f2 100644 --- a/crates/ui-egui/src/readjob.rs +++ b/crates/ui-egui/src/readjob.rs @@ -62,10 +62,10 @@ struct ReadJob { slot: crate::Slot>, } -/// Runs Add Data, Load All, Reload Data, archive reload/repair or model/script tools with `p`: its -/// read on a thread of its own when the desktop app makes it so ([`reads::read_ahead`]), after -/// any under way; here and now otherwise. While one is under way or waiting it answers that, not -/// what the command installs, which comes as it lands. +/// Runs Add Data, Load All, Reload Data, archive reload/repair, model/script tools or the topology +/// check with `p`: its read on a thread of its own when the desktop app makes it so +/// ([`reads::read_ahead`]), after any under way; here and now otherwise. While one is under way or +/// waiting it answers that, not what the command installs, which comes as it lands. pub fn run(app: &mut RustGisApp, command: &'static str, p: Value) -> Result { run_then(app, command, p, None) } @@ -242,6 +242,7 @@ fn called<'a>(command: &str, what: &'a str) -> &'a str { match (what.is_empty(), command) { (false, _) => what, (true, "script.run") => tl_raw!("Run Script"), + (true, "topology.validate") => tl_raw!("Error Inspector"), (true, _) => tl_raw!("Run Model"), } } @@ -345,6 +346,9 @@ fn start(app: &mut RustGisApp, mut asked: Asked, p: Value) -> Result