From 99b1d20706a5eca042b7c4c5f6abe8b866f3cae7 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Sun, 29 Nov 2026 23:44:22 -0300 Subject: [PATCH 1/3] fix(map): keep a Unique Values group together when the layer is classified again carry_edits_from carried each class on its own: the first class of a group made by group_classes still had the scheme's colour and counted as unedited, the others held a copy and counted as edited. When a new value moved the classes' places, the group split into two colours and two legend rows with one label. After the classes are carried over, old classes that shared one symbol and one label set by hand now take the new symbol of the first of them in class_order(), as group_classes does. No change to the saved format. Refs #3378 Co-Authored-By: Claude Opus 5.5 --- crates/map/src/symbology.rs | 84 ++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/crates/map/src/symbology.rs b/crates/map/src/symbology.rs index 280d69ee45..7b331c9c94 100644 --- a/crates/map/src/symbology.rs +++ b/crates/map/src/symbology.rs @@ -2403,8 +2403,22 @@ impl Renderer { /// the scheme gave it at its place in `old` (see [`carry_class`]). The other classes take the /// new scheme's colours at their new places, on the new base symbol. A different scheme /// recolours every class (picking a scheme is asking for its colours); a different field - /// starts fresh. + /// starts fresh. Unique Values classes that shared one symbol and one label set by hand, as + /// [`Renderer::group_classes`] leaves them, stay one group (#3378). pub fn carry_edits_from(&mut self, old: &Renderer, g: GeometryType) { + let regroup = matches!( + (&*self, old), + (Renderer::Unique { field, expression, .. }, Renderer::Unique { field: of, expression: oe, .. }) + if field == of && expression == oe + ); + self.carry_class_edits(old, g); + if regroup { + self.keep_groups(old); + } + } + + /// [`Renderer::carry_edits_from`] class by class. + fn carry_class_edits(&mut self, old: &Renderer, g: GeometryType) { match (self, old) { ( Renderer::Unique { @@ -2507,6 +2521,37 @@ impl Renderer { } } + /// Keeps the Unique Values groups of `old` together: the classes that shared one symbol and + /// one label set by hand, as [`Renderer::group_classes`] leaves them, take the symbol of the + /// first of them still here in [`Renderer::class_order`]. Carried class by class, the first + /// would count as unedited and the others as edited, and a value that moved their places + /// would split the group in two legend entries (#3378). + fn keep_groups(&mut self, old: &Renderer) { + let Renderer::Unique { classes: oc, class_labels: ol, .. } = old else { return }; + let mut groups: Vec<(&str, Symbol, Vec<&str>)> = Vec::new(); + for (k, s) in oc { + let Some(label) = ol.get(k.as_str()) else { continue }; + match groups.iter_mut().find(|(l, gs, _)| *l == label.as_str() && gs == s) { + Some((_, _, values)) if !values.contains(&k.as_str()) => values.push(k.as_str()), + Some(_) => {} + None => groups.push((label.as_str(), *s, vec![k.as_str()])), + } + } + groups.retain(|(_, _, values)| values.len() > 1); + if groups.is_empty() { + return; + } + let order = self.class_order(); + let Renderer::Unique { classes, .. } = self else { return }; + for (_, _, values) in &groups { + let first = order.iter().filter_map(|&i| classes.get(i)).find(|(k, _)| values.contains(&k.as_str())); + let Some(&(_, symbol)) = first else { continue }; + for (_, s) in classes.iter_mut().filter(|(k, _)| values.contains(&k.as_str())) { + *s = symbol; + } + } + } + /// Applies `f` to every symbol. pub fn map_symbols(&mut self, mut f: impl FnMut(&mut Symbol)) { match self { @@ -5956,6 +6001,43 @@ mod tests { assert_eq!(more, fresh, "another number of classes starts fresh"); } + /// A group made by `group_classes` stays one when the layer is classified again and a new + /// value moves the classes' places: its classes share the new symbol of the first (#3378). + #[test] + fn a_group_stays_one_when_classified_again() { + let g = GeometryType::Polygon; + let base = Symbol::default_for(g, 0); + let ramped = |keys: &[&str]| { + let n = keys.len(); + let classes: Vec<(&str, Symbol)> = + keys.iter().enumerate().map(|(k, v)| (*v, base.with_fill(Ramp::Ember.pick(k, n), g))).collect(); + unique(&classes, base.with_fill(OTHER_FILL, g), Ramp::Ember) + }; + let symbol = |r: &Renderer, v: &str| { + let Renderer::Unique { classes, .. } = r else { panic!("unique renderer expected") }; + classes.iter().find(|(k, _)| k == v).map(|(_, s)| *s).unwrap() + }; + let mut old = ramped(&["a", "b", "c"]); + group(&mut old, &["b", "c"], None).unwrap(); + let fresh = ramped(&["a", "aa", "b", "c"]); + let mut again = fresh.clone(); + again.carry_edits_from(&old, g); + assert_eq!(symbol(&again, "b"), symbol(&fresh, "b"), "b takes the scheme's colour at its new place"); + assert_eq!(symbol(&again, "c"), symbol(&again, "b"), "c stays with b"); + assert_eq!(rows(again.legend()), named(&["a", "aa", "b, c", OTHER_VALUES])); + // A class with only its own label edited is no group: it keeps the colour of its place. + let mut old = ramped(&["a", "b", "c"]); + let style = ClassStyle { label: Some("Bee".into()), ..Default::default() }; + old.set_class_style(&ClassRef::Value("b".into()), &style, g).unwrap(); + let mut again = fresh.clone(); + again.carry_edits_from(&old, g); + assert_eq!( + again.class_entries().iter().map(|e| e.symbol).collect::>(), + fresh.class_entries().iter().map(|e| e.symbol).collect::>() + ); + assert_eq!(labels_of(&again), ["a", "aa", "Bee", "c", OTHER_VALUES]); + } + fn unique_with(values: &[(&str, u8)]) -> Renderer { let sym = |v: u8| Symbol { fill: Rgba::rgb(v, 0, 0), ..Symbol::default_for(GeometryType::Point, 0) }; Renderer::Unique { From 3202f040f1eb99090b4a6096f3fc8d550e634bb1 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Sun, 29 Nov 2026 23:46:16 -0300 Subject: [PATCH 2/3] test(map): a class with only its label edited keeps its colour, as its own test Refs #3378 Co-Authored-By: Claude Opus 5.5 --- crates/map/src/symbology.rs | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/crates/map/src/symbology.rs b/crates/map/src/symbology.rs index 7b331c9c94..bd6da7f1d5 100644 --- a/crates/map/src/symbology.rs +++ b/crates/map/src/symbology.rs @@ -6001,40 +6001,47 @@ mod tests { assert_eq!(more, fresh, "another number of classes starts fresh"); } + /// Polygon Unique Values classes coloured by the Ember scheme, as `Renderer::unique` makes them. + fn ember(keys: &[&str]) -> Renderer { + let g = GeometryType::Polygon; + let base = Symbol::default_for(g, 0); + let n = keys.len(); + let classes: Vec<(&str, Symbol)> = keys.iter().enumerate().map(|(k, v)| (*v, base.with_fill(Ramp::Ember.pick(k, n), g))).collect(); + unique(&classes, base.with_fill(OTHER_FILL, g), Ramp::Ember) + } + /// A group made by `group_classes` stays one when the layer is classified again and a new /// value moves the classes' places: its classes share the new symbol of the first (#3378). #[test] fn a_group_stays_one_when_classified_again() { let g = GeometryType::Polygon; - let base = Symbol::default_for(g, 0); - let ramped = |keys: &[&str]| { - let n = keys.len(); - let classes: Vec<(&str, Symbol)> = - keys.iter().enumerate().map(|(k, v)| (*v, base.with_fill(Ramp::Ember.pick(k, n), g))).collect(); - unique(&classes, base.with_fill(OTHER_FILL, g), Ramp::Ember) - }; let symbol = |r: &Renderer, v: &str| { let Renderer::Unique { classes, .. } = r else { panic!("unique renderer expected") }; classes.iter().find(|(k, _)| k == v).map(|(_, s)| *s).unwrap() }; - let mut old = ramped(&["a", "b", "c"]); + let mut old = ember(&["a", "b", "c"]); group(&mut old, &["b", "c"], None).unwrap(); - let fresh = ramped(&["a", "aa", "b", "c"]); + let fresh = ember(&["a", "aa", "b", "c"]); let mut again = fresh.clone(); again.carry_edits_from(&old, g); assert_eq!(symbol(&again, "b"), symbol(&fresh, "b"), "b takes the scheme's colour at its new place"); assert_eq!(symbol(&again, "c"), symbol(&again, "b"), "c stays with b"); assert_eq!(rows(again.legend()), named(&["a", "aa", "b, c", OTHER_VALUES])); - // A class with only its own label edited is no group: it keeps the colour of its place. - let mut old = ramped(&["a", "b", "c"]); + } + + /// A class with only its own label edited is no group: classified again after a new value + /// moved the places, it keeps the scheme's colour of its place and its label (#3378). + #[test] + fn a_class_with_only_its_label_edited_keeps_its_colour() { + let g = GeometryType::Polygon; + let mut old = ember(&["a", "b", "c"]); let style = ClassStyle { label: Some("Bee".into()), ..Default::default() }; old.set_class_style(&ClassRef::Value("b".into()), &style, g).unwrap(); + let fresh = ember(&["a", "aa", "b", "c"]); let mut again = fresh.clone(); again.carry_edits_from(&old, g); - assert_eq!( - again.class_entries().iter().map(|e| e.symbol).collect::>(), - fresh.class_entries().iter().map(|e| e.symbol).collect::>() - ); + let symbols = |r: &Renderer| r.class_entries().iter().map(|e| e.symbol).collect::>(); + assert_eq!(symbols(&again), symbols(&fresh)); assert_eq!(labels_of(&again), ["a", "aa", "Bee", "c", OTHER_VALUES]); } From 9520fd6208284ad66608ff11b39aa23749eee058 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Sun, 29 Nov 2026 23:52:42 -0300 Subject: [PATCH 3/3] fix(map): a repeated value joins only its first class's group A value repeated in an imported style is drawn by its first class, so only that occurrence now counts when keep_groups finds the groups. A later one could join the value to a second group, whose symbol then overwrote the first group's hand-set look. Co-Authored-By: Claude Opus 5.5 --- crates/map/src/symbology.rs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/crates/map/src/symbology.rs b/crates/map/src/symbology.rs index bd6da7f1d5..a89c7a0968 100644 --- a/crates/map/src/symbology.rs +++ b/crates/map/src/symbology.rs @@ -2529,11 +2529,12 @@ impl Renderer { fn keep_groups(&mut self, old: &Renderer) { let Renderer::Unique { classes: oc, class_labels: ol, .. } = old else { return }; let mut groups: Vec<(&str, Symbol, Vec<&str>)> = Vec::new(); + // A repeated value is drawn by its first class only: a later one joins no group. + let mut seen = std::collections::HashSet::new(); for (k, s) in oc { - let Some(label) = ol.get(k.as_str()) else { continue }; + let Some(label) = ol.get(k.as_str()).filter(|_| seen.insert(k.as_str())) else { continue }; match groups.iter_mut().find(|(l, gs, _)| *l == label.as_str() && gs == s) { - Some((_, _, values)) if !values.contains(&k.as_str()) => values.push(k.as_str()), - Some(_) => {} + Some((_, _, values)) => values.push(k.as_str()), None => groups.push((label.as_str(), *s, vec![k.as_str()])), } } @@ -6029,6 +6030,26 @@ mod tests { assert_eq!(rows(again.legend()), named(&["a", "aa", "b, c", OTHER_VALUES])); } + /// A repeated value is drawn by its first class only, so only that one makes it part of a + /// group: one later in the list doesn't join it to another group, which would take its look. + #[test] + fn a_repeated_value_joins_only_its_first_classes_group() { + let g = GeometryType::Polygon; + let base = Symbol::default_for(g, 0); + let look = |fill: Rgba, stroke: Rgba| Symbol { stroke, ..base.with_fill(fill, g) }; + let a = look(Rgba::rgb(0xC0, 0x30, 0x20), Rgba::rgb(0, 0, 0xFF)); + let b = look(Rgba::rgb(0x20, 0xC0, 0x30), Rgba::rgb(0xFF, 0, 0xFF)); + let mut old = unique(&[("2", a), ("3", a), ("2", b), ("1", b)], base.with_fill(OTHER_FILL, g), Ramp::Ember); + if let Renderer::Unique { class_labels, .. } = &mut old { + class_labels.extend(["1", "2", "3"].map(|v| (v.to_string(), "G".to_string()))); + } + let mut again = ember(&["1", "2", "3"]); + again.carry_edits_from(&old, g); + let Renderer::Unique { classes, .. } = &again else { panic!("unique renderer expected") }; + let symbols: Vec<(&str, Symbol)> = classes.iter().map(|(k, s)| (k.as_str(), *s)).collect(); + assert_eq!(symbols, [("1", b), ("2", a), ("3", a)]); + } + /// A class with only its own label edited is no group: classified again after a new value /// moved the places, it keeps the scheme's colour of its place and its label (#3378). #[test]