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
5 changes: 3 additions & 2 deletions crates/analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ pub use density::{
pub use parallel::scratch_budget;
pub use parameters::StatisticList;
pub use tools::{
SimplifyNotes, TOOLS, find_field_case_insensitive, holds_coordinates, in_its_units, layer_raster_crs, max_buffer_bytes, min_pad,
param_applies, past_pole, points_from, points_in, projected_crs, raster_crs, raster_crs_in, simplify_geometry, zonal_bands,
PLAUSIBLE_ELEVATION, SimplifyNotes, TOOLS, find_field_case_insensitive, holds_coordinates, in_its_units, layer_raster_crs,
max_buffer_bytes, min_pad, param_applies, past_pole, points_from, points_in, projected_crs, raster_crs, raster_crs_in,
simplify_geometry, zonal_bands,
};

use rustgis_data::Dataset;
Expand Down
1 change: 1 addition & 0 deletions crates/analysis/src/tools.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! The tools.

mod check_dem;
pub use check_dem::PLAUSIBLE_ELEVATION;
mod check_geometry;
mod geometry;
mod network_analysis;
Expand Down
6 changes: 3 additions & 3 deletions crates/analysis/src/tools/check_dem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ const MAX_POINTS: usize = 100_000;
/// About how many cells one item of the scan reads: enough to share out on threads, few enough
/// items that a small model is checked in a few looks at Cancel.
const BLOCK_CELLS: usize = 1 << 16;
/// The lowest and highest values an elevation model can hold, in metres: below the Dead Sea's
/// shore and the deepest mine, above Everest.
pub(crate) const PLAUSIBLE_ELEVATION: (f64, f64) = (-500.0, 9000.0);
/// The lowest and highest values an elevation model of the land can hold, in metres: below the
/// Dead Sea's shore and the deepest mine, above Everest. A bathymetry goes deeper.
pub const PLAUSIBLE_ELEVATION: (f64, f64) = (-500.0, 9000.0);
/// The eight neighbours, as (column, row) steps.
const AROUND: [(i64, i64); 8] = [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)];

Expand Down
44 changes: 43 additions & 1 deletion crates/engine/src/cmd/raster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,7 @@ fn symbology(s: &mut Session, p: &Value) -> CmdResult {
})
.transpose()?;
let bnd = band(p, "band", &r)?;
let was = relief_band(s, id);
with(s, p, |rl, r| {
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));
Expand Down Expand Up @@ -1094,7 +1095,48 @@ fn symbology(s: &mut Session, p: &Value) -> CmdResult {
rl.style.stretch = Stretch::default();
}
Ok(())
})
})?;
if relief_band(s, id) != was {
relief_notice(s, id, &r);
}
Ok(Value::Null)
}

/// The band a layer's Shaded Relief is drawn from, if it is drawn as one.
fn relief_band(s: &Session, id: u32) -> Option<usize> {
match s.map().layer(id)?.raster.as_ref()?.style.renderer {
RasterRenderer::ShadedRelief { band, .. } => Some(band),
_ => None,
}
}

/// Says when a Shaded Relief is drawn from a band holding values outside
/// [`rustgis_analysis::PLAUSIBLE_ELEVATION`] (#3151). They may be real depths, as in a bathymetry
/// such as GEBCO, or a no-data the file doesn't declare, such as -32768, which the relief draws as
/// cliffs: the notice names both and leaves the choice to the user. A look at the band's
/// statistics, when the relief is applied or moves to another band, not at each change of its
/// light; Check DEM says where the values are.
fn relief_notice(s: &mut Session, id: u32, r: &Raster) {
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
let (lowest, highest) = rustgis_analysis::PLAUSIBLE_ELEVATION;
let Some(l) = s.map().layer(id) else { return };
let Some(RasterRenderer::ShadedRelief { band, .. }) = l.raster.as_ref().map(|rl| &rl.style.renderer) else { return };
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
let Some(stats) = r.bands.get(*band).map(|b| &b.stats).filter(|st| st.count > 0) else { return };
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
let mut odd = Vec::new();
if stats.min < lowest {
odd.push(format!("as low as {}", stats.min));
}
if stats.max > highest {
odd.push(format!("as high as {}", stats.max));
}
if odd.is_empty() {
return;
}
let text = format!(
"'{}' holds values outside {lowest} to {highest} ({}). Depths, as in a bathymetry, are real and drawn as they are; a no-data the file doesn't declare, such as -32768, is drawn as cliffs. Check DEM (checkDem) finds where the values are, and raster.mask hides those that aren't terrain",
l.name,
odd.join(" and ")
);
s.notify(text, false);
}

