Repository navigation
fix(labels): name locked labels by feature id, so a lock holds through edits that move rows #1134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1838,19 +1838,22 @@ fn visit_complete( | |
|
|
||
| /// A label waiting to be placed: the label, its priority and its key, and where else it may go | ||
| /// when its own place is taken (#1011): its `x`, `y` there, tried in order. Whatever moves the | ||
| /// label on its way to the screen moves them too, as it does its icon. | ||
| /// label on its way to the screen moves them too, as it does its icon. `row` is its feature's | ||
| /// row where it was offered: only a hint, checked against the key's feature id before use | ||
| /// (a streamed layer's rows are a chunk's), `usize::MAX` for none. | ||
| #[derive(Clone, Debug)] | ||
| struct Candidate { | ||
| label: PlacedLabel, | ||
| priority: f64, | ||
| key: rustgis_map::LabelKey, | ||
| alternatives: Vec<(f32, f32)>, | ||
| row: usize, | ||
| } | ||
|
|
||
| /// A label with no alternatives. | ||
| /// A label with no alternatives and no row. | ||
| impl From<(PlacedLabel, f64, rustgis_map::LabelKey)> for Candidate { | ||
| fn from((label, priority, key): (PlacedLabel, f64, rustgis_map::LabelKey)) -> Candidate { | ||
| Candidate { label, priority, key, alternatives: Vec::new() } | ||
| Candidate { label, priority, key, alternatives: Vec::new(), row: usize::MAX } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -2295,12 +2298,24 @@ fn raise_labels(map: &Map, view: &View, candidates: &mut [Candidate]) { | |
| let flat = map.flat(); | ||
| let cam = view.camera; | ||
| let (w, h) = (view.width, view.height); | ||
| for Candidate { label: l, key: (id, row, _, _), alternatives, .. } in candidates.iter_mut() { | ||
| // Each extruded layer's rows by feature id, the label keys naming features by id (#1046). | ||
| let mut rows: std::collections::HashMap<u32, std::collections::HashMap<usize, usize>> = std::collections::HashMap::new(); | ||
| for Candidate { label: l, key: (id, fid, _, _), alternatives, row: offered, .. } in candidates.iter_mut() { | ||
| let Some(layer) = map.layer(*id).filter(|l| l.geometry_type() == GeometryType::Polygon) else { continue }; | ||
| let Some(x) = &layer.extrusion else { continue }; | ||
| let features = &layer.dataset.features; | ||
| // The row it was offered from while its feature is there, else looked up by id; one not | ||
| // among them, as a streamed layer's, rises by the height no field gives. | ||
| let row = Some(*offered) | ||
|
jgregoriorincon marked this conversation as resolved.
Resolved
|
||
| .filter(|&r| features.get(r).is_some_and(|f| f.id as usize == *fid)) | ||
| .or_else(|| { | ||
| let by_id = rows.entry(*id).or_insert_with(|| features.iter().enumerate().map(|(r, f)| (f.id as usize, r)).collect()); | ||
|
jgregoriorincon marked this conversation as resolved.
Resolved
|
||
| by_id.get(fid).copied() | ||
| }) | ||
| .unwrap_or(usize::MAX); | ||
| let Some(m) = cam.try_to_map(f64::from(l.x), f64::from(l.y), w, h) else { continue }; | ||
| // At the label's place (Web Mercator stretches the ground toward the poles). | ||
| let up = (effects::extrusion_metres(&layer.dataset, x, *row) | ||
| let up = (effects::extrusion_metres(&layer.dataset, x, row) | ||
| / (flat.ground_per_unit(m) * map.body().ratio()).max(1e-9) | ||
| / cam.res.max(1e-9)) | ||
| .clamp(0.0, 1e7); | ||
|
|
@@ -3282,7 +3297,9 @@ fn render_frame(map: &mut Map, view: &View, full_res_rasters: bool, backend: Bac | |
| let layer: &rustgis_map::Layer = layer; | ||
| // Its symbol by expression, compiled once for the drawing (none: the layer's own). | ||
| let styler = layer.styler(Some(scale)); | ||
| let label_row = |row: usize| if complete_source { layer.dataset.features.get(row).map_or(row, |f| f.id as usize) } else { row }; | ||
| // A label is named by its feature's id, not its row: a lock holds through edits that | ||
| // move rows (#1046). | ||
| let label_id = |row: usize| layer.dataset.features.get(row).map_or(row, |f| f.id as usize); | ||
|
jgregoriorincon marked this conversation as resolved.
Resolved
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Possible stale lock after a delete and an add (confidence: medium, about 65%). The key now relies on feature ids being stable and never reused while a lock is held. A possible fix is a monotonic high-water mark on |
||
| let (labels, selection, ds_ptr) = (&layer.labels, &layer.selection, &layer.dataset); | ||
| // The globe draws the lon/lat geometry itself; the flat map its Web Mercator copy. | ||
| let geoms: Vec<Option<&Geometry>> = if globe.is_some() { | ||
|
|
@@ -3635,7 +3652,7 @@ fn render_frame(map: &mut Map, view: &View, full_res_rasters: bool, backend: Bac | |
| for (ci, class) in classes.classes_of(ds_ptr, row) { | ||
| // Locked, only the labels placed then: one class's, not every class's (and | ||
| // along a line, only its repeats placed then, below). | ||
| if locked_classes.as_ref().is_some_and(|k| !k.contains(&(lid, label_row(row), ci))) { | ||
| if locked_classes.as_ref().is_some_and(|k| !k.contains(&(lid, label_id(row), ci))) { | ||
| continue; | ||
| } | ||
| if point.is_none() && !along_line(class.placement, g) { | ||
|
|
@@ -3718,7 +3735,7 @@ fn render_frame(map: &mut Map, view: &View, full_res_rasters: bool, backend: Bac | |
| let pri = (n_layers - li) as f64 * 1e12 - ci as f64 * 1e9 + area; | ||
| let last = places.len().saturating_sub(1); | ||
| for (n, (i, mut lx, mut ly, angle, glyphs)) in places.into_iter().enumerate() { | ||
| let key = (lid, label_row(row), ci, i); | ||
| let key = (lid, label_id(row), ci, i); | ||
|
jgregoriorincon marked this conversation as resolved.
Resolved
jgregoriorincon marked this conversation as resolved.
Resolved
|
||
| // Locked, only the repeats placed then. | ||
| if lock.as_ref().is_some_and(|k| !k.contains_key(&key)) { | ||
| continue; | ||
|
|
@@ -3767,6 +3784,7 @@ fn render_frame(map: &mut Map, view: &View, full_res_rasters: bool, backend: Bac | |
| priority: pri - i as f64, | ||
| key, | ||
| alternatives, | ||
| row, | ||
| }); | ||
| } | ||
| } | ||
|
|
@@ -5018,7 +5036,8 @@ mod tests { | |
| #[test] | ||
| fn labels_try_their_alternatives() { | ||
| let blocker = || Candidate::from((label("Blocker", 100.0, None, 0.0), 9.0, (u32::MAX, 0, 0, 0))); | ||
| let taken = |alternatives| Candidate { label: label("Taken", 100.0, None, 0.0), priority: 1.0, key: (1, 0, 0, 0), alternatives }; | ||
| let taken = | ||
| |alternatives| Candidate { label: label("Taken", 100.0, None, 0.0), priority: 1.0, key: (1, 0, 0, 0), alternatives, row: 0 }; | ||
| let at = |p: &Placement| p.labels.iter().map(|l| (l.text.clone(), l.x, l.y)).collect::<Vec<_>>(); | ||
| // None: left out, as before. | ||
| let p = place_labels(vec![blocker(), taken(Vec::new())], Vec::new(), 400.0, 200.0, false); | ||
|
|
@@ -5049,6 +5068,7 @@ mod tests { | |
| priority: 1.0, | ||
| key: (1, 0, 0, 0), | ||
| alternatives, | ||
| row: 0, | ||
| }; | ||
| let c = tilt_candidate(offer((10.0, 50.0), vec![(10.0, 60.0), (10.0, 150.0), (20.0, 160.0)]), &at).unwrap(); | ||
| assert_eq!((c.label.x, c.label.y), (1010.0, 150.0)); | ||
|
|
@@ -5096,7 +5116,7 @@ mod tests { | |
| let (x, y) = ((i % 200) as f32 * 10.0, (i / 200) as f32 * 10.0); | ||
| let l = PlacedLabel { x, y, ..label("ab", x, None, 0.0) }; | ||
| let alternatives = if others { round.map(|(dx, dy)| (x + dx, y + dy)).to_vec() } else { Vec::new() }; | ||
| Candidate { label: l, priority: -f64::from(i), key: (u32::MAX, i as usize, 0, 0), alternatives } | ||
| Candidate { label: l, priority: -f64::from(i), key: (u32::MAX, i as usize, 0, 0), alternatives, row: usize::MAX } | ||
| }) | ||
| .collect() | ||
| }; | ||
|
|
@@ -6397,6 +6417,40 @@ mod tests { | |
| assert!((shapes[0].height / 200_000.0 - 1.0).abs() < 0.05, "{}", shapes[0].height); | ||
| } | ||
|
|
||
| /// A label's key names its feature by id (#1046): an extruded feature's label rises by its own | ||
| /// height, also once its row no longer follows its id. | ||
| #[test] | ||
| fn labels_rise_by_their_own_features_height() { | ||
| let mut m = Map::new("Map"); | ||
| let mut d = Dataset::new("blocks", GeometryType::Polygon, vec![Field::new("h", FieldType::Double)]); | ||
| let s = 0.1; | ||
| let ring = vec![Coord::new(-s, -s), Coord::new(s, -s), Coord::new(s, s), Coord::new(-s, s), Coord::new(-s, -s)]; | ||
| for h in [0.0, 100_000.0, 0.0] { | ||
| d.push(Some(Geometry::Polygon(vec![vec![ring.clone()]])), vec![Value::Float(h)]); | ||
| } | ||
| // The first one deleted: the tall one (id 2) is row 0 and the low one (id 3) row 1. | ||
| d.features.remove(0); | ||
| let id = m.add_dataset(d); | ||
| m.layer_mut(id).unwrap().extrusion = Some(rustgis_map::effects::Extrusion { | ||
| field: Some("h".into()), | ||
| height: 0.0, | ||
| factor: 1.0, | ||
| face_culling: true, | ||
| lighting: true, | ||
| shadows: true, | ||
| }); | ||
| let camera = Camera { center: Coord::new(0.0, 0.0), res: 2000.0, bearing: 0.0, pitch: 60.0 }; | ||
| let view = View { width: 400.0, height: 300.0, pixels_per_point: 1.0, camera, sky: false }; | ||
| let at_centre = |text: &str| PlacedLabel { y: 150.0, ..label(text, 200.0, None, 0.0) }; | ||
| // The tall one with no row, looked up by id; the low one offered from row 0 before the | ||
| // tall one took it there: the row is a hint the id overrules. | ||
| let low = Candidate { row: 0, ..(at_centre("Low"), 1.0, (id, 3, 0, 0)).into() }; | ||
| let mut c: Vec<Candidate> = vec![(at_centre("Tall"), 1.0, (id, 2, 0, 0)).into(), low]; | ||
| raise_labels(&m, &view, &mut c); | ||
| assert!(c[0].label.y < 150.0 - 15.0, "the tall one rises onto its roof: {}", c[0].label.y); | ||
| assert!((c[1].label.y - 150.0).abs() < 1.0, "the low one stays on the ground: {}", c[1].label.y); | ||
| } | ||
|
|
||
| /// Extruded features with junk heights or roof indices (hostile data) give finite geometry | ||
| /// or are left out, never garbage. | ||
| #[test] | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.