Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
232 changes: 173 additions & 59 deletions crates/engine/src/cmd/layer.rs

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions crates/engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,8 @@ struct PutAside {
url_requests: Vec<UrlRequest>,
policy_work: policy::downloads::Work,
landed_reads: Vec<(u64, LandedRead)>,
#[cfg(not(target_arch = "wasm32"))]
landed_windows: Vec<(u64, cmd::layer::CogWindow)>,
plugin_retries: Vec<cmd::plugins::fetch::Retry>,
url_waits: cmd::plugins::fetch::UrlWaits,
/// A style of the project still loading, which mustn't move the next one's view.
Expand Down Expand Up @@ -812,6 +814,10 @@ pub struct Session {
/// Downloads that landed already read (an ArcGIS layer's protocol buffer pages, joined),
/// waiting for `layer.addDownload` too.
pub(crate) landed_reads: Vec<(u64, LandedRead)>,
/// Windows of cloud-optimised GeoTIFFs the app read off the frame, waiting for
/// `layer.addDownload` too.
#[cfg(not(target_arch = "wasm32"))]
pub(crate) landed_windows: Vec<(u64, cmd::layer::CogWindow)>,
/// 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<UrlRequest>,
Expand Down Expand Up @@ -1142,6 +1148,8 @@ impl Session {
downloading: Vec::new(),
downloaded: Vec::new(),
landed_reads: Vec::new(),
#[cfg(not(target_arch = "wasm32"))]
landed_windows: Vec::new(),
url_requests: Vec::new(),
file_outputs: Vec::new(),
#[cfg(test)]
Expand Down Expand Up @@ -1232,6 +1240,18 @@ impl Session {
landed
}

/// Hands over a window of a cloud-optimised GeoTIFF read off the frame
/// ([`cmd::layer::read_cog_window`]), for `layer.addDownload {"id"}` as
/// [`Session::download_landed`] does.
#[cfg(not(target_arch = "wasm32"))]
pub fn download_landed_window(&mut self, id: u64, window: cmd::layer::CogWindow) -> bool {
let landed = self.landed(id);
if landed {
self.landed_windows.push((id, window));
}
landed
}

/// Takes a download off those under way: `false` when it isn't one of them (another project
/// was opened meanwhile).
fn landed(&mut self, id: u64) -> bool {
Expand All @@ -1250,6 +1270,12 @@ impl Session {
true
}

/// Whether a download is still under way for this project: `false` once another project was
/// opened.
pub fn download_under_way(&self, id: u64) -> bool {
self.downloading.contains(&id)
}

/// Forgets a download that failed.
pub fn download_failed(&mut self, id: u64) {
if self.download_owner(id).is_err() {
Expand Down Expand Up @@ -1793,6 +1819,8 @@ impl Session {
downloading: take(&mut self.downloading),
downloaded: take(&mut self.downloaded),
landed_reads: take(&mut self.landed_reads),
#[cfg(not(target_arch = "wasm32"))]
landed_windows: take(&mut self.landed_windows),
url_requests: take(&mut self.url_requests),
policy_work: self.take_policy_work(),
plugin_retries: take(&mut self.plugin_retries),
Expand Down Expand Up @@ -1846,6 +1874,8 @@ impl Session {
downloading,
downloaded,
landed_reads,
#[cfg(not(target_arch = "wasm32"))]
landed_windows,
url_requests,
policy_work,
plugin_retries,
Expand Down Expand Up @@ -1874,6 +1904,10 @@ impl Session {
(self.downloading, self.downloaded, self.url_requests) = (downloading, downloaded, url_requests);
self.restore_policy_work(policy_work);
self.landed_reads = landed_reads;
#[cfg(not(target_arch = "wasm32"))]
{
self.landed_windows = landed_windows;
}
(self.trend_results, self.raster_results, self.raster_runs) = (trend_results, raster_results, raster_runs);
#[cfg(feature = "geolibre")]
{
Expand Down
15 changes: 15 additions & 0 deletions crates/engine/src/policy/downloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ impl Session {
}
self.downloaded.retain(|(k, _)| *k != id);
self.landed_reads.retain(|(k, _)| *k != id);
#[cfg(not(target_arch = "wasm32"))]
self.landed_windows.retain(|(k, _)| *k != id);
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
let url = crate::sources::redact_text(p.get("url").and_then(Value::as_str).unwrap_or("download"));
if added {
Ok(Some(format!("{url}: it was added already for its plugin command, so it wasn't added again")))
Expand Down Expand Up @@ -357,6 +359,7 @@ impl Session {
if !self.download_is_pending(id)
&& !self.downloaded.iter().any(|(n, _)| *n == id)
&& !self.landed_reads.iter().any(|(n, _)| *n == id)
&& !self.landed_window(id)
{
return Err("this download belongs to a replaced project or was dropped".into());
}
Expand All @@ -372,10 +375,22 @@ impl Session {
self.downloading.retain(|n| *n != id);
self.downloaded.retain(|(n, _)| *n != id);
self.landed_reads.retain(|(n, _)| *n != id);
#[cfg(not(target_arch = "wasm32"))]
self.landed_windows.retain(|(n, _)| *n != id);
return Err(e);
}
Ok(())
}
/// Whether a window read off the frame landed for download `id` (#1330).
fn landed_window(&self, id: u64) -> bool {
#[cfg(not(target_arch = "wasm32"))]
return self.landed_windows.iter().any(|(n, _)| *n == id);
#[cfg(target_arch = "wasm32")]
{
let _ = id;
false
}
}
/// Owns a principal-free predicate that survives queues and browser awaits.
pub fn download_still(&self, id: u64) -> Option<crate::render::rustgis_tiles::cache::Still> {
let claim = self.policy.work.claims.get(&id)?;
Expand Down
71 changes: 71 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29333,6 +29333,77 @@ 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 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.
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn a_cog_windows_read_is_apart_from_adding_it() {
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();
let url = format!("{base}/scene.tif?token=hidden-1330");
let p = json!({"url": url, "bbox": [10.25, 45.5, 10.5, 45.75], "name": "Window"});
let asked = window_asked(&s, &p).unwrap();
let layers = s.map().layers.len();
let read = std::thread::spawn(move || read_cog_window(&asked)).join().unwrap().unwrap();
assert_eq!(s.map().layers.len(), layers, "nothing added yet");
let id = s.download_started();
assert!(s.download_landed_window(id, read));
let mark = s.notice_mark();
let r = s.run("layer.addDownload", &json!({"id": id, "url": url, "name": "Window"})).unwrap();
assert_eq!(r["window"], json!({"columns": [256, 512], "rows": [256, 512], "level": 0, "cellSize": 111_320.0 / 1024.0}));
assert!(s.landed_windows.is_empty());
let said: Vec<String> = s.notices_since(mark).iter().map(|n| n.text.clone()).collect();
assert!(said.iter().any(|n| n.starts_with("Window: a 256 × 256 window of the full image")), "{said:?}");
let w = s.map().layer_by_name("Window").and_then(|l| l.raster.as_ref()?.data.clone()).unwrap();
assert_eq!((w.value(0, 0, 0), w.value(0, 10, 5)), (Some(0.0), Some(15.0)));
assert_eq!(s.map().layer(r["layer"].as_u64().unwrap() as u32).map(|l| l.name.as_str()), Some("Window"));
// A download that isn't a window isn't added as one.
let id = s.download_started();
assert!(s.download_landed(id, b"{}".to_vec()));
let e = s.run("layer.addDownload", &json!({"id": id, "url": url, "window": {"bbox": [1, 2, 3, 4]}})).unwrap_err();
assert!(e.contains("\"window\" is for") && !e.contains("hidden-1330"), "{e}");
// A project opened while it was read: it isn't kept to be added.
let read = read_cog_window(&window_asked(&s, &p).unwrap()).unwrap();
let id = s.download_started();
s.run("project.new", &json!({})).unwrap();
assert!(!s.download_landed_window(id, read));
assert!(s.landed_windows.is_empty());
// Refused as the command refuses them, without the address's keys.
let bad = |more: Value| {
let mut q = json!({"url": "https://x.org/a.tif?token=hidden-1330", "bbox": [1, 2, 3, 4]});
q.as_object_mut().unwrap().extend(more.as_object().unwrap().clone());
window_asked(&s, &q).err().unwrap()
};
for (more, why) in [
(json!({"bbox": "here"}), "bbox is"),
(json!({"cellSize": -1}), "cellSize is"),
(json!({"whole": true}), "leave out"),
(json!({"crs": "EPSG:4326"}), "doesn't read crs"),
(json!({"url": "https://x.org/a.geojson?token=hidden-1330"}), "names a .geojson file"),
(json!({"url": "C:/data/a.tif"}), "address (Add Data opens files)"),
(json!({"geolibreProject": true}), "GeoLibre download state"),
(json!({"geolibreLayer": 1}), "GeoLibre download state"),
(json!({"geolibreZarr": false}), "GeoLibre download state"),
(json!({"service": true}), "doesn't read service"),
(json!({"coverage": true}), "doesn't read coverage"),
(json!({"bounds": [1, 2, 3, 4]}), "doesn't read bounds"),
(json!({"cube": true}), "doesn't read cube"),
] {
let e = bad(more);
assert!(e.contains(why) && !e.contains("hidden-1330"), "{why}: {e}");
}
// `"cube": false` asks for nothing.
assert!(window_asked(&s, &json!({"url": "https://x.org/a.tif", "bbox": [1, 2, 3, 4], "cube": false})).is_ok());
// A file that can't be read: said by the read, without the address's keys.
let (gone, _) = serve_ranges(b"not a tiff at all".to_vec());
let asked = window_asked(&s, &json!({"url": format!("{gone}/a.tif?token=hidden-1330"), "bbox": [1, 2, 3, 4]})).unwrap();
let e = read_cog_window(&asked).err().unwrap();
assert!(e.contains("/a.tif") && !e.contains("hidden-1330"), "{e}");
}

/// `layer.addUrl` with `"bbox"` (#1204): a window of a cloud-optimised GeoTIFF, read by range as a
/// raster layer, at full resolution or at the overview a cell size asks for; a palette file's
/// colours and class names come with it; the window is kept in a saved project, class names too.
Expand Down
Loading
Loading