Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / gtfs_ingest / src / bin / transit_shapes_pack.rs
transit_shapes_pack.rs200 lines · 8.4 KiBRaw
| 1 | //! Lines for `transit_shapes` read from a built `world.transit` pack instead of raw GTFS. |
| 2 | //! |
| 3 | //! A v7 pack's RAIL_LINES section holds exactly what [`read_feed`] takes out of each raw feed: |
| 4 | //! every whole, unsimplified shape of every rail route, with its route's type, colour, name and |
| 5 | //! id, in the same order. So the fold, corridor pass and writer downstream produce the same layer |
| 6 | //! from the pack as from the ~80 GB of feeds it was built from. |
| 7 | |
| 8 | use super::*; |
| 9 | use gtfs_ingest::reader::Reader; |
| 10 | use rayon::prelude::*; |
| 11 | |
| 12 | /// Every RAIL_LINES entry as a candidate line, in pack order. |
| 13 | pub(crate) fn pack_lines(path: &Path) -> Result<FeedLines, String> { |
| 14 | // Only what this reads: the RAIL_LINES records, the feed table and the string pool their |
| 15 | // names live in — about 300 MB of a 4.4 GB world pack. |
| 16 | let pack = Reader::open_sections( |
| 17 | path, |
| 18 | &[ |
| 19 | gtfs_ingest::reader::SEC_STRINGS, |
| 20 | gtfs_ingest::reader::SEC_FEEDS, |
| 21 | gtfs_ingest::reader::SEC_RAIL_LINES, |
| 22 | ], |
| 23 | )?; |
| 24 | let Some(recs) = pack.rail_lines() else { |
| 25 | return Err(format!( |
| 26 | "{} is a v{} pack with no RAIL_LINES section; rebuild it with scripts/maps/build_world_transit.ps1", |
| 27 | path.display(), |
| 28 | pack.version(), |
| 29 | )); |
| 30 | }; |
| 31 | let feeds: Vec<String> = (0..pack.feed_count()).map(|f| pack.feed_name(f)).collect(); |
| 32 | let mut out = FeedLines::default(); |
| 33 | // Built wide (the intercity test walks every line), then taken in pack order, so the first |
| 34 | // bad record is still the one reported. |
| 35 | let built: Vec<Result<Line, String>> = recs |
| 36 | .into_par_iter() |
| 37 | .map(|rec| { |
| 38 | let Some(mode) = mode_of(rec.route_type) else { |
| 39 | return Err(format!( |
| 40 | "RAIL_LINES carries route_type {}, which is not rail", |
| 41 | rec.route_type |
| 42 | )); |
| 43 | }; |
| 44 | let (color, fallback) = match rec.color { |
| 45 | 0 => (fallback_color(mode), true), |
| 46 | c => (c, false), |
| 47 | }; |
| 48 | Ok(Line { |
| 49 | intercity: intercity::is_intercity( |
| 50 | intercity::is_long_distance_type(rec.route_type), |
| 51 | &rec.points, |
| 52 | ), |
| 53 | points: rec.points, |
| 54 | color, |
| 55 | mode, |
| 56 | fallback, |
| 57 | route: rec.name, |
| 58 | route_key: format!("{}\u{0}{}", feeds[rec.feed_idx as usize], rec.route_id), |
| 59 | ordinal: 0, |
| 60 | lanes: 1, |
| 61 | taper: 255, |
| 62 | }) |
| 63 | }) |
| 64 | .collect(); |
| 65 | for line in built { |
| 66 | out.lines.push(line?); |
| 67 | out.shapes_read += 1; |
| 68 | } |
| 69 | Ok(out) |
| 70 | } |
| 71 | |
| 72 | #[cfg(test)] |
| 73 | mod tests { |
| 74 | use super::*; |
| 75 | use gtfs_ingest::index::{FeedDir, IndexBuilder}; |
| 76 | |
| 77 | /// Two feeds sharing a rail alignment plus a bus, through the real pack builder: the layer |
| 78 | /// derived from the pack is byte-identical to the one derived from the feeds. |
| 79 | #[test] |
| 80 | fn a_pack_draws_exactly_the_lines_of_the_feeds_it_was_built_from() { |
| 81 | let root = std::env::temp_dir().join(format!("transit_shapes_pack_{}", std::process::id())); |
| 82 | let feed = |tag: &str, rail_color: &str, lat: &str| -> PathBuf { |
| 83 | let dir = root.join(tag); |
| 84 | std::fs::create_dir_all(&dir).unwrap(); |
| 85 | let w = |f: &str, s: String| std::fs::write(dir.join(f), s).unwrap(); |
| 86 | w("agency.txt", "agency_id,agency_name,agency_url,agency_timezone\nA,A,http://a,America/Los_Angeles\n".into()); |
| 87 | w("stops.txt", format!("stop_id,stop_name,stop_lat,stop_lon\nS1,One,37.700,-122.40\nS2,Two,{lat},-122.40\n")); |
| 88 | w("routes.txt", format!( |
| 89 | "route_id,route_short_name,route_long_name,route_type,route_color\nR,Rail,,2,{rail_color}\nB,Bus,,3,FF0000\n" |
| 90 | )); |
| 91 | w( |
| 92 | "trips.txt", |
| 93 | "route_id,service_id,trip_id,shape_id\nR,WK,T1,SH1\nR,WK,T2,SH2\nB,WK,T3,SH1\n" |
| 94 | .into(), |
| 95 | ); |
| 96 | w( |
| 97 | "stop_times.txt", |
| 98 | "trip_id,arrival_time,departure_time,stop_id,stop_sequence\n\ |
| 99 | T1,08:00:00,08:00:00,S1,1\nT1,08:10:00,08:10:00,S2,2\n\ |
| 100 | T2,09:00:00,09:00:00,S2,1\nT2,09:10:00,09:10:00,S1,2\n\ |
| 101 | T3,10:00:00,10:00:00,S1,1\nT3,10:10:00,10:10:00,S2,2\n" |
| 102 | .into(), |
| 103 | ); |
| 104 | w("calendar.txt", "service_id,monday,tuesday,wednesday,thursday,friday,saturday,sunday,start_date,end_date\nWK,1,1,1,1,1,0,0,20240101,20301231\n".into()); |
| 105 | // A long outbound shape at full precision and a short return with a kink. |
| 106 | w( |
| 107 | "shapes.txt", |
| 108 | format!( |
| 109 | "shape_id,shape_pt_lat,shape_pt_lon,shape_pt_sequence\n\ |
| 110 | SH1,37.6999999,-122.4000001,1\nSH1,37.75,-122.4,2\nSH1,{lat},-122.4,3\n\ |
| 111 | SH2,{lat},-122.4,1\nSH2,37.75,-122.39,2\nSH2,37.7,-122.4,3\n" |
| 112 | ), |
| 113 | ); |
| 114 | dir |
| 115 | }; |
| 116 | let specs: Vec<FeedSpec> = vec![ |
| 117 | ("a".to_string(), feed("a", "0054A5", "37.800"), String::new()), |
| 118 | ("b".to_string(), feed("b", "", "39.500"), String::new()), |
| 119 | ]; |
| 120 | let mut builder = IndexBuilder::new("test"); |
| 121 | for (name, dir, prefix) in &specs { |
| 122 | let table = |f: &str| gtfs::read_table(dir, f); |
| 123 | let (stops, routes, trips) = ( |
| 124 | table("stops.txt").unwrap(), |
| 125 | table("routes.txt").unwrap(), |
| 126 | table("trips.txt").unwrap(), |
| 127 | ); |
| 128 | let (calendar, agency, shapes) = |
| 129 | (table("calendar.txt"), table("agency.txt"), gtfs::read_shapes(dir)); |
| 130 | builder |
| 131 | .add_feed_dir(&FeedDir { |
| 132 | name: name.clone(), |
| 133 | motis_prefix: prefix.clone(), |
| 134 | dir, |
| 135 | stops: &stops, |
| 136 | routes: &routes, |
| 137 | trips: &trips, |
| 138 | calendar: calendar.as_ref(), |
| 139 | calendar_dates: None, |
| 140 | agency: agency.as_ref(), |
| 141 | shapes: shapes.as_ref(), |
| 142 | }) |
| 143 | .unwrap(); |
| 144 | } |
| 145 | let mut blob = Vec::new(); |
| 146 | builder.finish_to(&mut blob).unwrap(); |
| 147 | let pack = root.join("test.transit"); |
| 148 | std::fs::write(&pack, &blob).unwrap(); |
| 149 | |
| 150 | let (from_feeds, from_pack) = (root.join("feeds.geojsonseq"), root.join("pack.geojsonseq")); |
| 151 | super::super::extra2::run_with_timer( |
| 152 | &from_feeds, |
| 153 | super::super::extra2::Inputs::Feeds(&specs), |
| 154 | None, |
| 155 | ) |
| 156 | .unwrap(); |
| 157 | super::super::extra2::run_with_timer( |
| 158 | &from_pack, |
| 159 | super::super::extra2::Inputs::Pack(&pack), |
| 160 | None, |
| 161 | ) |
| 162 | .unwrap(); |
| 163 | let (a, b) = ( |
| 164 | std::fs::read_to_string(&from_feeds).unwrap(), |
| 165 | std::fs::read_to_string(&from_pack).unwrap(), |
| 166 | ); |
| 167 | let stored = Reader::open(&pack).unwrap().rail_lines().expect("a v7 pack"); |
| 168 | let _ = std::fs::remove_dir_all(&root); |
| 169 | assert!( |
| 170 | stored |
| 171 | .iter() |
| 172 | .any(|l| l.points.first() == Some(&(376_999_999, -1_224_000_001)) |
| 173 | && l.points.len() == 3), |
| 174 | "every shapes.txt point, at full e7 precision" |
| 175 | ); |
| 176 | assert_eq!(stored.len(), 4, "two shapes per feed, and no bus"); |
| 177 | assert!(a.contains("\"intercity\":1"), "the 200 km line is intercity"); |
| 178 | assert!(!a.contains("FF0000"), "no bus"); |
| 179 | assert_eq!(a, b, "the pack-derived layer is byte-identical"); |
| 180 | } |
| 181 | |
| 182 | #[test] |
| 183 | fn a_v6_pack_is_refused_with_a_rebuild_hint() { |
| 184 | let path = |
| 185 | std::env::temp_dir().join(format!("transit_shapes_v6_{}.transit", std::process::id())); |
| 186 | let mut blob = vec![0u8; gtfs_ingest::index::HEADER_LEN + 27 * 16]; |
| 187 | blob[0..4].copy_from_slice(>fs_ingest::index::MAGIC.to_le_bytes()); |
| 188 | blob[4..8].copy_from_slice(&6u32.to_le_bytes()); |
| 189 | blob[8..12].copy_from_slice(&27u32.to_le_bytes()); |
| 190 | let end = (blob.len() as u64).to_le_bytes(); |
| 191 | for i in 0..27 { |
| 192 | let at = gtfs_ingest::index::HEADER_LEN + i * 16; |
| 193 | blob[at..at + 8].copy_from_slice(&end); |
| 194 | } |
| 195 | std::fs::write(&path, &blob).unwrap(); |
| 196 | let err = pack_lines(&path).err().expect("refused"); |
| 197 | let _ = std::fs::remove_file(&path); |
| 198 | assert!(err.contains("rebuild"), "{err}"); |
| 199 | } |
| 200 | } |