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

Modern-Apps.git / scripts / maps / osm_ingest / src / chains_s1.rs

chains_s1.rs108 lines · 4.2 KiBRaw
1/// Cut `path` into runs that each fit one edge's geometry.
2///
3/// Returns inclusive index ranges. A run of two nodes is always allowed even if
4/// its single segment is too long to delta-encode: the caller stores no geometry
5/// for it, and the reader's straight-chord fallback is then exactly right,
6/// because a two-node run *is* a straight chord.
7fn split_runs(path: &[u32], coords: &[geom::Pt]) -> Vec<(usize, usize)> {
8 let point = |n: u32| coords[n as usize];
9 let mut runs = Vec::new();
10 let mut start = 0usize;
11 let mut cost = 1u64;
12 let mut i = 1usize;
13 while i < path.len() {
14 let step = geom::segment_points(point(path[i - 1]), point(path[i]));
15 if cost + step <= u64::from(geom::MAX_POINTS) {
16 cost += step;
17 i += 1;
18 continue;
19 }
20 if i - 1 > start {
21 // Close the run before this segment and retry it with a fresh budget
22 // starting at the shared node.
23 runs.push((start, i - 1));
24 start = i - 1;
25 cost = 1;
26 } else {
27 // One segment that cannot be encoded at all, even alone.
28 runs.push((start, i));
29 start = i;
30 cost = 1;
31 i += 1;
32 }
33 }
34 if start < path.len() - 1 {
35 runs.push((start, path.len() - 1));
36 }
37 runs
38}
39
40/// Does dense node `n` survive as a graph node?
41///
42/// A chain endpoint always does. Anything of degree 1 or 3+ is a junction, a
43/// dead end or an anchor the walks must address. Degree 2 is the only
44/// collapsible case and is handled by the walk, not here.
45///
46/// Degree 0 is dropped unless it carries a transit stop: a node with no
47/// incident segment is a way the region filter removed (every road node outside
48/// the region reads degree 0 once its ways are skipped), dead weight in
49/// `nodes.bin` with no edge ever addressing it. An isolated stop is the one
50/// exception — the reconnect pass addresses stop nodes directly.
51#[inline]
52pub(crate) fn survives(endpoints: &Bitset, degree: &[u8], stop: &Bitset, n: u32) -> bool {
53 if endpoints.get(u64::from(n)) {
54 return true;
55 }
56 let d = degree[n as usize];
57 d != 2 && (d != 0 || stop.get(u64::from(n)))
58}
59
60// ---- the spill -----------------------------------------------------------
61//
62// Chains are the last structure in the build that scales with the road network
63// rather than with the node set, and every stage after them is a sequential scan.
64// So they go to two flat files and the stages read them back: the CSR build, the
65// reconnect BFS and each write round all stream, and none of them needs the chain
66// set resident.
67//
68// Two properties are worth the redundancy in the record:
69//
70// * **The header is fixed size**, so the chain count is the file's length divided
71// by the record size, and any stage can seek straight to chain *k* and process a
72// contiguous slice of chains without reading the ones before it.
73// * **`chains.pts` holds coordinates, not node ids**, so `coords` — the largest
74// array in the build, and the only one indexed by *marked* rather than surviving
75// node — can be freed the moment the spill is written. The cost is that the two
76// endpoint node ids have to be repeated in the header, which is eight bytes a
77// chain against gigabytes of coordinates.
78
79/// Bytes per record in `chains.hdr`. The trailing padding is reserved and written
80/// as zero, so the record can grow a field without changing the stride.
81pub(crate) const CHAIN_REC_BYTES: u64 = 48;
82
83/// Bytes per point in `chains.pts`: `i32 lat_e7`, `i32 lon_e7`.
84const CHAIN_PT_BYTES: u64 = 8;
85
86/// One chain, as `chains.hdr` stores it.
87#[derive(Clone, Copy)]
88pub(crate) struct ChainRec {
89 /// First point's index in `chains.pts`.
90 pub pts_start: u64,
91 pub pts_len: u32,
92 /// Dense id of the first point. Kept even though the point itself is stored,
93 /// because the writer needs the *node* to map to a final id and the
94 /// coordinates cannot be mapped back to one.
95 pub first: u32,
96 /// Dense id of the last point.
97 pub last: u32,
98 pub dist_mm: u32,
99 pub name_offset: u32,
100 pub fwd_lane_off: u32,
101 pub bwd_lane_off: u32,
102 pub fwd_lane_count: u16,
103 pub bwd_lane_count: u16,
104 pub type_: u8,
105 pub speed_limit: u8,
106 pub oneway: bool,
107}
108