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
2 changes: 1 addition & 1 deletion crates/engine/command-surface.txt
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ raster.rotation layer? angle
raster.setDefaultStyle fromLayer? layer? ramp? invert? stretch? low? high? n? resampling? reset?
raster.spectral layer lon? lat? points? zone?
raster.stretch layer? type low? high? n? min? max? like?
raster.symbology layer? type band? ramp? invert? classes? method? azimuth? altitude? zFactor? shadow? sky? fill? ambient? balanced? shadowSoftness? aerial? rock? grey?
raster.symbology layer? type band? ramp? invert? classes? method? azimuth? altitude? zFactor? shadow? sky? fill? ambient? balanced? shadowSoftness? aerial? terrain? rock? grey?
raster.zarrInfo path url bbox? variable?
report.activate report
report.attachments report? field
Expand Down
21 changes: 20 additions & 1 deletion crates/engine/src/cmd/raster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn commands() -> Vec<C> {
C::new("layer.addDataBytes", "Add Data (from file contents)", "Home › Add", add_bytes::<MAX_UPLOAD_BYTES>).roles(&[]).effects(crate::policy::Effects::NONE)
.params(r#"{"fileName": "name.tif|.tiff|.asc|.grb|.grib|.grb2|.grib2 (GRIB)|.hgt|.dt0|.dt1|.dt2|.geojson|.csv|.zip|.kml|.kmz|.gpx|.parquet|.fgb|.gpkg|.mbtiles|.jp2|.j2k|.dxf|.pdf (JPEG 2000: desktop, through GDAL; PDF: desktop, command line and Python)", "data": base64, "zoom"?: bool, "layer"?: "one layer of a zip, KML or GPX by name, or a PDF's page number (the first when left out or blank)", "name"?: text, "x"?, "y"?, "wkt"?, "crs"?, "delimiter"?, "paperSpace"?, "blocks"?, "stream"?, "as"? (as layer.addData)}"#),
C::new("raster.symbology", "Symbology", "Style › Rendering", symbology).when(need_raster).roles(&[]).effects(crate::policy::Effects::NONE).params(
r#"{"layer"?, "type": "stretch"|"rgb"|"classify"|"unique"|"colormap"|"shadedRelief", "band"?: 1.., "ramp"?, "invert"?: bool, "classes"?: 1..32, "method"?, "azimuth"?, "altitude"?, "zFactor"?, "shadow"?: 0..1 (shadedRelief: how dark the shadows the terrain casts are; left out, kept; 0 casts none), "sky"?: 0..1 (shadedRelief: how much the share of the sky each cell sees dims its ambient light; left out, kept; 0 leaves it whole), "fill"?: 0..1 (shadedRelief: a fill light from the side opposite the sun that lightens the slopes turned toward it; left out, kept; 0 has none), "ambient"?: 0..1|null (shadedRelief: the share of the colour a slope turned fully from the sun keeps; left out, kept; null, the default: 0.25, or 0.5 balanced), "balanced"?: bool (shadedRelief: flat ground keeps the ramp's colour and slopes facing the sun brighten, up to 1.3; left out, kept; false, the default, lights as before), "shadowSoftness"?: 0.5..15|null (shadedRelief: how soft the edges of its cast shadows are, as half the sun's apparent size in degrees; left out, kept; outside 0.5 to 15, refused; null, the default: 5), "aerial"?: 0..1|null (shadedRelief: aerial perspective, low ground on the layer's stretch lit with less contrast and hazed toward #ECECEC, the more the lower; left out, kept; outside 0 to 1, refused; null or 0, the default: none), "rock"?: {"from"?: 0..90, "to"?: 0..90, "color"?: #RRGGBB} | false (shadedRelief: steep slopes tinted toward a rock colour before the light, from the ground's own slope in degrees, without the Z factor: the ramp below from, the colour above to, blended between; left out, kept; an object's missing keys keep the current rock's, or 25, 40 and #B2ACA4; false or null removes it), "grey"?: bool (shadedRelief without its colour ramp)}"#,
r#"{"layer"?, "type": "stretch"|"rgb"|"classify"|"unique"|"colormap"|"shadedRelief", "band"?: 1.., "ramp"?, "invert"?: bool, "classes"?: 1..32, "method"?, "azimuth"?, "altitude"?, "zFactor"?, "shadow"?: 0..1 (shadedRelief: how dark the shadows the terrain casts are; left out, kept; 0 casts none), "sky"?: 0..1 (shadedRelief: how much the share of the sky each cell sees dims its ambient light; left out, kept; 0 leaves it whole), "fill"?: 0..1 (shadedRelief: a fill light from the side opposite the sun that lightens the slopes turned toward it; left out, kept; 0 has none), "ambient"?: 0..1|null (shadedRelief: the share of the colour a slope turned fully from the sun keeps; left out, kept; null, the default: 0.25, or 0.5 balanced), "balanced"?: bool (shadedRelief: flat ground keeps the ramp's colour and slopes facing the sun brighten, up to 1.3; left out, kept; false, the default, lights as before), "shadowSoftness"?: 0.5..15|null (shadedRelief: how soft the edges of its cast shadows are, as half the sun's apparent size in degrees; left out, kept; outside 0.5 to 15, refused; null, the default: 5), "aerial"?: 0..1|null (shadedRelief: aerial perspective, low ground on the layer's stretch lit with less contrast and hazed toward #ECECEC, the more the lower; left out, kept; outside 0 to 1, refused; null or 0, the default: none), "terrain"?: -1..1|null (shadedRelief: terrain type, each cell's gradient g reshaped for the sun's light only as tan 30° × (g / tan 30°)^(1 − 0.5 × terrain), steepened at most 8 times: above 0 gentle relief shows, below 0 steep ground stands out; cast shadows, sky, fill light and rock keep the ground's slope; left out, kept; outside -1 to 1, refused; null or 0, the default: lit as before), "rock"?: {"from"?: 0..90, "to"?: 0..90, "color"?: #RRGGBB} | false (shadedRelief: steep slopes tinted toward a rock colour before the light, from the ground's own slope in degrees, without the Z factor: the ramp below from, the colour above to, blended between; left out, kept; an object's missing keys keep the current rock's, or 25, 40 and #B2ACA4; false or null removes it), "grey"?: bool (shadedRelief without its colour ramp)}"#,
),
C::new("raster.stretch", "Stretch Type", "Style › Rendering", stretch).inputs(stretch_inputs).roles(&[]).effects(crate::policy::Effects::NONE)
.when(need_raster)
Expand Down Expand Up @@ -1044,6 +1044,11 @@ fn symbology(s: &mut Session, p: &Value) -> CmdResult {
}
_ => (0.0, 0.0, 0.0, None, None, false, None, 0.0),
};
// And its terrain type, likewise.
let cur_terrain = match rl.style.renderer {
RasterRenderer::ShadedRelief { terrain_type, .. } => terrain_type,
_ => 0.0,
};
RasterRenderer::ShadedRelief {
band: bi,
ramp: ramp.or(Some(Ramp::Terrain)).filter(|_| !b(p, "grey").unwrap_or(false)),
Expand Down Expand Up @@ -1074,6 +1079,11 @@ fn symbology(s: &mut Session, p: &Value) -> CmdResult {
Some(Value::Null) => 0.0,
Some(_) => relief_aerial(f(p, "aerial"))?,
},
terrain_type: match p.get("terrain") {
None => cur_terrain,
Some(Value::Null) => 0.0,
Some(_) => relief_terrain_type(f(p, "terrain"))?,
},
}
}
other => return Err(format!("unknown raster symbology '{other}'")),
Expand Down Expand Up @@ -1120,6 +1130,15 @@ fn relief_aerial(v: Option<f64>) -> Result<f64, String> {
}
}

