diff --git a/crates/map/src/symbology.rs b/crates/map/src/symbology.rs index 280d69ee45..a89c7a0968 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,38 @@ 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(); + // 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()).filter(|_| seen.insert(k.as_str())) else { continue }; + match groups.iter_mut().find(|(l, gs, _)| *l == label.as_str() && gs == s) { + Some((_, _, values)) => values.push(k.as_str()), + 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 +6002,70 @@ 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 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 = ember(&["a", "b", "c"]); + group(&mut old, &["b", "c"], None).unwrap(); + 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 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] + 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); + 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]); + } + 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 {