gtfs_structures/
raw_gtfs.rs

1use crate::objects::*;
2use crate::Error;
3use crate::GtfsReader;
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}
58
59impl RawGtfs {
60    /// Prints on stdout some basic statistics about the GTFS file (numbers of elements for each object). Mostly to be sure that everything was read
61    pub fn print_stats(&self) {
62        println!("GTFS data:");
63        println!("  Read in {:?}", self.read_duration);
64        println!("  Stops: {}", mandatory_file_summary(&self.stops));
65        println!("  Routes: {}", mandatory_file_summary(&self.routes));
66        println!("  Trips: {}", mandatory_file_summary(&self.trips));
67        println!("  Agencies: {}", mandatory_file_summary(&self.agencies));
68        println!("  Stop times: {}", mandatory_file_summary(&self.stop_times));
69        println!("  Shapes: {}", optional_file_summary(&self.shapes));
70        println!("  Fares: {}", optional_file_summary(&self.fare_attributes));
71        println!(
72            "  Frequencies: {}",
73            optional_file_summary(&self.frequencies)
74        );
75        println!("  Transfers: {}", optional_file_summary(&self.transfers));
76        println!("  Pathways: {}", optional_file_summary(&self.pathways));
77        println!("  Feed info: {}", optional_file_summary(&self.feed_info));
78        println!(
79            "  Translations: {}",
80            optional_file_summary(&self.translations)
81        );
82    }
83
84    /// Reads from an url (if starts with http), or a local path (either a directory or zipped file)
85    ///
86    /// To read from an url, build with read-url feature
87    /// See also [RawGtfs::from_url] and [RawGtfs::from_path] if you don’t want the library to guess
88    #[cfg(not(target_arch = "wasm32"))]
89    pub fn new(gtfs: &str) -> Result<Self, Error> {
90        GtfsReader::default().raw().read(gtfs)
91    }
92
93    /// Reads the raw GTFS from a local zip archive or local directory
94    pub fn from_path<P>(path: P) -> Result<Self, Error>
95    where
96        P: AsRef<Path>,
97    {
98        GtfsReader::default().raw().read_from_path(path)
99    }
100
101    /// Reads the raw GTFS from a remote url
102    ///
103    /// The library must be built with the read-url feature. Not available on WASM targets.
104    #[cfg(all(feature = "read-url", not(target_arch = "wasm32")))]
105    pub fn from_url<U: reqwest::IntoUrl>(url: U) -> Result<Self, Error> {
106        GtfsReader::default().raw().read_from_url(url)
107    }
108
109    /// Non-blocking read the raw GTFS from a remote url
110    ///
111    /// The library must be built with the read-url feature
112    #[cfg(feature = "read-url")]
113    pub async fn from_url_async<U: reqwest::IntoUrl>(url: U) -> Result<Self, Error> {
114        GtfsReader::default().raw().read_from_url_async(url).await
115    }
116
117    /// Reads for any object implementing [std::io::Read] and [std::io::Seek]
118    ///
119    /// Mostly an internal function that abstracts reading from an url or local file
120    pub fn from_reader<T: std::io::Read + std::io::Seek>(reader: T) -> Result<Self, Error> {
121        GtfsReader::default().raw().read_from_reader(reader)
122    }
123
124    pub(crate) fn unknown_to_default(&mut self) {
125        if let Ok(stops) = &mut self.stops {
126            for stop in stops.iter_mut() {
127                if let LocationType::Unknown(_) = stop.location_type {
128                    stop.location_type = LocationType::default();
129                }
130                if let Availability::Unknown(_) = stop.wheelchair_boarding {
131                    stop.wheelchair_boarding = Availability::default();
132                }
133            }
134        }
135        if let Ok(stop_times) = &mut self.stop_times {
136            for stop_time in stop_times.iter_mut() {
137                if let PickupDropOffType::Unknown(_) = stop_time.pickup_type {
138                    stop_time.pickup_type = PickupDropOffType::default();
139                }
140                if let PickupDropOffType::Unknown(_) = stop_time.drop_off_type {
141                    stop_time.drop_off_type = PickupDropOffType::default();
142                }
143                if let ContinuousPickupDropOff::Unknown(_) = stop_time.continuous_pickup {
144                    stop_time.continuous_pickup = ContinuousPickupDropOff::default();
145                }
146                if let ContinuousPickupDropOff::Unknown(_) = stop_time.continuous_drop_off {
147                    stop_time.continuous_drop_off = ContinuousPickupDropOff::default();
148                }
149            }
150        }
151        if let Ok(trips) = &mut self.trips {
152            for trip in trips.iter_mut() {
153                if let Availability::Unknown(_) = trip.wheelchair_accessible {
154                    trip.wheelchair_accessible = Availability::default();
155                }
156                if let BikesAllowedType::Unknown(_) = trip.bikes_allowed {
157                    trip.bikes_allowed = BikesAllowedType::default();
158                }
159            }
160        }
161    }
162}
163
164fn mandatory_file_summary<T>(objs: &Result<Vec<T>, Error>) -> String {
165    match objs {
166        Ok(vec) => format!("{} objects", vec.len()),
167        Err(e) => format!("Could not read {e}"),
168    }
169}
170
171fn optional_file_summary<T>(objs: &Option<Result<Vec<T>, Error>>) -> String {
172    match objs {
173        Some(objs) => mandatory_file_summary(objs),
174        None => "File not present".to_string(),
175    }
176}