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
67 changes: 54 additions & 13 deletions crates/engine/src/cmd/effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ pub fn commands() -> Vec<C> {
.effects(crate::policy::Effects::NONE)
.when(need_polygons)
.params(r#"{"layer"?, "lighting"?: bool, "shadows"?: bool}"#),
C::new("layer.extrusionBase", "Extrusion Base", "Style › Extrusion", extrusion_base)
.roles(&[])
.effects(crate::policy::Effects::NONE)
.when(need_polygons)
.params(r#"{"layer"?, "field"?: numeric field (metres; null or "" clears it), "base"?: metres (0 to 10,000,000)}"#),
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
]
}

Expand Down Expand Up @@ -175,19 +180,7 @@ fn extrusion(s: &mut Session, p: &Value) -> CmdResult {
let x = match ps(p, "type").ok_or("type is required")? {
"none" => None,
t @ ("height" | "field") => {
let field = if t == "field" {
let n = ps(p, "field").ok_or("field is required")?;
Some(
l.dataset
.field_index(n)
.and_then(|i| l.dataset.fields.get(i))
.filter(|f| f.ty.is_numeric())
.map(|f| f.name.clone())
.ok_or_else(|| format!("no numeric field '{n}'"))?,
)
} else {
None
};
let field = if t == "field" { Some(numeric_field(&l.dataset, ps(p, "field").ok_or("field is required")?)?) } else { None };
let keep = prev.unwrap_or_default();
Some(Extrusion {
field,
Expand All @@ -208,6 +201,54 @@ fn extrusion(s: &mut Session, p: &Value) -> CmdResult {
Ok(r)
}

/// The name of the numeric field `n` (matched as `field_index` matches), or why there is none.
fn numeric_field(ds: &rustgis_data::Dataset, n: &str) -> Result<String, String> {
ds.field_index(n)
.and_then(|i| ds.fields.get(i))
.filter(|f| f.ty.is_numeric())
.map(|f| f.name.clone())
.ok_or_else(|| format!("no numeric field '{n}'"))
}

/// `layer.extrusionBase`: what an extruded layer's walls stand on, a numeric field (`field`) else
/// a constant (`base`), in metres times the extrusion's factor. Each left out stays as it is, so
/// the height, its field and the factor never change here; null or an empty name clears the
/// field, and a base that is no number, or is outside 0 to 1e7 metres (the height's limit), is
/// refused rather than changed.
fn extrusion_base(s: &mut Session, p: &Value) -> CmdResult {
let id = s.layer_id(p)?;
let l = s.map().layer(id).ok_or("no such layer")?;
if l.extrusion.is_none() {
return Err("turn extrusion on first (Layer Extrusion)".into());
}
let field = match p.get("field") {
None => None,
Some(Value::Null) => Some(None),
Some(v) => match v.as_str().ok_or("field is a field name, or null to clear it")? {
"" => Some(None),
n => Some(Some(numeric_field(&l.dataset, n)?)),
},
};
let base = match p.get("base") {
None => None,
Some(_) => {
let b = f(p, "base").ok_or("base is a number of metres")?;
if !(0.0..=1e7).contains(&b) {
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
return Err("base is between 0 and 10,000,000 metres".into());
}
Some(b)
}
};
change_extrusion(s, p, |x| {
if let Some(v) = field {
x.base_field = v;
}
if let Some(v) = base {
x.base = v;
}
})
}

fn change_extrusion(s: &mut Session, p: &Value, f: impl FnOnce(&mut Extrusion)) -> CmdResult {
let id = s.layer_id(p)?;
let l = s.map_mut().layer_mut(id).ok_or("no such layer")?;
Expand Down
65 changes: 65 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20920,6 +20920,71 @@ fn repair_source_checks_the_extrusion_base_field() {
std::fs::remove_dir_all(dir).ok();
}

/// Extrusion Base sets what an extrusion stands on, a numeric field or a constant, and changes
/// nothing else: the height, its field and the factor stay, as does whatever is left out. Null or
/// an empty name clears the field; a field that isn't numeric, or a base that is no number or is
/// outside 0 to 1e7 metres, is refused, not changed (#1269).
#[test]
fn extrusion_base_sets_the_base_and_nothing_else() {
let mut s = world();
let x = |s: &Session| s.map().layer_by_name("Countries").and_then(|l| l.extrusion.clone());
for p in [json!({"layer": "Countries", "base": 5}), json!({"layer": "Countries", "field": "name"})] {
let e = s.run("layer.extrusionBase", &p).unwrap_err();
assert!(e.contains("turn extrusion on first"), "{p}: {e}");
}
s.run("layer.extrusion", &json!({"layer": "Countries", "type": "height", "height": 30, "factor": 2})).unwrap();
s.run("layer.extrusionBase", &json!({"layer": "Countries", "field": "pop_est", "base": 12})).unwrap();
let got = x(&s).unwrap();
assert_eq!((got.base_field.as_deref(), got.base), (Some("pop_est"), 12.0));
assert_eq!((got.field, got.height, got.factor), (None, 30.0, 2.0), "the height and factor stay");
// Each left out stays.
s.run("layer.extrusionBase", &json!({"layer": "Countries", "base": 20})).unwrap();
assert_eq!(x(&s).unwrap().base_field.as_deref(), Some("pop_est"));
s.run("layer.extrusionBase", &json!({"layer": "Countries", "field": "pop_est"})).unwrap();
assert_eq!(x(&s).unwrap().base, 20.0);
// A field that isn't numeric, or isn't there (a blank name too), a field that isn't a name,
// and a base that is no number or is outside 0 to 1e7 metres are refused, and change nothing.
let bad = [
json!({"field": "name"}),
json!({"field": "nowhere"}),
json!({"field": " "}),
json!({"field": 7}),
json!({"base": "ten"}),
json!({"base": null}),
json!({"base": -5}),
json!({"base": 1e12}),
];
for bad in bad {
let mut p = bad.clone();
p["layer"] = json!("Countries");
assert!(s.run("layer.extrusionBase", &p).is_err(), "{bad}");
}
let e = s.run("layer.extrusionBase", &json!({"layer": "Countries", "field": "name"})).unwrap_err();
assert!(e.contains("no numeric field 'name'"), "{e}");
let e = s.run("layer.extrusionBase", &json!({"layer": "Countries", "base": -5})).unwrap_err();
assert!(e.contains("between 0 and 10,000,000 metres"), "{e}");
assert_eq!((x(&s).unwrap().base_field.as_deref(), x(&s).unwrap().base), (Some("pop_est"), 20.0));
// Null or an empty name clears the field; the constant stays.
s.run("layer.extrusionBase", &json!({"layer": "Countries", "field": null})).unwrap();
assert_eq!((x(&s).unwrap().base_field, x(&s).unwrap().base), (None, 20.0));
s.run("layer.extrusionBase", &json!({"layer": "Countries", "field": "pop_est"})).unwrap();
s.run("layer.extrusionBase", &json!({"layer": "Countries", "field": ""})).unwrap();
assert_eq!(x(&s).unwrap().base_field, None);
// Both ends of the constant's range are taken.
s.run("layer.extrusionBase", &json!({"layer": "Countries", "base": 0})).unwrap();
assert_eq!(x(&s).unwrap().base, 0.0);
s.run("layer.extrusionBase", &json!({"layer": "Countries", "base": 1e7})).unwrap();
assert_eq!(x(&s).unwrap().base, 1e7);
// Changing the height keeps the base; each change undoes on its own.
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
s.run("layer.extrusion", &json!({"layer": "Countries", "type": "field", "field": "pop_est"})).unwrap();
assert_eq!(x(&s).unwrap().base, 1e7);
s.undo();
s.undo();
assert_eq!(x(&s).unwrap().base, 0.0);
let got = x(&s).unwrap();
assert_eq!((got.field, got.height, got.factor), (None, 30.0, 2.0), "nothing else changed on the way");
}

#[test]
fn area_review_attribute_edits_keep_retained_display_expressions() {
let (mut session, id) = area_review_fixture();
Expand Down
11 changes: 11 additions & 0 deletions docs/user-guide/cartography.md
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,17 @@ range); **Aggregation** draws points as hexagon or square bins coloured by count
oblique view on an untilted flat map and in perspective on a tilted map; the globe draws the
polygons flat. See [Rotating and tilting the map](maps.md#navigating).

An extrusion can stand on a **base**: its walls rise from the base to the base plus the height,
so a balcony or a bridge floats above the ground. The base is a numeric field's value (such as the
`base_height` the CityGML and CityJSON readers give each part), else a constant in metres, both
multiplied by the same **Factor** as the height. A value that is no number takes the constant,
and a negative base is the ground. Shadows fall from the base, roof labels sit at the top, and a
web scene (**Publish › Share As › Web Scene**) raises the parts too. It is set with the
`layer.extrusionBase` command (`field`, `base`), which changes nothing else; `"field": null`
clears the field, and turning extrusion off drops the base with it. Without a tilt, and on a
tilted map drawn without the graphics card, extrusions are painted in the order they stand on the
ground, so a part raised over a building behind it may be painted under it.

For a raster layer, the **Style** tab also has **Layer Blend** (multiply, screen, overlay,
darken, lighten, difference), **Swipe** (the layer shows only left of a divider you slide, to
compare it with what's beneath), **Masking** and **Rotation**.
Expand Down
Loading
Loading