diff --git a/crates/engine/src/cmd/layer.rs b/crates/engine/src/cmd/layer.rs index 4000b92f1f..855f92eae9 100644 --- a/crates/engine/src/cmd/layer.rs +++ b/crates/engine/src/cmd/layer.rs @@ -3361,6 +3361,15 @@ fn add_url_as_given(s: &mut Session, p: &Value) -> CmdResult { // adding it answered, as often as the run asks for it (the app downloaded and added it once). // Answering it changes nothing (the layer is its own undo step). let wanted = s.asked_for(p); + // A window of the view is answered by the one asked in the view the command sees now. + #[cfg(not(target_arch = "wasm32"))] + let wanted = match p.get("bbox") { + Some(b) if s.leave_downloads && is_view(Some(b)) => { + let at = ps(p, "url").map(str::trim).unwrap_or_default(); + Session::with_view(&wanted, Some(window_settled(s, p, at, None)?.bbox)) + } + _ => wanted, + }; if s.leave_downloads && let Some((answer, added)) = s.rerun_answer(&wanted) { @@ -3411,13 +3420,20 @@ fn add_url_as_given(s: &mut Session, p: &Value) -> CmdResult { } if window { window_checks(p, url, scan)?; - // A plugin's command run by the app leaves what would wait to the app, off the frame; a - // window waits for its header and tiles, which the app doesn't take over yet. + // A plugin's command run by the app leaves what would wait to the app, off the frame + // ([`read_cog_window`]); a box that can't be read is refused now, while the command runs. #[cfg(not(target_arch = "wasm32"))] if s.leave_downloads { - return Err(format!( - "{url}: a window (\"bbox\") waits for its tiles, which a plugin's command can't do in the app yet: run layer.addUrl from the command line or a script, or leave out \"bbox\" to draw the file in place" - )); + let settled = window_settled(s, p, url, None)?; + let mut asked = p.clone(); + asked["url"] = json!(url); + // `"view"` is the view now: the app reads the window on a later frame, and a command + // run again after a download sees the view it first ran with only while it runs. The + // request keeps the box beside what was asked, by which a run again is answered. + s.window_view = is_view(p.get("bbox")).then_some(settled.bbox); + let queued = s.request_url(&asked); + s.window_view = None; + return queued; } return add_cog_window(s, p, url); } @@ -4137,6 +4153,13 @@ pub struct WindowAsked { name: Option, } +#[cfg(all(test, not(target_arch = "wasm32")))] +impl WindowAsked { + pub(crate) fn bbox(&self) -> [f64; 4] { + self.bbox + } +} + /// A window read by [`read_cog_window`], waiting to be added to the project: its raster made and /// its GeoTIFF written, so that adding it holds no frame for long. #[cfg(not(target_arch = "wasm32"))] @@ -4153,9 +4176,9 @@ pub struct CogWindow { /// `p`'s box and cell size, read on the frame, which holds the view. #[cfg(not(target_arch = "wasm32"))] -fn window_settled(s: &Session, p: &Value, url: &str) -> Result { +fn window_settled(s: &Session, p: &Value, url: &str, seen: Option<[f64; 4]>) -> Result { let named = |e: String| if e.starts_with(url) { e } else { format!("{url}: {e}") }; - let bbox = window_box(s, p).map_err(named)?; + let bbox = window_box(s, p, seen).map_err(named)?; let cell = match p.get("cellSize").filter(|v| !v.is_null()) { None => None, Some(v) => Some(v.as_f64().filter(|c| c.is_finite() && *c > 0.0).ok_or_else(|| named("cellSize is a size above 0".into()))?), @@ -4168,18 +4191,25 @@ fn window_settled(s: &Session, p: &Value, url: &str) -> Result Result { + window_asked_in(s, p, None) +} + +/// [`window_asked`], `"bbox": "view"` being `seen` when given: the box the view was when a request +/// was queued ([`crate::UrlRequest::view_asked`]). +#[cfg(not(target_arch = "wasm32"))] +pub fn window_asked_in(s: &Session, p: &Value, seen: Option<[f64; 4]>) -> Result { let url = ps(p, "url").map(str::trim).filter(|u| !u.is_empty()).ok_or("url is required")?; let checked = web_address(url) .and_then(|_| super::raster::as_scan(p)) .and_then(|scan| window_checks(p, url, scan)) - .and_then(|()| window_settled(s, p, url)); + .and_then(|()| window_settled(s, p, url, seen)); checked.map_err(|e| crate::sources::message_with_address(url, &e)) } /// `layer.addUrl` with `"bbox"`, for those that wait: read here and added. #[cfg(not(target_arch = "wasm32"))] fn add_cog_window(s: &mut Session, p: &Value, url: &str) -> CmdResult { - let read = read_cog_window(&window_settled(s, p, url)?)?; + let read = read_cog_window(&window_settled(s, p, url, None)?)?; add_window(s, p, read) } @@ -4512,21 +4542,25 @@ pub(crate) fn box_in_file_longitudes(x0: f64, x1: f64, fx0: f64, fx1: f64) -> Re /// A window's box: `[west, south, east, north]` in degrees, or `"view"`, the map's extent. #[cfg(not(target_arch = "wasm32"))] -fn window_box(s: &Session, p: &Value) -> Result<[f64; 4], String> { +fn window_box(s: &Session, p: &Value, seen: Option<[f64; 4]>) -> Result<[f64; 4], String> { const NEED: &str = "bbox is [west, south, east, north] in degrees, or \"view\""; let [w, so, e, n] = match p.get("bbox") { - Some(Value::String(v)) if v.trim().eq_ignore_ascii_case("view") => { - let e = super::workspace::view_extent(s); - if e.is_empty() { - return Err("the map's extent isn't known; give bbox".into()); - } - // A view past 180° (across the antimeridian, or the world more than once) isn't cut - // to fit: what it shows past there would be left out. - if e.min_x < -180.0 || e.max_x > 180.0 { - return Err("the view reaches past 180° of longitude; give bbox".into()); + Some(v) if is_view(Some(v)) => match seen { + // The view a request was asked in ([`crate::UrlRequest::view_asked`]). + Some(b) => b, + None => { + let e = super::workspace::view_extent(s); + if e.is_empty() { + return Err("the map's extent isn't known; give bbox".into()); + } + // A view past 180° (across the antimeridian, or the world more than once) isn't + // cut to fit: what it shows past there would be left out. + if e.min_x < -180.0 || e.max_x > 180.0 { + return Err("the view reaches past 180° of longitude; give bbox".into()); + } + [e.min_x, e.min_y.max(-90.0), e.max_x, e.max_y.min(90.0)] } - [e.min_x, e.min_y.max(-90.0), e.max_x, e.max_y.min(90.0)] - } + }, Some(Value::Array(a)) => { let v: Option> = a.iter().map(|x| x.as_f64().filter(|x| x.is_finite())).collect(); match v.as_deref() { @@ -4540,6 +4574,12 @@ fn window_box(s: &Session, p: &Value) -> Result<[f64; 4], String> { if w < e && so < n && lon(w) && lon(e) && lat(so) && lat(n) { Ok([w, so, e, n]) } else { Err(NEED.into()) } } +/// `"bbox": "view"`, in any case or spacing. +#[cfg(not(target_arch = "wasm32"))] +fn is_view(bbox: Option<&Value>) -> bool { + bbox.and_then(Value::as_str).is_some_and(|v| v.trim().eq_ignore_ascii_case("view")) +} + /// A GeoTIFF's name from its address: the file name without its extension, or for a dated /// address (`…/landsat/{date:YYYY}.tif`) what is left of it once the dates are taken out, else its /// folder's name. diff --git a/crates/engine/src/cmd/plugin_fetch.rs b/crates/engine/src/cmd/plugin_fetch.rs index 7e5466ace3..e1e267a95a 100644 --- a/crates/engine/src/cmd/plugin_fetch.rs +++ b/crates/engine/src/cmd/plugin_fetch.rs @@ -321,11 +321,19 @@ pub(crate) struct Retry { tries: u32, } +/// The key a window of the view is told apart by when a command run again is answered +/// ([`Session::with_view`]): matched only, never read as a parameter. +const VIEW_ASKED: &str = "viewAsked"; + /// The downloads plugin commands wait for (#1202): those the app is making, with what was /// asked and since when, and those added, with what adding them answered, until the command /// waiting for them runs again. #[derive(Clone, Default)] pub(crate) struct UrlWaits { + /// The view's boxes of the window requests asked with `"bbox": "view"`, by their numbers: a + /// request for the view joins one being made only when it was asked in the same view. Kept + /// here, they go aside with a project and back on a rollback as the waits do. + pub(crate) views: std::collections::BTreeMap, owners: std::collections::BTreeMap, in_flight: Vec<(u64, Value, Instant)>, landed: Vec<(u64, Value, CmdResult, Added)>, @@ -616,11 +624,18 @@ impl UrlWaits { } } + /// Keeps the views of the requests still being made or landed only. + fn prune_views(&mut self) { + let live: Vec = self.in_flight.iter().map(|(n, ..)| *n).chain(self.landed.iter().map(|(n, ..)| *n)).collect(); + self.views.retain(|n, _| live.contains(n)); + } + /// Only what `waiting` still waits for is kept. fn keep_only(&mut self, waiting: &[Retry]) { let wanted = |n: &u64| waiting.iter().any(|r| r.requests.contains(n)); self.in_flight.retain(|(n, ..)| wanted(n)); self.landed.retain(|(n, ..)| wanted(n)); + self.views.retain(|n, _| wanted(n)); let retained: Vec = self .in_flight .iter() @@ -667,10 +682,13 @@ impl Session { /// eventual `layer.addDownload`; a command run again asks for the same. pub(crate) fn asked_for(&self, p: &Value) -> Value { let mut asked = without_request(p); - if self.web_logins.is_anonymous() - && let Some(o) = asked.as_object_mut() - { - o.insert("anonymous".into(), json!(true)); + if let Some(o) = asked.as_object_mut() { + // Only the session tells windows of the view apart ([`Session::with_view`]): a caller's + // own `viewAsked` is left out. + o.remove(VIEW_ASKED); + if self.web_logins.is_anonymous() { + o.insert("anonymous".into(), json!(true)); + } } asked } @@ -733,6 +751,17 @@ impl Session { Ok(None) } + /// What a window of the view was asked as, for a command run again to be answered by: `asked` + /// with the box the view was then (`UrlWaits::views`), so two windows of the view asked in + /// two views are told apart. Others are `asked` as it is. + pub(crate) fn with_view(asked: &Value, view: Option<[f64; 4]>) -> Value { + let mut asked = asked.clone(); + if let (Some(v), Some(o)) = (view, asked.as_object_mut()) { + o.insert(VIEW_ASKED.into(), json!(v)); + } + asked + } + /// Whether a command run again has an answer for `wanted`. #[cfg(not(target_arch = "wasm32"))] pub(crate) fn rerun_has(&self, wanted: &Value) -> bool { @@ -886,6 +915,7 @@ impl Session { fn poll_plugin_retries_at(&mut self, now: Instant) -> Vec<(String, CmdResult)> { self.url_waits.documents_in(); if self.plugin_retries.is_empty() { + self.url_waits.prune_views(); return Vec::new(); } // Said here too: the command may go on without it, and what it answers goes nowhere. @@ -917,12 +947,9 @@ impl Session { // runs, and the ones it waited for. These stay in `landed` until no command waits // for them (`keep_only`, below), so another command waiting for the same download // gets the answer too. - let mine = self - .url_waits - .landed - .iter() - .filter(|(n, ..)| r.requests.contains(n)) - .map(|(_, asked, answer, added)| (asked.clone(), answer.clone(), added.clone())); + let mine = self.url_waits.landed.iter().filter(|(n, ..)| r.requests.contains(n)).map(|(n, asked, answer, added)| { + (Self::with_view(asked, self.url_waits.views.get(n).copied()), answer.clone(), added.clone()) + }); self.rerun_landed = r.answered.iter().cloned().chain(mine).collect(); self.rerun_said = r.said; self.retry_tries = r.tries; diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 50b213aa37..2a324620ee 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -51,11 +51,18 @@ pub struct UrlRequest { key: u64, params: Value, authority: std::sync::Arc, + /// For a window asked with `"bbox": "view"`: the box the view was then, kept apart from its + /// parameters, which a caller gives and a command run again is matched by. + view: Option<[f64; 4]>, } impl UrlRequest { pub fn params(&self) -> &Value { &self.params } + /// The box `"bbox": "view"` was when the command asked (`cmd::layer::window_asked_in`). + pub fn view_asked(&self) -> Option<[f64; 4]> { + self.view + } } pub type CmdResult = Result; @@ -838,6 +845,8 @@ pub struct Session { /// Downloads a command asked for where nothing may wait (the browser): the parameters of /// `layer.addUrl` calls, for the front end to fetch and add (see [`Session::take_url_requests`]). pub(crate) url_requests: Vec, + /// The view's box a window request being queued keeps ([`UrlRequest::view_asked`]). + pub(crate) window_view: Option<[f64; 4]>, /// Set by [`Session::request_url`] for [`Session::guarded`]: the command running changed /// nothing, it only left a request (a plugin's own answer can't claim that). request_only: bool, @@ -1176,6 +1185,7 @@ impl Session { #[cfg(not(target_arch = "wasm32"))] landed_windows: Vec::new(), url_requests: Vec::new(), + window_view: None, file_outputs: Vec::new(), #[cfg(test)] queue_gp_files: false, diff --git a/crates/engine/src/policy/downloads.rs b/crates/engine/src/policy/downloads.rs index c529d8591f..afd0895867 100644 --- a/crates/engine/src/policy/downloads.rs +++ b/crates/engine/src/policy/downloads.rs @@ -10,6 +10,15 @@ use std::{ }, }; +/// What a window the app read off the frame says it read (`"window"`, #1330) is what its request +/// asked for: a box, and its cell size and `"cog"` if the request gave them, nothing else. +fn asked_window(asked: &Value, window: &Value) -> bool { + const KEYS: [&str; 3] = ["bbox", "cellSize", "cog"]; + let given = |k: &str| asked.get(k).filter(|v| !v.is_null()); + given("bbox").is_some() + && window.as_object().is_some_and(|w| w.keys().all(|k| KEYS.contains(&k.as_str())) && KEYS.iter().all(|k| given(k) == w.get(*k))) +} + pub(super) struct Claim { pub c: Continuation, pub params: Value, @@ -53,7 +62,7 @@ impl Session { requests: self .url_requests .iter() - .map(|q| UrlRequest { key: q.key, params: q.params.clone(), authority: q.authority.clone() }) + .map(|q| UrlRequest { key: q.key, params: q.params.clone(), authority: q.authority.clone(), view: q.view }) .collect(), queued: self.policy.work.queued.clone(), } @@ -234,6 +243,7 @@ impl Session { .iter() .find(|q| { crate::cmd::plugins::fetch::same_request(&q.params, &asked) + && q.view == self.window_view && self.same_url_caller(&q.authority, &c) && self.check_continuation(&q.authority).is_ok() }) @@ -251,8 +261,13 @@ impl Session { .call_requests .as_ref() .and_then(|_| { + // A window of the view is the one asked in the same view: another is read again. self.url_waits.in_flight(&asked, |n| { - self.url_waits.owner(n).is_some_and(|owner| self.same_url_caller(owner, &c) && self.check_continuation(owner).is_ok()) + self.url_waits.views.get(&n).copied() == self.window_view + && self + .url_waits + .owner(n) + .is_some_and(|owner| self.same_url_caller(owner, &c) && self.check_continuation(owner).is_ok()) }) }) .filter(|n| !self.url_waits.taken_away(*n, &self.project)); @@ -279,7 +294,11 @@ impl Session { self.next_pending = self.next_pending.wrapping_add(1); let key = self.next_pending; self.policy.work.queued.insert(key, Queued { c: Arc::downgrade(&c), import, consumed: Arc::new(AtomicBool::new(false)) }); - self.url_requests.push(UrlRequest { key, params, authority: c }); + let view = self.window_view.take(); + if let Some(v) = view { + self.url_waits.views.insert(number, v); + } + self.url_requests.push(UrlRequest { key, params, authority: c, view }); } if !import && let Some(asked_now) = self.call_requests.as_mut() @@ -358,6 +377,11 @@ impl Session { } let c = (*c).clone(); let id = self.download_started(); + // The view a window of the view was asked in, by its number, now that the request is made + // (the command's own waits may have been restored since it was queued). + if let (Some(v), Some(n)) = (request.view, request.params.get("request").and_then(Value::as_u64)) { + self.url_waits.views.insert(n, v); + } self.policy.work.claims.insert(id, Claim { c, params: request.params.clone(), live: Arc::new(AtomicBool::new(true)), import }); Ok(id) } @@ -446,7 +470,8 @@ impl Session { || (key == "whole" && value.as_bool() == Some(true)) || (key == "zoom" && crate::cmd::bool_value(value) == Some(false) - && claim.params.get(key).and_then(crate::cmd::bool_value) == Some(true)); + && claim.params.get(key).and_then(crate::cmd::bool_value) == Some(true)) + || (key == "window" && asked_window(&claim.params, value)); if !refinement && claim.params.get(key) != Some(value) { return Err("the download's address or parameters changed since it was requested".into()); } @@ -505,3 +530,25 @@ impl Session { self.policy.work.claims.remove(&id); } } + +#[cfg(test)] +mod tests { + use super::asked_window; + use serde_json::json; + + /// A window the app read completes its request only as what was asked (#1330): the same box, + /// cell size and `"cog"`, and nothing more; a request with no box takes no window. + #[test] + fn a_landed_window_completes_only_what_was_asked() { + let asked = json!({"url": "https://x.org/a.tif", "bbox": [10.25, 45.5, 10.5, 45.75], "cellSize": 30, "name": "W"}); + assert!(asked_window(&asked, &json!({"bbox": [10.25, 45.5, 10.5, 45.75], "cellSize": 30}))); + assert!(!asked_window(&asked, &json!({"bbox": [0, 0, 1, 1], "cellSize": 30})), "another box"); + assert!(!asked_window(&asked, &json!({"bbox": [10.25, 45.5, 10.5, 45.75]})), "its cell size left out"); + assert!(!asked_window(&asked, &json!({"bbox": [10.25, 45.5, 10.5, 45.75], "cellSize": 30, "cog": true})), "cog not asked"); + assert!(!asked_window(&asked, &json!({"bbox": [10.25, 45.5, 10.5, 45.75], "cellSize": 30, "url": "https://y.org"})), "another key"); + assert!(!asked_window(&asked, &json!("view")), "not an object"); + let view = json!({"url": "https://x.org/a.tif", "bbox": "view", "cellSize": null}); + assert!(asked_window(&view, &json!({"bbox": "view"})), "a null cell size is none given"); + assert!(!asked_window(&json!({"url": "https://x.org/a.tif"}), &json!({})), "no box asked"); + } +} diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index f6af788625..add6b0d757 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -33162,6 +33162,175 @@ fn a_windows_bands_are_started_within_its_wait() { assert_eq!(e, "2 of 4 bands read in 5 minutes, so the window is given up: take a smaller box or a larger cellSize"); } +/// A script or model the desktop app runs off the frame, as a plugin's command, leaves a window to +/// the app: answered pending, and queued, not refused (#1330). +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn a_script_run_by_the_app_leaves_a_window_to_it() { + let mut s = world(); + let p = json!({"url": "https://x.org/scene.tif", "bbox": [10.25, 45.5, 10.5, 45.75]}); + let answer = s.with_deferred_downloads(|s| s.run("layer.addUrl", &p)).unwrap(); + assert_eq!(answer["pending"], true, "{answer}"); + let asked = s.take_url_requests(); + assert_eq!(asked.len(), 1); + assert_eq!(asked[0].params()["bbox"], p["bbox"]); +} + +/// A command run again that asks for windows of the view in two views is answered each by the +/// window asked in that view, not twice by the first (#1330). +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn a_command_run_again_gets_each_view_s_own_window() { + let mut s = world(); + let p = json!({"url": "https://x.org/scene.tif", "bbox": "view"}); + let asked = s.asked_for(&p); + let mut views = Vec::new(); + for (extent, marker) in [([10.0, 45.0, 11.0, 46.0], "A"), ([20.0, 45.0, 21.0, 46.0], "B")] { + s.run("map.zoomToExtent", &json!({"extent": extent})).unwrap(); + let e = crate::cmd::workspace::view_extent(&s); + let view = [e.min_x, e.min_y, e.max_x, e.max_y]; + s.rerun_landed.push((Session::with_view(&asked, Some(view)), Ok(json!({"marker": marker})), Default::default())); + views.push(extent); + } + let answer = |s: &mut Session, extent: [f64; 4]| { + s.run("map.zoomToExtent", &json!({"extent": extent})).unwrap(); + s.with_deferred_downloads(|s| s.run("layer.addUrl", &p)).unwrap()["marker"].clone() + }; + assert_eq!(answer(&mut s, views[1]), "B"); + assert_eq!(answer(&mut s, views[0]), "A"); + // A caller can't write the key the view is told apart by. + let written = s.asked_for(&json!({"url": "https://x.org/scene.tif", "bbox": "view", "viewAsked": [0, 0, 1, 1]})); + assert_eq!(written, asked); +} + +/// A request for the view's window joins one queued only when it was asked in the same view: +/// asked in another, it is another request, read for its own view (#1330). +#[test] +fn a_window_of_the_view_joins_only_one_asked_in_the_same_view() { + let mut s = Session::new(Project::default()); + let p = json!({"url": "https://x.org/scene.tif", "bbox": "view"}); + let (a, b) = ([10.0, 45.0, 11.0, 46.0], [20.0, 45.0, 21.0, 46.0]); + for view in [Some(a), Some(a), Some(b), None] { + s.window_view = view; + s.request_url(&p).unwrap(); + s.window_view = None; + } + let views: Vec> = s.take_url_requests().iter().map(crate::UrlRequest::view_asked).collect(); + assert_eq!(views, [Some(a), Some(b), None], "the same view's asked once"); + assert_eq!(s.url_waits.views.len(), 2, "kept by request number"); +} + +/// A window of the view that is being read joins a command asking for it again in the same view; +/// asked in another view, or with a box, it is another request (#1330). +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn a_window_of_the_view_being_read_joins_only_the_same_view() { + let mut s = world(); + plugin("eng-window-flight", &["edit", "network"]); + let ask = |s: &mut Session, bbox: Value| { + let p = json!({"op": "layer.addUrl", "params": {"url": "https://x.org/scene.tif", "bbox": bbox, "name": "Window"}}); + s.run_leaving_downloads("plugin:eng-window-flight/f", &p).unwrap(); + s.take_url_requests() + }; + s.run("map.zoomToExtent", &json!({"extent": [10.0, 45.0, 11.0, 46.0]})).unwrap(); + let first = ask(&mut s, json!("view")); + assert_eq!(first.len(), 1); + s.download_started_for(&first[0]).unwrap(); + // While it is read: the same view joins it. + assert!(ask(&mut s, json!("view")).is_empty(), "the same view's window is read once"); + // Another view: another request, for that view. + s.run("map.zoomToExtent", &json!({"extent": [20.0, 45.0, 21.0, 46.0]})).unwrap(); + let other = ask(&mut s, json!("view")); + assert_eq!(other.len(), 1); + assert!(other[0].view_asked().is_some() && other[0].view_asked() != first[0].view_asked()); + // A box: another request, with no view. + let boxed = ask(&mut s, json!([10.25, 45.5, 10.5, 45.75])); + assert_eq!(boxed.len(), 1); + assert_eq!(boxed[0].view_asked(), None); +} + +/// A plugin's command asking for the view's window is read for the view it was asked in, though the +/// app reads it on a later frame, when the map may show another (#1330): a command run again after +/// a download sees the view it first ran with only while it runs. The box is kept beside the +/// request, not in what it asks, so the command run again is answered the raster added. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn a_window_asked_for_the_view_keeps_the_view_it_was_asked_in() { + use crate::cmd::layer::{read_cog_window, window_asked, window_asked_in}; + use rustgis_data::raster::cog::example; + let (base, _) = serve_ranges(example(1024, 256)); + let mut s = world(); + plugin("eng-window-view", &["edit", "network"]); + let layers = s.map().layers.len(); + s.run("map.zoomToExtent", &json!({"extent": [10.3, 45.55, 10.45, 45.7]})).unwrap(); + let seen = crate::cmd::workspace::view_extent(&s); + let p = json!({"op": "layer.addUrl", "params": {"url": format!("{base}/scene.tif"), "bbox": "view", "name": "Window"}}); + s.run_leaving_downloads("plugin:eng-window-view/f", &p).unwrap(); + let asked = s.take_url_requests(); + assert_eq!(asked.len(), 1); + assert_eq!(asked[0].params()["bbox"], "view", "what it asks is as it asked it"); + let close = + |a: [f64; 4], b: rustgis_geom::Extent| a.iter().zip([b.min_x, b.min_y, b.max_x, b.max_y]).all(|(a, b)| (a - b).abs() < 1e-9); + s.run("map.zoomToExtent", &json!({"extent": [-50.0, -20.0, -40.0, -10.0]})).unwrap(); + let window = window_asked_in(&s, asked[0].params(), asked[0].view_asked()).unwrap(); + assert!(close(window.bbox(), seen), "{:?} {seen:?}", window.bbox()); + // Without the request's box, `"view"` is the view now. + let now = crate::cmd::workspace::view_extent(&s); + assert!(close(window_asked(&s, asked[0].params()).unwrap().bbox(), now)); + // Read and added: the command run again is answered the raster added, and asks nothing more. + let id = s.download_started_for(&asked[0]).unwrap(); + assert!(s.download_landed_window(id, read_cog_window(&window).unwrap())); + let mut add = asked[0].params().clone(); + add["id"] = json!(id); + add["window"] = json!({"bbox": "view"}); + s.complete_download(id, &add).unwrap(); + let added = s.map().layer_by_name("Window").filter(|l| l.is_raster()).unwrap().id; + let ran = s.poll_plugin_retries(); + assert_eq!(ran.len(), 1, "{ran:?}"); + assert_eq!(ran[0].1.as_ref().unwrap()["layer"], json!(added), "{ran:?}"); + assert_eq!(s.map().layers.len(), layers + 1, "added once"); + assert!(s.take_url_requests().is_empty() && !s.plugin_retries_waiting()); +} + +/// A plugin's command asking for a window runs again once the app has read it and added it, and +/// its `layer.addUrl` then answers the raster added (#1330): the window it lands with is the one +/// the command asked for, so its claim takes it. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn a_plugin_command_runs_again_when_its_window_is_added() { + use crate::cmd::layer::{read_cog_window, window_asked}; + use rustgis_data::raster::cog::example; + let (base, _) = serve_ranges(example(1024, 256)); + let mut s = world(); + plugin("eng-window", &["edit", "network"]); + let layers = s.map().layers.len(); + let url = format!("{base}/scene.tif"); + let bbox = json!([10.25, 45.5, 10.5, 45.75]); + let p = json!({"op": "layer.addUrl", "params": {"url": url, "bbox": bbox, "name": "Window"}}); + s.run_leaving_downloads("plugin:eng-window/f", &p).unwrap(); + let asked = s.take_url_requests(); + assert_eq!(asked.len(), 1); + assert_eq!(s.map().layers.len(), layers, "nothing added while it is read"); + // The app reads it apart from the session and adds it with the request's number, and with + // what a task needs to read it again, as `start_window_read` does. + let id = s.download_started_for(&asked[0]).unwrap(); + let read = read_cog_window(&window_asked(&s, asked[0].params()).unwrap()).unwrap(); + assert!(s.download_landed_window(id, read)); + let mut add = asked[0].params().clone(); + add["id"] = json!(id); + add["window"] = json!({"bbox": bbox}); + s.complete_download(id, &add).unwrap(); + assert_eq!(s.map().layers.len(), layers + 1); + let added = s.map().layer_by_name("Window").filter(|l| l.is_raster()).unwrap().id; + let ran = s.poll_plugin_retries(); + assert_eq!(ran.len(), 1, "{ran:?}"); + let (cmd, answer) = &ran[0]; + assert_eq!(cmd, "plugin:eng-window/f"); + assert_eq!(answer.as_ref().unwrap()["layer"], json!(added), "{answer:?}"); + assert_eq!(s.map().layers.len(), layers + 1, "added once"); + assert!(s.take_url_requests().is_empty() && !s.plugin_retries_waiting()); +} + /// A window's read needs no session: on a thread, as the app reads it, then added when it lands /// (#1330). What can't be a window is refused before the read starts, and an error names the /// address without its keys. @@ -33429,11 +33598,19 @@ fn a_cog_window_comes_in_as_a_raster() { assert!(e.contains("names a .geojson file"), "{e}"); let e = s.run("layer.addUrl", &json!({"url": wrapped("https://data.example.com"), "bbox": [10.25, 45.5, 10.5, 45.75]})).unwrap_err(); assert!(!e.contains("names a .com file"), "{e}"); - // A plugin's command run by the app doesn't wait for a window: refused, and said. + // A plugin's command run by the app leaves a window to the app (#1330): asked for with its + // box and nothing fetched; a box that can't be read is refused as the command runs. + let before = sent.load(std::sync::atomic::Ordering::Relaxed); + let asked = json!({"url": format!("{base}/scene.tif"), "bbox": [10.25, 45.5, 10.5, 45.75], "name": "Left"}); s.leave_downloads = true; - let e = s.run("layer.addUrl", &json!({"url": format!("{base}/scene.tif"), "bbox": [10.25, 45.5, 10.5, 45.75]})).unwrap_err(); + assert_eq!(s.run("layer.addUrl", &asked).unwrap()["pending"], true); + let e = s.run("layer.addUrl", &json!({"url": format!("{base}/scene.tif"), "bbox": "here"})).unwrap_err(); s.leave_downloads = false; - assert!(e.contains("a plugin's command can't do in the app yet"), "{e}"); + assert!(e.contains("bbox is"), "{e}"); + assert_eq!(sent.load(std::sync::atomic::Ordering::Relaxed), before, "nothing fetched"); + let taken = s.take_url_requests(); + assert_eq!(taken.len(), 1); + assert_eq!(taken[0].params()["bbox"], asked["bbox"]); // Byte windows round nothing, and say nothing of it; an Int32 window of 16,777,216 plus the // byte value says how many `f32` rounded: the odd ones, half of its 65,536 cells. assert!(s.notices_since(mark0).iter().all(|n| !n.text.contains("rounded")), "{:?}", s.notices_since(mark0)); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index e1294788c3..79c08d514e 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -4500,7 +4500,7 @@ impl RustGisApp { } #[cfg(not(target_arch = "wasm32"))] if window { - return self.start_window_read(p, url, q, claim); + return self.start_window_read(p, url, q, claim, request.and_then(rustgis_engine::UrlRequest::view_asked)); } let shown = rustgis_engine::sources::address_in_message(&url); self.session.notify(i18n::fmt("Downloading {url}…", &[("url", &shown)]), false); @@ -4660,11 +4660,19 @@ impl RustGisApp { } /// Starts reading a window of a cloud-optimised GeoTIFF (`p`, a `ui.addUrl` with `"bbox"`) on a - /// thread, to add it with `q` when it lands (#1330). The command line and scripts wait for it - /// in `layer.addUrl` instead. What's wrong with what was asked is refused here, on the frame. + /// thread, to add it with `q` when it lands (#1330). The command line waits for it in + /// `layer.addUrl` instead; a plugin's command or a script the app runs leaves it to the app. + /// What's wrong with what was asked is refused here, on the frame. #[cfg(not(target_arch = "wasm32"))] - fn start_window_read(&mut self, p: &Value, url: String, mut q: Value, claim: Option) -> Result { - let asked = rustgis_engine::cmd::layer::window_asked(&self.session, p).and_then(|asked| { + fn start_window_read( + &mut self, + p: &Value, + url: String, + mut q: Value, + claim: Option, + seen: Option<[f64; 4]>, + ) -> Result { + let asked = rustgis_engine::cmd::layer::window_asked_in(&self.session, p, seen).and_then(|asked| { if self.ui.window_reads.len() >= MAX_WINDOW_READS { return Err(i18n::fmt( "{url}: {count} windows are being read already: ask again once one has been added", diff --git a/crates/ui-egui/src/pluginpanel.rs b/crates/ui-egui/src/pluginpanel.rs index 47b27a5723..29112ac0f7 100644 --- a/crates/ui-egui/src/pluginpanel.rs +++ b/crates/ui-egui/src/pluginpanel.rs @@ -985,6 +985,57 @@ mod tests { plugins::unload("ui-scan"); } + /// A plugin's command asking for a window of a cloud-optimised GeoTIFF doesn't wait for it: + /// the app reads it off the frame, adds it when it lands, and runs the command again, whose + /// `layer.addUrl` answers what was added (#1330). + #[test] + fn a_plugin_reads_a_window_off_the_frame() { + let _turn = crate::fetching::turn(); + use rustgis_engine::plugins; + use std::sync::atomic::Ordering; + let slow = crate::fetching::slow_ranges(rustgis_engine::data::raster::cog::example(1024, 256)); + let pkg = plugins::Package::from_zip(&plugins::testing::package("ui-window", &["edit", "network"])).unwrap(); + plugins::load(pkg, "test").unwrap(); + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1400.0, 860.0)).with_step_dt(0.05).build_ui_state( + |ui, app: &mut RustGisApp| { + let ctx = ui.ctx().clone(); + app.logic(&ctx); + app.ui(ui); + }, + RustGisApp::with_sample(), + ); + let url = format!("{}/scene.tif", slow.origin); + // The header in hand, so only the window's tiles wait. + rustgis_engine::render::rustgis_tiles::cog::info_blocking(&url).unwrap(); + slow.open.store(false, Ordering::Relaxed); + let params = json!({"url": url, "bbox": [10.25, 45.5, 10.5, 45.75], "name": "Window"}); + let r = h.state_mut().run("plugin:ui-window/f", json!({"op": "layer.addUrl", "params": params})).unwrap(); + assert_eq!(r["pending"], true); + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10); + while slow.held.load(Ordering::Relaxed) == 0 { + assert!(std::time::Instant::now() < deadline, "no request held"); + let frame = std::time::Instant::now(); + h.step(); + assert!(frame.elapsed() < std::time::Duration::from_secs(5), "the read held the frame"); + std::thread::sleep(std::time::Duration::from_millis(20)); + } + assert!(h.state().session.map().layer_by_name("Window").is_none(), "nothing waited"); + assert!(h.state().session.plugin_retries_waiting()); + slow.open.store(true, Ordering::Relaxed); + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30); + let landed = |app: &RustGisApp| app.session.map().layer_by_name("Window").is_some() && !app.session.plugin_retries_waiting(); + while !landed(h.state()) { + assert!(std::time::Instant::now() < deadline, "timed out waiting: {:?}", h.state().session.notices); + h.step(); + std::thread::sleep(std::time::Duration::from_millis(20)); + } + let app = h.state(); + assert!(app.session.map().layer_by_name("Window").is_some_and(|l| l.is_raster())); + assert!(app.session.map().layer_by_name("Window (2)").is_none(), "added once"); + assert!(app.session.notices.iter().all(|n| !n.error), "{:?}", app.session.notices); + plugins::unload("ui-window"); + } + /// A download that lands after its plugin command stopped waiting for it isn't added, and /// that is said once, with its address, whether or not that holds a key (#1202). #[test] diff --git a/docs/plugins.md b/docs/plugins.md index c7c1a600e2..8a567f6b29 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -483,7 +483,8 @@ download a file whole it answers `{"pending": true, "url": …}`: RustGIS downlo frame, saying so in the status bar, and adds the layer when it lands (its own undo step), as **Home › Add › Add Data › From URL** does. On the desktop that goes for reading a GeoTIFF's, FlatGeobuf's or GeoParquet's header too; in the web app a PMTiles archive, a cloud-optimised -GeoTIFF or a FlatGeobuf or GeoParquet whose header was fetched already is added at once. Up to 16 +GeoTIFF or a FlatGeobuf or GeoParquet whose header was fetched already is added at once. A window +of a cloud-optimised GeoTIFF (`"bbox"`, on the desktop) is read and added the same way. Up to 16 downloads wait at a time: a request past 16 fails, and if the command then fails too, none of its downloads start. Run by `rustgis-cli`, a script or a task, the command waits and answers what was added. diff --git a/docs/user-guide/data-formats.md b/docs/user-guide/data-formats.md index cf54f645e2..cc75685e93 100644 --- a/docs/user-guide/data-formats.md +++ b/docs/user-guide/data-formats.md @@ -191,12 +191,14 @@ tile), without a STAC catalogue: A window reads 256 bands at most. A window near the limit takes several hundred MB of memory while it is read and kept. - The Add Data From URL dialog has no box to fill in, so a window is asked for by a command. On - the command line and in a script, `layer.addUrl` waits for it and answers what was added. In the - desktop app, `ui.addUrl` with `"bbox"` reads it off the frame: the map stays in use, and the - layer is added when the read lands. These are read one at a time, with 16 under way at most, - and the next starts once the app has taken the last. If another project is opened meanwhile, one - still waiting isn't read, and one being read can't be cancelled yet: it runs to its end and - isn't added. Each is said in a notice. + the command line and in Python, `layer.addUrl` waits for it and answers what was added. In the + desktop app, `ui.addUrl` with `"bbox"`, and `layer.addUrl` with it from a plugin's command or + from a script or model run there, read it off the frame: the map stays in use, and the layer is + added when the read lands (a plugin's command then runs again with what adding it answered; a + script or model is answered `"pending"` and goes on, as with a whole file it downloads). These + are read one at a time, with 16 under way at most, and the next starts once the app has taken + the last. If another project is opened meanwhile, one still waiting isn't read, and one being + read can't be cancelled yet: it runs to its end and isn't added. Each is said in a notice. - The file's no-data, colour table (a palette file draws in its colours) and band names come with it, and class names given as `CLASS_` items in GDAL's metadata tag, as the MapBiomas files on Source Cooperative give theirs. These come from the file's own tags: an `.aux.xml`