Skip to main content

gtfs_structures/
raw_gtfs.rs

1use crate::Error;
2use crate::GtfsReader;
3use crate::objects::*;
4use std::path::Path;
5use web_time::Duration;
6
7/// Data structure that map the GTFS csv with little intelligence
8///
9/// This is used to analyze the GTFS and detect anomalies
10/// To manipulate the transit data, maybe [crate::Gtfs] will be more convienient
11#[derive(Debug)]
12pub struct RawGtfs {
13    /// Time needed to read and parse the archive
14    pub read_duration: Duration,
15    /// All Calendar, None if the file was absent as it is not mandatory
16    pub calendar: Option<Result<Vec<Calendar>, Error>>,
17    /// All Calendar dates, None if the file was absent as it is not mandatory
18    pub calendar_dates: Option<Result<Vec<CalendarDate>, Error>>,
19    /// All Stops
20    pub stops: Result<Vec<Stop>, Error>,
21    /// All Routes
22    pub routes: Result<Vec<Route>, Error>,
23    /// All Trips
24    pub trips: Result<Vec<RawTrip>, Error>,
25    /// All Agencies
26    pub agencies: Result<Vec<Agency>, Error>,
27    /// All shapes points, None if the file was absent as it is not mandatory
28    pub shapes: Option<Result<Vec<Shape>, Error>>,
29    /// All FareAttributes, None if the file was absent as it is not mandatory
30    pub fare_attributes: Option<Result<Vec<FareAttribute>, Error>>,
31    /// All FareRules, None if the file was absent as it is not mandatory
32    pub fare_rules: Option<Result<Vec<FareRule>, Error>>,
33    /// All FareProducts, None if the file was absent as it is not mandatory
34    pub fare_products: Option<Result<Vec<FareProduct>, Error>>,
35    /// All FareMedias, None if the file was absent as it is not mandatory
36    pub fare_media: Option<Result<Vec<FareMedia>, Error>>,
37    /// All RiderCategories, None if the file was absent as it is not mandatory
38    pub rider_categories: Option<Result<Vec<RiderCategory>, Error>>,
39    /// All Frequencies, None if the file was absent as it is not mandatory
40    pub frequencies: Option<Result<Vec<RawFrequency>, Error>>,
41    /// All Transfers, None if the file was absent as it is not mandatory
42    pub transfers: Option<Result<Vec<RawTransfer>, Error>>,
43    /// All Pathways, None if the file was absent as it is not mandatory
44    pub pathways: Option<Result<Vec<RawPathway>, Error>>,
45    /// All FeedInfo, None if the file was absent as it is not mandatory
46    pub feed_info: Option<Result<Vec<FeedInfo>, Error>>,
47    /// All StopTimes
48    pub stop_times: Result<Vec<RawStopTime>, Error>,
49    /// All files that are present in the feed
50    pub files: Vec<String>,
51    /// Format of the data read
52    pub source_format: SourceFormat,
53    /// sha256 sum of the feed
54    pub sha256: Option<String>,
55    /// All translations, None if the file was absent as it is not mandatory
56    pub translations: Option<Result<Vec<RawTranslation>, Error>>,
57    /// Base urls to ticket shops
58    pub ticketing_deep_links: Option<Result<Vec<TicketingDeepLink>, Error>>,
59    /// Identifiers to pass to ticket shops
60    pub ticketing_identifiers: Option<Result<Vec<TicketingIdentifier>, Error>>,
61}
62
63impl RawGtfs {
64    /// Prints on stdout some basic statistics about the GTFS file (numbers of elements for each object). Mostly to be sure that everything was read
65    pub fn print_stats(&self) {
66        println!("GTFS data:");
67        println!("  Read in {:?}", self.read_duration);
68        println!("  Stops: {}", mandatory_file_summary(&self.stops));
69        println!("  Routes: {}", mandatory_file_summary(&self.routes));
70        println!("  Trips: {}", mandatory_file_summary(&self.trips));
71        println!("  Agencies: {}", mandatory_file_summary(&self.agencies));
72        println!("  Stop times: {}", mandatory_file_summary(&self.stop_times));
73        println!("  Shapes: {}", optional_file_summary(&self.shapes));
74        println!("  Fares: {}", optional_file_summary(&self.fare_attributes));
75        println!(
76            "  Frequencies: {}",
77            optional_file_summary(&self.frequencies)
78        );
79        println!("  Transfers: {}", optional_file_summary(&self.transfers));
80        println!("  Pathways: {}", optional_file_summary(&self.pathways));
81        println!("  Feed info: {}", optional_file_summary(&self.feed_info));
82        println!(
83            "  Translations: {}",
84            optional_file_summary(&self.translations)
85        );
86        println!(
87            "  Ticketing deep links: {}",
88            optional_file_summary(&self.ticketing_deep_links)
89        );
90        println!(
91            "  Ticketing identifiers: {}",
92            optional_file_summary(&self.ticketing_identifiers)
93        );
94    }
95
96    /// Reads from an url (if starts with http), or a local path (either a directory or zipped file)
97    ///
98    /// To read from an url, build with read-url feature
99    /// See also [RawGtfs::from_url] and [RawGtfs::from_path] if you don’t want the library to guess
100    #[cfg(not(target_arch = "wasm32"))]
101    pub fn new(gtfs: &str) -> Result<Self, Error> {
102        GtfsReader::default().raw().read(gtfs)
103    }
104
105    /// Reads the raw GTFS from a local zip archive or local directory
106    pub fn from_path<P>(path: P) -> Result<Self, Error>
107    where
108        P: AsRef<Path>,
109    {
110        GtfsReader::default().raw().read_from_path(path)
111    }
112
113    /// Reads the raw GTFS from a remote url
114    ///
115    /// The library must be built with the read-url feature. Not available on WASM targets.
116    #[cfg(all(feature = "read-url", not(target_arch = "wasm32")))]
117    pub fn from_url<U: reqwest::IntoUrl>(url: U) -> Result<Self, Error> {
118        GtfsReader::default().raw().read_from_url(url)
119    }
120
121    /// Non-blocking read the raw GTFS from a remote url
122    ///
123    /// The library must be built with the read-url feature
124    #[cfg(feature = "read-url")]
125    pub async fn from_url_async<U: reqwest::IntoUrl>(url: U) -> Result<Self, Error> {
126        GtfsReader::default().raw().read_from_url_async(url).await
127    }
128
129    /// Reads for any object implementing [std::io::Read] and [std::io::Seek]
130    ///
131    /// Mostly an internal function that abstracts reading from an url or local file
132    pub fn from_reader<T: std::io::Read + std::io::Seek>(reader: T) -> Result<Self, Error> {
133        GtfsReader::default().raw().read_from_reader(reader)
134    }
135
136    pub(crate) fn unknown_to_default(&mut self) {
137        if let Ok(stops) = &mut self.stops {
138            for stop in stops.iter_mut() {
139                if let LocationType::Unknown(_) = stop.location_type {
140                    stop.location_type = LocationType::default();
141                }
142                if let Availability::Unknown(_) = stop.wheelchair_boarding {
143                    stop.wheelchair_boarding = Availability::default();
144                }
145            }
146        }
147        if let Ok(stop_times) = &mut self.stop_times {
148            for stop_time in stop_times.iter_mut() {
149                if let PickupDropOffType::Unknown(_) = stop_time.pickup_type {
150                    stop_time.pickup_type = PickupDropOffType::default();
151                }
152                if let PickupDropOffType::Unknown(_) = stop_time.drop_off_type {
153                    stop_time.drop_off_type = PickupDropOffType::default();
154                }
155                if let ContinuousPickupDropOff::Unknown(_) = stop_time.continuous_pickup {
156                    stop_time.continuous_pickup = ContinuousPickupDropOff::default();
157                }
158                if let ContinuousPickupDropOff::Unknown(_) = stop_time.continuous_drop_off {
159                    stop_time.continuous_drop_off = ContinuousPickupDropOff::default();
160                }
161            }
162        }
163        if let Ok(trips) = &mut self.trips {
164            for trip in trips.iter_mut() {
165                if let Availability::Unknown(_) = trip.wheelchair_accessible {
166                    trip.wheelchair_accessible = Availability::default();
167                }
168                if let BikesAllowedType::Unknown(_) = trip.bikes_allowed {
169                    trip.bikes_allowed = BikesAllowedType::default();
170                }
171            }
172        }
173    }
174}
175
176fn mandatory_file_summary<T>(objs: &Result<Vec<T>, Error>) -> String {
177    match objs {
178        Ok(vec) => format!("{} objects", vec.len()),
179        Err(e) => format!("Could not read {e}"),
180    }
181}
182
183fn optional_file_summary<T>(objs: &Option<Result<Vec<T>, Error>>) -> String {
184    match objs {
185        Some(objs) => mandatory_file_summary(objs),
186        None => "File not present".to_string(),
187    }
188}