/// A Shaded Relief's `"terrain"` (see `raster.symbology`), `v` being the number given, if it is
/// one: refused by name outside −1 to 1, never brought into it.
fn relief_terrain_type(v: Option<f64>) -> Result<f64, String> {
match v {
Some(v) if (-1.0..=1.0).contains(&v) => Ok(v),
_ => Err("\"terrain\" must be a number from -1 to 1, or null for 0".into()),
}
}

/// The rock of a Shaded Relief's `"rock"` (see `raster.symbology`): `false` or null for none, or
/// an object whose missing keys keep `cur`'s, or else [`Rock::DEFAULT`]'s.
fn relief_rock(v: &Value, cur: Option<Rock>) -> Result<Option<Rock>, String> {
Expand Down
1 change: 1 addition & 0 deletions crates/engine/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ pub const CAPABILITIES: &[&str] = &[
"layout-publication-legend",
"shaded-relief-shadow-softness",
"shaded-relief-aerial",
"shaded-relief-terrain-type",
];
/// An offline area keeping the versions that lost a sync or a Bring Back
/// (`OfflineArea::conflicts`, #2468): an older reader would save it again without them.
Expand Down
42 changes: 40 additions & 2 deletions crates/engine/src/project_capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ const COMMAND_CAPABILITIES: &[(&str, CommandCheck)] = &[
("layout-publication-legend", command_sets_publication_legend),
("shaded-relief-shadow-softness", command_sets_shadow_softness),
("shaded-relief-aerial", command_sets_aerial),
("shaded-relief-terrain-type", command_sets_terrain_type),
];

/// A saved `layout.publication` that leaves out or sets up its grid, scale bar or north arrow
Expand Down Expand Up @@ -247,6 +248,18 @@ fn command_sets_aerial(id: &str, params: &Value) -> bool {
}
}

