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

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

graph_build_part2_s1.rs170 lines · 6.1 KiBRaw
1fn way_blob(
2 state: &mut WayPass,
3 block: &pbf::PrimitiveBlock,
4 index: &NodeIndex,
5 coords: &[geom::Pt],
6 filter: RegionFilter,
7) -> Result<u8> {
8 let mut kinds = 0u8;
9 visit_block(block, KIND_WAYS, &mut kinds, &mut |el: Element| {
10 let Element::Way(w) = el else {
11 return Ok(());
12 };
13 let type_ = tags::get_hw_id(w.tags.get_str("highway"));
14 if type_ == 0 {
15 return Ok(());
16 }
17 // Region filter: keep the way when ANY node touches the box (the
18 // `complete_ways` rule — truncating at the boundary would invent a
19 // vertex). Coordinates are resolved by now, so this is a direct test.
20 if let Some(b) = filter.as_ref() {
21 let mut touches = false;
22 for r in w.refs {
23 if let Some(d) = index.dense(*r) {
24 let (lat_e7, lon_e7) = coords[d as usize];
25 if b.contains_e7(lat_e7, lon_e7) {
26 touches = true;
27 break;
28 }
29 }
30 }
31 if !touches {
32 return Ok(());
33 }
34 }
35 let attrs = way_attrs(&w, type_, &mut state.lanes, &mut state.names);
36 for pair in w.refs.windows(2) {
37 // The same predicate the within-way path uses, so the two collapse
38 // paths agree on what a segment is. Asking a different question here
39 // would make the reference path emit a zero-length self-loop for a
40 // repeated ref, and the byte-identity comparison between the two would
41 // stop meaning anything.
42 let Some((u, v)) = chains::segment(index, pair[0], pair[1]) else {
43 continue;
44 };
45 let (a, b) = (coords[u as usize], coords[v as usize]);
46 state.segs.push(Seg {
47 u,
48 v,
49 dist_mm: accurate_dist_mm(a.0, a.1, b.0, b.1),
50 name_offset: attrs.name,
51 type_: attrs.type_,
52 speed_limit: attrs.speed_limit,
53 oneway: attrs.oneway,
54 fwd_lane_off: attrs.fwd_lane_off,
55 fwd_lane_count: attrs.fwd_lane_count,
56 bwd_lane_off: attrs.bwd_lane_off,
57 bwd_lane_count: attrs.bwd_lane_count,
58 });
59 }
60 Ok(())
61 })?;
62 Ok(kinds)
63}
64
65/// Lane masks are pushed into `lanes` and the name interned into `names`, both
66/// chunk-local: the sink rebases the offsets when it folds the chunk in.
67pub(crate) fn way_attrs(
68 w: &crate::osm::WayView,
69 type_: u8,
70 lanes: &mut Vec<u16>,
71 names: &mut LocalNames,
72) -> WayAttrs {
73 let name = match w.tags.get("name") {
74 Some(n) => names.id(n),
75 None => u32::MAX,
76 };
77 let speed_limit = tags::parse_maxspeed(w.tags.get_str("maxspeed"));
78 let oneway = w.tags.get_str("oneway") == Some("yes");
79
80 // Forward lanes come from turn:lanes:forward, or from plain turn:lanes on a
81 // oneway; backward from turn:lanes:backward. Plain `lanes*` only refine the
82 // count. No turn:lanes at all means no lane data, and the router infers lanes
83 // from junction topology instead.
84 let tl = w.tags.get_str("turn:lanes");
85 let tlf = w.tags.get_str("turn:lanes:forward");
86 let tlb = w.tags.get_str("turn:lanes:backward");
87 let count_all = tags::parse_int_tag(w.tags.get_str("lanes"));
88 let count_fwd = tags::parse_int_tag(w.tags.get_str("lanes:forward"));
89 let count_bwd = tags::parse_int_tag(w.tags.get_str("lanes:backward"));
90
91 let fwd_spec = tlf.or(if oneway { tl } else { None });
92 let fwd_hint = if count_fwd > 0 {
93 count_fwd
94 } else if oneway {
95 count_all
96 } else {
97 0
98 };
99 let bwd_spec = if oneway { None } else { tlb };
100 let fwd = tags::build_dir_lanes(fwd_spec, fwd_hint);
101 let bwd = tags::build_dir_lanes(bwd_spec, count_bwd);
102
103 let mut push_lanes = |masks: &[u16]| -> (u32, u16) {
104 if masks.is_empty() {
105 return (NO_LANES, 0);
106 }
107 let off = lanes.len() as u32;
108 lanes.extend_from_slice(masks);
109 (off, masks.len() as u16)
110 };
111 let (fwd_lane_off, fwd_lane_count) = push_lanes(&fwd);
112 let (bwd_lane_off, bwd_lane_count) = push_lanes(&bwd);
113
114 WayAttrs {
115 name,
116 type_,
117 speed_limit,
118 oneway,
119 fwd_lane_off,
120 fwd_lane_count,
121 bwd_lane_off,
122 bwd_lane_count,
123 }
124}
125
126fn pass2_blob(state: &mut Pass2, block: &pbf::PrimitiveBlock, mask: &Bitset) -> Result<u8> {
127 let mut kinds = 0u8;
128 visit_block(block, KIND_NODES, &mut kinds, &mut |el: Element| {
129 if let Element::Node(n) = el {
130 if n.id < 0 || n.id as u64 >= BITSET_SIZE || !mask.get(n.id as u64) {
131 return Ok(());
132 }
133 let dense = mask.dense(n.id as u64);
134 // A `gtfs:stop_code:<feed>` tag means this node is a matched GTFS
135 // stop; its `name` (or the code itself) becomes the stop label the
136 // transit planner shows. Otherwise fall back to the plain OSM stop
137 // tags.
138 let gtfs_code = n
139 .tags
140 .iter()
141 .find(|(k, _)| k.starts_with(b"gtfs:stop_code:"))
142 .map(|(_, v)| n.tags.get("name").unwrap_or(v));
143 let stop_code = match gtfs_code {
144 Some(code) => Some(code),
145 None => tags::is_stop_node(
146 n.tags.get_str("highway"),
147 n.tags.get_str("railway"),
148 n.tags.get_str("public_transport"),
149 )
150 .then(|| n.tags.get("name").unwrap_or(UNNAMED_STOP)),
151 };
152 state.locs.push((dense, n.lat_e7, n.lon_e7));
153 if let Some(code) = stop_code {
154 state.stops.push((dense, code.to_vec()));
155 }
156 }
157 Ok(())
158 })?;
159 Ok(kinds)
160}
161
162/// What one stop-reconnection pass did, for [`Stats`] and for the log line that
163/// makes [`MIN_ROUTABLE_COMPONENT`] auditable.
164struct Reconnected {
165 lcc_size: u64,
166 /// Bidirectional connectors, sorted by `(source, target)`.
167 synth: Vec<Synth>,
168 already_connected: usize,
169 unreachable: usize,
170}