diff --git a/crates/engine/src/cmd/labels.rs b/crates/engine/src/cmd/labels.rs index 86487bd0a5..dab715795b 100644 --- a/crates/engine/src/cmd/labels.rs +++ b/crates/engine/src/cmd/labels.rs @@ -284,21 +284,20 @@ pub fn commands() -> Vec { // `to: null`: this call takes the line away. let clears = matches!(to, Some(None)); let id = s.layer_id(p)?; - let held = held_geometries(s.map().layer(id).ok_or("no such layer")?); + let layer = s.map().layer(id).ok_or("no such layer")?; + let held = held_geometries(layer); let (points, lines, _) = held; - // On a layer with no points it would do nothing; off is taken anywhere. - if obstacle.is_some_and(|w| w > 0) && !points { - return Err("obstacleWeight is for layers with points: lines and polygons aren't obstacles yet".into()); - } - if obstacle.is_some_and(|w| w > 0) - && let Some(layer) = s.map().layer(id) - { - if layer.graphics { - return Err("obstacleWeight isn't for a graphics layer: its points are text and images, which draw no obstacle".into()); - } - if layer.aggregation.is_some() { - return Err("obstacleWeight isn't for a cluster or heat map layer: it draws no points".into()); + // On a layer whose points aren't obstacles it would do nothing; off is taken anywhere. + // The rule is the layer's, shared with the Labeling pane's row. + if obstacle.is_some_and(|w| w > 0) && !layer.takes_obstacle_weight() { + return Err(if !points { + "obstacleWeight is for layers with points: lines and polygons aren't obstacles yet" + } else if layer.graphics { + "obstacleWeight isn't for a graphics layer: its points are text and images, which draw no obstacle" + } else { + "obstacleWeight isn't for a cluster or heat map layer: it draws no points" } + .into()); } let guide = label_guide(s, p, s.map().layer(id).ok_or("no such layer")?)?; // A repeat or line offset given where it does nothing (not along a line), kept for diff --git a/crates/map/src/lib.rs b/crates/map/src/lib.rs index 40007fc1f2..da663228d6 100644 --- a/crates/map/src/lib.rs +++ b/crates/map/src/lib.rs @@ -1414,6 +1414,13 @@ impl Layer { kind != GeometryType::None && (self.geometry_type() == kind || self.geometry_kinds.contains(&self.dataset.features, kind)) } + /// Whether the layer's points can be obstacles for labels (#3526): it has points, draws them + /// (not a graphics, cluster or heat map layer), so an obstacle weight above 0 means something. + /// The one rule for `labels.placement`'s `obstacleWeight` and the Labeling pane's row. + pub fn takes_obstacle_weight(&self) -> bool { + !self.graphics && self.aggregation.is_none() && self.has_geometry(GeometryType::Point) + } + /// Copies of every geometry in a map projection's coordinates (`key` names the projection), /// rebuilt lazily when the geometry or the projection changes. Points a projection can't show /// become NaN, which the renderer skips; features across a world projection's edge are cut diff --git a/crates/ui-egui/src/labelpane.rs b/crates/ui-egui/src/labelpane.rs index 9aec98d4ae..5314a9e592 100644 --- a/crates/ui-egui/src/labelpane.rs +++ b/crates/ui-egui/src/labelpane.rs @@ -984,8 +984,9 @@ fn square_to(app: &mut RustGisApp, ui: &mut Ui, l: &Layer, c: &Class) { /// Where labels crowd: every one shown, over the others, rather than some left out; the class's /// weight, the heavier winning where labels compete; its clearance, the room its labels keep free /// of others; whether it removes duplicates, one label of a text where its features lie closer than -/// so far (#2847); and callouts, which move a crowded label to a free spot nearby with a line back -/// to its feature, no longer than so far. +/// so far (#2847); callouts, which move a crowded label to a free spot nearby with a line back +/// to its feature, no longer than so far; and, for a layer with points, the weight of its points as +/// obstacles for labels: the layer's, whichever class is shown (#3526). fn crowding(app: &mut RustGisApp, ui: &mut Ui, l: &Layer, c: &Class) { use rustgis_engine::map::symbology::{ CALLOUT_REACH, DEFAULT_LABEL_DUPLICATES, LABEL_CLEARANCE, LABEL_DUPLICATES, LABEL_WEIGHT, callout_reach, label_clearance, @@ -1019,6 +1020,26 @@ fn crowding(app: &mut RustGisApp, ui: &mut Ui, l: &Layer, c: &Class) { } }); ui.end_row(); + // The layer's points as obstacles (#3526), for the layers the engine takes it for, whatever + // the class shown; 0 is none. + if l.takes_obstacle_weight() { + let input_label = ui.label(tl!("Labels keep off these points: weight")); + // As the weight: scoped to its layer, written when let go. + ui.push_id(("label_obstacle_weight", l.instance_id()), |ui| { + let mut v = f64::from(l.labels.obstacle_weight); + let drag = crate::widgets::Dragged::lend(ui, egui::Id::unique(("label_obstacle_weight_drag", l.instance_id())), &mut v); + let r = ui + .add(egui::DragValue::new(&mut v).update_while_editing(false).range(LABEL_WEIGHT).speed(0.05).max_decimals(0)) + .labelled_by(input_label.id) + .on_hover_text(tl!( + "A label whose class's weight isn't above this isn't placed over one of this layer's points, except its own feature's. 0: the points are no obstacle." + )); + if drag.settled(ui, &r, &mut v) && label_weight(v) != l.labels.obstacle_weight { + run(app, "labels.placement", c.params(json!({"obstacleWeight": label_weight(v)}))); + } + }); + ui.end_row(); + } let input_label = ui.label(tl!("Clearance")); // As the weight: scoped to its layer and class, written when let go (#2828). ui.push_id(("label_clearance", l.instance_id(), c.name), |ui| { @@ -1822,6 +1843,58 @@ mod tests { assert_eq!(h.state().session.undo_depth(), steps, "nothing to undo"); } + /// A point layer's obstacle weight (#3526) is shown and set in Position, under Weight: typed, + /// a fraction rounded; a layer of lines has no such row. + #[test] + fn an_obstacle_weight_is_set_in_the_pane() { + use egui::accesskit::Role; + const LABEL: &str = "Labels keep off these points: weight"; + let mut h = harness(with_cities()); + h.run(); + let cities = layer_id(h.state(), "Cities"); + h.state_mut().session.run("layer.select", &json!({"layer": cities})).unwrap(); + h.state_mut().session.run("labels.placement", &json!({"layer": cities, "obstacleWeight": 4})).unwrap(); + h.run(); + open_tab(&mut h, "Position"); + let weight = + |h: &egui_kittest::Harness<'_, RustGisApp>| h.state().session.map().layer_by_name("Cities").unwrap().labels.obstacle_weight; + assert_eq!(h.get_by_role_and_label(Role::SpinButton, LABEL).accesskit_node().numeric_value(), Some(4.0), "the layer's weight"); + let steps = h.state().session.undo_depth(); + h.get_by_role_and_label(Role::SpinButton, LABEL).focus(); + h.run(); + h.get_by_role_and_label(Role::SpinButton, LABEL).type_text("7.6"); + h.key_press(egui::Key::Enter); + h.run(); + assert_eq!(weight(&h), 8, "a fraction rounded"); + assert_eq!(h.state().session.undo_depth(), steps + 1, "one step"); + // The class's own weight is left alone. + let class_weight = h.state().session.map().layer_by_name("Cities").unwrap().labels.default.weight; + assert_eq!(class_weight, rustgis_engine::map::symbology::DEFAULT_LABEL_WEIGHT); + // A layer of lines takes no obstacle weight, so the row isn't shown. + let rivers = layer_id(h.state(), "Rivers"); + h.state_mut().session.run("layer.select", &json!({"layer": rivers})).unwrap(); + h.run(); + assert!(h.query_by_role_and_label(Role::SpinButton, LABEL).is_none(), "no row for lines"); + // Nor for polygons, a graphics layer, or a cluster or heat map layer: the engine takes the + // weight of none of them. + let countries = layer_id(h.state(), "Countries"); + h.state_mut().session.run("layer.select", &json!({"layer": countries})).unwrap(); + h.run(); + assert!(h.query_by_role_and_label(Role::SpinButton, LABEL).is_none(), "no row for polygons"); + h.state_mut().session.run("layer.select", &json!({"layer": cities})).unwrap(); + h.state_mut().session.map_mut().layer_mut(cities).unwrap().aggregation = + Some(rustgis_engine::map::effects::Aggregation { hexagons: true, size: 20.0 }); + h.run(); + assert!(h.query_by_role_and_label(Role::SpinButton, LABEL).is_none(), "no row for a cluster layer"); + h.state_mut().session.map_mut().layer_mut(cities).unwrap().aggregation = None; + h.state_mut().session.map_mut().layer_mut(cities).unwrap().graphics = true; + h.run(); + assert!(h.query_by_role_and_label(Role::SpinButton, LABEL).is_none(), "no row for a graphics layer"); + h.state_mut().session.map_mut().layer_mut(cities).unwrap().graphics = false; + h.run(); + assert!(h.query_by_role_and_label(Role::SpinButton, LABEL).is_some(), "the row is back for points"); + } + /// A label class's clearance (#2828) is shown and set in Position, under Weight: typed, a /// fraction kept; dragged, one step when let go; another layer chosen mid-drag, nothing. #[test] diff --git a/docs/user-guide/cartography.md b/docs/user-guide/cartography.md index a133526397..f4c2ab56ce 100644 --- a/docs/user-guide/cartography.md +++ b/docs/user-guide/cartography.md @@ -695,12 +695,13 @@ Turn labels on per layer (Labeling › Labels › **Label**), then: win over the towns' without moving the river layer above them, which would change what draws on top (`labels.placement={"layer": "Rivers", "weight": 8}`; a fraction is rounded and a number past 0 or 10 brought to it, with a notice; `null` is 5). -- **Obstacle weight**: how much labels keep off a layer's points (0 to 10, 0 to start with: no - obstacle). A label whose class's **Weight** isn't above it isn't placed over one of those - points: it tries its other places where its class has them or its callout, else it is left out - and shows in View Unplaced. A heavier label goes over them. A label never keeps off its own - feature's point, nor, for a point's label, one in the same place on the screen in any layer (an - annotation goes over the point it came from); it keeps off the other points, of its layer too. +- **Obstacle weight** (Labeling pane › Position, under Weight, for a layer with points): how + much labels keep off a layer's points (0 to 10, 0 to start with: no obstacle). A label whose + class's **Weight** isn't above it isn't placed over one of those points: it tries its other + places where its class has them or its callout, else it is left out and shows in View Unplaced. + A heavier label goes over them. A label never keeps off its own feature's point, nor, for a + point's label, one in the same place on the screen in any layer (an annotation goes over the + point it came from); it keeps off the other points, of its layer too. Only the points drawn count (a clustered or heat map layer draws none), whether or not their layer's labels are on; vector tile labels keep off them too. Points that show nothing (a wholly transparent layer or symbol) are none. Points in one point of the screen count as one, so a @@ -709,7 +710,8 @@ Turn labels on per layer (Labeling › Labels › **Label**), then: fewer than 250,000): past that, the layer's remaining points aren't kept off, and a notice says how many. Points a layer mask hides still count. The points of a graphics layer (its text and images) are no obstacle. A label moved by hand, one with Show all labels and locked labels are - drawn where they go, over them or not. Layers with points only for now, set by command + drawn where they go, over them or not. Layers with points only for now; the row is the same for + every class of the layer, and a command sets it too (`labels.placement={"layer": "Hospitals", "obstacleWeight": 5}`; a fraction is rounded and a number past 0 or 10 brought to it, with a notice; `0` or `null` turns it off). - **Clearance** (Labeling pane › Position): the room, in points, a class's labels keep free of