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
13 changes: 11 additions & 2 deletions crates/engine/src/cmd/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -858,8 +858,9 @@ pub(crate) fn touches_files(command: &str, params: &Value) -> bool {
/// (or a model, or a notebook) from a file, or a plugin without `files`, may set it: only
/// `gp.run`'s `{"tool", "params"}` shape, only `params.output`, only for the tools in
/// `rustgis_analysis::TOOLS`, which name their output layer and never a file through it (see
/// `out_name` there), not GeoLibre or plugin tools, which may write theirs as files, and only a
/// plain name, with no `/`, `\`, `.` or `:`. Anything else stays a place.
/// `out_name` there), not GeoLibre or plugin tools, which may write theirs as files; and
/// `gp.spatialJoin`'s and `gp.zonalStats`'s `output`, the same built-in tools run with their
/// parameters flat. Only a plain name, with no `/`, `\`, `.` or `:`. Anything else stays a place.
pub(crate) fn layer_name_left_out<'a>(command: &str, params: &'a Value) -> std::borrow::Cow<'a, Value> {
use std::borrow::Cow;
// Resolved as gp.run resolves it (by id or name, a built-in tool first), then held to the
Expand All @@ -873,6 +874,14 @@ pub(crate) fn layer_name_left_out<'a>(command: &str, params: &'a Value) -> std::
};
let plain = |t: &str| !t.trim().is_empty() && !t.contains(['/', '\\', '.', ':']) && !looks_like_path(t);
let named = || params.pointer("/params/output").and_then(Value::as_str).is_some_and(plain);
// Spatial Join and Zonal Statistics run their built-in tool with the parameters flat.
if matches!(command, "gp.spatialJoin" | "gp.zonalStats") && params.get("output").and_then(Value::as_str).is_some_and(plain) {
let mut out = params.clone();
if let Some(p) = out.as_object_mut() {
p.remove("output");
}
return Cow::Owned(out);
}
// Copied only when there is a name to leave out: other commands' parameters (a file's
// contents, say) are read as they are.
if command != "gp.run" || !built_in() || !named() {
Expand Down
3 changes: 2 additions & 1 deletion crates/engine/src/cmd/workflows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,8 @@ pub(crate) fn ahead_on_copy(s: &Session, command: &'static str, p: &Value) -> su

#[cfg(not(target_arch = "wasm32"))]
/// A script's run on a copy of the session ([`ahead_on_copy`]), unless its first line would stop
/// the copy at once (or the script doesn't read): then it runs here, with no copy made.
/// the copy at once, or the script has no lines or doesn't parse: then it runs here, with no copy
/// made.
pub(crate) fn script_ahead(s: &Session, p: &Value) -> Option<super::reads::ReadAhead> {
let lines = script_commands(ps(p, "script")?).ok()?;
let (_, id, params) = lines.first()?;
Expand Down
18 changes: 17 additions & 1 deletion crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2361,7 +2361,11 @@ fn imported_tasks_name_outputs_and_steps_are_edited() {
assert!(s.run("task.import", &json!({"text": to_a_file.to_string()})).is_err());
assert!(crate::cmd::items::touches_files("gp.run", &json!({"tool": "buffer", "params": {"output": "../x.shp"}})));
assert!(!crate::cmd::items::touches_files("gp.run", &json!({"tool": "buffer", "params": {"output": "Near cities"}})));
// Only a plain layer name, only gp.run's, only for RustGIS's own tools.
// Spatial Join and Zonal Statistics run their built-in tool with the parameters flat: a
// plain output name stays in the session there too.
assert!(!crate::cmd::items::touches_files("gp.spatialJoin", &json!({"target": "Cities", "join": "Countries", "output": "Joined"})));
assert!(!crate::cmd::items::touches_files("gp.zonalStats", &json!({"raster": "dem", "polygons": "Countries", "output": "Zones"})));
Comment thread
pick2510 marked this conversation as resolved.
Resolved
// Only a plain layer name, only gp.run's (and those two's), only for RustGIS's own tools.
for (command, params) in [
("gp.run", json!({"tool": "buffer", "params": {"output": "x.shp"}})),
("gp.run", json!({"tool": "buffer", "params": {"output": "sub/x"}})),
Expand All @@ -2373,6 +2377,8 @@ fn imported_tasks_name_outputs_and_steps_are_edited() {
("gp.run", json!({"tool": "someOtherTool", "params": {"output": "x"}})),
("gp.run", json!({"tool": "buffer", "params": {"input": "Cities", "nested": {"output": "x"}}})),
("layer.rename", json!({"layer": "Cities", "output": "x"})),
("gp.spatialJoin", json!({"target": "Cities", "join": "Countries", "output": "C:/temp/x.shp"})),
("gp.zonalStats", json!({"raster": "dem", "polygons": "Countries", "output": "../zones"})),
] {
assert!(crate::cmd::items::touches_files(command, &params), "{command} {params}");
}
Expand Down Expand Up @@ -30354,6 +30360,16 @@ fn a_scripts_tools_run_ahead_are_taken_by_its_run() {
assert_eq!(s.project.gp_history.len(), 2);
std::fs::remove_dir_all(&dir).ok();

// Spatial Join with its output named stays on the copy: its name is a layer's, not a file's,
// so it and the tool after it are taken, and the layer is made under that name.
let join = r#"gp.spatialJoin {"target": "Points", "join": "Points", "output": "Joined"}"#;
let p = json!({"script": format!("{join}\n{}", tool(1000))});
let mut s = session();
crate::cmd::reads::read_ahead(&s, "script.run", &p).unwrap().run(&Control::stoppable()).unwrap().placed(&mut s);
s.run("script.run", &p).unwrap();
assert_eq!(s.tools_taken, 2);
assert!(s.map().layer_by_name("Joined").is_some());
Comment thread
pick2510 marked this conversation as resolved.
Resolved

// An edit stops the copy (the rows it makes there are never the run here's): the tool
// before it is taken, the one after it runs here.
let p = json!({"script": format!("{}\nedit.addField {{\"layer\": \"Points\", \"name\": \"n\", \"type\": \"long\"}}\n{}", tool(1000), tool(2000))});
Expand Down
Loading