Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
main ▾

Modern-Apps.git / scripts / maps / wikidesc / src / ids.rs

ids.rs80 lines · 3.0 KiBRaw
1//! OSM element ids as `.mamaps` writes them.
2//!
3//! **This must agree with `mamaps_build`'s `extract::tagged_id` exactly.** The archive is looked up
4//! by the id the renderer hands the app — `PlacedLabel.featureId`, which comes from the basemap's
5//! v3 feature-id side table — so an encoding that disagrees produces an archive where every lookup
6//! silently misses. Nothing would error; descriptions would simply never appear.
7//!
8//! The scheme: `(osm_id << 2) | element`, with the element tag in the low two bits. A bare OSM id
9//! does not identify an element, because a node, a way and a relation may all be numbered 123.
10//! Tags start at one so that an all-zero id stays reserved for "no id".
11//!
12//! Duplicated here rather than shared because `mamaps_build` does not expose a library, and a
13//! four-line function behind a new crate dependency would be worse than a four-line function with
14//! a test that states what it must match.
15
16/// Reserved for a feature with no upstream OSM element.
17pub const ID_NONE: u64 = 0;
18
19pub const ELEMENT_NODE: u64 = 1;
20pub const ELEMENT_WAY: u64 = 2;
21pub const ELEMENT_RELATION: u64 = 3;
22
23/// An OSM id tagged with its id space, or [`ID_NONE`] for an id OSM would never issue.
24pub fn tagged_id(id: i64, element: u64) -> u64 {
25 if id <= 0 {
26 return ID_NONE;
27 }
28 ((id as u64) << 2) | element
29}
30
31pub fn node(id: i64) -> u64 {
32 tagged_id(id, ELEMENT_NODE)
33}
34
35pub fn way(id: i64) -> u64 {
36 tagged_id(id, ELEMENT_WAY)
37}
38
39pub fn relation(id: i64) -> u64 {
40 tagged_id(id, ELEMENT_RELATION)
41}
42
43#[cfg(test)]
44mod tests {
45 use super::*;
46
47 /// The whole reason the low bits exist: three different things may share a number.
48 #[test]
49 fn the_same_number_as_a_node_way_and_relation_are_three_ids() {
50 let (n, w, r) = (node(123), way(123), relation(123));
51 assert_ne!(n, w);
52 assert_ne!(w, r);
53 assert_ne!(n, r);
54 assert_eq!(n, (123 << 2) | 1);
55 assert_eq!(w, (123 << 2) | 2);
56 assert_eq!(r, (123 << 2) | 3);
57 }
58
59 /// Pinned against `mamaps_build`'s `extract::tagged_id`. If that changes, this must change with
60 /// it, or every lookup in the archive misses without erroring.
61 #[test]
62 fn the_encoding_matches_what_mamaps_build_writes() {
63 // (id as u64) << 2 | element, tags 1/2/3, id <= 0 -> ID_NONE.
64 assert_eq!(tagged_id(1, ELEMENT_NODE), 5);
65 assert_eq!(tagged_id(1, ELEMENT_WAY), 6);
66 assert_eq!(tagged_id(1, ELEMENT_RELATION), 7);
67 assert_eq!(tagged_id(0, ELEMENT_NODE), ID_NONE);
68 assert_eq!(tagged_id(-5, ELEMENT_WAY), ID_NONE);
69 }
70
71 /// A real OSM id is around 2^33 today; shifted left two it is nowhere near overflowing, and
72 /// nowhere near the sign bit that `PlacedLabel.featureId` (a Kotlin Long) would care about.
73 #[test]
74 fn a_realistic_id_stays_well_inside_the_range() {
75 let big = 12_000_000_000i64;
76 let tagged = way(big);
77 assert_eq!(tagged >> 2, big as u64, "the id survives the round trip");
78 assert!(tagged < i64::MAX as u64, "and stays positive as a Kotlin Long");
79 }
80}