Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / gtfs_ingest / src / index_part7.rs
index_part7.rs67 lines · 2.9 KiBRaw
| 1 | /// The RAIL_LINES section: every line the basemap's rail layer draws, as `transit_shapes` reads it |
| 2 | /// out of the raw feeds, so that layer can be rebuilt from the pack alone. |
| 3 | /// |
| 4 | /// Host-only. The device never reads it, and `mamaps_pack` writes it out of the app archive, so |
| 5 | /// it is self-contained (its own strings and blobs) rather than adding to STRINGS or SHAPE_COORDS, |
| 6 | /// which ship. Layout: |
| 7 | /// |
| 8 | /// ```text |
| 9 | /// u32 line_count |
| 10 | /// line_count x { u32 feed_idx, route_type, color (0 = none), name_off, route_id_off, blob_off } |
| 11 | /// u32 strings_len, strings (NUL-terminated; offsets above index it, NONE = empty) |
| 12 | /// blobs (shapes::encode, deduplicated; blob_off indexes from here) |
| 13 | /// ``` |
| 14 | /// |
| 15 | /// Lines are in feed order, then route_id, then shape_id ascending: the order the raw path builds |
| 16 | /// them in, which is what its order-dependent dedup needs to reach the same output. |
| 17 | struct RailLines { |
| 18 | records: Vec<u8>, |
| 19 | count: u32, |
| 20 | pool: StringPool, |
| 21 | blobs: ShapeBlobs, |
| 22 | } |
| 23 | |
| 24 | pub const RAIL_LINE_REC_BYTES: usize = 24; |
| 25 | |
| 26 | impl RailLines { |
| 27 | fn new() -> RailLines { |
| 28 | RailLines { records: Vec::new(), count: 0, pool: StringPool::new(), blobs: ShapeBlobs::new() } |
| 29 | } |
| 30 | |
| 31 | /// Every whole, unsimplified shape of every rail route in one feed. |
| 32 | fn add_feed(&mut self, feed_idx: u32, feed: &FeedView) -> Result<(), String> { |
| 33 | let Some(shapes) = feed.shapes else { return Ok(()) }; |
| 34 | let rail = crate::rail::rail_routes(feed.routes); |
| 35 | for (route_id, shape_ids) in &crate::rail::shape_ids_by_route(feed.trips, &rail) { |
| 36 | let (_, color, fallback, name, route_type) = &rail[route_id.as_str()]; |
| 37 | let raw_color = if *fallback { 0 } else { *color }; |
| 38 | for shape_id in shape_ids { |
| 39 | let Some(shape) = shapes.get(shape_id.as_str()) else { continue }; |
| 40 | if shape.lat_e7.len() < 2 { |
| 41 | continue; |
| 42 | } |
| 43 | let points: Vec<(i32, i32)> = |
| 44 | shape.lat_e7.iter().zip(&shape.lon_e7).map(|(&lat, &lon)| (lat, lon)).collect(); |
| 45 | let blob_off = self.blobs.intern(&shapes::encode(&points))?; |
| 46 | for v in [feed_idx, *route_type, raw_color, self.pool.intern(name), self.pool.intern(route_id), blob_off] { |
| 47 | append_u32(&mut self.records, v); |
| 48 | } |
| 49 | self.count += 1; |
| 50 | } |
| 51 | } |
| 52 | if self.pool.overflowed { |
| 53 | return Err("RAIL_LINES strings passed 4 GiB".to_string()); |
| 54 | } |
| 55 | Ok(()) |
| 56 | } |
| 57 | |
| 58 | fn into_section(self) -> Vec<u8> { |
| 59 | let mut out = Vec::with_capacity(8 + self.records.len() + self.pool.bytes.len() + self.blobs.bytes.len()); |
| 60 | append_u32(&mut out, self.count); |
| 61 | out.extend_from_slice(&self.records); |
| 62 | append_u32(&mut out, self.pool.bytes.len() as u32); |
| 63 | out.extend_from_slice(&self.pool.bytes); |
| 64 | out.extend_from_slice(&self.blobs.bytes); |
| 65 | out |
| 66 | } |
| 67 | } |