Skip to content

Commit

Permalink
filter for minimum day
Browse files Browse the repository at this point in the history
  • Loading branch information
kylerchin committed Jan 7, 2025
1 parent 014a0e7 commit 9eb6493
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
64 changes: 62 additions & 2 deletions src/schedule_filtering/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::BTreeSet;

use std::collections::BTreeMap;
use gtfs_structures::*;
use std::collections::HashMap;

pub fn include_only_route_types(gtfs: Gtfs, route_types: Vec<gtfs_structures::RouteType>) -> Gtfs {
let mut gtfs = gtfs;
Expand Down Expand Up @@ -66,6 +67,65 @@ pub fn include_only_route_types(gtfs: Gtfs, route_types: Vec<gtfs_structures::Ro
gtfs
}

fn minimum_day_filter(gtfs: Gtfs, naive_date: chrono::NaiveDate) -> Gtfs {
let mut gtfs = gtfs;

let mut throwout_calendar_list = BTreeSet::new();
let mut routes_to_trips: HashMap<String, Vec<String>> = HashMap::new();
let mut trips_removed: BTreeSet<String> = BTreeSet::new();

for (calendar_id, calendar) in &gtfs.calendar {
if calendar.end_date < naive_date {
let contains_any_exceptions_greater_than_or_equal_to_date =
match gtfs.calendar_dates.get(calendar_id) {
Some(calendar_dates) => {
calendar_dates.iter().any(|calendar_date| calendar_date.date >= naive_date)
},
None => false
};

if !contains_any_exceptions_greater_than_or_equal_to_date {
throwout_calendar_list.insert(calendar_id.clone());
}
}
}

for (trip_id, trip) in &gtfs.trips {
routes_to_trips.entry(trip.route_id.clone())
.and_modify(|x| x.push(trip_id.clone()))
.or_insert(vec![trip_id.clone()]);

if throwout_calendar_list.contains(&trip.service_id) {
trips_removed.insert(trip_id.clone());
}
}

let mut throwout_routes_list: BTreeSet<String> = BTreeSet::new();

for (route_id, trip_ids) in routes_to_trips {
let mark_for_deletion = trip_ids.iter().all(|x| trips_removed.contains(x));

if mark_for_deletion {
throwout_routes_list.insert(route_id.clone());
}
}

for route_id in throwout_routes_list {
gtfs.routes.remove(&route_id);
}

for trip_id in trips_removed {
gtfs.trips.remove(&trip_id);
}

for calendar in &throwout_calendar_list {
gtfs.calendar.remove(calendar);
gtfs.calendar_dates.remove(calendar);
}

gtfs
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -80,6 +140,6 @@ mod tests {
let gtfs = include_only_route_types(gtfs, vec![gtfs_structures::RouteType::Subway]);

println!("ends with");
let final_stats = gtfs.print_stats();
gtfs.print_stats();
}
}
2 changes: 1 addition & 1 deletion transitland-atlas
Submodule transitland-atlas updated 186 files

0 comments on commit 9eb6493

Please sign in to comment.