From af43062602f7d3f239291fdcecc4f9dfc4d3c93e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Sat, 28 Nov 2026 17:20:52 -0500 Subject: [PATCH 1/6] feat(layout): order a legend's layers without reordering the map (#3279) --- crates/assistant/skills/print-layout/SKILL.md | 2 +- crates/engine/command-surface.txt | 4 +- crates/engine/src/cmd/layer.rs | 31 ++-- crates/engine/src/cmd/layout.rs | 3 + crates/engine/src/cmd/layout/elements.rs | 8 +- crates/engine/src/cmd/layout/inputs.rs | 25 ++-- crates/engine/src/cmd/layout/legend.rs | 31 +++- .../engine/src/cmd/layout/publication/mod.rs | 1 + .../src/cmd/layout/publication/parts.rs | 1 + crates/engine/src/cmd/layout/update.rs | 13 ++ crates/engine/src/project.rs | 1 + crates/engine/src/project_capabilities.rs | 20 +++ crates/engine/src/tests.rs | 7 + crates/engine/src/tests/layout_files.rs | 1 + .../engine/src/tests/layout_legend_order.rs | 134 ++++++++++++++++++ crates/map/src/layout.rs | 19 ++- crates/map/src/layout/elements.rs | 5 + crates/map/src/layout/tests.rs | 28 ++++ crates/render/src/layout.rs | 3 + crates/render/src/layout/legend.rs | 49 ++++++- .../render/src/layout/legend/count_tests.rs | 2 + crates/render/src/layout/tests.rs | 95 +++++++++++++ crates/render/src/stream_tests.rs | 1 + crates/render/src/svg.rs | 2 + docs/maintaining.md | 7 + docs/user-guide/layouts.md | 9 +- schema/fixtures/layout_legend_order.rgis | 25 ++++ schema/project.schema.json | 2 + 28 files changed, 491 insertions(+), 38 deletions(-) create mode 100644 crates/engine/src/tests/layout_legend_order.rs create mode 100644 schema/fixtures/layout_legend_order.rgis diff --git a/crates/assistant/skills/print-layout/SKILL.md b/crates/assistant/skills/print-layout/SKILL.md index f3061c7457..35ee148573 100644 --- a/crates/assistant/skills/print-layout/SKILL.md +++ b/crates/assistant/skills/print-layout/SKILL.md @@ -27,7 +27,7 @@ description: Make a printable page layout — map frame, title, legend, north ar labels' size) and `"fit"` (`"adjustDivisionValue"`, `"adjustDivision"`, `"adjustWidth"`). A `"legend"` can take `"columns"` (1–6), `"fit"` (`"manual"`, `"adjustSize"`, `"adjustColumns"`, `"adjustColumnsAndSize"`: shrink its text or add columns to fit its box), `"showCounts": true` (each class row ends in its number of features; none for a layer read from a database, over the web or a view at a time from other files), - `"showLayerNames": false` and `"classLabels"` (`{"": {"": ""}}`: its own text for a + `"showLayerNames": false`, `"layerOrder": [""]` (those layers first, the map unchanged) and `"classLabels"` (`{"": {"": ""}}`: its own text for a class, the layer keeping its label). A `"text"` or `"dynamicText"` can take `"valign"` (`"top"`, `"middle"`, `"bottom"`), `"lineSpacing"` (0.5–3, default 1) and `"case"` diff --git a/crates/engine/command-surface.txt b/crates/engine/command-surface.txt index 61eb1b2b01..986dcf07ec 100644 --- a/crates/engine/command-surface.txt +++ b/crates/engine/command-surface.txt @@ -282,7 +282,7 @@ layer.transparency layer? value layer.visibility layer? visible? layer.zoomTo layer? layout.activate name layout -layout.addElement layout? kind frame? of? text? size? color? bold? italic? align? letterSpacing? rotation? wrap? valign? lineSpacing? case? font? fallback? title? allClasses? hideOther? showNoData? exclude? columns? textSize? classLabels? showCounts? showLayerNames? orientation? ticks? tickSide? reverse? fill? width? radius? dash? dir? head? angle? length? image? path? layer? field? label? group? statistic? order? bars? other? stack? values? decimals? barColor? classes? labelAlign? mirror? max? hideLabels? barOutline? rows? step? type? crs? short? mgrs? labels? style? tickLength? format? hemisphere? edges? labelOrientation? labelSize? off? name? direction? at? outline? units? divisions? subdivisions? divisionsBeforeZero? labelPosition? unitLabel? unitLabelPosition? fit? x? y? w? h? background? +layout.addElement layout? kind frame? of? text? size? color? bold? italic? align? letterSpacing? rotation? wrap? valign? lineSpacing? case? font? fallback? title? allClasses? hideOther? showNoData? exclude? layerOrder? columns? textSize? classLabels? showCounts? showLayerNames? orientation? ticks? tickSide? reverse? fill? width? radius? dash? dir? head? angle? length? image? path? layer? field? label? group? statistic? order? bars? other? stack? values? decimals? barColor? classes? labelAlign? mirror? max? hideLabels? barOutline? rows? step? type? crs? short? mgrs? labels? style? tickLength? format? hemisphere? edges? labelOrientation? labelSize? off? name? direction? at? outline? units? divisions? subdivisions? divisionsBeforeZero? labelPosition? unitLabel? unitLabelPosition? fit? x? y? w? h? background? layout.addFrame layout? shape? points? map? x? y? w? h? layout.arrange layout? element to layout.arrangement favourite? save? arrangement? remove? @@ -304,7 +304,7 @@ layout.save layout? path layout.series layout? layer nameField? frame? margin? off? layout.seriesPage layout? page layout.seriesRefresh layout? -layout.update layout? element x? y? w? h? name? visible? locked? text? size? color? bold? italic? align? letterSpacing? rotation? wrap? valign? lineSpacing? case? font? fallback? title? allClasses? hideOther? showNoData? exclude? labels? classLabels? columns? textSize? showLayerNames? showCounts? orientation? ticks? tickSide? reverse? style? fill? outline? radius? width? dash? dir? head? angle? length? units? divisions? subdivisions? divisionsBeforeZero? labelPosition? unitLabel? unitLabelPosition? fit? map? scale? center? bearing? pitch? grid? extentIndicator? border? section? layer? field? label? group? statistic? order? bars? other? stack? values? decimals? barColor? classes? labelAlign? mirror? max? barOutline? hideLabels? rows? background? +layout.update layout? element x? y? w? h? name? visible? locked? text? size? color? bold? italic? align? letterSpacing? rotation? wrap? valign? lineSpacing? case? font? fallback? title? allClasses? hideOther? showNoData? exclude? layerOrder? labels? classLabels? columns? textSize? showLayerNames? showCounts? orientation? ticks? tickSide? reverse? style? fill? outline? radius? width? dash? dir? head? angle? length? units? divisions? subdivisions? divisionsBeforeZero? labelPosition? unitLabel? unitLabelPosition? fit? map? scale? center? bearing? pitch? grid? extentIndicator? border? section? layer? field? label? group? statistic? order? bars? other? stack? values? decimals? barColor? classes? labelAlign? mirror? max? barOutline? hideLabels? rows? background? locator.find locator? text limit? zoom? map.activate name map.addTiles url kind? name? bounds? attribution? minZoom? maxZoom? tileSize? singleImage? tms? subdomains? layers? styleLayers? opacity? blend? resampling? overlay? top? replace? id? view? start? end? unit? interval? dates? legend? connection? diff --git a/crates/engine/src/cmd/layer.rs b/crates/engine/src/cmd/layer.rs index c3ff12fdf6..319feed508 100644 --- a/crates/engine/src/cmd/layer.rs +++ b/crates/engine/src/cmd/layer.rs @@ -7309,7 +7309,15 @@ fn add_sample(s: &mut Session, p: &Value) -> CmdResult { Ok(json!({"layer": id})) } -/// Legends that leave out or label layer `old` of the active map, in the frames that show it, +/// Gives `old` its new name `new` in a legend's list of layer names, each once, should the new +/// name have been listed already. +fn rename_listed(names: &mut Vec, old: &str, new: &str) { + names.iter_mut().filter(|x| x.eq_ignore_ascii_case(old)).for_each(|x| *x = new.to_string()); + let mut seen = std::collections::HashSet::new(); + names.retain(|x| seen.insert(x.to_ascii_lowercase())); +} + +/// Legends that leave out, order or label layer `old` of the active map, in the frames that show it, /// follow it: to its new name, or (`None`, the layer deleted) by dropping it, so a later layer of /// that name isn't left out or labelled unasked. Called once the layer is renamed or gone: while /// another layer still bears `old` (names may repeat), the legends keep it for that one. Renamed @@ -7335,7 +7343,7 @@ pub(crate) fn legends_follow_layer(s: &mut Session, old: &str, new: Option<&str> .map(|e| e.id) .collect(); for e in &mut layout.elements { - if let rustgis_map::layout::ElementKind::Legend { frame, exclude, labels, class_labels, .. } = &mut e.kind + if let rustgis_map::layout::ElementKind::Legend { frame, exclude, layer_order, labels, class_labels, .. } = &mut e.kind && frames.contains(frame) { // Its label (#1654), taken off the old name; onto the new one unless another layer @@ -7367,20 +7375,15 @@ pub(crate) fn legends_follow_layer(s: &mut Session, old: &str, new: Option<&str> Some(n) if shared && !exclude.iter().any(|x| x.eq_ignore_ascii_case(n)) => { exclude.retain(|x| !x.eq_ignore_ascii_case(old)); } - Some(n) => { - exclude.iter_mut().filter(|x| x.eq_ignore_ascii_case(old)).for_each(|x| *x = n.to_string()); - // Once each, should the new name have been left out already. - let mut seen: Vec = Vec::new(); - exclude.retain(|x| { - let first = !seen.iter().any(|y| y.eq_ignore_ascii_case(x)); - if first { - seen.push(x.clone()); - } - first - }); - } + Some(n) => rename_listed(exclude, old, n), None => exclude.retain(|x| !x.eq_ignore_ascii_case(old)), } + // Its place in the legend's order (#3279) goes with it; onto a name already + // there, the layers sharing it go together at the first of the two. + match new { + Some(n) => rename_listed(layer_order, old, n), + None => layer_order.retain(|x| !x.eq_ignore_ascii_case(old)), + } } } } diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index 6d7b985a41..aa81c12908 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -205,6 +205,7 @@ pub fn commands() -> Vec { r##""hideOther"?: bool (legend: leave out ), "##, r##""showNoData"?: bool (legend: include Graduated Colors’ missing-value symbol; default false), "##, r##""exclude"?: [layer names] (legend: layers it leaves out though the map draws them), "##, + r##""layerOrder"?: [layer names] (legend: these layers first, in this order, the others after them as the map draws them; the map is not changed), "##, r##""columns"?: 1..6 (legend: the columns its rows are laid in; default 1; with a fit that adds columns, the fewest it starts from), "##, r##""textSize"?: points, 4 to 100 (legend: its layers' names and classes' labels, its title, rows, swatches and gaps following in proportion and a point symbol keeping the size the map draws it at; default 9; scaleBar: its labels, the room over the bar and between them following, the bar keeping its size; default 8; refused outside that range, and for other kinds unless null, as showNoData is), "##, r##""classLabels"?: {"": {"": ""}} (legend: its own text for a class, by the label the layer shows for it, up to 200 characters; the layer keeps its label; a layer or class it can't find is refused), "##, @@ -306,6 +307,7 @@ pub fn commands() -> Vec { r##""hideOther"?: bool (legend), "##, r##""showNoData"?: bool (legend; default false), "##, r##""exclude"?: [layer names] (legend; [] lists every layer again), "##, + r##""layerOrder"?: [layer names] (legend, as in layout.addElement; [] returns to the map's order), "##, r##""labels"?: {"": "