From 529f0b77951f0658a55719aa3e3472fed20b38b5 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Tue, 10 Nov 2026 13:03:43 +0100 Subject: [PATCH 1/6] feat(engine): the Error Inspector's topology check runs in the background and takes no work limit there (#1291) The last item of #1291 (topology's MAX_WORK) and an AGENTS.md "Never hold the window" gap: topology.validate checked every rule on the UI thread, with a fixed limit of 50 million pair tests. - In the desktop app the check runs on a thread of its own first, as Add Data and Load All do (`reads::prepare`, `design::topology_ahead`), with the status bar's progress and Cancel. The command takes it as it lands when the rules and their layers (rows, fields, shown rows) are the ones checked. When one changed meanwhile it says so and keeps the errors it had, rather than check again on the UI thread. The errors are kept, and said, as before. - The checks run on one thread, so their limit stays the same on every machine for a run that can't be cancelled. A run that can (the Error Inspector's) takes none (`topology::past_limit`), as the other capped tools do. The run's control is read only once the limit is passed; past it, such a run looks at Cancel every 4,096 units, so the loops within one feature that the limit bounded (one path's crossings, one end's polar scan) stop when it is cancelled. The gaps check's union of every polygon is one step, and its doc comment says so. - A read whose thread doesn't start runs on the UI thread with the limits of a run that can't be cancelled, since its Cancel can't be clicked there. - Scripts, the assistant and the command line call commands one at a time on the session, so they check where they ask, with the limit; the doc comment says so. --- crates/analysis/src/topology.rs | 77 ++++++++++++-- crates/engine/src/cmd/design.rs | 150 ++++++++++++++++++++++----- crates/engine/src/cmd/reads.rs | 4 +- crates/engine/src/policy/dispatch.rs | 11 +- crates/engine/src/tests.rs | 67 ++++++++++++ crates/ui-egui/src/lib.rs | 5 + crates/ui-egui/src/readjob.rs | 29 +++++- docs/user-guide/editing.md | 7 ++ 8 files changed, 311 insertions(+), 39 deletions(-) diff --git a/crates/analysis/src/topology.rs b/crates/analysis/src/topology.rs index 06b02c813c..3ec9b437b9 100644 --- a/crates/analysis/src/topology.rs +++ b/crates/analysis/src/topology.rs @@ -35,9 +35,37 @@ 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; + } + LOOKS.with(|n| { + n.set(n.get().wrapping_add(1)); + n.get().is_multiple_of(LOOK_EVERY) + }) && crate::control::cancelled() +} + +/// How many units of work past [`MAX_WORK`] go by between looks at Cancel ([`past_limit`]). +const LOOK_EVERY: u32 = 4096; + +thread_local! { + /// The units counted past [`MAX_WORK`] on this thread, for [`past_limit`]'s looks. + static LOOKS: 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 +141,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 +161,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()); @@ -186,7 +215,7 @@ fn self_crossing(path: &[Coord], closed: bool) -> Option { break; } work += 1; - if work > MAX_WORK { + if past_limit(work) { return path.first().copied(); } let (lo, hi) = (i.min(j), i.max(j)); @@ -269,7 +298,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 +350,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 +386,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 +785,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 +1472,33 @@ 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 as it goes, and stops. + let control = crate::control::Control::stoppable(); + let looked = crate::run_under("Topology", &control, || { + control.cancel(); + Ok((0..2 * LOOK_EVERY).any(|_| past_limit(MAX_WORK + 1))) + }); + assert!(looked.is_err(), "the run ends cancelled"); + let control = crate::control::Control::stoppable(); + crate::run_under("Topology", &control, || { + control.cancel(); + assert!((0..2 * LOOK_EVERY).any(|_| past_limit(MAX_WORK + 1)), "a cancelled run stops past the limit"); + Ok(()) + }) + .unwrap_err(); + } + #[test] fn checks_find_problems() { let mut ds = Dataset::new("p", GeometryType::Polygon, vec![]); diff --git a/crates/engine/src/cmd/design.rs b/crates/engine/src/cmd/design.rs index 0504c37c74..318e55b4b3 100644 --- a/crates/engine/src/cmd/design.rs +++ b/crates/engine/src/cmd/design.rs @@ -2054,47 +2054,149 @@ 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. +#[derive(Clone)] +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 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 { + 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)?, + }; 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..8f4fba5931 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`] +//! The Error Inspector's topology check runs the same way (#1291). //! 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..55befdfd71 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -38414,3 +38414,70 @@ 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(); + assert_eq!(s.run("topology.validate", &Value::Null).unwrap()["errors"], 1, "the overlap rule's error, the other rule's none"); + + // 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..d68ee6a197 100644 --- a/crates/ui-egui/src/readjob.rs +++ b/crates/ui-egui/src/readjob.rs @@ -62,7 +62,8 @@ struct ReadJob { slot: crate::Slot>, } -/// Runs Add Data, Load All, Reload Data, archive reload/repair or model/script tools with `p`: its +/// 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. @@ -242,6 +243,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,12 +347,14 @@ fn start(app: &mut RustGisApp, mut asked: Asked, p: Value) -> Result Date: Tue, 10 Nov 2026 13:57:07 +0100 Subject: [PATCH 2/6] fix(engine): a cancelled topology check stops between rules; a path past the work limit marks its rule incomplete (#1291) --- crates/analysis/src/topology.rs | 41 +++++++++++++++++++++++++++------ crates/engine/src/cmd/design.rs | 8 ++++++- crates/engine/src/cmd/reads.rs | 4 ++-- crates/ui-egui/src/readjob.rs | 7 +++--- docs/user-guide/editing.md | 4 +++- 5 files changed, 49 insertions(+), 15 deletions(-) diff --git a/crates/analysis/src/topology.rs b/crates/analysis/src/topology.rs index 3ec9b437b9..1213c2826e 100644 --- a/crates/analysis/src/topology.rs +++ b/crates/analysis/src/topology.rs @@ -195,8 +195,9 @@ 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. +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); @@ -216,7 +217,7 @@ fn self_crossing(path: &[Coord], closed: bool) -> Option { } work += 1; if past_limit(work) { - return path.first().copied(); + 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. @@ -224,16 +225,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() { @@ -248,7 +251,14 @@ 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, + Err(start) => { + limited = true; + Some(start) + } + }; + if let Some(c) = found { out.push(TopologyError { rows: vec![*r], at: plane_to_lonlat(c) }); break; } @@ -257,7 +267,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, @@ -1550,6 +1560,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 318e55b4b3..9c7021b955 100644 --- a/crates/engine/src/cmd/design.rs +++ b/crates/engine/src/cmd/design.rs @@ -2105,7 +2105,8 @@ fn same_topology(was: &[TopologyInput], now: &[TopologyInput]) -> bool { }) } -/// Checks each rule, in order, a result for each: a layer with a latitude out of range is an error, +/// Checks each rule, in order, a result for each (fewer only in a cancelled run, whose result is +/// dropped): 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`). @@ -2113,6 +2114,11 @@ fn check_topology(inputs: &[TopologyInput], tol: f64) -> Result t::overlaps(&i.data, &i.rows), diff --git a/crates/engine/src/cmd/reads.rs b/crates/engine/src/cmd/reads.rs index 8f4fba5931..91d06ff15e 100644 --- a/crates/engine/src/cmd/reads.rs +++ b/crates/engine/src/cmd/reads.rs @@ -1,7 +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`] -//! The Error Inspector's topology check runs the same way (#1291). +//! 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 diff --git a/crates/ui-egui/src/readjob.rs b/crates/ui-egui/src/readjob.rs index d68ee6a197..f09a343d59 100644 --- a/crates/ui-egui/src/readjob.rs +++ b/crates/ui-egui/src/readjob.rs @@ -63,10 +63,9 @@ struct ReadJob { } /// 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. +/// 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) } diff --git a/docs/user-guide/editing.md b/docs/user-guide/editing.md index 10ca81f8cf..8219e11352 100644 --- a/docs/user-guide/editing.md +++ b/docs/user-guide/editing.md @@ -135,7 +135,9 @@ you edit the value or choose units; click **Apply elevation** to apply the new i **Cancel** stops only once it is done). A layer edited while it is checked makes the check say so: run Validate again. Checked from a script, the assistant or the command line, it holds the window and stops after a fixed amount of work, saying which rules weren't checked - completely. + completely (a gaps rule's join of its polygons has no such limit). A line or ring too complex + to check for self-intersections within the limit is listed at its first point, to be looked + at, and its rule is said to be incomplete. Beyond 85.05° they follow the geometry tools (see [Geoprocessing tools](analysis.md#geoprocessing-tools), **Near the poles**). - **Status**: features added, changed and deleted per layer since the last save. From 8c42ef16d29276cbed5e0aa04bbc803a12c4a5a3 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Tue, 10 Nov 2026 14:34:15 +0100 Subject: [PATCH 3/6] test(analysis): drop the cancelled-run block that couldn't fail (#1291) --- crates/analysis/src/topology.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/crates/analysis/src/topology.rs b/crates/analysis/src/topology.rs index 1213c2826e..00135cc4c5 100644 --- a/crates/analysis/src/topology.rs +++ b/crates/analysis/src/topology.rs @@ -1495,12 +1495,6 @@ mod tests { 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 as it goes, and stops. let control = crate::control::Control::stoppable(); - let looked = crate::run_under("Topology", &control, || { - control.cancel(); - Ok((0..2 * LOOK_EVERY).any(|_| past_limit(MAX_WORK + 1))) - }); - assert!(looked.is_err(), "the run ends cancelled"); - let control = crate::control::Control::stoppable(); crate::run_under("Topology", &control, || { control.cancel(); assert!((0..2 * LOOK_EVERY).any(|_| past_limit(MAX_WORK + 1)), "a cancelled run stops past the limit"); From edb3ee17d4873494d898676d6e5bce62b36ba9a6 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Tue, 10 Nov 2026 15:09:12 +0100 Subject: [PATCH 4/6] fix(analysis): a topology check past its limit looks at Cancel every 4,096 units, not calls (#1291) --- crates/analysis/src/topology.rs | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/crates/analysis/src/topology.rs b/crates/analysis/src/topology.rs index 00135cc4c5..af0d6890ea 100644 --- a/crates/analysis/src/topology.rs +++ b/crates/analysis/src/topology.rs @@ -52,18 +52,24 @@ fn past_limit(work: usize) -> bool { if !crate::control::uncapped() { return true; } - LOOKS.with(|n| { - n.set(n.get().wrapping_add(1)); - n.get().is_multiple_of(LOOK_EVERY) + // 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: u32 = 4096; +const LOOK_EVERY: usize = 4096; thread_local! { - /// The units counted past [`MAX_WORK`] on this thread, for [`past_limit`]'s looks. - static LOOKS: std::cell::Cell = const { std::cell::Cell::new(0) }; + /// 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. @@ -1493,11 +1499,14 @@ mod tests { }) .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 as it goes, and stops. + // 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!((0..2 * LOOK_EVERY).any(|_| past_limit(MAX_WORK + 1)), "a cancelled run stops past the limit"); + 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(); From d8549a6a5e8b7515e420db3d5b9fd377ecfa7072 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Tue, 10 Nov 2026 15:36:47 +0100 Subject: [PATCH 5/6] fix(engine): a cancelled topology check is an error, never a short result; only the topology check takes the cap when its thread doesn't start (#1291) --- crates/engine/src/cmd/design.rs | 11 ++++------- crates/ui-egui/src/readjob.rs | 7 ++++--- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/crates/engine/src/cmd/design.rs b/crates/engine/src/cmd/design.rs index 9c7021b955..928d0b8ac7 100644 --- a/crates/engine/src/cmd/design.rs +++ b/crates/engine/src/cmd/design.rs @@ -2105,8 +2105,7 @@ fn same_topology(was: &[TopologyInput], now: &[TopologyInput]) -> bool { }) } -/// Checks each rule, in order, a result for each (fewer only in a cancelled run, whose result is -/// dropped): a layer with a latitude out of range is an error, +/// 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`). @@ -2114,11 +2113,9 @@ fn check_topology(inputs: &[TopologyInput], tol: f64) -> Result t::overlaps(&i.data, &i.rows), diff --git a/crates/ui-egui/src/readjob.rs b/crates/ui-egui/src/readjob.rs index f09a343d59..6aa729e8f2 100644 --- a/crates/ui-egui/src/readjob.rs +++ b/crates/ui-egui/src/readjob.rs @@ -346,14 +346,15 @@ fn start(app: &mut RustGisApp, mut asked: Asked, p: Value) -> Result Date: Tue, 10 Nov 2026 16:12:37 +0100 Subject: [PATCH 6/6] fix(engine): topology rules whose layer is gone are said; a cancelled self-intersection check lists nothing (#1291) --- crates/analysis/src/topology.rs | 5 ++++- crates/engine/src/cmd/design.rs | 25 ++++++++++++++++++++----- crates/engine/src/tests.rs | 3 +++ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/crates/analysis/src/topology.rs b/crates/analysis/src/topology.rs index af0d6890ea..1f2b0643c7 100644 --- a/crates/analysis/src/topology.rs +++ b/crates/analysis/src/topology.rs @@ -202,7 +202,8 @@ 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, and -/// says so (`Err`), so the check is said to be incomplete. +/// 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(); @@ -259,6 +260,8 @@ pub fn self_intersections(ds: &Dataset, rows: &[usize]) -> Checked { let m: Vec = p.iter().map(|c| lonlat_to_plane(*c)).collect(); 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) diff --git a/crates/engine/src/cmd/design.rs b/crates/engine/src/cmd/design.rs index 928d0b8ac7..497b759364 100644 --- a/crates/engine/src/cmd/design.rs +++ b/crates/engine/src/cmd/design.rs @@ -2056,7 +2056,6 @@ fn topology_rules(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. -#[derive(Clone)] struct TopologyInput { rule: TopologyRule, layer: String, @@ -2105,10 +2104,10 @@ fn same_topology(was: &[TopologyInput], now: &[TopologyInput]) -> bool { }) } -/// 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`). +/// 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 mut found = Vec::with_capacity(inputs.len()); @@ -2178,6 +2177,22 @@ fn topology_validate(s: &mut Session, p: &Json) -> CmdResult { 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. diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 55befdfd71..04187a87e4 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -38469,7 +38469,10 @@ fn a_topology_check_runs_ahead_and_is_taken() { 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();