Skip to content

Commit

Permalink
Merge pull request #484 from patochectp/date_time_estimated
Browse files Browse the repository at this point in the history
[feature] manage date time estimated
  • Loading branch information
mergify[bot] authored Dec 2, 2019
2 parents 221b649 + b58c142 commit 8e02b82
Show file tree
Hide file tree
Showing 37 changed files with 565 additions and 259 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ cargo test
```

## NTFS: Level of Support
`transit_model` is partially supporting `v0.10.0` of NTFS (see [CHANGELOG in
`transit_model` is partially supporting `v0.11.0` of NTFS (see [CHANGELOG in
French](https://github.com/CanalTP/ntfs-specification/blob/master/ntfs_changelog_fr.md)).
From the standard, some of the functionalities are not fully supported:
- No support for Line Groups (files `line_groups.txt` and `line_group_links.txt`)
Expand Down
8 changes: 7 additions & 1 deletion benches/read_gtfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ use transit_model::gtfs;
#[bench]
fn read_gtfs(bencher: &mut Bencher) {
bencher.iter(|| {
gtfs::read_from_path("./tests/fixtures/gtfs2ntfs/minimal/input", None, None).unwrap()
gtfs::read_from_path(
"./tests/fixtures/gtfs2ntfs/minimal/input",
None,
None,
false,
)
.unwrap()
});
}
2 changes: 1 addition & 1 deletion examples/gtfs_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use serde_json::json;
use transit_model::Result;

fn run() -> Result<()> {
let objects = transit_model::gtfs::read_from_path(".", None, None)?;
let objects = transit_model::gtfs::read_from_path(".", None, None, false)?;
let json_objs = json!(objects);
println!("{:?}", json_objs.to_string());
Ok(())
Expand Down
9 changes: 6 additions & 3 deletions src/gtfs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ fn read<H>(
file_handler: &mut H,
config_path: Option<impl AsRef<Path>>,
prefix: Option<String>,
on_demand_transport: bool,
) -> Result<Model>
where
for<'a> &'a mut H: read_utils::FileHandler,
Expand Down Expand Up @@ -292,7 +293,7 @@ where
read::read_routes(file_handler, &mut collections)?;
collections.equipments = CollectionWithId::new(equipments.into_equipments())?;
collections.comments = comments;
read::manage_stop_times(&mut collections, file_handler)?;
read::manage_stop_times(&mut collections, file_handler, on_demand_transport)?;
read::manage_frequencies(&mut collections, file_handler)?;
read::manage_pathways(&mut collections, file_handler)?;
collections.levels = read_utils::read_opt_collection(file_handler, "levels.txt")?;
Expand Down Expand Up @@ -321,9 +322,10 @@ pub fn read_from_path<P: AsRef<Path>>(
p: P,
config_path: Option<P>,
prefix: Option<String>,
on_demand_transport: bool,
) -> Result<Model> {
let mut file_handle = read_utils::PathFileHandler::new(p.as_ref().to_path_buf());
read(&mut file_handle, config_path, prefix)
read(&mut file_handle, config_path, prefix, on_demand_transport)
}

/// Imports a `Model` from a zip file containing the [GTFS](http://gtfs.org/).
Expand All @@ -339,9 +341,10 @@ pub fn read_from_zip<P: AsRef<Path>>(
path: P,
config_path: Option<P>,
prefix: Option<String>,
on_demand_transport: bool,
) -> Result<Model> {
let mut file_handler = read_utils::ZipHandler::new(path)?;
read(&mut file_handler, config_path, prefix)
read(&mut file_handler, config_path, prefix, on_demand_transport)
}

#[derive(PartialOrd, Ord, Debug, Clone, Eq, PartialEq, Hash)]
Expand Down
115 changes: 109 additions & 6 deletions src/gtfs/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
model::Collections,
objects::{
self, CommentLinksT, Coord, KeysValues, Pathway, StopLocation, StopTime as NtfsStopTime,
StopType, Time, TransportType, VehicleJourney,
StopTimePrecision, StopType, Time, TransportType, VehicleJourney,
},
read_utils::{read_collection, read_objects, FileHandler},
utils::*,
Expand Down Expand Up @@ -366,7 +366,11 @@ where
}
}

pub fn manage_stop_times<H>(collections: &mut Collections, file_handler: &mut H) -> Result<()>
pub fn manage_stop_times<H>(
collections: &mut Collections,
file_handler: &mut H,
on_demand_transport: bool,
) -> Result<()>
where
for<'a> &'a mut H: FileHandler,
{
Expand Down Expand Up @@ -422,6 +426,11 @@ where
stop_time.stop_id
)
})?;
let precision = match (on_demand_transport, st_values.datetime_estimated) {
(_, false) => Some(StopTimePrecision::Exact),
(false, true) => Some(StopTimePrecision::Approximate),
(true, true) => Some(StopTimePrecision::Estimated),
};

vj.stop_times.push(objects::StopTime {
stop_point_idx,
Expand All @@ -434,6 +443,7 @@ where
drop_off_type: stop_time.drop_off_type,
datetime_estimated: st_values.datetime_estimated,
local_zone_id: stop_time.local_zone_id,
precision,
});
}
}
Expand Down Expand Up @@ -1163,6 +1173,7 @@ where
drop_off_type: stop_time.drop_off_type,
datetime_estimated,
local_zone_id: stop_time.local_zone_id,
precision: stop_time.precision.clone(),
})
.collect();
start_time = start_time + Time::new(0, 0, frequency.headway_secs);
Expand Down Expand Up @@ -2321,7 +2332,7 @@ mod tests {
collections.stop_points = stop_points;

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

assert_eq!(
vec![
Expand All @@ -2336,6 +2347,7 @@ mod tests {
drop_off_type: 0,
datetime_estimated: true,
local_zone_id: None,
precision: Some(StopTimePrecision::Approximate),
},
StopTime {
stop_point_idx: collections.stop_points.get_idx("sp:02").unwrap(),
Expand All @@ -2348,6 +2360,7 @@ mod tests {
drop_off_type: 1,
datetime_estimated: false,
local_zone_id: None,
precision: Some(StopTimePrecision::Exact),
},
StopTime {
stop_point_idx: collections.stop_points.get_idx("sp:03").unwrap(),
Expand All @@ -2360,6 +2373,7 @@ mod tests {
drop_off_type: 1,
datetime_estimated: false,
local_zone_id: None,
precision: Some(StopTimePrecision::Exact),
},
],
collections.vehicle_journeys.into_vec()[0].stop_times
Expand Down Expand Up @@ -2404,7 +2418,7 @@ mod tests {
collections.stop_points = stop_points;

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

assert_eq!(
vec![
Expand All @@ -2419,6 +2433,7 @@ mod tests {
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(),
Expand All @@ -2431,6 +2446,7 @@ mod tests {
drop_off_type: 1,
datetime_estimated: false,
local_zone_id: None,
precision: Some(StopTimePrecision::Exact),
},
],
collections.vehicle_journeys.into_vec()[0].stop_times
Expand Down Expand Up @@ -2896,7 +2912,7 @@ mod tests {
collections.stop_points = stop_points;

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

assert_eq!(
vec![
Expand Down Expand Up @@ -2968,7 +2984,7 @@ mod tests {
collections.stop_points = stop_points;

super::read_routes(&mut handler, &mut collections).unwrap();
let val = super::manage_stop_times(&mut collections, &mut handler);
let val = super::manage_stop_times(&mut collections, &mut handler, false);

// the first stop time of the vj has no departure/arrival, it's an error
let err = val.unwrap_err();
Expand Down Expand Up @@ -3062,4 +3078,91 @@ mod tests {
assert_eq!(4, levels.len());
})
}
#[test]
fn gtfs_stop_times_precision() {
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,my first desc,0.1,1.2,0,\n\
sp:02,my stop point name 2,,0.2,1.5,0,\n\
sp:03,my stop point name 2,,0.2,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,,";

let stop_times_content = "trip_id,arrival_time,departure_time,stop_id,stop_sequence,stop_headsign,pickup_type,drop_off_type,shape_dist_traveled,timepoint\n\
1,06:00:00,06:00:00,sp:01,1,over there,,,,0\n\
1,06:06:27,06:06:27,sp:02,2,,2,1,,1\n\
1,06:06:27,06:06:27,sp:03,3,,2,1,,";

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).unwrap();
super::manage_stop_times(&mut collections, &mut handler, true).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: true,
local_zone_id: None,
precision: Some(StopTimePrecision::Estimated),
},
StopTime {
stop_point_idx: collections.stop_points.get_idx("sp:02").unwrap(),
sequence: 2,
arrival_time: Time::new(6, 6, 27),
departure_time: Time::new(6, 6, 27),
boarding_duration: 0,
alighting_duration: 0,
pickup_type: 2,
drop_off_type: 1,
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, 6, 27),
departure_time: Time::new(6, 6, 27),
boarding_duration: 0,
alighting_duration: 0,
pickup_type: 2,
drop_off_type: 1,
datetime_estimated: false,
local_zone_id: None,
precision: Some(StopTimePrecision::Exact),
},
],
collections.vehicle_journeys.into_vec()[0].stop_times
);
});
}
}
4 changes: 4 additions & 0 deletions src/gtfs/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,7 @@ mod tests {
drop_off_type: 1,
datetime_estimated: false,
local_zone_id: None,
precision: None,
},
objects::StopTime {
stop_point_idx: sps.get_idx("OIF:SP:36:2127").unwrap(),
Expand All @@ -892,6 +893,7 @@ mod tests {
drop_off_type: 0,
datetime_estimated: false,
local_zone_id: None,
precision: None,
},
],
};
Expand Down Expand Up @@ -1105,6 +1107,7 @@ mod tests {
drop_off_type: 0,
datetime_estimated: false,
local_zone_id: None,
precision: None,
},
StopTime {
stop_point_idx: stop_points.get_idx("sp:01").unwrap(),
Expand All @@ -1117,6 +1120,7 @@ mod tests {
drop_off_type: 1,
datetime_estimated: true,
local_zone_id: Some(3),
precision: None,
},
];
let vehicle_journeys = CollectionWithId::from(VehicleJourney {
Expand Down
1 change: 1 addition & 0 deletions src/kv1/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ fn make_vjs_and_stop_times(
drop_off_type: 0,
datetime_estimated: false,
local_zone_id: None,
precision: None,
});
}
collections.vehicle_journeys =
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ mod validity_period;
pub mod vptranslator;

/// Current version of the NTFS format
pub const NTFS_VERSION: &str = "0.10.0";
pub const NTFS_VERSION: &str = "0.11.0";

lazy_static::lazy_static! {
/// Current datetime
Expand Down
1 change: 1 addition & 0 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,7 @@ mod tests {
drop_off_type: 0,
datetime_estimated: false,
local_zone_id: Some(0),
precision: None,
};
collections
.vehicle_journeys
Expand Down
1 change: 1 addition & 0 deletions src/netex_idf/offers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ fn stop_times(
drop_off_type,
datetime_estimated: false,
local_zone_id,
precision: None,
};

Ok(stop_time)
Expand Down
7 changes: 6 additions & 1 deletion src/ntfs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ struct StopTime {
local_zone_id: Option<u16>,
stop_headsign: Option<String>,
stop_time_id: Option<String>,
#[serde(rename = "stop_time_precision")]
precision: Option<StopTimePrecision>,
}

#[derivative(Default)]
Expand Down Expand Up @@ -392,7 +394,7 @@ mod tests {
("feed_end_date".to_string(), "20180131".to_string()),
("feed_publisher_name".to_string(), "Nicaragua".to_string()),
("feed_start_date".to_string(), "20180130".to_string()),
("ntfs_version".to_string(), "0.10.0".to_string()),
("ntfs_version".to_string(), "0.11.0".to_string()),
("tartare_platform".to_string(), "dev".to_string()),
],
collections
Expand Down Expand Up @@ -631,6 +633,7 @@ mod tests {
drop_off_type: 1,
datetime_estimated: false,
local_zone_id: None,
precision: Some(StopTimePrecision::Exact),
},
StopTime {
stop_point_idx: stop_points.get_idx("OIF:SP:36:2127").unwrap(),
Expand All @@ -643,6 +646,7 @@ mod tests {
drop_off_type: 0,
datetime_estimated: false,
local_zone_id: None,
precision: Some(StopTimePrecision::Exact),
},
],
},
Expand Down Expand Up @@ -1057,6 +1061,7 @@ mod tests {
drop_off_type: 2,
datetime_estimated: false,
local_zone_id: None,
precision: None,
}],
});

Expand Down
Loading

0 comments on commit 8e02b82

Please sign in to comment.