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
1 change: 1 addition & 0 deletions crates/engine/command-surface.txt
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ layer.fields layer?
layer.fillLayers layer? value? index? other? fills
layer.geocode table? layer? address provider? url? name? places?
layer.group layer? group before?
layer.groupClasses layer? values label?
layer.indent layer?
layer.invertRamp layer?
layer.lightShadow layer? lighting? shadows?
Expand Down
112 changes: 101 additions & 11 deletions crates/engine/src/cmd/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::path::Path;

use rustgis_data::{Dataset, GeometryType, photo};
use rustgis_map::symbology::{ClassRef, ClassStyle, LABEL_SIZE, MAX_STROKES};
use rustgis_map::symbology::{ClassRef, ClassStyle, LABEL_SIZE, MAX_LABEL, MAX_STROKES};
use rustgis_map::{
ClassMethod, Glyph, HiddenFills, LineCap, LineJoin, LineMark, LineMarkers, LineStroke, Marker, NoFill, Picture, Placements, Ramp,
Renderer, Rgba,
Expand Down Expand Up @@ -229,6 +229,11 @@ pub fn commands() -> Vec<C> {
.params(
r##"{"layer"?, "value"?: text|number (a Unique Values class), "index"?: number (a Graduated Colors class, 0 = the lowest), "other"?: true (Unique Values' <all other values>), "color"?: "#RRGGBB" (preserves polygon translucency, minimum alpha 150)|"#RRGGBBAA" (sets alpha exactly), "outline"?: "#RRGGBB"|"#RRGGBBAA", "label"?: text (the legend label; "" goes back to the value or range)}"##,
),
C::new("layer.groupClasses", "Group Classes", "Style › Drawing", group_classes).roles(&[]).effects(crate::policy::Effects::NONE)
.when(need_feature_layer)
.params(
r##"{"layer"?, "values": [text|number, …] (two or more Unique Values classes; they take the symbol, strokes and fills of the first of them in the class list's order, and one label, so a legend lists them as one entry), "label"?: text (default, or when empty: the values joined with ", "; cut to 200 characters)}"##,
),
C::new("layer.color", "Symbol Color", "Style › Drawing", |s, p| {
let hex = s_req(p, "color")?;
let c = Rgba::from_hex(hex).ok_or("color must be #RRGGBB or #RRGGBBAA")?;
Expand Down Expand Up @@ -8365,6 +8370,20 @@ fn symbology(s: &mut Session, p: &Value) -> CmdResult {
Ok(out)
}

/// A Unique Values class's key, as a command's parameter names it.
fn class_value(v: &Value) -> Result<String, String> {
Ok(match v {
Value::String(t) => t.clone(),
// As the data prints it (the classes' own keys): `1000.0` is the class "1000".
Value::Number(n) => match (n.as_i64(), n.as_f64()) {
(Some(i), _) => rustgis_data::Value::Int(i).to_string(),
(None, Some(x)) => rustgis_data::Value::Float(x).to_string(),
_ => n.to_string(),
},
_ => return Err("value must be a text or a number".into()),
})
}

/// The class `value`, `index` or `other: true` name in a command's parameters, or `None` when
/// none does.
fn class_ref(p: &Value) -> Result<Option<ClassRef>, String> {
Expand All @@ -8375,16 +8394,7 @@ fn class_ref(p: &Value) -> Result<Option<ClassRef>, String> {
};
Ok(Some(match (p.get("value"), index, other) {
(None, None, false) => return Ok(None),
(Some(v), None, false) => ClassRef::Value(match v {
Value::String(t) => t.clone(),
// As the data prints it (the classes' own keys): `1000.0` is the class "1000".
Value::Number(n) => match (n.as_i64(), n.as_f64()) {
(Some(i), _) => rustgis_data::Value::Int(i).to_string(),
(None, Some(x)) => rustgis_data::Value::Float(x).to_string(),
_ => n.to_string(),
},
_ => return Err("value must be a text or a number".into()),
}),
(Some(v), None, false) => ClassRef::Value(class_value(v)?),
(None, Some(i), false) => ClassRef::Index(usize::try_from(i).map_err(|_| "index is too large")?),
(None, None, true) => ClassRef::Other,
_ => return Err("name one class: value (Unique Values), index (Graduated Colors) or other: true".into()),
Expand Down Expand Up @@ -8425,6 +8435,86 @@ fn class_style(s: &mut Session, p: &Value) -> CmdResult {
Ok(json!({"label": e.label, "color": e.symbol.fill.hex(), "outline": e.symbol.stroke.hex()}))
}

/// `layer.groupClasses`: several Unique Values classes listed as one legend entry (#3308).
fn group_classes(s: &mut Session, p: &Value) -> CmdResult {
let values = p.get("values").and_then(Value::as_array).ok_or("values is a list of two or more Unique Values classes")?;
let values = values.iter().map(class_value).collect::<Result<Vec<_>, _>>()?;
let label = match p.get("label") {
None => None,
Some(Value::String(t)) => Some(t.as_str()),
Some(_) => return Err("label must be a text (an empty one joins the values)".into()),
};
let id = s.layer_id(p)?;
let l = s.map_mut().layer_mut(id).ok_or("no such layer")?;
let mut group = l.renderer.group_classes(&values, label)?;
// What No Fill hid follows the group too, or Restore Fill would set the classes apart.
let first = ClassRef::Value(group.first.clone());
let at = symbol_position(&l.renderer, &first);
let mut now = 0;
l.renderer.map_symbols(|_| now += 1);
if let Some(hidden) = &mut l.hidden_fills {
let others: std::collections::HashSet<&str> = group.values.iter().filter(|v| **v != group.first).map(String::as_str).collect();
let fills = hidden.class_fills.iter().find(|(c, _)| *c == first).map(|(_, f)| f.clone());
for (c, own) in &hidden.class_fills {
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
if let ClassRef::Value(v) = c
&& others.contains(v.as_str())
&& Some(own) != fills.as_ref()
{
group.replaced.insert(v.clone());
}
}
hidden.class_fills.retain(|(c, _)| !matches!(c, ClassRef::Value(v) if others.contains(v.as_str())));
if let Some(fills) = fills {
let copies = group.values.iter().filter(|v| others.contains(v.as_str()));
hidden.class_fills.extend(copies.map(|v| (ClassRef::Value(v.clone()), fills.clone())));
}
// The saved opacities are by symbol, while they still match them, as Restore Fill needs.
if let (Renderer::Unique { classes, .. }, true) = (&l.renderer, hidden.alphas.len() == now)
&& let Some(a) = at.and_then(|i| hidden.alphas.get(i).copied())
{
for ((k, _), alpha) in classes.iter().zip(&mut hidden.alphas) {
// Every entry of a repeated value, the first one's too.
if others.contains(k.as_str()) || *k == group.first {
if *alpha != a {
group.restyled.insert(k.clone());
}
*alpha = a;
}
}
}
}
if !group.replaced.is_empty() {
s.notify(
format!(
"{} class(es) lost the strokes or fills they had of their own, to match '{}'; Undo brings them back",
group.replaced.len(),
group.first
),
false,
);
}
if !group.restyled.is_empty() {
s.notify(
format!(
"{} class(es) took the symbol of '{}' (colour, outline, size, marker or the opacity No Fill hid); Undo brings theirs back",
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
group.restyled.len(),
group.first
),
false,
);
}
if group.cut > 0 {
s.notify(format!("The group's label was cut to at most {MAX_LABEL} characters; {} were left out", group.cut), false);
}
if group.relabelled > 0 {
s.notify(
format!("{} class(es) had a legend label of their own, replaced by '{}'; Undo brings them back", group.relabelled, group.label),
false,
);
}
Ok(json!({"label": group.label, "values": group.values}))
}

/// These loaders interpret `layer` inside an external file/database, never as a map layer.
/// The selector remains frozen with its path/address, including the loader's empty default.
pub(crate) fn external_dataset_selector(command: &str) -> bool {
Expand Down
Loading
Loading