Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / osm_ingest / src / bin / road_graph.rs
road_graph.rs149 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 | |
| 6 | use std::process::ExitCode; |
| 7 | |
| 8 | const 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. |
| 24 | fn 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!(" {n:>15} {tail:>15} {:>12.6}% {:>12.4}", p * 100.0, p * (n as f64).sqrt()); |
| 45 | } |
| 46 | println!(); |
| 47 | println!("max |target - source| {}", c.max_delta); |
| 48 | println!( |
| 49 | "i16 delta escapes {} ({:.4}% of edges)", |
| 50 | c.delta_escapes, |
| 51 | c.delta_escapes as f64 / e * 100.0 |
| 52 | ); |
| 53 | println!( |
| 54 | "u24 dist_mm escapes {} ({:.6}% of edges), max dist_mm {} mm", |
| 55 | c.dist_escapes, |
| 56 | c.dist_escapes as f64 / e * 100.0, |
| 57 | c.max_dist_mm |
| 58 | ); |
| 59 | println!( |
| 60 | "merged escape rows {} ({:.4}% of edges, {} bytes of table)", |
| 61 | c.escapes, |
| 62 | c.escapes as f64 / e * 100.0, |
| 63 | c.escapes * 12 |
| 64 | ); |
| 65 | println!(); |
| 66 | println!("max out-degree {}", c.max_degree); |
| 67 | println!( |
| 68 | "degree-0 nodes {} ({:.4}% of nodes)", |
| 69 | c.degree_zero_nodes, |
| 70 | c.degree_zero_nodes as f64 / stats.node_count.max(1) as f64 * 100.0 |
| 71 | ); |
| 72 | println!( |
| 73 | "named edges {} ({:.4}% of edges)", |
| 74 | c.named_edges, |
| 75 | c.named_edges as f64 / e * 100.0 |
| 76 | ); |
| 77 | println!( |
| 78 | "name pool {} byte(s) in {} unique name(s)", |
| 79 | stats.name_bytes, stats.unique_names |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | fn main() -> ExitCode { |
| 84 | let args: Vec<String> = std::env::args().skip(1).collect(); |
| 85 | let (input, out_dir, opts) = match osm_ingest::graph_build::parse_args(&args) { |
| 86 | Ok(v) => v, |
| 87 | Err(e) => { |
| 88 | eprintln!("ERROR: {e}\n{USAGE}"); |
| 89 | return ExitCode::FAILURE; |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | let want_stats = opts.stats; |
| 94 | match osm_ingest::graph_build::build_with(&input, &out_dir, opts) { |
| 95 | Ok(stats) => { |
| 96 | println!( |
| 97 | "{} node(s), {} edge(s), {} unique name(s) ({} bytes), largest component \ |
| 98 | {} node(s)", |
| 99 | stats.node_count, |
| 100 | stats.edge_count, |
| 101 | stats.unique_names, |
| 102 | stats.name_bytes, |
| 103 | stats.lcc_size, |
| 104 | ); |
| 105 | // The three counters that make MIN_ROUTABLE_COMPONENT auditable. A |
| 106 | // large "found nothing" is the signal that the threshold is too high. |
| 107 | println!( |
| 108 | "Transit stops: {} already on a routable component, {} reconnected, \ |
| 109 | {} found nothing", |
| 110 | stats.stops_already_connected, stats.reconnected_stops, stats.stops_unreachable |
| 111 | ); |
| 112 | println!( |
| 113 | "Compaction: {} -> {} node(s) ({:.2}x), {} -> {} directed edge(s) ({:.2}x), \ |
| 114 | {} chain(s) split for the 256-point limit", |
| 115 | stats.raw_node_count, |
| 116 | stats.node_count, |
| 117 | stats.raw_node_count as f64 / stats.node_count.max(1) as f64, |
| 118 | stats.raw_edge_count, |
| 119 | stats.edge_count, |
| 120 | stats.raw_edge_count as f64 / stats.edge_count.max(1) as f64, |
| 121 | stats.chain_splits |
| 122 | ); |
| 123 | println!( |
| 124 | "intermediate.bin: {} byte(s), {} edge(s) store a polyline, \ |
| 125 | {} defer to a twin", |
| 126 | stats.intermediate_bytes, stats.geometry_edges, stats.reversed_edges |
| 127 | ); |
| 128 | println!( |
| 129 | "edges.bin: {} byte(s), {} edge(s) needed the escape table ({:.4}%), \ |
| 130 | {} named ({:.2}%)", |
| 131 | stats.edges_bytes, |
| 132 | stats.escape_count, |
| 133 | stats.escape_count as f64 / stats.edge_count.max(1) as f64 * 100.0, |
| 134 | stats.named_edges, |
| 135 | stats.named_edges as f64 / stats.edge_count.max(1) as f64 * 100.0 |
| 136 | ); |
| 137 | println!("Peak RSS: {}", osm_ingest::mem::peak_rss_report()); |
| 138 | println!("Wrote {}", out_dir.display()); |
| 139 | if want_stats { |
| 140 | print_census(&stats); |
| 141 | } |
| 142 | ExitCode::SUCCESS |
| 143 | } |
| 144 | Err(e) => { |
| 145 | eprintln!("ERROR: {e}"); |
| 146 | ExitCode::FAILURE |
| 147 | } |
| 148 | } |
| 149 | } |