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

Modern-Apps.git / scripts / maps / osm_ingest / src / bin / road_graph.rs

road_graph.rs153 lines · 6.0 KiBRaw
1//! `road_graph IN.osm.pbf [--out DIR]` — build the single global routing graph.
2//!
3//! Replaces `scripts/maps/generator.cpp`. See `osm_ingest::graph_build` for the
4//! on-disk contract.
5
6use std::process::ExitCode;
7
8const USAGE: &str = "Usage: road_graph IN.osm.pbf [--out DIR] [--reference-collapse] \
9 [--rounds N] [--spill-dir DIR] [--spill-pts-dir DIR] [--threads N] \
10 [--region california|world] [--stats] (default --out map_data, --rounds 1, \
11 streaming within-way collapse; --reference-collapse opts into the \
12 whole-graph reference path)";
13
14/// Print what a narrower `edges.bin` record would cost on this extract.
15///
16/// The `c` column is the point of the table. A single escape rate measured on one
17/// region says nothing about a planet 22x larger, but for nodes ordered along a
18/// space-filling curve the tail decays as `P(|delta| > n) = c * n^(-1/2)`: a level-k
19/// quadrant boundary produces a delta of order `N / 4^k` and is crossed with
20/// probability proportional to `2^k`, and the constant is the ratio of edge length
21/// to node spacing, which does not depend on `N`. So `c` is what transfers between
22/// extracts, and a `c` column that is flat down the rows is the evidence that the
23/// model holds at all.
24fn print_census(stats: &osm_ingest::graph_build::Stats) {
25 let c = &stats.census;
26 let e = stats.edge_count.max(1) as f64;
27 println!();
28 println!("=== edges.bin narrowing census ===");
29 println!("directed edges {}", stats.edge_count);
30 println!("nodes {}", stats.node_count);
31 println!();
32 println!("|target - source| tail (a level-k quadrant boundary costs ~N/4^k):");
33 println!(" n edges > n fraction c = P*sqrt(n)");
34 for k in 0..32u32 {
35 let n = 1u64 << k;
36 let tail = c.delta_tail(k);
37 // Once the tail empties the row says nothing, and the whole point is the
38 // rows either side of the +-32767 boundary.
39 if tail == 0 {
40 println!(" {n:>15} {tail:>15} (tail empty)");
41 break;
42 }
43 let p = tail as f64 / e;
44 println!(
45 " {n:>15} {tail:>15} {:>12.6}% {:>12.4}",
46 p * 100.0,
47 p * (n as f64).sqrt()
48 );
49 }
50 println!();
51 println!("max |target - source| {}", c.max_delta);
52 println!(
53 "i16 delta escapes {} ({:.4}% of edges)",
54 c.delta_escapes,
55 c.delta_escapes as f64 / e * 100.0
56 );
57 println!(
58 "u24 dist_mm escapes {} ({:.6}% of edges), max dist_mm {} mm",
59 c.dist_escapes,
60 c.dist_escapes as f64 / e * 100.0,
61 c.max_dist_mm
62 );
63 println!(
64 "merged escape rows {} ({:.4}% of edges, {} bytes of table)",
65 c.escapes,
66 c.escapes as f64 / e * 100.0,
67 c.escapes * 12
68 );
69 println!();
70 println!("max out-degree {}", c.max_degree);
71 println!(
72 "degree-0 nodes {} ({:.4}% of nodes)",
73 c.degree_zero_nodes,
74 c.degree_zero_nodes as f64 / stats.node_count.max(1) as f64 * 100.0
75 );
76 println!(
77 "named edges {} ({:.4}% of edges)",
78 c.named_edges,
79 c.named_edges as f64 / e * 100.0
80 );
81 println!(
82 "name pool {} byte(s) in {} unique name(s)",
83 stats.name_bytes, stats.unique_names
84 );
85}
86
87fn main() -> ExitCode {
88 let args: Vec<String> = std::env::args().skip(1).collect();
89 let (input, out_dir, opts) = match osm_ingest::graph_build::parse_args(&args) {
90 Ok(v) => v,
91 Err(e) => {
92 eprintln!("ERROR: {e}\n{USAGE}");
93 return ExitCode::FAILURE;
94 }
95 };
96
97 let want_stats = opts.stats;
98 match osm_ingest::graph_build::build_with(&input, &out_dir, opts) {
99 Ok(stats) => {
100 println!(
101 "{} node(s), {} edge(s), {} unique name(s) ({} bytes), largest component \
102 {} node(s)",
103 stats.node_count,
104 stats.edge_count,
105 stats.unique_names,
106 stats.name_bytes,
107 stats.lcc_size,
108 );
109 // The three counters that make MIN_ROUTABLE_COMPONENT auditable. A
110 // large "found nothing" is the signal that the threshold is too high.
111 println!(
112 "Transit stops: {} already on a routable component, {} reconnected, \
113 {} found nothing",
114 stats.stops_already_connected, stats.reconnected_stops, stats.stops_unreachable
115 );
116 println!(
117 "Compaction: {} -> {} node(s) ({:.2}x), {} -> {} directed edge(s) ({:.2}x), \
118 {} chain(s) split for the 256-point limit",
119 stats.raw_node_count,
120 stats.node_count,
121 stats.raw_node_count as f64 / stats.node_count.max(1) as f64,
122 stats.raw_edge_count,
123 stats.edge_count,
124 stats.raw_edge_count as f64 / stats.edge_count.max(1) as f64,
125 stats.chain_splits
126 );
127 println!(
128 "intermediate.bin: {} byte(s), {} edge(s) store a polyline, \
129 {} defer to a twin",
130 stats.intermediate_bytes, stats.geometry_edges, stats.reversed_edges
131 );
132 println!(
133 "edges.bin: {} byte(s), {} edge(s) needed the escape table ({:.4}%), \
134 {} named ({:.2}%)",
135 stats.edges_bytes,
136 stats.escape_count,
137 stats.escape_count as f64 / stats.edge_count.max(1) as f64 * 100.0,
138 stats.named_edges,
139 stats.named_edges as f64 / stats.edge_count.max(1) as f64 * 100.0
140 );
141 println!("Peak RSS: {}", osm_ingest::mem::peak_rss_report());
142 println!("Wrote {}", out_dir.display());
143 if want_stats {
144 print_census(&stats);
145 }
146 ExitCode::SUCCESS
147 }
148 Err(e) => {
149 eprintln!("ERROR: {e}");
150 ExitCode::FAILURE
151 }
152 }
153}