Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tech] Deduplicate gtfs stoptimes #799

Merged
merged 2 commits into from
Jul 27, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 115 additions & 12 deletions src/gtfs/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,16 +375,8 @@ where
let mut tmp_vjs = BTreeMap::new();
let stop_times = read_objects::<_, StopTime>(file_handler, file_name, true)?;

for mut stop_time in stop_times {
for stop_time in stop_times {
if let Some(vj_idx) = collections.vehicle_journeys.get_idx(&stop_time.trip_id) {
// consume the stop headsign
if let Some(headsign) = stop_time.stop_headsign.take() {
headsigns.insert(
(stop_time.trip_id.clone(), stop_time.stop_sequence),
headsign,
);
}

tmp_vjs
.entry(vj_idx)
.or_insert_with(Vec::new)
Expand All @@ -396,15 +388,23 @@ where
)
}
}
collections.stop_time_headsigns = headsigns;

for (vj_idx, mut stop_times) in tmp_vjs {
stop_times.sort_unstable_by_key(|st| st.stop_sequence);
stop_times.dedup_by(|st2, st1| {
let is_same_seq = st2.stop_sequence == st1.stop_sequence;
if is_same_seq {
warn!(
"remove duplicated stop_sequence '{}' of vehicle '{}'",
st2.stop_sequence, st2.trip_id
);
}
is_same_seq
});
let st_values = interpolate_undefined_stop_times(
&collections.vehicle_journeys[vj_idx].id,
&stop_times,
)?;

let company_idx = collections
.companies
.get_idx(&collections.vehicle_journeys[vj_idx].company_id);
Expand All @@ -416,7 +416,12 @@ where
(false, true) => Some(StopTimePrecision::Approximate),
(true, true) => Some(StopTimePrecision::Estimated),
};

if let Some(headsign) = &stop_time.stop_headsign {
headsigns.insert(
(stop_time.trip_id.clone(), stop_time.stop_sequence),
headsign.clone(),
);
}
if let Some(message) = on_demand_transport_comment.as_ref() {
if stop_time.pickup_type == 2 || stop_time.drop_off_type == 2 {
if let Some(company_idx) = company_idx {
Expand Down Expand Up @@ -455,6 +460,9 @@ where
}
}
}

collections.stop_time_headsigns = headsigns;

Ok(())
}

Expand Down Expand Up @@ -2552,6 +2560,101 @@ mod tests {
});
}

#[test]
fn gtfs_stop_times_deduplicated() {
let routes_content = "route_id,agency_id,route_short_name,route_long_name,route_type,route_color,route_text_color\n\
route_1,agency_1,1,My line 1,3,8F7A32,FFFFFF";

let stops_content =
"stop_id,stop_name,stop_desc,stop_lat,stop_lon,location_type,parent_station\n\
sp:01,my stop point name 1,,0.1,1.1,0,\n\
sp:02,my stop point name 2,,0.2,1.2,0,\n\
sp:03,my stop point name 3,,0.3,1.3,0,\n\
sp:04,my stop point name 4,,0.4,1.4,0,\n\
sp:05,my stop point name 5,,0.5,1.5,0,";

let trips_content =
"trip_id,route_id,direction_id,service_id,wheelchair_accessible,bikes_allowed\n\
1,route_1,0,service_1,,";

// Duplicated stops sequences 1 and 2
// Duplicates should not be taken into account ("first come" rule)
// Uniqueness is on trip_id / stop_sequence
let stop_times_content = "trip_id,arrival_time,departure_time,stop_id,stop_sequence\n\
1,06:00:00,06:00:00,sp:01,1\n\
1,06:00:10,06:00:10,sp:04,1\n\
1,06:11:00,06:11:00,sp:02,2\n\
1,06:11:10,06:11:10,sp:05,2\n\
1,06:22:00,06:22:00,sp:03,3";

test_in_tmp_dir(|path| {
let mut handler = PathFileHandler::new(path.to_path_buf());
create_file_with_content(path, "routes.txt", routes_content);
create_file_with_content(path, "trips.txt", trips_content);
create_file_with_content(path, "stop_times.txt", stop_times_content);
create_file_with_content(path, "stops.txt", stops_content);

let mut collections = Collections::default();
let (contributor, dataset, _) = read_utils::read_config(None::<&str>).unwrap();
collections.contributors = CollectionWithId::new(vec![contributor]).unwrap();
collections.datasets = CollectionWithId::new(vec![dataset]).unwrap();

let mut comments: CollectionWithId<Comment> = CollectionWithId::default();
let mut equipments = EquipmentList::default();
let (_, stop_points, _) =
super::read_stops(&mut handler, &mut comments, &mut equipments).unwrap();
collections.stop_points = stop_points;

super::read_routes(&mut handler, &mut collections, false).unwrap();
super::manage_stop_times(&mut collections, &mut handler, false, None).unwrap();

assert_eq!(
vec![
StopTime {
stop_point_idx: collections.stop_points.get_idx("sp:01").unwrap(),
sequence: 1,
arrival_time: Time::new(6, 0, 0),
departure_time: Time::new(6, 0, 0),
boarding_duration: 0,
alighting_duration: 0,
pickup_type: 0,
drop_off_type: 0,
datetime_estimated: false,
local_zone_id: None,
precision: Some(StopTimePrecision::Exact),
},
StopTime {
stop_point_idx: collections.stop_points.get_idx("sp:02").unwrap(),
sequence: 2,
arrival_time: Time::new(6, 11, 0),
departure_time: Time::new(6, 11, 0),
boarding_duration: 0,
alighting_duration: 0,
pickup_type: 0,
drop_off_type: 0,
datetime_estimated: false,
local_zone_id: None,
precision: Some(StopTimePrecision::Exact),
},
StopTime {
stop_point_idx: collections.stop_points.get_idx("sp:03").unwrap(),
sequence: 3,
arrival_time: Time::new(6, 22, 0),
departure_time: Time::new(6, 22, 0),
boarding_duration: 0,
alighting_duration: 0,
pickup_type: 0,
drop_off_type: 0,
datetime_estimated: false,
local_zone_id: None,
precision: Some(StopTimePrecision::Exact),
},
],
collections.vehicle_journeys.into_vec()[0].stop_times
);
});
}

#[test]
fn gtfs_stop_times() {
let routes_content = "route_id,agency_id,route_short_name,route_long_name,route_type,route_color,route_text_color\n\
Expand Down