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

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

graph_build_part1_s2.rs91 lines · 3.3 KiBRaw
1/// The reference path: build every segment, then collapse chains across way
2/// boundaries with [`compact`].
3///
4/// Kept as the baseline the within-way path is measured against. Its cost is the
5/// segment array and the incidence CSR over it, which together are the reason a
6/// planet build does not fit.
7#[allow(clippy::too_many_arguments)]
8fn collapse_segments<W: std::io::Write + Send>(
9 input: &Path,
10 spill_dir: &Path,
11 spill_pts_dir: &Path,
12 blobs: &[pbf::BlobLoc],
13 blob_kinds: &[u8],
14 index: &NodeIndex,
15 coords: &[geom::Pt],
16 stop: &Bitset,
17 slots: u32,
18 filter: RegionFilter,
19 pool: &mut NamePool<W>,
20 timer: &mut crate::timing::PhaseTimer,
21) -> Result<Collapsed> {
22 timer.switch("collapse-segments");
23 // One record per consecutive way-node pair, still carrying both directions'
24 // lane data. Splitting into directed edges is deferred until after
25 // compaction, because pairing a `u -> v` edge with its `v -> u` twin is only
26 // unambiguous while they are still one object.
27 let mut segs: Vec<Seg> = Vec::new();
28 let mut lane_pool: Vec<u16> = Vec::new();
29 let way_kinds = pbf::run_pass_sink(
30 input,
31 blobs,
32 pbf::PassSpec { blob_kinds: Some(blob_kinds), want: KIND_WAYS, label: "Pass 3: segments" },
33 WayPass::default,
34 |state, block| way_blob(state, block, index, coords, filter),
35 |chunk| {
36 let name_map = chunk.names.flush(pool).map_err(io_err)?;
37 let lane_base = lane_pool.len() as u32;
38 lane_pool.extend_from_slice(&chunk.lanes);
39 cap_lane_pool(lane_pool.len())?;
40 segs.reserve(chunk.segs.len());
41 for mut s in chunk.segs {
42 s.name_offset = if s.name_offset == u32::MAX {
43 NO_NAME
44 } else {
45 name_map[s.name_offset as usize]
46 };
47 if s.fwd_lane_count > 0 {
48 s.fwd_lane_off += lane_base;
49 }
50 if s.bwd_lane_count > 0 {
51 s.bwd_lane_off += lane_base;
52 }
53 segs.push(s);
54 }
55 Ok(())
56 },
57 )?;
58 report_way_blobs(blobs.len(), &way_kinds);
59
60 let raw_edge_count: u64 = segs.iter().map(|s| if s.oneway { 1u64 } else { 2 }).sum();
61 println!(
62 "Built {} segment(s) ({raw_edge_count} directed edge(s) before compaction)",
63 segs.len()
64 );
65
66 timer.switch("collapse-compact");
67 let comp = compact::compact(slots, &segs, &lane_pool, |n| coords[n as usize], |n| {
68 stop.get(u64::from(n))
69 });
70 drop(segs);
71
72 // A slot with no coordinates has no segments either, so compaction has no
73 // reason to collapse it and would hand it a real node id. Filter it out here
74 // instead: this is the second half of the dangling-reference guard.
75 let mut kept = Bitset::new(u64::from(slots));
76 for i in 0..slots {
77 if comp.new_id[i as usize] != REMOVED && index.present.get(u64::from(i)) {
78 kept.set(u64::from(i));
79 }
80 }
81 let spill = chains::Spill::split(spill_dir, spill_pts_dir);
82 spill.write(&comp.chains, &comp.pts, coords)?;
83 Ok(Collapsed {
84 spill,
85 kept,
86 lane_pool,
87 chain_count: comp.chains.len() as u64,
88 splits: comp.splits,
89 raw_edge_count,
90 })
91}