diff --git a/crates/engine/src/cmd/sources.rs b/crates/engine/src/cmd/sources.rs index e411d33e00..b4475dac7a 100644 --- a/crates/engine/src/cmd/sources.rs +++ b/crates/engine/src/cmd/sources.rs @@ -5,14 +5,14 @@ use serde_json::{Value, json}; use super::ps; -use crate::sources::{KINDS, Source, library}; +use crate::sources::{KINDS, Source, URL_FLAGS, library}; use crate::{CmdResult, CommandSpec as C, Session}; pub fn commands() -> Vec { vec![ C::new("sources.list", "List Data Sources", "Home › Add › Data Sources", list).roles(&[]).effects(crate::policy::Effects::NONE).view(), C::new("sources.save", "Save Data Source", "Home › Add › Data Sources", |s, p| keep(s, p, "saved")).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).view().params( - r#"{"kind": "service"|"database"|"stac"|"file"|"url"|"tiles"|"zarr"|"s3", "address": "its path, web address or connection (never with a password)", "name"?: text, "extra"?: {"type"?: service type, "layer"?: text}}"#, + r#"{"kind": "service"|"database"|"stac"|"file"|"url"|"tiles"|"zarr"|"s3", "address": "its path, web address or connection (never with a password)", "name"?: text, "extra"?: {"type"?: service type, "layer"?: text, "service"?: "true" (a url source: a web service's features, read in pages), "coverage"?: "true" (a url source: a WCS coverage's GetCoverage address), "coverageCrs"?: "EPSG:" (with coverage: the system it was asked in)}}"#, ), C::new("sources.favourite", "Favourite Data Source", "Home › Add › Data Sources", favourite).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() @@ -39,9 +39,21 @@ fn adds_with(kind: &str) -> &'static str { } } +/// How this source is added: [`adds_with`] its kind, with the flags a `url` source's add had. +fn add_with(s: &Source) -> String { + let mut keys = vec!["url".to_string()]; + if s.kind == "url" { + keys.extend(URL_FLAGS.iter().filter(|f| s.flag(f)).map(|f| format!("{f}: true"))); + if s.flag("coverage") && s.extra.get("coverageCrs").is_some() { + keys.push("coverageCrs".into()); + } + } + if keys.len() == 1 { adds_with(&s.kind).to_string() } else { format!("layer.addUrl {{{}}}", keys.join(", ")) } +} + fn shown(s: &Source) -> Value { let mut v = serde_json::to_value(s).unwrap_or(Value::Null); - v["addWith"] = json!(adds_with(&s.kind)); + v["addWith"] = json!(add_with(s)); v } diff --git a/crates/engine/src/sources.rs b/crates/engine/src/sources.rs index 43d0100c91..ca2dd1922e 100644 --- a/crates/engine/src/sources.rs +++ b/crates/engine/src/sources.rs @@ -506,7 +506,20 @@ fn same(a: &Source, b: &Source) -> bool { identity(&a.kind, &a.address) == identity(&b.kind, &b.address) } +/// What a `url` source's add may have had that changes how its address is read, kept in `extra` +/// for adding it again with `layer.addUrl`: a service's features are read page by page, a WCS +/// coverage in the system it was asked in (`coverageCrs`, kept with it). +pub const URL_FLAGS: [&str; 2] = ["service", "coverage"]; + +/// A flag as `extra` keeps it: its values are text. +const FLAG_ON: &str = "true"; + impl Source { + /// Whether the add this source records had `flag` (one of [`URL_FLAGS`]). + pub fn flag(&self, flag: &str) -> bool { + self.extra.get(flag).and_then(Value::as_str) == Some(FLAG_ON) + } + /// A source's identity: its kind and address. Everywhere a list is matched (kept, forgotten, /// noted as recent, shown as a favourite) it is by this; `extra` (a layer, a service type) /// is what the last one kept said. @@ -922,7 +935,24 @@ pub fn added_by(id: &str, p: &Value) -> Option { let layer = |k: &str| text(p, k).map(|l| json!({k: l})).unwrap_or(Value::Null); match id { "layer.addData" => Some(src("file", text(p, "path")?, text(p, "name"), layer("layer"))), - "layer.addUrl" | "layer.addDownload" => Some(src("url", text(p, "url")?, text(p, "name"), layer("layer"))), + "layer.addUrl" | "layer.addDownload" => { + let mut extra = serde_json::Map::new(); + if let Some(l) = text(p, "layer") { + extra.insert("layer".into(), json!(l)); + } + for flag in URL_FLAGS { + if p.get(flag).and_then(crate::cmd::bool_value) == Some(true) { + extra.insert(flag.into(), json!(FLAG_ON)); + } + } + if extra.contains_key("coverage") + && let Some(crs) = text(p, "coverageCrs") + { + extra.insert("coverageCrs".into(), json!(crs)); + } + let extra = if extra.is_empty() { Value::Null } else { Value::Object(extra) }; + Some(src("url", text(p, "url")?, text(p, "name"), extra)) + } "map.addTiles" => { let url = text(p, "url")?; let mut extra = serde_json::Map::new(); @@ -1697,6 +1727,16 @@ MIIE added_by("service.add", &json!({"url": "https://h/wms", "layer": "a", "type": "wms"})).unwrap().extra, json!({"layer": "a", "type": "wms"}) ); + let service = json!({"url": "https://h/collections/a/items", "name": "A", "service": true}); + assert_eq!(added_by("layer.addUrl", &service).unwrap().extra, json!({"service": "true"})); + assert_eq!(clean(added_by("layer.addDownload", &service).unwrap()).unwrap().extra, json!({"service": "true"})); + assert_eq!(added_by("layer.addUrl", &json!({"url": "https://h/a.geojson", "service": false})).unwrap().extra, Value::Null); + let coverage = json!({"url": "https://h/wcs?REQUEST=GetCoverage", "coverage": true, "coverageCrs": "EPSG:2056"}); + let kept = clean(added_by("layer.addDownload", &coverage).unwrap()).unwrap(); + assert_eq!(kept.extra, json!({"coverage": "true", "coverageCrs": "EPSG:2056"})); + assert!(kept.flag("coverage") && !kept.flag("service")); + // A system without the coverage flag isn't one the address is read in. + assert_eq!(added_by("layer.addUrl", &json!({"url": "https://h/a.tif", "coverageCrs": "EPSG:2056"})).unwrap().extra, Value::Null); assert!(added_by("layer.remove", &json!({})).is_none()); assert!(added_by("layer.addData", &json!({})).is_none()); assert!(library().iter().all(|(_, s)| clean(s.clone()).is_ok())); diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index af5180eb7a..6264ad5eec 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -27165,6 +27165,10 @@ fn a_service_query_added_by_address_is_read_in_pages_without_the_app() { assert_eq!((sym.marker, sym.fill), (rustgis_map::symbology::Marker::Square, rustgis_map::Rgba::rgb(230, 0, 0))); let texts: Vec<_> = s.notices.iter().map(|n| n.text.as_str()).collect(); assert!(!texts.iter().any(|t| t.contains("record limit") || t.contains("aren't read")), "{texts:?}"); + // Recent keeps that it was read as a service, so adding it again reads every page too. + let recent = &s.run("sources.list", &Value::Null).unwrap()["recent"][0]; + assert_eq!((&recent["kind"], &recent["extra"]), (&json!("url"), &json!({"service": "true"}))); + assert_eq!(recent["addWith"], "layer.addUrl {url, service: true}"); // Bounds that aren't a geographic rectangle are refused before anything is fetched, as when the // app adds the download. let e = s.run("layer.addUrl", &json!({"url": url, "name": "Bad", "service": true, "bounds": [10, 0, 5, 1]})).unwrap_err(); diff --git a/crates/engine/src/tests/services_own.rs b/crates/engine/src/tests/services_own.rs index 79183ce12e..0912bf7c2e 100644 --- a/crates/engine/src/tests/services_own.rs +++ b/crates/engine/src/tests/services_own.rs @@ -311,4 +311,11 @@ fn a_coverage_answer_without_its_system_is_read_in_the_one_asked_for() { assert_eq!(crs_of(&s, &added["layer"]), lv95); let added = s.run("layer.addUrl", &json!({"url": unsaid, "name": "Given", "coverage": true, "coverageCrs": "EPSG:2056"})).unwrap(); assert_eq!(crs_of(&s, &added["layer"]), lv95); + // Recent keeps both, so adding it again reads it in the same system. + let recent = &s.run("sources.list", &serde_json::Value::Null).unwrap()["recent"][0]; + assert_eq!((&recent["address"], &recent["extra"]), (&json!(unsaid), &json!({"coverage": "true", "coverageCrs": "EPSG:2056"}))); + assert_eq!(recent["addWith"], "layer.addUrl {url, coverage: true, coverageCrs}"); + // coverageCrs alone doesn't make a source a coverage. + let saved = s.run("sources.save", &json!({"kind": "url", "address": unsaid, "extra": {"coverageCrs": "EPSG:2056"}})).unwrap(); + assert_eq!(saved["addWith"], "layer.addUrl {url}"); } diff --git a/crates/ui-egui/src/sourcespane.rs b/crates/ui-egui/src/sourcespane.rs index b2e2a7ba2c..dda123c7ac 100644 --- a/crates/ui-egui/src/sourcespane.rs +++ b/crates/ui-egui/src/sourcespane.rs @@ -148,6 +148,16 @@ pub fn open(app: &mut RustGisApp, s: &Source) -> Result<(), String> { if let Some(l) = text(x, "layer") { p["layer"] = json!(l); } + for flag in rustgis_engine::sources::URL_FLAGS { + if s.flag(flag) { + p[flag] = json!(true); + } + } + if s.flag("coverage") + && let Some(crs) = text(x, "coverageCrs") + { + p["coverageCrs"] = json!(crs); + } app.run("ui.addUrl", p).map(|_| ()) } "tiles" => { @@ -516,3 +526,57 @@ pub fn show(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { }); }); } + +#[cfg(test)] +mod tests { + use super::*; + use rustgis_engine::render::rustgis_tiles::{ + address::{Origin, Reach}, + cache, + }; + + /// A service's features added by address are read page by page when added again from Recent, + /// as they were the first time. + #[test] + fn a_service_read_by_address_is_read_in_pages_again_from_recent() { + let first = "https://recent-pages.example/collections/parcels/items"; + let next = "https://recent-pages.example/collections/parcels/items?page=2"; + let _turn = crate::fetching::turn(); + cache::set_fetcher_with_origin(move |url, ceiling, done, _| { + let clean = cache::split_download_limit(&url).0.to_string(); + let links = if clean == first { json!([{"rel":"next","type":"application/geo+json","href":next}]) } else { json!([]) }; + let body = json!({"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[0,0]},"properties":{}}],"links":links}); + let origin = Origin::with_observed_reach(&clean, Reach::Public.min(ceiling)).unwrap(); + done(Ok(cache::Fetched { bytes: body.to_string().into_bytes(), origin })); + }); + let mut app = RustGisApp::new(rustgis_engine::Session::new(rustgis_engine::Project::default())); + let read = |app: &mut RustGisApp| { + for _ in 0..4 { + crate::serviceui::poll_pages(app, &egui::Context::default()); + } + app.session.map().layers.last().map(|l| l.dataset.features.len()) + }; + app.run("ui.addUrl", json!({"url": first, "name": "Parcels", "service": true})).unwrap(); + assert_eq!(read(&mut app), Some(2), "{:?}", app.dialogs.error); + let recent = app.session.sources.recent.first().cloned().unwrap(); + assert_eq!((recent.kind.as_str(), recent.address.as_str()), ("url", first)); + open(&mut app, &recent).unwrap(); + assert_eq!(read(&mut app), Some(2), "{:?}", app.dialogs.error); + assert_eq!(app.session.map().layers.len(), 2); + } + + /// A WCS coverage added by address is asked for again from Recent as a coverage, in the system + /// it was asked in the first time. + #[test] + fn a_coverage_is_added_again_from_recent_in_the_system_it_was_asked_in() { + let _turn = crate::fetching::turn(); + cache::set_fetcher(|_, _| {}); + let mut app = RustGisApp::new(rustgis_engine::Session::new(rustgis_engine::Project::default())); + let url = "https://wcs-recent.invalid/wcs?SERVICE=WCS&VERSION=2.0.1&REQUEST=GetCoverage&COVERAGEID=dem"; + let added = json!({"id": 1, "url": url, "name": "Terrain", "coverage": true, "coverageCrs": "EPSG:2056"}); + let recent = rustgis_engine::sources::clean(rustgis_engine::sources::added_by("layer.addDownload", &added).unwrap()).unwrap(); + open(&mut app, &recent).unwrap(); + let asked = app.ui.downloads.last().map(|d| (d.params["coverage"].clone(), d.params["coverageCrs"].clone())); + assert_eq!(asked, Some((json!(true), json!("EPSG:2056")))); + } +}