/// A saved `raster.symbology` that sets a Shaded Relief's terrain type (#3142): an older reader runs
/// it without the key, saying nothing, so the relief would be lit from its unshaped slopes. Read as
/// the command reads it (its type, and a numeric string is its number): `null` and 0 light as an
/// older reader does; another renderer reads no terrain type.
fn command_sets_terrain_type(id: &str, params: &Value) -> bool {
match id {
"raster.symbology" => crate::cmd::raster::symbology_is_relief(params) && crate::cmd::f(params, "terrain").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_terrain_type)),
_ => false,
}
}

/// A saved `labels.placement` that has a class remove duplicates, read as the command reads it
/// (#2847): an older reader runs it without the key, saying nothing, so every label would show.
/// False or null is what an older reader keeps.
Expand Down Expand Up @@ -1079,6 +1092,15 @@ fn sets_aerial(layer: &Layer) -> bool {
layer.raster.as_ref().is_some_and(|r| matches!(&r.style.renderer, RasterRenderer::ShadedRelief { aerial, .. } if *aerial != 0.0))
}

/// Whether a layer sets its Shaded Relief's terrain type (#3142): an older reader would light it
/// from its unshaped slopes and save the layer again without the setting.
fn sets_terrain_type(layer: &Layer) -> bool {
layer
.raster
.as_ref()
.is_some_and(|r| matches!(&r.style.renderer, RasterRenderer::ShadedRelief { terrain_type, .. } if *terrain_type != 0.0))
}

