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: 5 additions & 4 deletions crates/map/src/raster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1454,8 +1454,9 @@ impl Rock {
}

/// Shaded Relief's detail on flat ground (#3143): each cell's height above the mean height within
/// `size` cells around it (a square `2 × size + 1` cells a side, without its no data) moves its
/// direct light by [`Flats::shift`], on flat ground only.
/// `size` cells around it (a square `2 × size + 1` cells a side, without its no data, and cut off
/// at the grid's edges unless a whole turn wraps) moves its direct light by [`Flats::shift`], on
/// flat ground only.
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub struct Flats {
pub amount: f64,
Expand All @@ -1474,8 +1475,8 @@ impl Flats {
/// How far (0–255) the direct light of a cell on a slope of `degrees` (the ground's own, see
/// [`slope`]) moves when it stands `above` its neighbourhood's mean (in the DEM's units):
/// `amount` (0 to 1) × a weight of 1 on flat ground fading to 0 at [`Flats::SLOPE`] × `40 ×
/// above` kept within ±80, so a metre up brightens by 40 and a channel darkens. 0 when that
/// isn't a number.
/// above` kept within ±80, so a unit of height up (a metre in a DEM in metres) brightens by 40
/// and a channel darkens. 0 when that isn't a number.
pub fn shift(&self, degrees: f64, above: f64) -> f64 {
let w = (1.0 - degrees / Self::SLOPE).clamp(0.0, 1.0);
let shift = self.amount.clamp(0.0, 1.0) * w * (40.0 * above).clamp(-80.0, 80.0);
Expand Down
77 changes: 72 additions & 5 deletions crates/ui-egui/src/panes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rustgis_engine::analysis::{ParamKind, tools};
use rustgis_engine::cmd::layer::hidden_fill_opacity;
use rustgis_engine::data::GeometryType;
use rustgis_engine::map::fills::FillLayer;
use rustgis_engine::map::raster::{Normalizer, RasterLegend, RasterStyle, Rock, relief_ambient};
use rustgis_engine::map::raster::{Flats, Normalizer, RasterLegend, RasterStyle, Rock, relief_ambient};
use rustgis_engine::map::symbology::{
ClassRef, ClassStyle, Dash, LegendEntry, LineCap, LineJoin, LineMark, LineMarkers, LineStroke, MAX_EXPRESSION_CLASSES, MAX_LABEL,
MAX_LINE_MARKER_SIZE, MAX_MARKER_INTERVAL, MAX_STROKE_OFFSET, MAX_STROKE_WIDTH, MAX_STROKES, MIN_MARKER_INTERVAL, OPEN_ABOVE,
Expand Down Expand Up @@ -7598,15 +7598,15 @@ fn raster_symbology(app: &mut RustGisApp, ui: &mut Ui, l: &Layer, t: &Tokens) {
shadow_softness,
aerial,
terrain_type,
// Not in this pane yet (#3143).
flats: _,
flats,
} => {
use rustgis_engine::map::shadow::{SOFT_EDGE_MAX, SOFT_EDGE_MIN, soft_edge};
let (az, alt, zf, sh, sk, fl) = (*azimuth, *altitude, *z_factor, *shadow, *sky, *fill);
let ss = soft_edge(*shadow_softness);
let am = relief_ambient(*ambient, *balanced);
let ae = *aerial;
let tt = *terrain_type;
let flats = *flats;
let rp = ramp.as_ref().map(|r| r.name().to_string());
let relief = move |band: usize, az: f64, alt: f64, zf: f64, sh: f64| {
let mut p = json!({
Expand All @@ -7620,7 +7620,7 @@ fn raster_symbology(app: &mut RustGisApp, ui: &mut Ui, l: &Layer, t: &Tokens) {
};
band_combo(app, ui, "Band", *band, &|i| ("raster.symbology".into(), relief(i, az, alt, zf, sh)));
egui::Grid::new("relief").num_columns(2).spacing(vec2(10.0, 4.0)).show(ui, |ui| {
for (label, v, range, speed, which) in [
let mut rows = vec![
("Azimuth", az, 0.0..=360.0, 1.0, 0),
("Altitude", alt, 0.0..=90.0, 0.5, 1),
("Z factor", zf, 0.001..=1000.0, 0.01, 2),
Expand All @@ -7631,7 +7631,13 @@ fn raster_symbology(app: &mut RustGisApp, ui: &mut Ui, l: &Layer, t: &Tokens) {
("Ambient", am, 0.0..=1.0, 0.01, 6),
("Aerial perspective", ae, 0.0..=1.0, 0.01, 8),
("Terrain type", tt, -1.0..=1.0, 0.01, 9),
] {
("Flat-area detail", flats.map_or(0.0, |f| f.amount), 0.0..=1.0, 0.01, 10),
];
// Its neighbourhood, once it is on.
if let Some(f) = flats {
rows.push(("Flat-area size", f64::from(f.size), 1.0..=f64::from(Flats::MAX_SIZE), 1.0, 11));
}
for (label, v, range, speed, which) in rows {
let input_label = ui.label(tl!(label));
let mut x = v;
// Kept across a drag until it is let go (#607).
Expand All @@ -7657,6 +7663,10 @@ fn raster_symbology(app: &mut RustGisApp, ui: &mut Ui, l: &Layer, t: &Tokens) {
9 => r.on_hover_text(tl!(
"How the sun lights slopes gentler or steeper than 30°: above 0 brings out low hills, below 0 stresses steep ground, from -1 to 1"
)),
10 => r.on_hover_text(tl!(
"Levees, terraces and shallow channels on ground flatter than 4°, lit by their height above the ground around them, from 0 (none) to 1"
)),
11 => r.on_hover_text(tl!("How many cells around each cell its height is compared with, from 1 to 32")),
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
_ => r,
};
if drag.settled(ui, &r, &mut x) && x != v {
Expand All @@ -7675,6 +7685,8 @@ fn raster_symbology(app: &mut RustGisApp, ui: &mut Ui, l: &Layer, t: &Tokens) {
7 => p["shadowSoftness"] = json!(x),
8 => p["aerial"] = json!(x),
9 => p["terrain"] = json!(x),
10 => p["flats"] = if x > 0.0 { json!({"amount": x}) } else { json!(false) },
11 => p["flats"] = json!({"size": x.round()}),
_ => {}
}
let _ = app.run("raster.symbology", p);
Expand Down Expand Up @@ -10425,4 +10437,59 @@ pub(crate) mod tests {
assert_eq!(h.state().dialogs.open, Some("renameLayer"));
assert_eq!(h.state().dialogs.text, "Rivers");
}

/// A Shaded Relief's style pane sets its flat-area detail (#3143): an amount turns it on, with
/// the default size, and shows its size, which is set in whole cells; 0 turns it off again.
#[test]
fn the_relief_pane_sets_its_flat_area_detail() {
use egui::accesskit::{Action, ActionData, ActionRequest, Role};
use egui_kittest::kittest::{NodeT, Queryable};
use rustgis_engine::data::raster::{ColorHint, Crs, GeoTransform, Raster};
let mut app = RustGisApp::with_sample();
let gt = GeoTransform { origin_x: 0.0, origin_y: 1.0, pixel_width: 0.5, pixel_height: -0.5 };
let r = Raster::new("dem", 2, 2, vec![vec![0.0, 1.0, 2.0, 3.0]], gt, Crs::LonLat, None, ColorHint::Gray, "Float32").unwrap();
let id = app.session.map_mut().add_raster("dem", rustgis_engine::map::RasterLayer::from_path(r, "dem.tif"));
// The pane styles the layer selected.
app.session.active_layer = Some(id);
app.run("raster.symbology", json!({"layer": id, "type": "shadedRelief"})).unwrap();
let mut fonts = false;
let mut h = egui_kittest::Harness::builder().with_size(vec2(420.0, 900.0)).build_ui_state(
move |ui, app: &mut RustGisApp| {
if !fonts {
crate::theme::install_fonts(ui.ctx(), &[], None);
fonts = true;
return;
}
let layer = app.session.map().layer(id).unwrap().clone();
raster_symbology(app, ui, &layer, &Tokens::light());
},
app,
);
h.run_steps(3);
let flats = |h: &egui_kittest::Harness<'_, RustGisApp>| match h.state().session.map().layer(id).and_then(|l| l.raster.as_ref()) {
Some(rl) => match rl.style.renderer {
RasterRenderer::ShadedRelief { flats, .. } => flats,
ref other => panic!("{other:?}"),
},
None => panic!("no raster"),
};
let set = |h: &mut egui_kittest::Harness<'_, RustGisApp>, label: &str, value: f64| {
let (node, tree) = h.get_by_role_and_label(Role::SpinButton, label).accesskit_node().locate();
h.event(egui::Event::AccessKitActionRequest(ActionRequest {
action: Action::SetValue,
target_tree: tree,
target_node: node,
data: Some(ActionData::NumericValue(value)),
}));
h.run_steps(3);
};
assert!(h.query_by_role_and_label(Role::SpinButton, "Flat-area size").is_none(), "no size while it is off");
set(&mut h, "Flat-area detail", 0.7);
assert_eq!(flats(&h), Some(Flats { amount: 0.7, size: Flats::DEFAULT.size }));
set(&mut h, "Flat-area size", 9.6);
assert_eq!(flats(&h), Some(Flats { amount: 0.7, size: 10 }), "a whole number of cells");
set(&mut h, "Flat-area detail", 0.0);
assert_eq!(flats(&h), None);
assert!(h.query_by_role_and_label(Role::SpinButton, "Flat-area size").is_none(), "off again, no size");
}
}
14 changes: 14 additions & 0 deletions docs/user-guide/data.md
Original file line number Diff line number Diff line change
Expand Up @@ -1855,6 +1855,20 @@ the relief as before; about 0.6 is a good start for a plateau or a plain with lo
outside -1 to 1 is refused; `"terrain": null` or 0 removes it. It is saved with the project only
when set.

**Flat-area detail**, on a Shaded Relief layer (`raster.symbology` `"flats"`, an amount from 0 to
1), brings out the landforms of flat ground, which the light draws as one even tone: levees,
terraces, abandoned channels and shallow valleys on a plain or a plateau. Each cell is compared
with the mean height of the cells within **Flat-area size** cells around it (6 by default, 1 to
32): a cell standing above it is lit brighter, one sunk below it darker, by 40 levels of 255 for
each unit of height (a metre in a DEM in metres), at most 80, times the amount. Only ground
flatter than 4° changes, fading out as its own slope nears 4°, so hills and mountains keep their
light. A wider size picks out wider landforms: about 3 for small ones, 6 for a 90 m DEM's
plateau. It is best on a terrain model: on a surface model (Copernicus, SRTM), forest edges,
fields, roads and embankments are heights too and show up as relief. In a script, `"flats":
{"amount": 0.8, "size": 6}` sets it, an object's missing keys keep the current values (or 1
and 6), and `false`, `null` or an amount of 0 removes it; an amount outside 0 to 1 or a size that
isn't a whole number from 1 to 32 is refused. It is saved with the project only when set.

With **Resampling** set to bilinear, a Shaded Relief layer's light (its hillshade, cast shadows,
sky visibility and fill light) and its rock tint are blended between the cells' centres, as its
values are, so a cell drawn over many pixels shades smoothly instead of in squares; nearest keeps
Expand Down
Loading