Skip to content

Commit

Permalink
Merge pull request #396 from woshilapin/frequencies
Browse files Browse the repository at this point in the history
Read, Write, Merge and Sanitize frequencies in NTFS
  • Loading branch information
mergify[bot] authored Oct 4, 2019
2 parents e2448c3 + 4bfe278 commit 37ec01c
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub struct Collections {
pub lines: CollectionWithId<Line>,
pub routes: CollectionWithId<Route>,
pub vehicle_journeys: CollectionWithId<VehicleJourney>,
pub frequencies: Collection<Frequency>,
pub physical_modes: CollectionWithId<PhysicalMode>,
pub stop_areas: CollectionWithId<StopArea>,
pub stop_points: CollectionWithId<StopPoint>,
Expand Down Expand Up @@ -81,6 +82,7 @@ impl Collections {
lines,
routes,
mut vehicle_journeys,
frequencies,
physical_modes,
stop_areas,
stop_points,
Expand Down Expand Up @@ -111,6 +113,7 @@ impl Collections {
self.commercial_modes.merge(commercial_modes);
self.lines.try_merge(lines)?;
self.routes.try_merge(routes)?;
self.frequencies.merge(frequencies);
self.physical_modes.extend(physical_modes);
self.stop_areas.try_merge(stop_areas)?;
self.prices_v1.merge(prices_v1);
Expand Down Expand Up @@ -403,6 +406,7 @@ impl Collections {
update_comments_idx(&mut routes, &comment_old_idx_to_new_idx);
self.routes = CollectionWithId::new(routes)?;
update_comments_idx(&mut vjs, &comment_old_idx_to_new_idx);
let vehicle_journeys_used: HashSet<String> = vjs.iter().map(|vj| vj.id.clone()).collect();
self.vehicle_journeys = CollectionWithId::new(vjs)?;

let vj_old_idx_to_new_idx: HashMap<Idx<VehicleJourney>, Idx<VehicleJourney>> = self
Expand Down Expand Up @@ -463,6 +467,8 @@ impl Collections {
self.transfers.retain(|t| {
stop_points_used.contains(&t.from_stop_id) && stop_points_used.contains(&t.to_stop_id)
});
self.frequencies
.retain(|frequency| vehicle_journeys_used.contains(&frequency.vehicle_journey_id));

Ok(())
}
Expand Down
2 changes: 2 additions & 0 deletions src/ntfs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ pub fn read<P: AsRef<path::Path>>(path: P) -> Result<Model> {
collections.lines = make_collection_with_id(path, "lines.txt")?;
collections.routes = make_collection_with_id(path, "routes.txt")?;
collections.vehicle_journeys = make_collection_with_id(path, "trips.txt")?;
collections.frequencies = make_opt_collection(path, "frequencies.txt")?;
collections.physical_modes = make_collection_with_id(path, "physical_modes.txt")?;
collections.companies = make_collection_with_id(path, "companies.txt")?;
collections.equipments = make_opt_collection_with_id(path, "equipments.txt")?;
Expand Down Expand Up @@ -231,6 +232,7 @@ pub fn write<P: AsRef<path::Path>>(
&model.stop_time_headsigns,
&model.stop_time_ids,
)?;
write::write_collection(path, "frequencies.txt", &model.frequencies)?;
common_format::write_calendar_dates(path, &model.calendars)?;
write::write_stops(path, &model.stop_points, &model.stop_areas)?;
write::write_comments(path, model)?;
Expand Down
9 changes: 9 additions & 0 deletions src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,15 @@ impl GetObjectType for VehicleJourney {
}
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Frequency {
#[serde(rename = "trip_id")]
pub vehicle_journey_id: String,
pub start_time: Time,
pub end_time: Time,
pub headway_secs: u32,
}

#[derive(Debug)]
pub enum TimeError {
WrongFormat,
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/ntfs/frequencies.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
trip_id,start_time,end_time,headway_secs
M1F1,06:00:00,08:00:00,300
M1B1,06:00:00,08:00:00,300
16 changes: 8 additions & 8 deletions tests/fixtures/ntfs/stop_times.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
trip_id,stop_sequence,stop_id,arrival_time,departure_time,stop_time_id
M1F1,0,NATM,9:00:00,9:00:00,
M1F1,1,GDLM,09:10:00,09:10:00,
M1F1,2,CHAM,09:20:00,09:20:00,
M1F1,3,CDGM,09:40:00,09:40:00,
M1B1,9,NATM,11:10:00,11:10:00,
M1B1,8,GDLM,11:00:00,11:00:00,
M1B1,7,CHAM,10:50:00,10:50:00,
M1B1,6,CDGM,10:40:00,10:40:00,
M1F1,0,NATM,00:00:00,00:00:00,
M1F1,1,GDLM,00:10:00,00:10:00,
M1F1,2,CHAM,00:20:00,00:20:00,
M1F1,3,CDGM,00:40:00,00:40:00,
M1B1,9,NATM,00:30:00,00:30:00,
M1B1,8,GDLM,00:20:00,00:20:00,
M1B1,7,CHAM,00:10:00,00:10:00,
M1B1,6,CDGM,00:00:00,00:00:00,
B42F1,10,GDLB,10:10:00,10:10:00,
B42F1,20,MTPB,10:20:00,10:20:00,
B42B1,30,GDLB,07:10:00,07:10:00,
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/ntfs2ntfs/frequencies/frequencies.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
trip_id,start_time,end_time,headway_secs
M1F1,06:00:00,08:00:00,300
M1B1,06:00:00,08:00:00,300
21 changes: 21 additions & 0 deletions tests/fixtures/ntfs2ntfs/frequencies/stop_times.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
stop_id,trip_id,stop_sequence,arrival_time,departure_time,boarding_duration,alighting_duration,pickup_type,drop_off_type,datetime_estimated,local_zone_id,stop_headsign,stop_time_id
NATM,M1F1,0,00:00:00,00:00:00,0,0,0,0,0,,,
GDLM,M1F1,1,00:10:00,00:10:00,0,0,0,0,0,,,
CHAM,M1F1,2,00:20:00,00:20:00,0,0,0,0,0,,,
CDGM,M1F1,3,00:40:00,00:40:00,0,0,0,0,0,,,
NATM,M1B1,9,00:30:00,00:30:00,0,0,0,0,0,,,
GDLM,M1B1,8,00:20:00,00:20:00,0,0,0,0,0,,,
CHAM,M1B1,7,00:10:00,00:10:00,0,0,0,0,0,,,
CDGM,M1B1,6,00:00:00,00:00:00,0,0,0,0,0,,,
GDLB,B42F1,10,10:10:00,10:10:00,0,0,0,0,0,,,
MTPB,B42F1,20,10:20:00,10:20:00,0,0,0,0,0,,,
GDLB,B42B1,30,07:10:00,07:10:00,0,0,0,0,0,,,
MTPB,B42B1,20,07:00:00,07:00:00,0,0,0,0,0,,,
NATR,RERAF1,1,08:09:00,08:10:00,0,0,0,0,0,,,
GDLR,RERAF1,2,08:14:00,08:15:00,0,0,0,0,0,,,
CDGR,RERAF1,3,08:19:00,08:20:00,0,0,0,0,0,,,
DEFR,RERAF1,5,08:24:00,08:25:00,0,0,0,0,0,,,
NATR,RERAB1,21,09:49:00,09:50:00,0,0,0,0,0,,,
GDLR,RERAB1,13,09:44:00,09:45:00,0,0,0,0,0,,,
CDGR,RERAB1,8,09:39:00,09:40:00,0,0,0,0,0,,,StopTime:RERAB1-8:0
DEFR,RERAB1,5,09:24:00,09:25:00,0,0,0,0,0,,,StopTime:RERAB1-5:1
7 changes: 7 additions & 0 deletions tests/fixtures/ntfs2ntfs/frequencies/trips.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
trip_id,route_id,physical_mode_id,dataset_id,service_id,trip_headsign,block_id,company_id,trip_property_id,geometry_id
M1F1,M1F,Metro,TGDS,Week,,,TGC,,
M1B1,M1B,Metro,TGDS,Week,,,TGC,,
B42F1,B42F,Bus,TGDS,Week,,,TGC,,
B42B1,B42B,Bus,TGDS,Week,,,TGC,,
RERAF1,RERAF,RapidTransit,TGDS,Week,,,TGC,,
RERAB1,RERAB,Bus,TGDS,Week,,,TGC,,
1 change: 1 addition & 0 deletions tests/merge_ntfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ fn merge_collections_ok() {
assert_eq!(collections.lines.len(), 6);
assert_eq!(collections.routes.len(), 8);
assert_eq!(collections.vehicle_journeys.len(), 8);
assert_eq!(collections.frequencies.len(), 2);
assert_eq!(collections.stop_time_headsigns.len(), 1);
assert_eq!(collections.stop_time_ids.len(), 5);

Expand Down
29 changes: 28 additions & 1 deletion tests/read_ntfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use std::collections::HashMap;
use transit_model;
use transit_model::model::{GetCorresponding, Model};
use transit_model::model::{Collections, GetCorresponding, Model};
use transit_model::objects::*;
use transit_model::test_utils::*;
use transit_model_collection::{CollectionWithId, Id, Idx};
Expand Down Expand Up @@ -224,3 +224,30 @@ fn optional_empty_collections_not_created() {
assert!(!entries.contains("admin_stations.txt"));
});
}

#[test]
fn preserve_frequencies() {
let ntm = transit_model::ntfs::read("tests/fixtures/ntfs/").unwrap();
test_in_tmp_dir(|output_dir| {
transit_model::ntfs::write(&ntm, output_dir, get_test_datetime()).unwrap();
compare_output_dir_with_expected(
&output_dir,
Some(vec!["frequencies.txt", "stop_times.txt", "trips.txt"]),
"tests/fixtures/ntfs2ntfs/frequencies",
);
});
}

#[test]
fn sanitize_frequencies() {
let mut collections = Collections::default();
let frequency = Frequency {
vehicle_journey_id: String::from("vehicle_journey_id_which_doesn_t_exist"),
start_time: Time::new(0, 0, 0),
end_time: Time::new(0, 0, 0),
headway_secs: 0,
};
collections.frequencies.push(frequency);
collections.sanitize().unwrap();
assert_eq!(collections.frequencies.len(), 0);
}

0 comments on commit 37ec01c

Please sign in to comment.