/// Whether a layer is a window of a Zarr variable following the time slider (#1720): an older
/// reader would drop the window and read the whole map at each step.
fn zarr_window(layer: &Layer) -> bool {
Expand Down Expand Up @@ -1123,6 +1145,7 @@ fn layer_capabilities(layer: &Layer) -> Vec<&'static str> {
(relief_light(layer), "shaded-relief-light"),
(sets_shadow_softness(layer), "shaded-relief-shadow-softness"),
(sets_aerial(layer), "shaded-relief-aerial"),
(sets_terrain_type(layer), "shaded-relief-terrain-type"),
(layer.size_by.is_some(), super::SIZE_BY_CAPABILITY),
(layer.has_picture_markers(), "picture-markers"),
(layer.any_picture(svg_picture), "svg-markers"),
Expand Down Expand Up @@ -1159,7 +1182,7 @@ fn map_capabilities<'a>(maps: impl IntoIterator<Item = &'a Map>) -> Vec<&'static
let (mut polygon, mut svg) = (false, false);
let (mut offline_area_id, mut paper, mut native, mut guided, mut weighed, mut cleared, mut deduped) =
(false, false, false, false, false, false, false);
let (mut crisp, mut bed, mut walls_top, mut soft, mut aerial) = (false, false, false, false, false);
let (mut crisp, mut bed, mut walls_top, mut soft, mut aerial, mut terrain_type) = (false, false, false, false, false, false);
for map in maps {
offline_area_id |= map.offline_area_id.is_some();
// A blank basemap's paper colour (#2532), which an older reader would draw and save white.
Expand Down Expand Up @@ -1198,6 +1221,7 @@ fn map_capabilities<'a>(maps: impl IntoIterator<Item = &'a Map>) -> Vec<&'static
light |= map.layers.iter().any(relief_light);
soft |= map.layers.iter().any(sets_shadow_softness);
aerial |= map.layers.iter().any(sets_aerial);
terrain_type |= map.layers.iter().any(sets_terrain_type);
sized |= map.layers.iter().any(|layer| layer.size_by.is_some());
pictures |= map.layers.iter().any(Layer::has_picture_markers);
svg |= map.layers.iter().any(|layer| layer.any_picture(svg_picture));
Expand All @@ -1221,6 +1245,7 @@ fn map_capabilities<'a>(maps: impl IntoIterator<Item = &'a Map>) -> Vec<&'static
(light, "shaded-relief-light"),
(soft, "shaded-relief-shadow-softness"),
(aerial, "shaded-relief-aerial"),
(terrain_type, "shaded-relief-terrain-type"),
(legends, "normalized-legend-urls"),
(sized, super::SIZE_BY_CAPABILITY),
(pictures, "picture-markers"),
Expand Down Expand Up @@ -1757,6 +1782,14 @@ mod tests {
(&[], "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"})),
(&["shaded-relief-terrain-type"], "raster.symbology", json!({"type": "shadedRelief", "terrain": -0.6})),
(&["shaded-relief-terrain-type"], "raster.symbology", json!({"type": "Shaded Relief", "terrain": "0.6"})),
// null and 0 (a numeric string too) light as an older reader does; only a Shaded
// Relief, its type read as the command reads it (left out, a stretch), reads the key.
(&[], "raster.symbology", json!({"type": "shadedRelief", "terrain": null})),
(&[], "raster.symbology", json!({"type": "hillshade", "terrain": "0"})),
(&[], "raster.symbology", json!({"type": "stretch", "terrain": 0.6})),
(&[], "raster.symbology", json!({"terrain": 0.6})),
// 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 Down Expand Up @@ -1786,6 +1819,10 @@ mod tests {
assert_eq!(uses("script.run", &json!({ "script": script })), needs && kind == "shadedRelief", "{kind} {key} {value}");
}
}
for (kind, terrain, needs) in [("shadedRelief", "0", false), ("shadedRelief", "0.6", true), ("stretch", "0.6", false)] {
let script = format!(r#"raster.symbology {{"type": "{kind}", "terrain": "{terrain}"}}"#);
assert_eq!(command_sets_terrain_type("script.run", &json!({ "script": script })), needs, "{kind} {terrain}");
}
}

