Skip to content

Commit

Permalink
Read windows from ntfs and add some data controls
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudOggy committed Sep 19, 2024
1 parent 85af09e commit 71f36c8
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/ntfs/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,8 @@ where
sequence: stop_time.stop_sequence,
arrival_time: stop_time.arrival_time,
departure_time: stop_time.departure_time,
start_pickup_drop_off_window: None,
end_pickup_drop_off_window: None,
start_pickup_drop_off_window: stop_time.start_pickup_drop_off_window,
end_pickup_drop_off_window: stop_time.end_pickup_drop_off_window,
boarding_duration: stop_time.boarding_duration,
alighting_duration: stop_time.alighting_duration,
pickup_type: stop_time.pickup_type,
Expand Down
47 changes: 39 additions & 8 deletions src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -687,10 +687,14 @@ impl GetObjectType for VehicleJourney {

impl VehicleJourney {
pub fn first_departure_time(&self) -> Option<Time> {
self.stop_times.first().and_then(|st| st.departure_time)
self.stop_times
.first()
.and_then(|st| st.departure_time.or(st.start_pickup_drop_off_window))
}
pub fn last_arrival_time(&self) -> Option<Time> {
self.stop_times.last().and_then(|st| st.arrival_time)
self.stop_times
.last()
.and_then(|st| st.arrival_time.or(st.end_pickup_drop_off_window))
}
}

Expand All @@ -715,23 +719,50 @@ impl VehicleJourney {
for window in self.stop_times.windows(2) {
let curr_st = &window[0];
let next_st = &window[1];

if curr_st.sequence == next_st.sequence {
return Err(StopTimeError::DuplicateStopSequence {
duplicated_sequence: curr_st.sequence,
vj_id: self.id.clone(),
});
}
#[allow(clippy::suspicious_operation_groupings)]
if let Some(dt) = curr_st.departure_time {
if (curr_st.arrival_time > curr_st.departure_time)
|| (curr_st.departure_time > next_st.arrival_time)
{

let dt = curr_st
.departure_time
.or(curr_st.start_pickup_drop_off_window)
.unwrap_or_default();

// Only 2 valid possibilities:
// - arrival_time and departure_time are filled, but not start_pickup_drop_off_window and end_pickup_drop_off_window
// - start_pickup_drop_off_window and end_pickup_drop_off_window are filled, but not arrival_time and departure_time
match (
curr_st.arrival_time,
curr_st.departure_time,
curr_st.start_pickup_drop_off_window,
curr_st.end_pickup_drop_off_window,
) {
(Some(_), Some(_), None, None) => (),
(None, None, Some(_), Some(_)) => (),
_ => {
return Err(StopTimeError::IncoherentStopTimes {
first_incorrect_sequence: curr_st.sequence,
first_incorrect_time: dt,
vj_id: self.id.clone(),
});
})
}
};

if (curr_st.arrival_time > curr_st.departure_time)
|| (curr_st.start_pickup_drop_off_window > curr_st.end_pickup_drop_off_window)
|| (curr_st.departure_time.is_some()
&& next_st.arrival_time.is_some()
&& curr_st.departure_time > next_st.arrival_time)
{
return Err(StopTimeError::IncoherentStopTimes {
first_incorrect_sequence: curr_st.sequence,
first_incorrect_time: dt,
vj_id: self.id.clone(),
});
}
}
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/ntfs_with_windows/input/lines.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
line_id,line_code,line_name,forward_line_name,backward_line_name,line_color,line_text_color,line_sort_order,network_id,commercial_mode_id,geometry_id,line_opening_time,line_closing_time
MFDI:C01376,6,6,,,6ECA97,0,,MFDI:Operator_100,Metro,,,
MFDI:C01376,6,6,,,6ECA97,000000,,MFDI:Operator_100,Metro,,,
MFDI:C02060,Filéo RS,Filéo Roissy Sud - Villeparisis et Mitry (sur réservation),,,4890CD,FFFFFF,,MFDI:1071,Bus,,,
MFDI:C02491,Soir,Soir Saint-Germain-en-Laye,,,652C8F,FFFFFF,,MFDI:1062,Bus,,,
MFDI:C02513,Soir,Soir Versailles-Chantiers,,,640082,FFFFFF,,MFDI:1061,Bus,,,
5 changes: 5 additions & 0 deletions tests/fixtures/ntfs_with_windows/output/lines.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
line_id,line_code,line_name,forward_line_name,backward_line_name,line_color,line_text_color,line_sort_order,network_id,commercial_mode_id,geometry_id,line_opening_time,line_closing_time
MFDI:C01376,6,6,,,6ECA97,000000,,MFDI:Operator_100,Metro,,10:30:00,11:03:00
MFDI:C02060,Filéo RS,Filéo Roissy Sud - Villeparisis et Mitry (sur réservation),,,4890CD,FFFFFF,,MFDI:1071,Bus,,02:39:00,03:15:00
MFDI:C02491,Soir,Soir Saint-Germain-en-Laye,,,652C8F,FFFFFF,,MFDI:1062,Bus,,23:15:00,23:48:00
MFDI:C02513,Soir,Soir Versailles-Chantiers,,,640082,FFFFFF,,MFDI:1061,Bus,,22:30:00,22:55:00
4 changes: 2 additions & 2 deletions tests/read_ntfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,13 +558,13 @@ fn ntfs_with_duplicated_objects_without_id() {
}

#[test]
fn preserve_stoptimes_windows() {
fn preserve_stoptimes_windows_and_enhance_line_opening_time() {
let ntm = transit_model::ntfs::read("tests/fixtures/ntfs_with_windows/input/").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!["stop_times.txt"]),
Some(vec!["lines.txt", "stop_times.txt"]),
"tests/fixtures/ntfs_with_windows/output",
);
});
Expand Down

0 comments on commit 71f36c8

Please sign in to comment.