/// `raster.symbology`'s `"type"` as the command matches it: lower case, without spaces, `_` or `-`;
Expand Down
48 changes: 48 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7737,6 +7737,54 @@ fn custom_projections_switch_keep_scale_query_and_render() {
}
}

/// A Shaded Relief drawn from a band holding values outside the range of a land elevation (an
/// undeclared no-data of -32768, a height of 65535, a depth of a bathymetry) says so and names
/// them, without calling a depth a no-data, once: not again at a change of its light. A clean band,
/// or one whose no-data is declared, says nothing (#3151).
#[test]
fn shaded_relief_names_values_outside_a_land_elevation() {
let dir = std::env::temp_dir().join(format!("rustgis-engine-relief-values-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let mut s = Session::new(Project::default());
for (name, head, corner, said) in [
("hole.asc", "", "-32768", Some("as low as -32768")),
("high.asc", "", "65535", Some("as high as 65535")),
("deep.asc", "", "-10900", Some("as low as -10900")),
("clean.asc", "", "818", None),
("declared.asc", "NODATA_value -32768\n", "-32768", None),
] {
let path = dir.join(name);
let grid =
format!("ncols 3\nnrows 3\nxllcorner 10\nyllcorner 40\ncellsize 0.001\n{head}812 815 {corner}\n809 811 814\n806 808 810\n");
std::fs::write(&path, grid).unwrap();
let id = s.run("layer.addData", &json!({"path": path})).unwrap()["layer"].as_u64().unwrap();
let mark = s.notice_mark();
s.run("raster.symbology", &json!({"layer": id, "type": "shadedRelief"})).unwrap();
let notice = s.notices_since(mark).iter().find(|n| n.text.contains("holds values outside")).map(|n| n.text.clone());
match said {
Some(values) => {
let notice = notice.unwrap_or_else(|| panic!("{name}: {:?}", s.notices_since(mark)));
assert!(notice.contains(values) && notice.contains("Check DEM (checkDem)"), "{notice}");
// A depth is real: the notice names it as one, and the undeclared no-data apart from it.
assert!(
notice.contains("Depths, as in a bathymetry, are real") && notice.contains("a no-data the file doesn't declare"),
"{notice}"
);
}
None => assert_eq!(notice, None, "{name}"),
}
// A change of the relief's light says nothing again.
let mark = s.notice_mark();
s.run("raster.symbology", &json!({"layer": id, "type": "shadedRelief", "azimuth": 270})).unwrap();
assert!(!s.notices_since(mark).iter().any(|n| n.text.contains("holds values outside")), "{name}");
// Another symbology says nothing of it.
let mark = s.notice_mark();
s.run("raster.symbology", &json!({"layer": id, "type": "stretch"})).unwrap();
assert!(!s.notices_since(mark).iter().any(|n| n.text.contains("holds values outside")), "{name}");
}
std::fs::remove_dir_all(dir).unwrap();
}

/// Shaded Relief's cast shadows: set, kept when left out, clamped, a new one without; saved with
/// the project only when there are some, and drawn in an export (waited for).
#[test]
Expand Down
13 changes: 13 additions & 0 deletions docs/user-guide/data.md
Original file line number Diff line number Diff line change
Expand Up @@ -1733,6 +1733,19 @@ stretch, and an opaque black cell still counts. The alpha band's own statistics
alpha with a value, see-through ones included.
**Change Detection** gives no-data where either image is see-through.

When **Shaded Relief** is applied, or moved to another band, it posts a notice if the band's lowest
or highest value lies outside −500 to 9,000. The notice gives those two extremes, not each such
value. They come from the band's statistics, which leave out a no-data the file declares, so only
the values it doesn't declare are named. On a layer held as an overview of its file, such as a
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
large cloud-optimised GeoTIFF, they are the overview's unless the file stores its own, so a lone
cell of such a value may not show in them; Check DEM runs on the same cells and says when they are
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
an overview's. The values named may be real depths, as in a bathymetry such as GEBCO, which are
drawn as they are, or a no-data the file doesn't declare, such as −32768, which the relief draws as
cliffs. Check DEM (`checkDem`, see [Checking an elevation
model](analysis.md#checking-an-elevation-model)) finds where they are, and `raster.mask` hides
those that aren't terrain. A change of the relief's light, such as its azimuth or shadow, posts no
notice again.

**Cast shadows**, on a Shaded Relief layer (`raster.symbology` `"shadow"`, 0 to 1), darkens the
slopes the terrain hides from the sun as well as those facing away from it: a valley behind a ridge
is in shadow, with a soft edge. Only the direct light is darkened: a slope in full shadow keeps the
Expand Down
Loading