/// `report.existingTemplate` imports a layout file as `layout.import` does, so a step of it
Expand Down Expand Up @@ -3469,7 +3506,7 @@ mod tests {
}))
.unwrap();
let clean = Layer::new_raster(1, "DEM", raster);
for field in ["shadow", "sky", "fill", "rock", "ambient", "balanced", "shadow_softness", "aerial"] {
for field in ["shadow", "sky", "fill", "rock", "ambient", "balanced", "shadow_softness", "aerial", "terrain_type"] {
let mut value = serde_json::to_value(&clean).unwrap();
value["raster"]["style"]["renderer"]["ShadedRelief"][field] = match field {
"rock" => serde_json::to_value(rustgis_map::raster::Rock::DEFAULT).unwrap(),
Expand All @@ -3488,6 +3525,7 @@ mod tests {
"ambient" | "balanced" => "shaded-relief-light",
"shadow_softness" => "shaded-relief-shadow-softness",
"aerial" => "shaded-relief-aerial",
"terrain_type" => "shaded-relief-terrain-type",
_ => "shaded-relief-effects",
};
assert_eq!(document["requires"], json!([needs]), "{field}: {document}");
Expand Down
50 changes: 50 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7962,6 +7962,56 @@ fn shaded_relief_keeps_its_aerial_perspective() {
assert_eq!(serde_json::to_value(&fixture).unwrap()["requires"], json!(["shaded-relief-aerial"]));
}

/// Shaded Relief's terrain type (#3142) is set, kept when left out, refused by name outside −1 to 1
/// (never brought into it), saved only when set, and back when the project is opened again; null
/// or 0 lights as before again, which saves nothing new.
#[test]
fn shaded_relief_keeps_its_terrain_type() {
let mut s = with_raster();
let terrain = |s: &Session| match s.map().layers.iter().find_map(|l| l.raster.as_ref()).map(|r| &r.style.renderer) {
Some(rustgis_map::RasterRenderer::ShadedRelief { terrain_type, .. }) => *terrain_type,
other => panic!("{other:?}"),
};
let saved = |s: &mut Session| s.project.saved_text(None).unwrap().0;
s.run("raster.symbology", &json!({"type": "shadedRelief", "shadow": 0.8})).unwrap();
assert_eq!(terrain(&s), 0.0);
assert!(!saved(&mut s).contains("\"terrain_type\":"), "none saves nothing new");
s.run("raster.symbology", &json!({"type": "shadedRelief", "terrain": -0.6})).unwrap();
s.run("raster.symbology", &json!({"type": "shadedRelief", "azimuth": 200})).unwrap();
assert_eq!(terrain(&s), -0.6, "left out, kept");
for refused in [json!(-1.5), json!(1.01), json!("hills"), json!(true)] {
let e = s.run("raster.symbology", &json!({"type": "shadedRelief", "terrain": refused})).unwrap_err();
assert!(e.contains("\"terrain\"") && e.contains("-1 to 1"), "{refused}: {e}");
assert_eq!(terrain(&s), -0.6, "{refused}: refused, nothing changed");
}
s.run("raster.symbology", &json!({"type": "shadedRelief", "terrain": "1"})).unwrap();
assert_eq!(terrain(&s), 1.0, "a numeric string is its number");
let text = saved(&mut s);
assert!(text.contains("\"terrain_type\":1.0") && text.contains("shaded-relief-terrain-type"), "saved, and required by name");
let nanos = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| d.as_nanos());
let dir = std::env::temp_dir().join(format!("rustgis-terrain-type-{}-{nanos}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("terrain.rgis");
s.run("project.saveAs", &json!({"path": path.display().to_string()})).unwrap();
let mut t = Session::new(Project::default());
t.run("project.open", &json!({"path": path.display().to_string()})).unwrap();
let _ = std::fs::remove_dir_all(&dir);
assert_eq!(terrain(&t), 1.0, "back when opened");
for none in [json!(null), json!(0)] {
s.run("raster.symbology", &json!({"type": "shadedRelief", "terrain": 0.3})).unwrap();
s.run("raster.symbology", &json!({"type": "shadedRelief", "terrain": none})).unwrap();
assert_eq!(terrain(&s), 0.0, "{none}: none");
}
s.run("raster.symbology", &json!({"type": "shadedRelief", "terrain": 0.3})).unwrap();
s.run("edit.undo", &json!({})).unwrap();
assert_eq!(terrain(&s), 0.0, "undone");
assert!(!saved(&mut s).contains("\"terrain_type\":"), "none saves nothing new");
// The fixture reads back as saved, requiring what it sets by name.
let fixture = Project::from_json(include_str!("../../../schema/fixtures/shaded_relief_terrain_type.rgis")).unwrap();
assert_eq!(terrain(&Session::new(fixture.clone())), 0.6);
assert_eq!(serde_json::to_value(&fixture).unwrap()["requires"], json!(["shaded-relief-terrain-type"]));
}

/// Shaded Relief's rock on steep slopes (#1913): an object turns it on, its missing keys from the
/// current rock or the defaults; kept when left out; `false` removes it; a `from` not below `to`
/// or a bad colour is refused; saved with the project only when set.
Expand Down
1 change: 1 addition & 0 deletions crates/map/src/layout/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,7 @@ fn a_balanced_reliefs_colour_bar_is_its_flat_ground() {
balanced,
shadow_softness: None,
aerial: 0.0,
terrain_type: 0.0,
};
let Some(ColorScale::Ramp { lo, hi, colors }) = color_scale(&rl) else { panic!("a ramp") };
let colorizer = Colorizer::new(&rl.style, &r, None);
Expand Down
1 change: 1 addition & 0 deletions crates/map/src/qgis/raster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ fn renderer_style(node: Node, pipe: Node, r: Node, kind: &str, layer: &str, repo
balanced: false,
shadow_softness: None,
aerial: 0.0,
terrain_type: 0.0,
};
(renderer, Stretch::None)
}
Expand Down
Loading
Loading