Skip to content

Commit

Permalink
Implement unusable_trip rule (#211)
Browse files Browse the repository at this point in the history
* Implement unusable_trip rule

Fixes #206.

* Fix implementation

* Properly plug the new rule
  • Loading branch information
ptitfred authored Jan 16, 2025
1 parent 738d1b1 commit a7a3101
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 2 deletions.
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,11 @@ Here is a human friendly list of them :
| DuplicateStopSequence | Error | Several stop times in a trip have the same `stop_sequence` value. The stop_sequence values within a trip must be unique. |
| ExtraFile | Information | The file does not belong to a GTFS archive |
| UnusedShapeId | Information | A shape_id defined in shapes.txt is not used elsewhere in the GTFS |
| UnusableTrip | Error | A trip must visit more than one stop in stop_times.txt to be usable by passengers for boarding and alighting. |
| | | |
| NegativeTravelTime | Warning | The travel duration between two stops is negative. |
| NegativeStopDuration | Warning | The `departure_time` at a stop is earlier than its `arrival_time`. |
| MissingName | Error | An agency, a route or a stop has its name missing. |
| MissingName | Error | An agency, a route or a stop has its name missing. |
| MissingCoordinates | Warning | A shape point or a stop is missing its coordinate(s). |
| NullDuration | Warning | The travel duration between two stops is null. |
| MissingLanguage | Warning | The publisher language code is missing. |
Expand Down
3 changes: 3 additions & 0 deletions src/issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ pub enum IssueType {
DuplicateStopSequence,
/// The .txt files within the GTFS are located in a subfolder, which is now explicitly forbidden by the specification
SubFolder,
/// A trip must visit more than one stop in stop_times.txt to be usable by passengers for
/// boarding and alighting.
UnusableTrip,
}

/// Represents an object related to another object that is causing an issue.
Expand Down
3 changes: 2 additions & 1 deletion src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ pub fn validate_and_metadata(
.chain(validators::fare_attributes::validate(gtfs))
.chain(validators::feed_info::validate(gtfs))
.chain(validators::stop_times::validate(gtfs))
.chain(validators::interpolated_stoptimes::validate(gtfs)),
.chain(validators::interpolated_stoptimes::validate(gtfs))
.chain(validators::unusable_trip::validate(gtfs)),
);
issues
.iter_mut()
Expand Down
1 change: 1 addition & 0 deletions src/validators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ pub mod shapes;
pub mod stop_times;
pub mod stops;
pub mod sub_folder;
pub mod unusable_trip;
pub mod unused_stop;
39 changes: 39 additions & 0 deletions src/validators/unusable_trip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::collections::HashSet;

use gtfs_structures::Trip;

use crate::issues::Issue;
use crate::{IssueType, Severity};

pub fn validate(gtfs: &gtfs_structures::Gtfs) -> Vec<Issue> {
gtfs.trips
.values()
.filter_map(|trip| {
let mut stops = HashSet::new();
for stop_time in &trip.stop_times {
stops.insert(stop_time.stop.id.to_owned());
if stops.len() > 1 {
break;
}
}
if stops.len() < 2 {
Some(mk_issue(trip))
} else {
None
}
})
.collect()
}

fn mk_issue(trip: &Trip) -> Issue {
Issue::new_with_obj(Severity::Error, IssueType::UnusableTrip, trip)
}

#[test]
fn test() {
let gtfs = gtfs_structures::Gtfs::new("test_data/unusable_trip").unwrap();
let issues = validate(&gtfs);

assert_eq!(1, issues.len());
assert_eq!("AB1", issues[0].object_id);
}
2 changes: 2 additions & 0 deletions test_data/unusable_trip/agency.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
agency_id,agency_name,agency_url,agency_timezone
DTA,Demo Transit Authority,http://google.com,America/Los_Angeles
2 changes: 2 additions & 0 deletions test_data/unusable_trip/routes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
route_id,agency_id,route_short_name,route_long_name,route_desc,route_type,route_url,route_color,route_text_color
AB,DTA,,Airport - Bullfrog,,3,,,
5 changes: 5 additions & 0 deletions test_data/unusable_trip/stop_times.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
trip_id,arrival_time,departure_time,stop_id,stop_sequence,stop_headsign,pickup_type,drop_off_type,shape_dist_traveled
AB1,8:00:00,8:00:00,BEATTY_AIRPORT,1,,,,
AB1,10:00:00,10:00:00,BEATTY_AIRPORT,1,,,,
AB2,10:00:00,10:00:00,BEATTY_AIRPORT,1,,,,
AB2,12:00:00,12:00:00,CENTRAL_STATION,1,,,,
3 changes: 3 additions & 0 deletions test_data/unusable_trip/stops.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
stop_id,stop_name,stop_desc,stop_lat,stop_lon,zone_id,stop_url,location_type,parent_station
BEATTY_AIRPORT,Nye County Airport (Demo),,36.868446,-116.784582,,,,
CENTRAL_STATION,Central Station (Demo),,36.924526,-115.741512,,,,
3 changes: 3 additions & 0 deletions test_data/unusable_trip/trips.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
route_id,service_id,trip_id,trip_headsign,direction_id,block_id,shape_id
AB,FULLW,AB1,to Bullfrog,0,1,
AB,FULLW,AB2,to Bullfrog,0,1,

0 comments on commit a7a3101

Please sign in to comment.