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
15 changes: 13 additions & 2 deletions crates/engine/src/cmd/raster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ pub(crate) fn add_named_bytes_read(

fn symbology(s: &mut Session, p: &Value) -> CmdResult {
let (id, r) = get(s, p)?;
let kind = ps(p, "type").unwrap_or("stretch").to_string();
let kind = symbology_kind(p);
// A custom ramp deleted from the project is still found on the raster that uses it.
let ramp = ps(p, "ramp")
.map(|n| {
Expand All @@ -994,7 +994,7 @@ fn symbology(s: &mut Session, p: &Value) -> CmdResult {
let cur_band = rl.style.renderer.bands().first().copied().unwrap_or(0);
let bi = bnd.unwrap_or(cur_band).min(r.band_count().saturating_sub(1));
let cur_ramp = rl.style.renderer.ramp();
rl.style.renderer = match kind.to_ascii_lowercase().replace([' ', '_', '-'], "").as_str() {
rl.style.renderer = match kind.as_str() {
"stretch" => {
let ramp = ramp.or(cur_ramp.filter(|r| !r.is_qualitative())).unwrap_or_else(Ramp::gray);
// Left out, a stretch keeps its own inversion (a band change); a new one isn't inverted.
Expand Down Expand Up @@ -1087,6 +1087,17 @@ fn symbology(s: &mut Session, p: &Value) -> CmdResult {
})
}

/// `raster.symbology`'s `"type"` as the command matches it: lower case, without spaces, `_` or `-`;
/// left out, a stretch.
pub(crate) fn symbology_kind(p: &Value) -> String {
ps(p, "type").unwrap_or("stretch").to_ascii_lowercase().replace([' ', '_', '-'], "")
}

/// Whether a `raster.symbology` draws a Shaded Relief, the only renderer that reads its relief keys.
pub(crate) fn symbology_is_relief(p: &Value) -> bool {
matches!(symbology_kind(p).as_str(), "shadedrelief" | "hillshade")
}

/// A Shaded Relief's `"shadowSoftness"` (see `raster.symbology`), `v` being the number given, if
/// it is one: refused by name outside the range, never brought into it. The default's own value
/// is kept as none, so a project that sets nothing new saves as it did.
Expand Down
36 changes: 28 additions & 8 deletions crates/engine/src/project_capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,22 +219,26 @@ fn command_sets_publication_legend(id: &str, params: &Value) -> bool {

/// A saved `raster.symbology` that sets a Shaded Relief's shadow softness (#3144): an older reader
/// runs it without the key, saying nothing, so the shadows would be traced at the default disc.
/// `null` asks for that default.
/// Read as the command reads it (its type, and a numeric string is its number): `null` and the
/// default, which the command keeps as none, ask for that disc; another renderer reads no softness.
fn command_sets_shadow_softness(id: &str, params: &Value) -> bool {
match id {
"raster.symbology" => params.get("shadowSoftness").is_some_and(|v| !v.is_null()),
"raster.symbology" => {
crate::cmd::raster::symbology_is_relief(params)
&& crate::cmd::f(params, "shadowSoftness").is_some_and(|v| v != rustgis_map::shadow::SOFT_EDGE)
}
"script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_sets_shadow_softness)),
_ => false,
}
}

/// A saved `raster.symbology` that sets a Shaded Relief's aerial perspective (#3141): an older reader
/// runs it without the key, saying nothing, so the low ground would be drawn without its haze.
/// Read as the command reads it (a numeric string is its number): `null` and 0 ask for none,
/// which an older reader draws.
/// Read as the command reads it (its type, and a numeric string is its number): `null` and 0 ask
/// for none, which an older reader draws; another renderer reads no aerial perspective.
fn command_sets_aerial(id: &str, params: &Value) -> bool {
match id {
"raster.symbology" => crate::cmd::f(params, "aerial").is_some_and(|v| v != 0.0),
"raster.symbology" => crate::cmd::raster::symbology_is_relief(params) && crate::cmd::f(params, "aerial").is_some_and(|v| v != 0.0),
"script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_sets_aerial)),
_ => false,
}
Expand Down Expand Up @@ -1682,6 +1686,17 @@ mod tests {
// Read as the command reads it: a numeric string is its number, so "0" is none too.
(&[], "raster.symbology", json!({"type": "shadedRelief", "aerial": "0"})),
(&["shaded-relief-aerial"], "raster.symbology", json!({"type": "shadedRelief", "aerial": "0.5"})),
// Only a Shaded Relief draws them, its type read as the command reads it: left out, the
// command draws a stretch, which an older reader draws the same.
(&[], "raster.symbology", json!({"type": "stretch", "aerial": 0.6, "shadowSoftness": 1.5})),
(&[], "raster.symbology", json!({"aerial": 0.6, "shadowSoftness": 1.5})),
(&["shaded-relief-aerial"], "raster.symbology", json!({"type": "hillshade", "aerial": 0.6})),
(&["shaded-relief-shadow-softness"], "raster.symbology", json!({"type": "Shaded Relief", "shadowSoftness": 1.5})),
// The default softness, 5, is kept as none, which an older reader draws; a numeric
// string is its number.
(&[], "raster.symbology", json!({"type": "shadedRelief", "shadowSoftness": 5})),
(&[], "raster.symbology", json!({"type": "shadedRelief", "shadowSoftness": "5"})),
(&["shaded-relief-shadow-softness"], "raster.symbology", json!({"type": "shadedRelief", "shadowSoftness": "1.5"})),
// An SVG image is a picture marker as well, in a node as in a saved command.
(&["picture-markers", "svg-markers"], "layer.marker", json!({"image": svg})),
(&[], "buffer", json!({"input": "Cities", "distance": 1})),
Expand All @@ -1702,9 +1717,14 @@ mod tests {
let missing: Vec<&str> = COMMAND_CAPABILITIES.iter().map(|&(name, _)| name).filter(|name| !covered.contains(name)).collect();
assert!(missing.is_empty(), "add a sample for each command capability, missing: {missing:?}");
// A saved script's step is read as the command reads it too.
for (aerial, needs) in [("0", false), ("0.5", true)] {
let script = format!(r#"raster.symbology {{"type": "shadedRelief", "aerial": "{aerial}"}}"#);
assert_eq!(command_sets_aerial("script.run", &json!({ "script": script })), needs, "{aerial}");
for (key, value, needs) in
[("aerial", "0", false), ("aerial", "0.5", true), ("shadowSoftness", "5", false), ("shadowSoftness", "1.5", true)]
{
for kind in ["shadedRelief", "stretch"] {
let script = format!(r#"raster.symbology {{"type": "{kind}", "{key}": "{value}"}}"#);
let uses = if key == "aerial" { command_sets_aerial } else { command_sets_shadow_softness };
assert_eq!(uses("script.run", &json!({ "script": script })), needs && kind == "shadedRelief", "{kind} {key} {value}");
}
}
}

Expand Down
Loading