From a38c26351269c65158f82769ac692a8fa81468d9 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Wed, 4 Nov 2026 16:26:34 +0100 Subject: [PATCH 1/4] feat(ui): Export Features and Export Raster write their file in the background (#1291 step 3) --- crates/analysis/src/control.rs | 15 ++++ crates/analysis/src/lib.rs | 2 + crates/data/src/export.rs | 9 +- crates/data/src/interop.rs | 8 ++ crates/data/src/io.rs | 76 ++++++++++++++++- crates/data/src/lib.rs | 5 ++ crates/data/src/progress.rs | 128 ++++++++++++++++++++++++++++ crates/data/src/raster/cogwrite.rs | 23 +++++ crates/data/src/raster/geotiff.rs | 37 +++++++- crates/data/src/source/fgb.rs | 1 + crates/data/src/source/gpkg.rs | 1 + crates/data/src/source/parquet.rs | 3 + crates/engine/src/cmd/exports.rs | 130 +++++++++++++++++++++++++++++ crates/engine/src/cmd/mod.rs | 1 + crates/engine/src/cmd/raster.rs | 36 ++++---- crates/engine/src/cmd/share.rs | 45 +++++----- crates/engine/src/tests.rs | 65 +++++++++++++++ crates/ui-egui/src/chrome.rs | 6 ++ crates/ui-egui/src/exportjob.rs | 113 +++++++++++++++++++++++++ crates/ui-egui/src/lib.rs | 23 ++++- docs/user-guide/sharing.md | 7 ++ 21 files changed, 685 insertions(+), 49 deletions(-) create mode 100644 crates/data/src/progress.rs create mode 100644 crates/engine/src/cmd/exports.rs create mode 100644 crates/ui-egui/src/exportjob.rs diff --git a/crates/analysis/src/control.rs b/crates/analysis/src/control.rs index 76cde698c3..5ab3ced986 100644 --- a/crates/analysis/src/control.rs +++ b/crates/analysis/src/control.rs @@ -51,6 +51,21 @@ impl Control { } } +/// The same flag and count reach the data crate's writers (an export run under the control): +/// their steps count here, and they stop when the run is cancelled. +impl rustgis_data::progress::Probe for Control { + fn cancelled(&self) -> bool { + self.is_cancelled() + } + fn begin(&self, total: u64) { + self.done.store(0, Ordering::Relaxed); + self.total.store(total, Ordering::Relaxed); + } + fn advance(&self, n: u64) { + self.done.fetch_add(n, Ordering::Relaxed); + } +} + /// A tool stopped because it was asked to. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct Cancelled; diff --git a/crates/analysis/src/lib.rs b/crates/analysis/src/lib.rs index 2771d6a335..0c681e395a 100644 --- a/crates/analysis/src/lib.rs +++ b/crates/analysis/src/lib.rs @@ -481,6 +481,8 @@ pub fn run_under(name: &str, control: &std::sync::Arc, work return Err(format!("{name} was cancelled before it started")); } let _current = control::install(Some(control.clone())); + // A file the work writes or reads counts and stops with it too. + let _data = rustgis_data::progress::install(Some(control.clone())); // No count until the work starts one: a control used before doesn't show that run's. control::begin(0); let res = work(); diff --git a/crates/data/src/export.rs b/crates/data/src/export.rs index 467cfd18bd..691d96dafb 100644 --- a/crates/data/src/export.rs +++ b/crates/data/src/export.rs @@ -6,7 +6,7 @@ use std::path::Path; -use crate::io::{CsvWriter, GeoJsonWriter, PROJECTED_FORMATS, check_projected, project_features, write_atomic_with}; +use crate::io::{CsvWriter, GeoJsonWriter, PROJECTED_FORMATS, check_projected, project_each, write_atomic_with}; use crate::raster::Crs; use crate::source::FeatureSource; use crate::{DataError, Dataset, Feature, GeometryType, Result}; @@ -68,11 +68,14 @@ fn write_chunks_in(src: &dyn FeatureSource, layer: &Dataset, path: &Path, crs: C let mut total = 0usize; let mut skipped = 0usize; let mut feed = |add: &mut dyn FnMut(&[Feature]) -> Result<()>| -> Result<()> { - // A GeoPackage export that starts again feeds the features again. + // A GeoPackage export that starts again feeds the features again, and counts them again. total = 0; + // The writers count the features; a source that doesn't know how many leaves the total at 0. + crate::progress::begin(src.info().count.map_or(0, |n| usize::try_from(n).unwrap_or(usize::MAX))); let batch = src.visit_chunks(None, bytes, &mut |mut chunk| { if let Some(t) = &t { - project_features(&mut chunk, t, crs)?; + // Counted as the writer writes them, so only stopped on a cancel here. + project_each(&mut chunk, t, crs, false)?; } total = total.saturating_add(chunk.len()); add(&chunk) diff --git a/crates/data/src/interop.rs b/crates/data/src/interop.rs index 0d1dc4785b..9aaa441741 100644 --- a/crates/data/src/interop.rs +++ b/crates/data/src/interop.rs @@ -62,6 +62,10 @@ pub fn to_kml(ds: &Dataset, name_field: Option) -> String { xml(&ds.name) ); for (r, f) in ds.features.iter().enumerate() { + // A cancelled export stops here; its caller then writes nothing. + if crate::progress::step(1).is_err() { + break; + } let Some(g) = &f.geometry else { continue }; let name = name_field.map(|fi| ds.value(r, fi).to_string()).unwrap_or_else(|| f.id.to_string()); let data: String = ds @@ -89,6 +93,10 @@ pub fn to_gpx(ds: &Dataset, name_field: Option) -> String { let pt = |c: &Coord| format!("lat=\"{}\" lon=\"{}\"", c.y, c.x); let mut tracks = String::new(); for (r, f) in ds.features.iter().enumerate() { + // A cancelled export stops here; its caller then writes nothing. + if crate::progress::step(1).is_err() { + break; + } let Some(g) = &f.geometry else { continue }; let name = xml(&name_field.map(|fi| ds.value(r, fi).to_string()).unwrap_or_else(|| f.id.to_string())); match g { diff --git a/crates/data/src/io.rs b/crates/data/src/io.rs index 1e4df175b2..a612401547 100644 --- a/crates/data/src/io.rs +++ b/crates/data/src/io.rs @@ -373,9 +373,22 @@ pub fn project(ds: &Dataset, crs: Crs) -> Result { } /// Converts features' longitude/latitude geometry in place to the coordinates of `crs`, through -/// its transformer `t`. Fails on a coordinate the system can't hold, naming its feature. +/// its transformer `t`, as a step of the progress. Fails on a coordinate the system can't hold, +/// naming its feature. pub(crate) fn project_features(features: &mut [crate::Feature], t: &crate::raster::Transformer, crs: Crs) -> Result<()> { + crate::progress::begin(features.len()); + project_each(features, t, crs, true) +} + +/// [`project_features`] without a step of its own: it counts each feature in the step under way +/// when `count` is set, and otherwise only stops once the run is cancelled. +pub(crate) fn project_each(features: &mut [crate::Feature], t: &crate::raster::Transformer, crs: Crs, count: bool) -> Result<()> { for f in features { + if count { + crate::progress::step(1)?; + } else { + crate::progress::check()?; + } if let Some(g) = f.geometry.take() { let mut ok = true; let p = g.map_coords(&|c| t.from_lonlat(c).unwrap_or(Coord::new(f64::NAN, f64::NAN))); @@ -445,6 +458,7 @@ pub fn write_bytes_in(ds: &Dataset, ext: &str, crs: Crs) -> Result<(Vec, usi /// [`write_bytes_in`] for a dataset already in `crs`'s coordinates. fn projected_bytes(ds: &Dataset, ext: &str, crs: Crs) -> Result<(Vec, usize)> { + crate::progress::begin(ds.features.len()); Ok(match ext { "geojson" | "json" => { let mut w = GeoJsonWriter::new(Vec::new(), &ds.name, &ds.fields)?; @@ -454,12 +468,24 @@ fn projected_bytes(ds: &Dataset, ext: &str, crs: Crs) -> Result<(Vec, usize) "csv" => (to_csv(ds)?.into_bytes(), 0), "fgb" => (crate::source::fgb::write_in(ds, crs)?, without_geometry(ds)), "parquet" | "geoparquet" => (crate::source::parquet::write(ds)?, 0), - "kml" => (crate::interop::to_kml(ds, name_field(ds)).into_bytes(), without_geometry(ds)), + "kml" => { + let kml = crate::interop::to_kml(ds, name_field(ds)); + // A cancelled run cuts the document short: it is never written. + crate::progress::check()?; + (kml.into_bytes(), without_geometry(ds)) + } "kmz" => { let kml = crate::interop::to_kml(ds, name_field(ds)).into_bytes(); + // A cancelled run cuts the document short: it is never written. + crate::progress::check()?; (crate::zip::write(&[("doc.kml".to_string(), kml)]).map_err(DataError::Format)?, without_geometry(ds)) } - "gpx" => (crate::interop::to_gpx(ds, name_field(ds)).into_bytes(), without_geometry(ds)), + "gpx" => { + let gpx = crate::interop::to_gpx(ds, name_field(ds)); + // A cancelled run cuts the document short: it is never written. + crate::progress::check()?; + (gpx.into_bytes(), without_geometry(ds)) + } "shp" | "zip" => { let (parts, left_out) = shapefile_parts_in(ds, crs)?; let base: String = ds.name.chars().map(|c| if c.is_alphanumeric() || matches!(c, '-' | '_' | ' ') { c } else { '_' }).collect(); @@ -496,6 +522,7 @@ pub fn write_path_in(ds: &Dataset, path: &Path, crs: Crs) -> Result { "gpkg" => { #[cfg(not(target_arch = "wasm32"))] { + crate::progress::begin(ds.features.len()); crate::source::gpkg::write_in(ds, path, crs)?; Ok(0) } @@ -503,6 +530,7 @@ pub fn write_path_in(ds: &Dataset, path: &Path, crs: Crs) -> Result { Err(DataError::Unsupported("GeoPackage in the browser".into())) } "shp" => { + crate::progress::begin(ds.features.len()); let (parts, left_out) = shapefile_parts_in(ds, crs)?; // Every part is written to a temporary file first and renamed into place only once all // are written, the .shp last, so a failed write leaves the old Shapefile whole. @@ -1198,6 +1226,7 @@ impl GeoJsonWriter { /// Writes the next features. pub(crate) fn write(&mut self, features: &[crate::Feature]) -> Result<()> { for f in features { + crate::progress::step(1)?; if !std::mem::take(&mut self.first) { self.out.write_all(b",")?; } @@ -1668,6 +1697,7 @@ impl CsvWriter { /// Writes the next features, a row each. pub(crate) fn write(&mut self, features: &[crate::Feature]) -> Result<()> { for f in features { + crate::progress::step(1)?; let mut row: Vec = f.attrs.iter().map(csv_cell).collect(); if self.point { let c = f.geometry.as_ref().and_then(|g| g.label_point()); @@ -2472,6 +2502,7 @@ pub fn shapefile_parts_in(ds: &Dataset, crs: Crs) -> Result<(Vec<(&'static str, let mut w = shapefile::Writer::new(shapes, table.build_with_dest(&mut dbf)); let multi_points = ds.features.iter().any(|f| matches!(f.geometry, Some(Geometry::MultiPoint(_)))); for f in &ds.features { + crate::progress::step(1)?; let ok = match (&f.geometry, ds.geometry_type) { (Some(Geometry::Point(c)), GeometryType::Point) if !multi_points && finite(&c) => { w.write_shape_and_record(&pt(c), &record(f)).map_err(fmt)?; @@ -3627,4 +3658,43 @@ mod tests { let note = shapefile_names_note(&layer(&[&huge]).fields, "h.shp").unwrap(); assert!(note.len() < 200 && note.contains(&format!("'{}…' → 'xxxxxxxxxx'", "x".repeat(40))), "{note}"); } + + /// A cancelled export stops with [`DataError::Cancelled`] and leaves nothing at (or beside) + /// its destination, in every format; without a probe the same exports are written. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn a_cancelled_export_leaves_no_file() { + use crate::progress::tests::Counted; + let fields = vec![Field::new("name", FieldType::Text), Field::new("n", FieldType::Integer)]; + let mut ds = Dataset::new("points", GeometryType::Point, fields); + for i in 0..3000u64 { + let c = Coord::new(i as f64 / 100.0, (i % 80) as f64); + ds.features.push(crate::Feature { + id: i + 1, + geometry: Some(Geometry::Point(c)), + attrs: vec![Value::Text(format!("p{i}")), Value::Int(i as i64)], + }); + } + let dir = tempfile::tempdir().unwrap(); + let exts = ["geojson", "csv", "fgb", "gpkg", "parquet", "shp", "kml", "gpx"]; + for ext in exts { + let path = dir.path().join(format!("out.{ext}")); + let probe = Counted::cancelling_after(100); + let res = { + let _g = crate::progress::install(Some(probe.clone())); + write_path_in(&ds, &path, Crs::LonLat) + }; + assert!(matches!(res, Err(DataError::Cancelled)), "{ext}: {res:?}"); + let left: Vec<_> = std::fs::read_dir(dir.path()).unwrap().map(|e| e.unwrap().file_name()).collect(); + assert!(left.is_empty(), "{ext} left {left:?}"); + } + for ext in exts { + let path = dir.path().join(format!("out.{ext}")); + write_path_in(&ds, &path, Crs::LonLat).unwrap(); + assert!(path.exists(), "{ext}"); + } + for e in ["shp", "shx", "dbf"] { + assert!(dir.path().join(format!("out.{e}")).exists()); + } + } } diff --git a/crates/data/src/lib.rs b/crates/data/src/lib.rs index cf7e0e9adb..c8ea656da5 100644 --- a/crates/data/src/lib.rs +++ b/crates/data/src/lib.rs @@ -31,6 +31,7 @@ pub mod memory; pub mod ogc; pub mod osm; pub mod photo; +pub mod progress; pub mod query; pub mod raster; pub mod s3_address; @@ -67,6 +68,9 @@ pub enum DataError { /// which Zarr reads as its fill value, told apart from a slice that can't be read. #[error("{0}: none of the slice's chunks could be read")] NoChunks(String), + /// Stopped because its run was cancelled ([`progress`]). + #[error("cancelled")] + Cancelled, } pub type Result = std::result::Result; @@ -102,6 +106,7 @@ impl DataError { DataError::Io(std::io::Error::new(e.kind(), without_secret(e.to_string(), secret))) } DataError::Io(e) => DataError::Io(e), + DataError::Cancelled => DataError::Cancelled, } } } diff --git a/crates/data/src/progress.rs b/crates/data/src/progress.rs new file mode 100644 index 0000000000..8602a7483a --- /dev/null +++ b/crates/data/src/progress.rs @@ -0,0 +1,128 @@ +//! Cancelling a long read or write and following its progress (#1291 step 3). Whoever runs it +//! (the app's export on a thread of its own) installs a [`Probe`] on that thread; the writers' +//! loops say how far they have come with [`begin`] and [`step`], and `step` stops them once the +//! probe was cancelled. With none installed, as for a script or the web app, they only run. +//! +//! The analysis crate's run control is a probe, so the same Cancel and progress reach a tool and +//! an export. + +use std::cell::RefCell; +use std::sync::Arc; + +use crate::{DataError, Result}; + +/// A run's cancel flag and progress count, as a writer sees them. +pub trait Probe: Send + Sync { + /// Whether the run was asked to stop. + fn cancelled(&self) -> bool; + /// Starts counting a step of `total` items (features, rows, tiles), from none done. + fn begin(&self, total: u64); + /// Counts `n` more items of the step done. + fn advance(&self, n: u64); +} + +thread_local! { + static CURRENT: RefCell>> = const { RefCell::new(None) }; +} + +/// Makes `probe` this thread's current one until the guard is dropped, which puts back the one +/// before. +pub fn install(probe: Option>) -> Installed { + Installed(CURRENT.with(|c| c.replace(probe))) +} + +/// Puts back the probe before when dropped (see [`install`]). +pub struct Installed(Option>); + +impl Drop for Installed { + fn drop(&mut self) { + let before = self.0.take(); + // Never during the thread's teardown: a guard outlives nothing that late. + let _ = CURRENT.try_with(|c| c.replace(before)); + } +} + +/// Starts counting a step of `total` items for the progress. +pub(crate) fn begin(total: usize) { + CURRENT.with(|c| { + if let Some(p) = c.borrow().as_ref() { + p.begin(total as u64); + } + }); +} + +/// Counts `n` more items done, and stops the write once its run was asked to: call it for each +/// feature, row or tile of a long loop. +pub(crate) fn step(n: usize) -> Result<()> { + CURRENT.with(|c| match c.borrow().as_ref() { + Some(p) if p.cancelled() => Err(DataError::Cancelled), + Some(p) => { + p.advance(n as u64); + Ok(()) + } + None => Ok(()), + }) +} + +/// Stops the write once its run was asked to, counting nothing: for a check after a loop that +/// can't stop by itself. +pub(crate) fn check() -> Result<()> { + CURRENT.with(|c| match c.borrow().as_ref() { + Some(p) if p.cancelled() => Err(DataError::Cancelled), + _ => Ok(()), + }) +} + +#[cfg(test)] +#[allow(clippy::unwrap_used)] +pub(crate) mod tests { + use super::*; + use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; + + /// A probe that counts, and is cancelled when told or (with `after`) once that many items + /// were counted. + #[derive(Default)] + pub(crate) struct Counted { + pub(crate) cancel: AtomicBool, + pub(crate) done: AtomicU64, + pub(crate) total: AtomicU64, + pub(crate) after: Option, + } + + impl Counted { + /// A probe cancelled once `n` items were counted. + pub(crate) fn cancelling_after(n: u64) -> Arc { + Arc::new(Counted { after: Some(n), ..Default::default() }) + } + } + + impl Probe for Counted { + fn cancelled(&self) -> bool { + self.cancel.load(Ordering::Relaxed) || self.after.is_some_and(|n| self.done.load(Ordering::Relaxed) >= n) + } + fn begin(&self, total: u64) { + self.total.store(total, Ordering::Relaxed); + self.done.store(0, Ordering::Relaxed); + } + fn advance(&self, n: u64) { + self.done.fetch_add(n, Ordering::Relaxed); + } + } + + #[test] + fn a_step_counts_until_cancelled_and_the_probe_before_comes_back() { + assert!(step(1).is_ok(), "without a probe a write only runs"); + let p = Arc::new(Counted::default()); + { + let _g = install(Some(p.clone())); + begin(10); + step(3).unwrap(); + assert_eq!((p.done.load(Ordering::Relaxed), p.total.load(Ordering::Relaxed)), (3, 10)); + p.cancel.store(true, Ordering::Relaxed); + assert!(matches!(step(1), Err(DataError::Cancelled))); + assert!(matches!(check(), Err(DataError::Cancelled))); + } + assert!(check().is_ok()); + assert!(step(1).is_ok(), "the guard put back no probe"); + } +} diff --git a/crates/data/src/raster/cogwrite.rs b/crates/data/src/raster/cogwrite.rs index 8da68f6624..dfc2c55b18 100644 --- a/crates/data/src/raster/cogwrite.rs +++ b/crates/data/src/raster/cogwrite.rs @@ -558,9 +558,11 @@ pub fn write_to(r: &Raster, band: usize, flavour: Flavour, out: // Smallest overview first, the full image last; each in row order. let mut places: Vec> = vec![Vec::new(); levels.len()]; + crate::progress::begin(tiles); for (i, lv) in levels.iter().enumerate().rev() { for ty in 0..lv.down { for tx in 0..lv.across { + crate::progress::step(1)?; let zipped = miniz_oxide::deflate::compress_to_vec_zlib(&tile_samples(r, &plan, lv, tx, ty), DEFLATE_LEVEL); if !big && pos + zipped.len() as u64 > u64::from(u32::MAX) { return Err(err("the file is too large for classic TIFF")); @@ -1002,4 +1004,25 @@ mod tests { let (alpha, _) = &cases[1]; assert_eq!((geotiff::written_bands(alpha, 0), written_bands(alpha, 0).unwrap()), (vec![0], vec![0, 1])); } + + /// A cancelled GeoTIFF or COG write stops with [`DataError::Cancelled`], the COG leaving no + /// file; without a probe both are written. + #[test] + fn a_cancelled_raster_write_stops() { + use crate::progress::{install, tests::Counted}; + let (w, h) = (1100u32, 1100u32); + let r = utm(w, h, vec![(0..w * h).map(|i| (i % 997) as f32).collect()], ColorHint::Gray, "Float32", None); + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("out.tif"); + { + let _g = install(Some(Counted::cancelling_after(2))); + assert!(matches!(write_file(&r, 0, &path), Err(DataError::Cancelled))); + // Cancelled before it starts: the GeoTIFF writer counts nothing. + let _g = install(Some(Counted::cancelling_after(0))); + assert!(matches!(geotiff::write(&r, 0), Err(DataError::Cancelled))); + } + assert_eq!(std::fs::read_dir(dir.path()).unwrap().count(), 0); + write_file(&r, 0, &path).unwrap(); + assert!(geotiff::write(&r, 0).is_ok()); + } } diff --git a/crates/data/src/raster/geotiff.rs b/crates/data/src/raster/geotiff.rs index 90c68299de..e1908fe77a 100644 --- a/crates/data/src/raster/geotiff.rs +++ b/crates/data/src/raster/geotiff.rs @@ -1391,12 +1391,43 @@ pub fn written_bands(r: &Raster, band: usize) -> Vec { /// Writes a raster as a Deflate-compressed GeoTIFF: a colour raster's colour bands (RGB(A) bytes /// when they are 8-bit, else 32-bit floats, alpha kept), so it reads back in colour; otherwise /// `band` (0-based) as 32-bit floats. `band` is for rasters that aren't colour ones: a colour -/// raster always writes its colour bands, as its 8-bit form did before. +/// raster always writes its colour bands, as its 8-bit form did before. A cancelled run stops +/// it (with [`DataError::Cancelled`]) within a strip. pub fn write(r: &Raster, band: usize) -> Result> { + match encode(r, band) { + Ok(bytes) => Ok(bytes), + // The encoder's error for a cancel only says the write stopped. + Err(e) => crate::progress::check().and(Err(e)), + } +} + +/// The file's bytes, written by the encoder a strip's compressed output at a time: each write +/// fails once the run was cancelled. It counts no progress: the encoder doesn't say where a +/// strip ends. +struct Probed(W); + +impl std::io::Write for Probed { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + crate::progress::check().map_err(std::io::Error::other)?; + self.0.write(buf) + } + fn flush(&mut self) -> std::io::Result<()> { + self.0.flush() + } +} + +impl Seek for Probed { + fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result { + self.0.seek(pos) + } +} + +/// [`write`], a cancel failing as any write error does. +fn encode(r: &Raster, band: usize) -> Result> { use tiff::encoder::{Compression, DeflateLevel, TiffEncoder, colortype}; // A scan's place is only where it was laid on the view: not coordinates to write into a file. r.require_coordinates().map_err(err)?; - let mut out = std::io::Cursor::new(Vec::new()); + let mut out = Probed(std::io::Cursor::new(Vec::new())); let mut enc = TiffEncoder::new(&mut out).map_err(err)?.with_compression(Compression::Deflate(DeflateLevel::Balanced)); let gt = r.transform; if gt.pixel_height > 0.0 || gt.pixel_width < 0.0 { @@ -1491,7 +1522,7 @@ pub fn write(r: &Raster, band: usize) -> Result> { tags!(img, &[band]); img.write_data(&b.data).map_err(err)?; } - Ok(out.into_inner()) + Ok(out.0.into_inner()) } #[cfg(test)] diff --git a/crates/data/src/source/fgb.rs b/crates/data/src/source/fgb.rs index 67a08d1887..b35b0b9da3 100644 --- a/crates/data/src/source/fgb.rs +++ b/crates/data/src/source/fgb.rs @@ -707,6 +707,7 @@ pub(crate) fn write_features( let mut left_out = 0; feed(&mut |features| { for feat in features { + crate::progress::step(1)?; let Some(g) = &feat.geometry else { left_out += 1; continue; diff --git a/crates/data/src/source/gpkg.rs b/crates/data/src/source/gpkg.rs index 16e33c9121..d9e2146e32 100644 --- a/crates/data/src/source/gpkg.rs +++ b/crates/data/src/source/gpkg.rs @@ -825,6 +825,7 @@ fn insert(conn: &Connection, layout: &Layout, features: &[crate::Feature], keep_ let mut index = if has_geom { Some(conn.prepare_cached(&format!("INSERT INTO {rt} VALUES (?1, ?2, ?3, ?4, ?5)")).map_err(err)?) } else { None }; for f in features { + crate::progress::step(1)?; if has_geom && !multi_points && matches!(f.geometry, Some(crate::Geometry::MultiPoint(_))) && layout.geom_type == "POINT" { return Ok(Some(Redo::MultiPoints)); } diff --git a/crates/data/src/source/parquet.rs b/crates/data/src/source/parquet.rs index fd9a2a9d34..bb848916a3 100644 --- a/crates/data/src/source/parquet.rs +++ b/crates/data/src/source/parquet.rs @@ -1242,6 +1242,7 @@ pub(crate) fn write_features( let mut extent = Extent::EMPTY; feed(&mut |features| { for rows in features.chunks(WRITE_GROUP_ROWS) { + crate::progress::step(rows.len())?; for g in rows.iter().filter_map(|f| f.geometry.as_ref()) { let t = wkb_type_name(g); if !types.contains(&t) { @@ -1313,6 +1314,8 @@ pub(crate) fn write_features( } Ok(()) })?; + // A run cancelled during the last row group gets no footer either. + crate::progress::check()?; if has_geom { types.sort_unstable(); let e = extent; diff --git a/crates/engine/src/cmd/exports.rs b/crates/engine/src/cmd/exports.rs new file mode 100644 index 0000000000..79f1c2f21e --- /dev/null +++ b/crates/engine/src/cmd/exports.rs @@ -0,0 +1,130 @@ +//! Exports that write a file from what the session shares (a layer's features, a raster's +//! pixels). A script or the command line writes it where the command is called; the app writes it +//! on a thread of its own with progress and Cancel (#1291 step 3): [`export_start`] reads the +//! session, [`ExportRun::run`] writes the file, and [`export_land`] says what was written, as the +//! command would have. It lands whatever layer is active or project is open by then: the file is +//! written either way, and an export changes nothing in the project. + +use serde_json::Value; + +use crate::{CmdResult, Session}; + +/// What an export wrote: the command's answer, and what it says of the file (the last says it +/// was exported). +pub struct Wrote { + pub(crate) answer: Value, + pub(crate) notes: Vec, +} + +/// The writing of an export, its inputs read: it needs no session. +pub(crate) type Work = Box Result + Send>; + +/// What an export reads of the session, and the work that writes its file. +type Start = fn(&Session, &Value) -> Result; + +/// The exports the app writes on another thread ([`export_start`]), each with what it is called +/// (in its progress, and a cancelled run's error) and what it reads of the session. +const IN_BACKGROUND: &[(&str, &str, Start)] = &[ + ("share.exportLayer", "Export Features", super::share::layer_work), + ("share.exportRaster", "Export Raster", super::raster::export_work), +]; + +/// `command` and what it is called, when the app writes its file on another thread (see +/// [`export_start`]). +pub fn runs_in_background(command: &str) -> Option<(&'static str, &'static str)> { + IN_BACKGROUND.iter().find(|(c, ..)| *c == command).map(|(c, name, _)| (*c, *name)) +} + +/// `command`'s export written here: it says what it wrote. +pub(crate) fn write_here(s: &mut Session, p: &Value, command: &str) -> CmdResult { + let &(_, name, start) = IN_BACKGROUND.iter().find(|(c, ..)| *c == command).ok_or_else(|| format!("{command} isn't an export"))?; + let work = start(s, p)?; + // Where a person at a terminal can stop it with Ctrl-C, as on the app's thread. + let wrote = if s.stoppable { written_under(name, &rustgis_analysis::control::Control::stoppable(), work)? } else { work()? }; + Ok(said(s, wrote)) +} + +/// `work` under `control`, as [`rustgis_analysis::run_under`] runs it, except that a file once +/// placed is written: a Cancel that comes after the last step doesn't turn it into a failure. +fn written_under(name: &str, control: &std::sync::Arc, work: Work) -> Result { + let mut wrote = None; + let res = rustgis_analysis::run_under(name, control, || work().map(|w| wrote = Some(w))); + match wrote { + Some(w) => Ok(w), + None => Err(res.err().unwrap_or_else(|| format!("{name} wrote nothing"))), + } +} + +/// Says what an export wrote, and gives its answer. +fn said(s: &mut Session, wrote: Wrote) -> Value { + for note in wrote.notes { + s.notify(note, false); + } + wrote.answer +} + +#[cfg(not(target_arch = "wasm32"))] +/// An export ([`IN_BACKGROUND`]) started to write its file on another thread ([`ExportRun::run`]) +/// while the session goes on: its inputs are read now (cheaply: the features and pixels are +/// shared), and [`export_land`] says what it wrote. +pub struct ExportRun { + /// What the export is called, for its progress and a cancelled run's error. + pub name: &'static str, + work: Work, +} + +#[cfg(not(target_arch = "wasm32"))] +impl ExportRun { + /// Writes the file, blocking until it's written; `control` stops it and counts its progress + /// (see [`rustgis_analysis::control`]). A cancelled export leaves no file, or the one there + /// before. + pub fn run(self, control: &std::sync::Arc) -> Result { + let ExportRun { name, work } = self; + // A bug in a writer must not take the app down. + std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| written_under(name, control, work))) + .unwrap_or_else(|_| Err(format!("{name} failed unexpectedly"))) + } +} + +#[cfg(not(target_arch = "wasm32"))] +/// Starts `command` (one of [`IN_BACKGROUND`]'s) with `p`, to write its file on another thread: +/// its parameters checked and its inputs read now. +pub fn export_start(s: &mut Session, command: &str, p: &Value) -> Result { + let &(_, name, start) = + IN_BACKGROUND.iter().find(|(c, ..)| *c == command).ok_or_else(|| format!("{command} doesn't run in the background"))?; + Ok(ExportRun { name, work: start(s, p)? }) +} + +#[cfg(not(target_arch = "wasm32"))] +/// Says what [`ExportRun::run`] wrote, as its command would have, and gives its answer. +pub fn export_land(s: &mut Session, wrote: Wrote) -> Value { + said(s, wrote) +} + +#[cfg(test)] +#[allow(clippy::unwrap_used)] +mod tests { + use super::*; + use rustgis_analysis::control::Control; + + #[test] + fn a_cancel_after_the_file_is_placed_leaves_it_written() { + let control = Control::new(); + let late = control.clone(); + // The work places its file, then Cancel comes before the run returns. + let work: Work = Box::new(move || { + late.cancel(); + Ok(Wrote { answer: serde_json::json!({"path": "x"}), notes: vec!["Exported x".into()] }) + }); + let wrote = written_under("Export Raster", &control, work).unwrap(); + assert_eq!(wrote.notes, ["Exported x"]); + // A writer that stopped for the cancel is the cancelled run's error. + let control = Control::new(); + let stopped = control.clone(); + let work: Work = Box::new(move || { + stopped.cancel(); + Err("cancelled".into()) + }); + assert!(written_under("Export Raster", &control, work).err().unwrap().contains("was cancelled")); + } +} diff --git a/crates/engine/src/cmd/mod.rs b/crates/engine/src/cmd/mod.rs index f7c4efd28f..d197735633 100644 --- a/crates/engine/src/cmd/mod.rs +++ b/crates/engine/src/cmd/mod.rs @@ -13,6 +13,7 @@ pub mod earthdata; pub mod edit; pub mod effects; pub mod embeddings; +pub mod exports; pub mod geocode; pub mod geolibre; pub mod gp; diff --git a/crates/engine/src/cmd/raster.rs b/crates/engine/src/cmd/raster.rs index 912696c85a..8c900d55d2 100644 --- a/crates/engine/src/cmd/raster.rs +++ b/crates/engine/src/cmd/raster.rs @@ -87,7 +87,7 @@ pub fn commands() -> Vec { r#"{"fromLayer"?: bool (the active or named layer's colour ramp stretch; the settings apply to every raster added later that would be a grey stretch, opened files and analysis results alike), "layer"?, "ramp"?, "invert"?: bool, "stretch"?: "none"|"minMax"|"percentClip"|"standardDeviation"|"histogramEqualize", "low"?, "high"?: %, "n"?: deviations, "resampling"?: "nearest"|"bilinear", "reset"?: bool (back to the black-to-white grey stretch)}"#, ), C::new("raster.info", "Raster Information", "Data › Information", info).view().when(need_raster).params(r#"{"layer"?}"#), - C::new("share.exportRaster", "Export Raster", "Data › Information", export).view().when(need_raster).params( + C::new("share.exportRaster", "Export Raster", "Data › Information", |s, p| super::exports::write_here(s, p, "share.exportRaster")).view().when(need_raster).params( r#"{"layer"?, "path": "*.tif", "band"?: 1.. (rasters that aren't colour ones; a colour raster writes its colour bands), "cog"?: bool (a cloud-optimised GeoTIFF: 512 px tiles, Deflate, overviews; default false, an untiled GeoTIFF)}"#, ), ] @@ -891,29 +891,36 @@ pub fn geotiff_bytes(s: &Session, p: &Value) -> Result<(String, Vec), String Ok((r.name.clone(), bytes.map_err(|e| e.to_string())?)) } -fn export(s: &mut Session, p: &Value) -> CmdResult { +/// `share.exportRaster`'s inputs, and the work that writes the raster as a GeoTIFF (a +/// cloud-optimised one with `cog`). +pub(crate) fn export_work(s: &Session, p: &Value) -> Result { let path = ps(p, "path").ok_or("path is required")?.to_string(); let (_, r) = get(s, p)?; - let mut cog = None; - if b(p, "cog") == Some(true) { + let bi = band(p, "band", &r)?.unwrap_or(0); + let cog = b(p, "cog") == Some(true); + Ok(Box::new(move || write_geotiff(&r, bi, cog, &path))) +} + +/// Writes raster `r` (band `bi`, or its colour bands) to `path`, and says what the file holds. +fn write_geotiff(r: &Raster, bi: usize, cog: bool, path: &str) -> Result { + let cog = if cog { // Tiles go to the file as they are made: the whole file is never held in memory. - let bi = band(p, "band", &r)?.unwrap_or(0); - cog = Some(raster::cogwrite::write_file(&r, bi, Path::new(&path)).map_err(|e| format!("{path}: {e}"))?); + Some(raster::cogwrite::write_file(r, bi, Path::new(path)).map_err(|e| format!("{path}: {e}"))?) } else { - let (_, bytes) = geotiff_bytes(s, p)?; - rustgis_data::io::write_atomic(Path::new(&path), &bytes).map_err(|e| format!("{path}: {e}"))?; - } + let bytes = raster::geotiff::write(r, bi).map_err(|e| e.to_string())?; + rustgis_data::io::write_atomic(Path::new(path), &bytes).map_err(|e| format!("{path}: {e}"))?; + None + }; let mut note = match r.overview { Some((fw, fh)) => format!(" (the {} × {} overview of the {fw} × {fh} raster)", r.width, r.height), None => String::new(), }; // Values changed when the file was read are written as they are held: those of the bands the // writer writes, as it chooses them. - let bi = band(p, "band", &r)?.unwrap_or(0); let bands = if cog.is_some() { - raster::cogwrite::written_bands(&r, bi).map_err(|e| e.to_string())? + raster::cogwrite::written_bands(r, bi).map_err(|e| e.to_string())? } else { - raster::geotiff::written_bands(&r, bi) + raster::geotiff::written_bands(r, bi) }; let written: u64 = bands.iter().filter_map(|b| r.rounded.get(*b)).sum(); if written > 0 { @@ -932,11 +939,10 @@ fn export(s: &mut Session, p: &Value) -> CmdResult { if !unnamed.is_empty() { note.push_str(&format!("; the class names of band(s) {} aren't written: a GeoTIFF keeps the first band's", unnamed.join(", "))); } - let spaced = raster::gdal_metadata_changed(&r, &bands); + let spaced = raster::gdal_metadata_changed(r, &bands); if spaced > 0 { note.push_str(&format!("; {spaced} band or class name(s) held control characters, written as spaces")); } - s.notify(format!("Exported {path}{note}"), false); let mut v = match r.overview { Some((fw, fh)) => json!({"path": path, "width": r.width, "height": r.height, "overviewOf": [fw, fh]}), None => json!({"path": path}), @@ -944,7 +950,7 @@ fn export(s: &mut Session, p: &Value) -> CmdResult { if let (Some(o), Some(c)) = (v.as_object_mut(), cog) { o.insert("cog".into(), json!({"overviews": c.levels.len().saturating_sub(1), "bigTiff": c.big, "bytes": c.bytes})); } - Ok(v) + Ok(super::exports::Wrote { answer: v, notes: vec![format!("Exported {path}{note}")] }) } /// Pixel values of the topmost visible raster under a longitude/latitude point. diff --git a/crates/engine/src/cmd/share.rs b/crates/engine/src/cmd/share.rs index d830fa05d2..45ea3f8b60 100644 --- a/crates/engine/src/cmd/share.rs +++ b/crates/engine/src/cmd/share.rs @@ -7,6 +7,7 @@ use rustgis_data::raster::Crs; use rustgis_render::View; use serde_json::{Value, json}; +use super::exports::{Work, Wrote}; use super::{f, need_layer, ps}; use crate::{CmdResult, CommandSpec as C, Session}; @@ -15,7 +16,7 @@ pub fn commands() -> Vec { C::new("share.exportMap", "Export Map", "Publish › Output", export_map) .view() .params(r#"{"path": "*.png", "width"?: px, "height"?: px, "dpi"?: 96..600}"#), - C::new("share.exportLayer", "Export Features", "Publish › Output", export_layer) + C::new("share.exportLayer", "Export Features", "Publish › Output", |s, p| super::exports::write_here(s, p, "share.exportLayer")) .view() .when(need_layer) .params(r#"{"layer"?, "path": "*.geojson|*.csv|*.fgb|*.gpkg|*.parquet|*.shp|*.zip|*.kml|*.kmz|*.gpx", "crs"?: "EPSG:… (or WKT) for a GeoPackage, Shapefile or FlatGeobuf (default: the system the layer was read in, else EPSG:4326)"}"#), @@ -102,10 +103,18 @@ fn export_map(s: &mut Session, p: &Value) -> CmdResult { /// what the file couldn't hold: features without drawable geometry, a missing `.prj`, Z or M /// values dropped on import, positions that are approximate or aren't longitude/latitude, field /// names a Shapefile cuts. -fn export_layer(s: &mut Session, p: &Value) -> CmdResult { +pub(crate) fn layer_work(s: &Session, p: &Value) -> Result { let id = s.layer_id(p)?; let path = ps(p, "path").ok_or("path is required")?.to_string(); - let l = s.map().layer(id).ok_or("no layer")?; + // The layer as it is now: its features are shared, and a streamed one's source is read on the + // export's thread. + let l = s.map().layer(id).ok_or("no layer")?.clone(); + let asked = ps(p, "crs").map(str::trim).filter(|c| !c.is_empty()).map(Crs::parse).transpose()?; + Ok(Box::new(move || write_layer(&l, &path, asked))) +} + +/// Writes layer `l` to `path`, in `asked` or the system the format defaults to. +fn write_layer(l: &rustgis_map::Layer, path: &str, asked: Option) -> Result { let ext = Path::new(&path).extension().and_then(|e| e.to_str()).unwrap_or("").to_ascii_lowercase(); // A streamed layer exports its whole source, not the features read for the view: a chunk at // a time where the source and format allow, so its size isn't limited by memory. @@ -114,10 +123,7 @@ fn export_layer(s: &mut Session, p: &Value) -> CmdResult { let ds = chunks.as_ref().map(|(_, layer)| layer).or(whole.as_ref()).unwrap_or(&l.dataset); // GeoJSON, KML and GPX are longitude/latitude; the others keep the layer's own system unless // told another. - let crs = match ps(p, "crs").map(str::trim).filter(|c| !c.is_empty()) { - Some(text) => Crs::parse(text)?, - None => rustgis_data::io::default_export_crs(ds, &ext), - }; + let crs = asked.unwrap_or_else(|| rustgis_data::io::default_export_crs(ds, &ext)); let (written, left_out, skipped) = match &chunks { Some((src, layer)) => { let e = rustgis_data::export::write_source_in(src.as_ref(), layer, Path::new(&path), crs).map_err(|e| e.to_string())?; @@ -129,31 +135,28 @@ fn export_layer(s: &mut Session, p: &Value) -> CmdResult { } }; // A streamed layer's extent is its whole source's. - let notes = rustgis_data::export_notes([(ds, l.extent())], &path, crs); + let notes = rustgis_data::export_notes([(ds, l.extent())], path, crs); // A Shapefile (`.shp`, or zipped to `.zip`) cuts field names to dBase's. let shapefile = matches!(ext.as_str(), "shp" | "zip"); let mut dbf_notes = Vec::new(); if shapefile { - dbf_notes.extend(rustgis_data::io::shapefile_names_note(&ds.fields, &path)); - dbf_notes.extend(rustgis_data::io::shapefile_values_notes(ds, &path)); + dbf_notes.extend(rustgis_data::io::shapefile_names_note(&ds.fields, path)); + dbf_notes.extend(rustgis_data::io::shapefile_values_notes(ds, path)); } // FlatGeobuf, KML, GPX and Shapefile hold features with geometry only (a Shapefile, only // geometry it can draw). + let mut said = Vec::new(); if left_out > 0 { - s.notify(format!("{path}: {left_out} features without drawable geometry were left out"), false); + said.push(format!("{path}: {left_out} features without drawable geometry were left out")); } if skipped > 0 { - s.notify(format!("{path}: {skipped} rows couldn't be read and were left out"), false); - } - for note in notes { - s.notify(note, false); - } - for note in dbf_notes { - s.notify(note, false); + said.push(format!("{path}: {skipped} rows couldn't be read and were left out")); } + said.extend(notes); + said.extend(dbf_notes); if shapefile && rustgis_data::raster::wkt::to_wkt(crs).is_none() { - s.notify(format!("{path}: no .prj written, as {} has no WKT form", crs.label()), false); + said.push(format!("{path}: no .prj written, as {} has no WKT form", crs.label())); } - s.notify(format!("Exported {path}"), false); - Ok(json!({"path": path, "features": written, "leftOut": left_out, "crs": crs.label()})) + said.push(format!("Exported {path}")); + Ok(Wrote { answer: json!({"path": path, "features": written, "leftOut": left_out, "crs": crs.label()}), notes: said }) } diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index c02e457b12..c2755fc3fe 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -26488,6 +26488,71 @@ fn raster_tools_run_on_another_thread() { assert!(runs_in_background("raster.editPixels").is_none()); } +/// Export Features and Export Raster written on another thread, as the app writes them (#1291 +/// step 3): the file, the answer and what the export says are those of one written here, nothing +/// is said until it lands, and one cancelled before it starts writes nothing (a cancel in the +/// middle of a write is the data crate's test). +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn exports_run_on_another_thread() { + use crate::cmd::exports::{export_land, export_start, runs_in_background}; + use rustgis_analysis::control::Control; + let mut s = world(); + s.run("layer.addDataBytes", &json!({"fileName": "dem.tif", "data": dem_b64()})).unwrap(); + let dem = s.map().layers.iter().find(|l| l.is_raster()).unwrap().name.clone(); + let dir = std::env::temp_dir().join(format!("rustgis-exports-thread-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let at = |name: &str| dir.join(name).display().to_string(); + for (command, file, p) in [ + ("share.exportLayer", "countries.geojson", json!({"layer": "Countries"})), + ("share.exportLayer", "countries.shp", json!({"layer": "Countries"})), + ("share.exportLayer", "countries.gpkg", json!({"layer": "Countries", "crs": "EPSG:3857"})), + ("share.exportRaster", "dem.tif", json!({"layer": dem})), + ("share.exportRaster", "dem_cog.tif", json!({"layer": dem, "cog": true})), + ] { + assert!(runs_in_background(command).is_some(), "{command}"); + let (here, there, cancelled) = (at(&format!("here_{file}")), at(&format!("there_{file}")), at(&format!("cancelled_{file}"))); + let with = |path: &str| { + let mut q = p.clone(); + q["path"] = json!(path); + q + }; + let said = |s: &Session, mark: u64, path: &str| -> Vec { + s.notices_since(mark).iter().map(|n| n.text.replace(path, "PATH")).collect() + }; + let mark = s.notice_mark(); + let answer_here = s.run(command, &with(&here)).unwrap(); + let said_here = said(&s, mark, &here); + let run = export_start(&mut s, command, &with(&there)).unwrap(); + let mark = s.notice_mark(); + let wrote = std::thread::spawn(move || run.run(&Control::new())).join().unwrap().unwrap(); + assert!(s.notices_since(mark).is_empty(), "{command}: nothing is said before it lands"); + let answer_there = export_land(&mut s, wrote); + assert_eq!(said(&s, mark, &there), said_here, "{command} {file}"); + let unpathed = |v: &Value, path: &str| v.to_string().replace(path, "PATH"); + assert_eq!(unpathed(&answer_there, &there), unpathed(&answer_here, &here), "{command} {file}"); + // A GeoPackage holds the time it was written: its features are compared instead. + if file.ends_with(".gpkg") { + let read = |path: &str| rustgis_data::io::read_path(std::path::Path::new(path)).unwrap().0.features.len(); + assert_eq!(read(&there), read(&here), "{file}"); + } else { + assert_eq!(std::fs::read(&there).unwrap(), std::fs::read(&here).unwrap(), "{file}"); + } + let run = export_start(&mut s, command, &with(&cancelled)).unwrap(); + let control = Control::new(); + control.cancel(); + assert!(run.run(&control).err().unwrap().contains("was cancelled"), "{command}"); + assert!(!std::path::Path::new(&cancelled).exists(), "{file}: a cancelled export writes nothing"); + } + // Their inputs are read when they start: a missing layer or a bad system is refused then. + assert!(export_start(&mut s, "share.exportLayer", &json!({"layer": "nowhere", "path": at("x.geojson")})).is_err()); + assert!(export_start(&mut s, "share.exportLayer", &json!({"layer": "Countries", "path": at("x.gpkg"), "crs": "EPSG:nope"})).is_err()); + assert!(export_start(&mut s, "share.exportRaster", &json!({"layer": dem, "path": at("x.tif"), "band": 99})).is_err()); + // Map and layout exports draw the map: they still run where they're called. + assert!(runs_in_background("share.exportMap").is_none() && runs_in_background("layout.export").is_none()); + let _ = std::fs::remove_dir_all(&dir); +} + /// k-means classes and a deep learning classification made on another thread, as the Analysis pane /// runs them (#1291 step 3): the classes handed over (`ran`) are the ones the command makes here, a /// model trained on the thread is kept in the project only once its classes are added, and a raster diff --git a/crates/ui-egui/src/chrome.rs b/crates/ui-egui/src/chrome.rs index 3ad67d8cec..d3c0752172 100644 --- a/crates/ui-egui/src/chrome.rs +++ b/crates/ui-egui/src/chrome.rs @@ -339,6 +339,12 @@ pub fn status_bar(app: &mut RustGisApp, ui: &mut Ui) { let _ = app.run("select.clear", Value::Null); } } + // An export writing its file: its progress and Cancel. + #[cfg(not(target_arch = "wasm32"))] + if app.export_job.is_some() { + sep(ui, &t); + crate::exportjob::status(app, ui, &t); + } ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { // The CPU/GPU backend toggle, then the frame rate (live while the map moves, // "— fps" when idle), then the last render's cost. diff --git a/crates/ui-egui/src/exportjob.rs b/crates/ui-egui/src/exportjob.rs new file mode 100644 index 0000000000..c62d60cc70 --- /dev/null +++ b/crates/ui-egui/src/exportjob.rs @@ -0,0 +1,113 @@ +//! Export Features and Export Raster on the desktop write their file on a thread of their own +//! (#1291 step 3), so a large layer or raster doesn't hold the window: the status bar shows the +//! export under way, how far it has come and Cancel, and [`land`] says what it wrote, as the +//! command says when it runs here. + +use std::sync::Arc; + +use egui::Ui; +use rustgis_engine::analysis::control::Control; +use rustgis_engine::cmd::exports::{self, Wrote}; +use serde_json::{Value, json}; + +use crate::RustGisApp; +use crate::theme::Tokens; + +/// An export under way: what it is called, its Cancel and progress, and what it wrote once done. +pub struct ExportJob { + name: &'static str, + control: Arc, + slot: crate::Slot>, +} + +/// Starts `command` ([`exports::runs_in_background`]) with `p` on a thread of its own. One at a +/// time: a second is refused while the first runs. +pub fn start(app: &mut RustGisApp, command: &'static str, p: Value) -> Result { + if let Some(job) = &app.export_job { + return Err(crate::i18n::fmt("{tool} is still running: wait for it, or cancel it.", &[("tool", tl_raw!(job.name))])); + } + let run = exports::export_start(&mut app.session, command, &p)?; + let name = run.name; + // Cancel stops it. + let control = Control::stoppable(); + let (ctl, slot) = (control.clone(), crate::Slot::default()); + let out = slot.clone(); + std::thread::Builder::new() + .name(name.into()) + .spawn(move || { + let wrote = run.run(&ctl); + // Filled even when its lock is poisoned, or the export would show as running forever. + *out.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = Some(wrote); + }) + .map_err(|e| e.to_string())?; + app.export_job = Some(ExportJob { name, control, slot }); + Ok(json!({"started": name})) +} + +/// Says what an export wrote once it's done (or why it failed): called every frame. `true` while +/// one is still running. +pub fn land(app: &mut RustGisApp) -> bool { + let Some(job) = &app.export_job else { return false }; + let done = job.slot.lock().unwrap_or_else(std::sync::PoisonError::into_inner).take(); + let Some(done) = done else { return true }; + app.export_job = None; + match done { + // Its notices are the command's; the answer was for a script, which waits for its own. + Ok(wrote) => { + exports::export_land(&mut app.session, wrote); + } + Err(e) => app.session.notify(e, true), + } + false +} + +/// The export under way in the status bar: its name, progress and Cancel. +pub fn status(app: &RustGisApp, ui: &mut Ui, t: &Tokens) { + let Some(job) = &app.export_job else { return }; + ui.spinner(); + // Two labels, not one sentence put together from translated pieces. + ui.label(egui::RichText::new(tl!("Running…")).size(11.5).color(t.text_dim)); + ui.label(egui::RichText::new(tl!(job.name)).size(11.5).strong()); + // How far its current step has come, when its writer counts. + if let Some((done, total)) = job.control.progress() { + ui.add(egui::ProgressBar::new(done as f32 / total.max(1) as f32).show_percentage().desired_width(120.0).desired_height(12.0)); + } + if ui.add_enabled(!job.control.is_cancelled(), egui::Button::new(tl!("Cancel")).small()).clicked() { + job.control.cancel(); + } +} + +#[cfg(test)] +#[allow(clippy::unwrap_used)] +mod tests { + use super::*; + + #[test] + fn export_features_writes_on_a_thread_and_lands() { + let mut app = RustGisApp::with_sample(); + app.session.active_layer = app.session.map().layer_by_name("Cities").map(|l| l.id); + let dir = std::env::temp_dir().join(format!("rustgis-export-job-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let (path, other) = (dir.join("cities.geojson"), dir.join("again.geojson")); + let pick = |p: &std::path::Path| crate::dialogs::PICKED.with(|c| *c.borrow_mut() = Some(p.display().to_string())); + pick(&path); + assert_eq!(app.run("ui.exportLayer", json!({"format": "geojson"})).unwrap()["started"], "Export Features"); + // One at a time: a second waits for the first. + pick(&other); + assert!(app.run("ui.exportLayer", json!({"format": "geojson"})).err().unwrap().contains("still running")); + let end = std::time::Instant::now() + std::time::Duration::from_secs(60); + // Another layer picked while it writes: the export still lands, for the layer it started on. + app.session.active_layer = app.session.map().layer_by_name("Countries").map(|l| l.id); + while land(&mut app) { + assert!(std::time::Instant::now() < end, "the export never landed"); + std::thread::sleep(std::time::Duration::from_millis(5)); + } + assert!(app.export_job.is_none()); + let exported = format!("Exported {}", path.display()); + assert!(app.session.notices.iter().any(|n| n.text == exported && !n.error), "{:?}", app.session.notices.last()); + assert!(!other.exists(), "the refused export wrote nothing"); + let (written, _) = rustgis_engine::data::io::read_path(&path).unwrap(); + assert_eq!(written.features.len(), app.session.map().layer_by_name("Cities").unwrap().dataset.features.len()); + let _ = std::fs::remove_dir_all(&dir); + } +} diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 874c8a7f1d..a3b25cbc1e 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -33,6 +33,8 @@ mod docpanes; mod earthaccessui; mod editpanes; mod embeddingsui; +#[cfg(not(target_arch = "wasm32"))] +mod exportjob; mod failureui; mod fieldlists; mod fieldmenu; @@ -921,6 +923,9 @@ pub struct RustGisApp { pub(crate) graphic_format: graphicformat::GraphicFormat, pub forms: editpanes::Forms, pub workflow: analysispane::Workflow, + /// An export writing its file on its own thread (see [`exportjob`]). + #[cfg(not(target_arch = "wasm32"))] + export_job: Option, /// ModelBuilder's view: the model on its canvas. pub mb: modelbuilder::ModelBuilder, pub ws: workspacepanes::WorkspaceState, @@ -1104,6 +1109,8 @@ impl RustGisApp { graphic_format: graphicformat::GraphicFormat::default(), forms: editpanes::Forms { distance_units: "Meters".into(), ..editpanes::Forms::default() }, workflow: analysispane::Workflow::default(), + #[cfg(not(target_arch = "wasm32"))] + export_job: None, mb: modelbuilder::ModelBuilder::default(), ws: workspacepanes::WorkspaceState::default(), time: timebar::TimeBarState::default(), @@ -3420,6 +3427,10 @@ impl RustGisApp { .map(|l| format!("{}.tif", l.name)) .unwrap_or_default(); if let Some(path) = dialogs::pick_save(&name, &["tif", "tiff"], tl!("GeoTIFF")) { + // Written on a thread of its own, with progress and Cancel in the status bar. + #[cfg(not(target_arch = "wasm32"))] + return exportjob::start(self, "share.exportRaster", json!({"path": path})); + #[cfg(target_arch = "wasm32")] return self.run("share.exportRaster", json!({"path": path})); } } @@ -3447,6 +3458,10 @@ impl RustGisApp { // in the name, as in "U.S. Cities", isn't taken for one). let typed = std::path::Path::new(&path).extension().is_some_and(|e| e.eq_ignore_ascii_case(&ext)); let path = if typed { path } else { format!("{path}.{ext}") }; + // Written on a thread of its own, with progress and Cancel in the status bar. + #[cfg(not(target_arch = "wasm32"))] + return exportjob::start(self, "share.exportLayer", json!({"path": path})); + #[cfg(target_arch = "wasm32")] return self.run("share.exportLayer", json!({"path": path})); } } @@ -4129,11 +4144,11 @@ impl RustGisApp { fieldmenu::play(self, &ctx); chrome::shortcuts(self, &ctx); panes::follow_layer_filter(self); - // A Zarr summary, a trend or a workflow running on its thread lands whatever is in view, - // the File menu's backstage too, so a project saved there holds it (all are looked at: `|`, - // not `||`), and the frames go on while one runs so it's taken when it ends. + // A Zarr summary, a trend, a workflow or an export running on its thread lands whatever is + // in view, the File menu's backstage too, so a project saved there holds it (all are looked + // at: `|`, not `||`), and the frames go on while one runs so it's taken when it ends. #[cfg(not(target_arch = "wasm32"))] - if workspacepanes::land_summary(self) | analysispane::land_trend(self) | analysispane::land_job(self) { + if workspacepanes::land_summary(self) | analysispane::land_trend(self) | analysispane::land_job(self) | exportjob::land(self) { ctx.request_repaint_after(std::time::Duration::from_millis(200)); } if self.ui.backstage { diff --git a/docs/user-guide/sharing.md b/docs/user-guide/sharing.md index 8da298bf8d..c9f928f979 100644 --- a/docs/user-guide/sharing.md +++ b/docs/user-guide/sharing.md @@ -19,6 +19,13 @@ table. **Data › Export** also writes rasters to GeoTIFF, and **Data Interop** KML, GPX or CSV with WKT. Layouts, reports and presentations export from their own tabs and panes (see [Layouts and Reports](layouts.md)). +On the desktop, Export Features and Export Raster write their file in the background, so the map +and the rest of the window stay in use. While one runs, the status bar shows its name, a progress +bar (Export Raster's untiled GeoTIFF shows none) and **Cancel**; when it ends its messages appear +as before. One export runs at a time. A cancelled export writes nothing, and a file it was to +replace stays as it was. Scripts, models, the AI Assistant and the command line still wait for +the file. Export Map and layout exports still run before the window goes on. + GeoPackage exports require a new filename. Exporting to an existing `.gpkg` is refused to protect its tables and data, even if the save dialog offers to replace the file. Export does not append a table to an existing GeoPackage or replace one of its tables. From 1c8a94df84760e3b718c39968985e470c14de264 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Wed, 4 Nov 2026 17:19:39 +0100 Subject: [PATCH 2/4] fix(data): a Cancel that comes as an export's file is put in place leaves the old file (#1433 review) --- crates/data/src/io.rs | 29 +++++++++++++++++++++++++++-- crates/data/src/lib.rs | 13 ++++++++++++- crates/data/src/raster/cogwrite.rs | 2 ++ crates/data/src/source/gpkg.rs | 2 ++ crates/engine/src/cmd/exports.rs | 7 ++++--- 5 files changed, 47 insertions(+), 6 deletions(-) diff --git a/crates/data/src/io.rs b/crates/data/src/io.rs index a612401547..64fceb1040 100644 --- a/crates/data/src/io.rs +++ b/crates/data/src/io.rs @@ -545,11 +545,12 @@ pub fn write_path_in(ds: &Dataset, path: &Path, crs: Crs) -> Result { } result }); - if let Err(e) = staged { + // A cancel before the parts land leaves the old Shapefile as it was. + if let Err(e) = staged.map_err(DataError::from).and_then(|()| crate::progress::check()) { for (tmp, _) in &written { let _ = std::fs::remove_file(tmp); } - return Err(e.into()); + return Err(e); } written.sort_by_key(|(_, target)| target == path); // The old parts are moved aside first and put back if any new part fails to land, so @@ -676,6 +677,10 @@ pub fn write_atomic_with>( out.get_ref().sync_all()?; // Closed before the rename, including on Windows. drop(out); + // A cancel that came while it was written leaves the file there before as it was. + if crate::progress::check().is_err() { + return Err(std::io::Error::other(DataError::Cancelled).into()); + } std::fs::rename(&tmp, path)?; std::mem::forget(staged); Ok(value) @@ -3688,6 +3693,26 @@ mod tests { let left: Vec<_> = std::fs::read_dir(dir.path()).unwrap().map(|e| e.unwrap().file_name()).collect(); assert!(left.is_empty(), "{ext} left {left:?}"); } + // Cancelled once every feature is written, before the file is moved into place: a file + // there before stays as it was (a GeoPackage export never replaces one). + for ext in exts { + let path = dir.path().join(format!("old.{ext}")); + if ext != "gpkg" { + std::fs::write(&path, b"old").unwrap(); + } + let probe = Counted::cancelling_after(ds.features.len() as u64); + let res = { + let _g = crate::progress::install(Some(probe.clone())); + write_path_in(&ds, &path, Crs::LonLat) + }; + assert!(matches!(res, Err(DataError::Cancelled)), "{ext}: {res:?}"); + if ext != "gpkg" { + assert_eq!(std::fs::read(&path).unwrap(), b"old", "{ext}"); + std::fs::remove_file(&path).unwrap(); + } + let left: Vec<_> = std::fs::read_dir(dir.path()).unwrap().map(|e| e.unwrap().file_name()).collect(); + assert!(left.is_empty(), "{ext} left {left:?}"); + } for ext in exts { let path = dir.path().join(format!("out.{ext}")); write_path_in(&ds, &path, Crs::LonLat).unwrap(); diff --git a/crates/data/src/lib.rs b/crates/data/src/lib.rs index c8ea656da5..aec3c4bd50 100644 --- a/crates/data/src/lib.rs +++ b/crates/data/src/lib.rs @@ -56,7 +56,7 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, thiserror::Error)] pub enum DataError { #[error("i/o: {0}")] - Io(#[from] std::io::Error), + Io(std::io::Error), #[error("{0}")] Format(String), /// The server refused the login: no password, or a wrong one. @@ -75,6 +75,17 @@ pub enum DataError { pub type Result = std::result::Result; +/// An I/O error, or [`DataError::Cancelled`] when it carries one: a write that only returns I/O +/// errors stops with it for a cancelled run ([`progress`]). +impl From for DataError { + fn from(e: std::io::Error) -> Self { + if e.get_ref().and_then(|inner| inner.downcast_ref::()).is_some_and(|d| matches!(d, DataError::Cancelled)) { + return DataError::Cancelled; + } + DataError::Io(e) + } +} + /// Replaces `secret` in `message` with asterisks; the text itself when it holds none. fn without_secret(message: String, secret: &str) -> String { if message.contains(secret) { message.replace(secret, "********") } else { message } diff --git a/crates/data/src/raster/cogwrite.rs b/crates/data/src/raster/cogwrite.rs index dfc2c55b18..3a4cedde73 100644 --- a/crates/data/src/raster/cogwrite.rs +++ b/crates/data/src/raster/cogwrite.rs @@ -617,6 +617,8 @@ pub fn write_file(r: &Raster, band: usize, path: &Path) -> Result { let f = w.into_inner().map_err(|e| err(e.error()))?; f.sync_all()?; drop(f); + // A cancel that came while it was written leaves the file there before as it was. + crate::progress::check()?; std::fs::rename(&tmp, path)?; Ok(info) })(); diff --git a/crates/data/src/source/gpkg.rs b/crates/data/src/source/gpkg.rs index d9e2146e32..0ae10c26d6 100644 --- a/crates/data/src/source/gpkg.rs +++ b/crates/data/src/source/gpkg.rs @@ -615,6 +615,8 @@ fn install(conn: Connection, tmp: tempfile::TempPath, path: &Path) -> Result<()> conn.close().map_err(|(_, e)| err(e))?; // Built without syncing (fast); synced once before installation. Windows needs a write handle. std::fs::OpenOptions::new().write(true).open(&tmp)?.sync_all()?; + // A cancel that came while it was built leaves no file (the temporary one is removed). + crate::progress::check()?; // Refuse journals that appeared during the build; persistence also closes the file race. check_new_destination(path)?; tmp.persist_noclobber(path).map_err(|e| destination_error(path, e.error))?; diff --git a/crates/engine/src/cmd/exports.rs b/crates/engine/src/cmd/exports.rs index 79f1c2f21e..c584bef24c 100644 --- a/crates/engine/src/cmd/exports.rs +++ b/crates/engine/src/cmd/exports.rs @@ -65,8 +65,9 @@ fn said(s: &mut Session, wrote: Wrote) -> Value { #[cfg(not(target_arch = "wasm32"))] /// An export ([`IN_BACKGROUND`]) started to write its file on another thread ([`ExportRun::run`]) -/// while the session goes on: its inputs are read now (cheaply: the features and pixels are -/// shared), and [`export_land`] says what it wrote. +/// while the session goes on: its inputs are taken now (cheaply: the features and pixels are +/// shared, and a streamed layer's source is read on the export's thread), and [`export_land`] +/// says what it wrote. pub struct ExportRun { /// What the export is called, for its progress and a cancelled run's error. pub name: &'static str, @@ -88,7 +89,7 @@ impl ExportRun { #[cfg(not(target_arch = "wasm32"))] /// Starts `command` (one of [`IN_BACKGROUND`]'s) with `p`, to write its file on another thread: -/// its parameters checked and its inputs read now. +/// its parameters checked and its inputs taken now. pub fn export_start(s: &mut Session, command: &str, p: &Value) -> Result { let &(_, name, start) = IN_BACKGROUND.iter().find(|(c, ..)| *c == command).ok_or_else(|| format!("{command} doesn't run in the background"))?; From a417c1dc5ad5f0bcd72366c1d4b3124259de3636 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Wed, 4 Nov 2026 17:47:16 +0100 Subject: [PATCH 3/4] fix(data): KML/GPX stop with an error when cancelled, each Shapefile part checks, and closing the app stops an export (#1433 review) --- apps/rustgis/src/main.rs | 1 + crates/data/src/interop.rs | 24 ++++++++++-------------- crates/data/src/io.rs | 26 ++++++++++---------------- crates/data/src/raster/cogwrite.rs | 3 ++- crates/data/src/source/gpkg.rs | 3 ++- crates/engine/src/cmd/workflows.rs | 4 ++-- crates/ui-egui/src/exportjob.rs | 19 +++++++++++++++++++ crates/ui-egui/src/lib.rs | 7 +++++++ 8 files changed, 53 insertions(+), 34 deletions(-) diff --git a/apps/rustgis/src/main.rs b/apps/rustgis/src/main.rs index 5460bf4889..9c4b4791d1 100644 --- a/apps/rustgis/src/main.rs +++ b/apps/rustgis/src/main.rs @@ -325,6 +325,7 @@ impl eframe::App for App { /// Every way eframe closes the app comes here, Cmd+Q on macOS included, which may not /// return from `run_native`: the run ended cleanly. fn on_exit(&mut self) { + self.0.end_exports(); self.0.end_autosave(); running::close(&self.1); } diff --git a/crates/data/src/interop.rs b/crates/data/src/interop.rs index 9aaa441741..7cce4e92a0 100644 --- a/crates/data/src/interop.rs +++ b/crates/data/src/interop.rs @@ -56,16 +56,14 @@ fn kml_geometry(g: &Geometry) -> String { } /// A KML document, one placemark per feature, named by `name_field` and with every attribute. -pub fn to_kml(ds: &Dataset, name_field: Option) -> String { +/// A cancelled run ([`crate::progress`]) stops it with [`crate::DataError::Cancelled`]. +pub fn to_kml(ds: &Dataset, name_field: Option) -> crate::Result { let mut out = format!( "\n{}\n", xml(&ds.name) ); for (r, f) in ds.features.iter().enumerate() { - // A cancelled export stops here; its caller then writes nothing. - if crate::progress::step(1).is_err() { - break; - } + crate::progress::step(1)?; let Some(g) = &f.geometry else { continue }; let name = name_field.map(|fi| ds.value(r, fi).to_string()).unwrap_or_else(|| f.id.to_string()); let data: String = ds @@ -82,21 +80,19 @@ pub fn to_kml(ds: &Dataset, name_field: Option) -> String { )); } out.push_str("\n"); - out + Ok(out) } /// A GPX file: points become waypoints, lines tracks (polygon outlines too). -pub fn to_gpx(ds: &Dataset, name_field: Option) -> String { +/// A cancelled run ([`crate::progress`]) stops it with [`crate::DataError::Cancelled`]. +pub fn to_gpx(ds: &Dataset, name_field: Option) -> crate::Result { let mut out = String::from( "\n\n", ); let pt = |c: &Coord| format!("lat=\"{}\" lon=\"{}\"", c.y, c.x); let mut tracks = String::new(); for (r, f) in ds.features.iter().enumerate() { - // A cancelled export stops here; its caller then writes nothing. - if crate::progress::step(1).is_err() { - break; - } + crate::progress::step(1)?; let Some(g) = &f.geometry else { continue }; let name = xml(&name_field.map(|fi| ds.value(r, fi).to_string()).unwrap_or_else(|| f.id.to_string())); match g { @@ -121,7 +117,7 @@ pub fn to_gpx(ds: &Dataset, name_field: Option) -> String { } out.push_str(&tracks); out.push_str("\n"); - out + Ok(out) } /// Well-known text for a geometry. @@ -523,9 +519,9 @@ mod tests { fn writes_kml_gpx_wkt() { let mut ds = Dataset::new("t", GeometryType::Point, vec![Field::new("name", FieldType::Text)]); ds.push(Some(Geometry::Point(Coord::new(2.35, 48.85))), vec![Value::Text("Paris & co".into())]); - let k = to_kml(&ds, Some(0)); + let k = to_kml(&ds, Some(0)).unwrap(); assert!(k.contains("Paris & co") && k.contains("2.35,48.85")); - let g = to_gpx(&ds, Some(0)); + let g = to_gpx(&ds, Some(0)).unwrap(); assert!(g.contains("")); assert_eq!(wkt(&Geometry::Line(vec![vec![Coord::new(0.0, 0.0), Coord::new(1.0, 1.0)]])), "LINESTRING (0 0, 1 1)"); assert!(to_wkt_csv(&ds).starts_with("wkt,name\nPOINT (2.35 48.85),Paris & co")); diff --git a/crates/data/src/io.rs b/crates/data/src/io.rs index 64fceb1040..6ad79a5692 100644 --- a/crates/data/src/io.rs +++ b/crates/data/src/io.rs @@ -468,24 +468,12 @@ fn projected_bytes(ds: &Dataset, ext: &str, crs: Crs) -> Result<(Vec, usize) "csv" => (to_csv(ds)?.into_bytes(), 0), "fgb" => (crate::source::fgb::write_in(ds, crs)?, without_geometry(ds)), "parquet" | "geoparquet" => (crate::source::parquet::write(ds)?, 0), - "kml" => { - let kml = crate::interop::to_kml(ds, name_field(ds)); - // A cancelled run cuts the document short: it is never written. - crate::progress::check()?; - (kml.into_bytes(), without_geometry(ds)) - } + "kml" => (crate::interop::to_kml(ds, name_field(ds))?.into_bytes(), without_geometry(ds)), "kmz" => { - let kml = crate::interop::to_kml(ds, name_field(ds)).into_bytes(); - // A cancelled run cuts the document short: it is never written. - crate::progress::check()?; + let kml = crate::interop::to_kml(ds, name_field(ds))?.into_bytes(); (crate::zip::write(&[("doc.kml".to_string(), kml)]).map_err(DataError::Format)?, without_geometry(ds)) } - "gpx" => { - let gpx = crate::interop::to_gpx(ds, name_field(ds)); - // A cancelled run cuts the document short: it is never written. - crate::progress::check()?; - (gpx.into_bytes(), without_geometry(ds)) - } + "gpx" => (crate::interop::to_gpx(ds, name_field(ds))?.into_bytes(), without_geometry(ds)), "shp" | "zip" => { let (parts, left_out) = shapefile_parts_in(ds, crs)?; let base: String = ds.name.chars().map(|c| if c.is_alphanumeric() || matches!(c, '-' | '_' | ' ') { c } else { '_' }).collect(); @@ -559,6 +547,11 @@ pub fn write_path_in(ds: &Dataset, path: &Path, crs: Crs) -> Result { let mut placed: Vec = Vec::new(); let mut result = Ok(()); for (tmp, target) in &written { + // A cancel before the last part lands rolls back like a failed rename. + if crate::progress::check().is_err() { + result = Err(std::io::Error::other(DataError::Cancelled)); + break; + } if target.exists() { // Any failure here rolls back like a failed rename. let bak = match temp_beside(target) { @@ -677,7 +670,8 @@ pub fn write_atomic_with>( out.get_ref().sync_all()?; // Closed before the rename, including on Windows. drop(out); - // A cancel that came while it was written leaves the file there before as it was. + // A cancel that came while it was written leaves the file there before as it was. This is + // the last look: the rename commits the file, and a Cancel after it finds it written. if crate::progress::check().is_err() { return Err(std::io::Error::other(DataError::Cancelled).into()); } diff --git a/crates/data/src/raster/cogwrite.rs b/crates/data/src/raster/cogwrite.rs index 3a4cedde73..f27c0d5c48 100644 --- a/crates/data/src/raster/cogwrite.rs +++ b/crates/data/src/raster/cogwrite.rs @@ -617,7 +617,8 @@ pub fn write_file(r: &Raster, band: usize, path: &Path) -> Result { let f = w.into_inner().map_err(|e| err(e.error()))?; f.sync_all()?; drop(f); - // A cancel that came while it was written leaves the file there before as it was. + // A cancel that came while it was written leaves the file there before as it was. This is + // the last look: the rename commits the file, and a Cancel after it finds it written. crate::progress::check()?; std::fs::rename(&tmp, path)?; Ok(info) diff --git a/crates/data/src/source/gpkg.rs b/crates/data/src/source/gpkg.rs index 0ae10c26d6..966152d2fb 100644 --- a/crates/data/src/source/gpkg.rs +++ b/crates/data/src/source/gpkg.rs @@ -615,7 +615,8 @@ fn install(conn: Connection, tmp: tempfile::TempPath, path: &Path) -> Result<()> conn.close().map_err(|(_, e)| err(e))?; // Built without syncing (fast); synced once before installation. Windows needs a write handle. std::fs::OpenOptions::new().write(true).open(&tmp)?.sync_all()?; - // A cancel that came while it was built leaves no file (the temporary one is removed). + // A cancel that came while it was built leaves no file (the temporary one is removed). This + // is the last look: persisting commits the file, and a Cancel after it finds it written. crate::progress::check()?; // Refuse journals that appeared during the build; persistence also closes the file race. check_new_destination(path)?; diff --git a/crates/engine/src/cmd/workflows.rs b/crates/engine/src/cmd/workflows.rs index 469f7110ae..b9a3e1b991 100644 --- a/crates/engine/src/cmd/workflows.rs +++ b/crates/engine/src/cmd/workflows.rs @@ -1110,8 +1110,8 @@ fn convert(s: &mut Session, p: &Value) -> CmdResult { let text = match ps(p, "format").ok_or("format is required")? { "geojson" => rustgis_data::io::to_geojson(ds).to_string(), "csv" => rustgis_data::io::to_csv(ds).map_err(|e| e.to_string())?, - "kml" => rustgis_data::interop::to_kml(ds, name_field), - "gpx" => rustgis_data::interop::to_gpx(ds, name_field), + "kml" => rustgis_data::interop::to_kml(ds, name_field).map_err(|e| e.to_string())?, + "gpx" => rustgis_data::interop::to_gpx(ds, name_field).map_err(|e| e.to_string())?, "wkt" => rustgis_data::interop::to_wkt_csv(ds), f => return Err(format!("unknown format '{f}'")), }; diff --git a/crates/ui-egui/src/exportjob.rs b/crates/ui-egui/src/exportjob.rs index c62d60cc70..253adf746d 100644 --- a/crates/ui-egui/src/exportjob.rs +++ b/crates/ui-egui/src/exportjob.rs @@ -61,6 +61,18 @@ pub fn land(app: &mut RustGisApp) -> bool { false } +/// Cancels the export under way and waits up to `wait` for its thread to stop: the writers stop +/// within a moment and remove their temporary file. One that is still going then is left to the +/// process's end. +pub fn stop(app: &mut RustGisApp, wait: std::time::Duration) { + let Some(job) = app.export_job.take() else { return }; + job.control.cancel(); + let end = std::time::Instant::now() + wait; + while job.slot.lock().unwrap_or_else(std::sync::PoisonError::into_inner).is_none() && std::time::Instant::now() < end { + std::thread::sleep(std::time::Duration::from_millis(10)); + } +} + /// The export under way in the status bar: its name, progress and Cancel. pub fn status(app: &RustGisApp, ui: &mut Ui, t: &Tokens) { let Some(job) = &app.export_job else { return }; @@ -108,6 +120,13 @@ mod tests { assert!(!other.exists(), "the refused export wrote nothing"); let (written, _) = rustgis_engine::data::io::read_path(&path).unwrap(); assert_eq!(written.features.len(), app.session.map().layer_by_name("Cities").unwrap().dataset.features.len()); + // Closing the app cancels an export under way and waits for it: no temporary file is left. + pick(&other); + app.run("ui.exportLayer", json!({"format": "geojson"})).unwrap(); + stop(&mut app, std::time::Duration::from_secs(60)); + assert!(app.export_job.is_none()); + let left: Vec<_> = std::fs::read_dir(&dir).unwrap().filter_map(|e| e.ok()).map(|e| e.file_name()).collect(); + assert!(left.iter().all(|n| !n.to_string_lossy().ends_with(".tmp")), "{left:?}"); let _ = std::fs::remove_dir_all(&dir); } } diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index a3b25cbc1e..849f6c72df 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -1767,6 +1767,13 @@ impl RustGisApp { autosave::end(self); } + /// The app closing (the desktop app's `on_exit`): an export still writing is cancelled and + /// given a moment to stop, so it leaves no temporary file beside its destination. + #[cfg(not(target_arch = "wasm32"))] + pub fn end_exports(&mut self) { + exportjob::stop(self, std::time::Duration::from_secs(5)); + } + /// Whether this shell opens files by path (the desktop app; the web app picks them in the /// browser instead). pub(crate) fn opens_files(&self) -> bool { From 50f5831714c207ddcc7e076a190e4db2827aaf7b Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Wed, 4 Nov 2026 18:09:28 +0100 Subject: [PATCH 4/4] fix(ui): an export's thread keeps its requests in the session's diagnostics (#1433 review) --- crates/ui-egui/src/exportjob.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/ui-egui/src/exportjob.rs b/crates/ui-egui/src/exportjob.rs index 253adf746d..cd579745bd 100644 --- a/crates/ui-egui/src/exportjob.rs +++ b/crates/ui-egui/src/exportjob.rs @@ -32,9 +32,12 @@ pub fn start(app: &mut RustGisApp, command: &'static str, p: Value) -> Result