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
9 changes: 9 additions & 0 deletions crates/engine/src/cmd/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4425,6 +4425,15 @@ fn add_url_as_given(s: &mut Session, p: &Value) -> CmdResult {
// probe, a download) is left to the app, which does it off the frame, as in the browser.
#[cfg(not(target_arch = "wasm32"))]
if s.leave_downloads {
// A data cube's GeoTIFF (`"cube"`, as `stac.addCube` adds them) is a tile layer whose
// tiles are read in place on later frames: nothing waits, so it is added now, its header
// unread, as in the browser (#2418). Left to the app, no tile layer would come. One that
// isn't cloud-optimised says so as the map draws it (its header's error, as any tile
// layer's). The address is a web one (`web_address` above).
let tiff = is_tiff_url(url) || b(p, "cog").unwrap_or(false);
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
if b(p, "cube") == Some(true) && tiff && !scan && !b(p, "whole").unwrap_or(false) {
return add_cog(s, p, url, false);
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
}
// The address as the app downloads it (trimmed): the request, its download and the
// command run again all name it the same.
let mut asked = p.clone();
Expand Down
17 changes: 11 additions & 6 deletions crates/engine/src/cmd/stac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1263,13 +1263,21 @@ fn add_cube(s: &mut Session, p: &Value) -> CmdResult {
return Err(e);
}
};
let file = first.split('#').next().unwrap_or(first);
// A file left to the app to download (a plugin's command in the app, whose address
// doesn't name a GeoTIFF) added nothing yet, and would come as a file read whole.
if r.get("pending").and_then(Value::as_bool) == Some(true) {
undo(s);
return Err(format!(
"{file}: left to the app to download, not read in place as a GeoTIFF; a data cube needs cloud-optimised GeoTIFFs (.tif) read in place"
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
));
}
// The tile layer add_url put in (at the index it gives, and the file's address) follows
// the slider through its dates; a file read whole instead (a GeoTIFF that isn't
// cloud-optimised) can't.
let m = s.map_mut();
let file = first.split('#').next();
let at = r.get("index").and_then(Value::as_u64).and_then(|i| usize::try_from(i).ok());
match at.and_then(|i| m.tiles.get_mut(i)).filter(|t| t.url.split('#').next() == file) {
match at.and_then(|i| m.tiles.get_mut(i)).filter(|t| t.url.split('#').next() == Some(file)) {
Some(t) => {
t.time_urls = urls;
t.name = name;
Expand All @@ -1280,10 +1288,7 @@ fn add_cube(s: &mut Session, p: &Value) -> CmdResult {
}
None => {
undo(s);
return Err(format!(
"{}: not a cloud-optimised GeoTIFF read in place; a data cube needs those",
first.split('#').next().unwrap_or(first)
));
return Err(format!("{file}: not a cloud-optimised GeoTIFF read in place; a data cube needs those"));
}
}
}
Expand Down
97 changes: 97 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26718,6 +26718,103 @@ fn a_stac_data_cube_on_the_time_slider() {
assert_eq!((s.map().tiles.len(), s.map().layers.len()), (tiles, layers));
}

/// A plugin's command in the app (`leave_downloads`) adds a STAC data cube as the command line
/// does (#2418): a tile layer per grid tile, its files read in place with nothing left to the app,
/// and the slider over their dates; `cube.open` with a STAC entry goes the same way. A file whose
/// address doesn't name a GeoTIFF is left to the app, and the refusal says so; one that isn't
/// cloud-optimised is added, and the map drawing it says so.
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn a_stac_data_cube_from_a_plugin_command_in_the_app() {
use crate::render::rustgis_tiles::cubes;
let (cog, _) = serve_ranges(rustgis_data::raster::cog::example(512, 256));
let item = |id: &str, tile: &str, date: &str, href: &str| {
format!(
r#"{{"type": "Feature", "id": "{id}", "collection": "cube", "bbox": [10, 45, 11, 46], "geometry": null,
"properties": {{"datetime": "{date}T00:00:00Z", "bdc:tiles": ["{tile}"]}},
"assets": {{"NDVI": {{"href": "{cog}/{href}", "eo:bands": [{{"scale": 0.0001}}]}}}}}}"#
)
};
let features =
[item("a1", "T1", "2024-01-01", "a1.tif"), item("a2", "T1", "2024-01-17", "a2.tif"), item("b1", "T2", "2024-01-01", "b1.tif")]
.join(",");
let base = serve_routes(vec![("/v1/search", format!(r#"{{"type": "FeatureCollection", "features": [{features}]}}"#))], 8);
let api = format!("{base}/v1");
let cube = json!({"url": api, "collection": "cube", "asset": "ndvi", "bbox": [10, 45, 11, 46], "start": "2024-01-01", "end": "2024-01-31", "range": [0, 0.9]});
let mut s = Session::new(Project::default());
s.leave_downloads = true;
let r = s.run("stac.addCube", &cube).unwrap();
assert_eq!((r["tiles"].as_u64(), r["dates"].as_u64(), r["layers"].as_u64()), (Some(2), Some(2), Some(2)), "{r}");
assert!(s.take_url_requests().is_empty(), "a cube's files are read in place, not left to the app to download");
let t1 = s.map().tiles.iter().find(|t| t.name.ends_with("T1")).unwrap();
assert_eq!(t1.time_urls.len(), 2);
assert!(t1.time_urls.iter().all(|(_, u)| u.ends_with("#range=0,9000")), "{:?}", t1.time_urls);
assert_eq!(s.map().tiles.iter().filter(|t| !t.time_urls.is_empty()).count(), 2);
assert_eq!(s.run("time.info", &Value::Null).unwrap()["steps"], 2);
s.run("time.goTo", &json!({"step": 1})).unwrap();
assert!(s.map().tiles.iter().find(|t| t.name.ends_with("T1")).unwrap().url.contains("/a2.tif"));
// cube.open with a STAC entry of cube.catalog runs stac.addCube: the same tile layers.
let mut entry = cubes::find("bdc-mod13q1-ndvi").unwrap();
entry.url = api.clone();
entry.source = cubes::Source::Stac { collection: "cube".into(), asset: "NDVI".into() };
(entry.bbox, entry.start, entry.end) = ([0.0, 40.0, 20.0, 50.0], "2023-05-01".into(), Some("2024-12-31".into()));
(entry.range, entry.max_cloud) = (Some([0.0, 0.9]), None);
let mut opened = Session::new(Project::default());
opened.leave_downloads = true;
let r = crate::cmd::cube_catalog::open_entry(
&mut opened,
&entry,
&json!({"bbox": [10, 45, 11, 46], "start": "2024-01-01", "end": "2024-01-31"}),
)
.unwrap();
assert_eq!((r["tiles"].as_u64(), r["layers"].as_u64()), (Some(2), Some(2)), "{r}");
assert!(opened.take_url_requests().is_empty());
assert_eq!(opened.run("time.info", &Value::Null).unwrap()["steps"], 2);
// A file whose address names no GeoTIFF is left to the app to download: said so, nothing kept
// (the request goes with the failed command), and not taken for one that isn't cloud-optimised.
let unnamed = item("u1", "U", "2024-01-01", "files/u1");
let unnamed_api = serve_routes(vec![("/v1/search", format!(r#"{{"type": "FeatureCollection", "features": [{unnamed}]}}"#))], 2);
let mut odd = cube.clone();
odd["url"] = json!(format!("{unnamed_api}/v1"));
let (tiles, layers) = (s.map().tiles.len(), s.map().layers.len());
let e = s.run("stac.addCube", &odd).unwrap_err();
assert!(e.contains("left to the app to download") && !e.contains("not a cloud-optimised"), "{e}");
assert_eq!((s.map().tiles.len(), s.map().layers.len()), (tiles, layers));
assert!(s.take_url_requests().is_empty(), "the failed command's request is taken back with it");
// A file that isn't cloud-optimised is added all the same (its header isn't read on the
// frame); its header's read fails, and the map drawing it says why, as for any tile layer.
let gt = rustgis_data::raster::GeoTransform { origin_x: 10.0, origin_y: 46.0, pixel_width: 0.5, pixel_height: -0.5 };
let crs = rustgis_data::raster::Crs::LonLat;
let plain =
rustgis_data::raster::Raster::new("p", 2, 2, vec![vec![1.0; 4]], gt, crs, None, rustgis_data::raster::ColorHint::Gray, "Float32")
.unwrap();
let (stripped, _) = serve_ranges(rustgis_data::raster::geotiff::write(&plain, 0).unwrap());
let one = item("p1", "P", "2024-01-01", "p1.tif").replace(&format!("{cog}/"), &format!("{stripped}/"));
let stripped_api = serve_routes(vec![("/v1/search", format!(r#"{{"type": "FeatureCollection", "features": [{one}]}}"#))], 2);
let mut flat = Session::new(Project::default());
flat.leave_downloads = true;
let mut q = cube.clone();
q["url"] = json!(format!("{stripped_api}/v1"));
let r = flat.run("stac.addCube", &q).unwrap();
assert_eq!(r["layers"], 1, "{r}");
let address = flat.map().tiles.iter().find(|t| !t.time_urls.is_empty()).map(|t| t.url.clone()).unwrap_or_default();
assert!(address.contains(&format!("{stripped}/p1.tif")), "{address}");
let e = crate::render::rustgis_tiles::cog::info_blocking(&address).err().unwrap_or_default();
assert!(e.contains(crate::render::rustgis_tiles::cog::NOT_COG), "{e}");
// A GeoTIFF export says the tile layers that failed (on the ground a gap looks like data).
let tif = std::env::temp_dir().join(format!("rustgis-2418-{}.tif", std::process::id()));
flat.zoom_to(&rustgis_geom::Extent::new(10.0, 45.0, 11.0, 46.0));
let n = flat.notices.len();
let _ = flat.run("share.exportMap", &json!({"path": tif.display().to_string()}));
std::fs::remove_file(&tif).ok();
let now = crate::render::rustgis_tiles::cog::info(&address).map(|i| i.is_some());
assert!(
flat.notices[n..].iter().any(|n| n.text.contains(crate::render::rustgis_tiles::cog::NOT_COG)),
"{now:?} {:?}",
&flat.notices[n..]
);
}

/// An entry that gives no range, as JAXA Earth's don't, is added with one from a sample of its
/// files (`"range": "auto"`): the same for every date. Its credit goes on the tile layers it
/// added, and its collection's licence comes back with it.
Expand Down
Loading