Skip to content

Commit

Permalink
Merge pull request #203 from nberard/nd-89-gtfs-stop-zones
Browse files Browse the repository at this point in the history
remove stop_zone in gtfs
  • Loading branch information
datanel authored Mar 20, 2019
2 parents 7601ee7 + a8c216a commit 82dc71c
Show file tree
Hide file tree
Showing 24 changed files with 124 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
authors = ["Kisio Digital <[email protected]>"]
name = "navitia_model"
version = "0.3.1"
version = "0.3.2"
edition = "2018"

[dependencies]
Expand Down
3 changes: 3 additions & 0 deletions fixtures/ntfs2gtfs/input/calendar_dates.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
service_id,date,exception_type
service:1,20180101,1
service:2,20180201,1
2 changes: 2 additions & 0 deletions fixtures/ntfs2gtfs/input/commercial_modes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
commercial_mode_id,commercial_mode_name
Metro,Metro
3 changes: 3 additions & 0 deletions fixtures/ntfs2gtfs/input/companies.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
company_id,company_name
company:kept,The Great Company
company:removed,The Bad Company
3 changes: 3 additions & 0 deletions fixtures/ntfs2gtfs/input/contributors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
contributor_id,contributor_name
contributor:kept,The Great Contributor used
contributor:removed,The Great Contributor useless
3 changes: 3 additions & 0 deletions fixtures/ntfs2gtfs/input/datasets.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dataset_id,contributor_id,dataset_start_date,dataset_end_date
dataset:kept,contributor:kept,20180101,20181231
dataset:removed,contributor:removed,20180101,20181231
3 changes: 3 additions & 0 deletions fixtures/ntfs2gtfs/input/equipments.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
equipment_id,wheelchair_boarding,elevator
eq:kept,1,
eq:removed,,1
2 changes: 2 additions & 0 deletions fixtures/ntfs2gtfs/input/feed_infos.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
feed_info_param,feed_info_value
ntfs_version,0.7
3 changes: 3 additions & 0 deletions fixtures/ntfs2gtfs/input/lines.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
line_id,line_name,network_id,commercial_mode_id,geometry_id
line:1,Metro 1,network:kept,Metro,
line:2,Metro 2,network:removed,Metro,
3 changes: 3 additions & 0 deletions fixtures/ntfs2gtfs/input/networks.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
network_id,network_name
network:kept,The Great Network
network:removed,The Great Network to be removed
2 changes: 2 additions & 0 deletions fixtures/ntfs2gtfs/input/physical_modes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
physical_mode_id,physical_mode_name
Metro,Metro
5 changes: 5 additions & 0 deletions fixtures/ntfs2gtfs/input/routes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
route_id,route_name,line_id,geometry_id
route:1,whatever,line:1,
route:2,whatever,line:1,
route:3,whatever,line:2,
route:4,whatever,line:2,
8 changes: 8 additions & 0 deletions fixtures/ntfs2gtfs/input/stop_times.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
trip_id,stop_sequence,stop_id,arrival_time,departure_time,datetime_estimated,stop_time_id,stop_headsign
trip:1,0,stop:point:1,9:00:00,9:00:00,,stoptime:1,
trip:1,1,stop:point:2,09:10:00,09:10:00,,stoptime:2,
trip:2,0,stop:zone:1,9:00:00,9:00:00,,stoptime:3,
trip:2,1,stop:zone:2,09:10:00,09:10:00,,stoptime:4,
trip:3,0,stop:point:1,9:00:00,9:00:00,,stoptime:5,
trip:3,1,stop:zone:2,09:10:00,09:10:00,,stoptime:5,
trip:3,0,stop:point:2,9:00:00,9:00:00,,stoptime:6,
7 changes: 7 additions & 0 deletions fixtures/ntfs2gtfs/input/stops.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
stop_id,stop_name,stop_lat,stop_lon,location_type,parent_station,equipment_id
stop:area:1,whatever,48.8,2.3,1,,
stop:area:2,whatever,48.8,2.3,1,,
stop:point:1,whatever,48.8,2.3,0,stop:area:1,
stop:point:2,whatever,48.8,2.3,0,stop:area:1,
stop:zone:1,whatever,48.8,2.3,2,stop:area:2,
stop:zone:2,whatever,48.8,2.3,2,stop:area:2,
4 changes: 4 additions & 0 deletions fixtures/ntfs2gtfs/input/trips.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
route_id,service_id,trip_id,company_id,physical_mode_id,dataset_id,trip_property_id,geometry_id
route:1,service:1,trip:1,company:kept,Metro,dataset:kept,,
route:2,service:1,trip:2,company:removed,Metro,dataset:removed,,
route:2,service:1,trip:3,company:kept,Metro,dataset:kept,,
2 changes: 2 additions & 0 deletions fixtures/ntfs2gtfs/output/agency.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
agency_id,agency_name,agency_url,agency_timezone,agency_lang,agency_phone,agency_email
network:kept,The Great Network,http://www.navitia.io/,Europe/Paris,,,
3 changes: 3 additions & 0 deletions fixtures/ntfs2gtfs/output/calendar_dates.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
service_id,date,exception_type
service:1,20180101,1
service:2,20180201,1
2 changes: 2 additions & 0 deletions fixtures/ntfs2gtfs/output/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,route_sort_order
line:1,network:kept,,Metro 1,,1,,,,
5 changes: 5 additions & 0 deletions fixtures/ntfs2gtfs/output/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,pickup_type,drop_off_type,local_zone_id,stop_headsign,timepoint
trip:1,09:00:00,09:00:00,stop:point:1,0,0,0,,,1
trip:1,09:10:00,09:10:00,stop:point:2,1,0,0,,,1
trip:3,09:00:00,09:00:00,stop:point:1,0,0,0,,,1
trip:3,09:00:00,09:00:00,stop:point:2,0,0,0,,,1
4 changes: 4 additions & 0 deletions fixtures/ntfs2gtfs/output/stops.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
stop_id,stop_code,stop_name,stop_desc,stop_lon,stop_lat,zone_id,stop_url,location_type,parent_station,stop_timezone,wheelchair_boarding
stop:point:1,,whatever,,2.3,48.8,,,0,stop:area:1,,0
stop:point:2,,whatever,,2.3,48.8,,,0,stop:area:1,,0
stop:area:1,,whatever,,2.3,48.8,,,1,,,0
3 changes: 3 additions & 0 deletions fixtures/ntfs2gtfs/output/trips.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
route_id,service_id,trip_id,trip_headsign,trip_short_name,direction_id,block_id,shape_id,wheelchair_accessible,bikes_allowed
line:1,service:1,trip:1,whatever,,1,,,0,0
line:1,service:1,trip:3,whatever,,1,,,0,0
3 changes: 2 additions & 1 deletion src/bin/ntfs2gtfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ fn run() -> Result<()> {
info!("Launching ntfs2gtfs...");
let opt = Opt::from_args();
let model = navitia_model::ntfs::read(opt.input)?;
navitia_model::gtfs::write(&model, opt.output)?;

navitia_model::gtfs::write(model, opt.output)?;
Ok(())
}

Expand Down
24 changes: 23 additions & 1 deletion src/gtfs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ mod read;
mod write;

use crate::collection::CollectionWithId;
use crate::collection::Idx;
use crate::common_format;
use crate::common_format::{manage_calendars, Availability};
use crate::gtfs::read::EquipmentList;
use crate::model::{Collections, Model};
use crate::objects;
use crate::objects::Time;
use crate::objects::{StopPoint, StopType};
use crate::read_utils;
use crate::read_utils::add_prefix;
use crate::utils::*;
Expand Down Expand Up @@ -384,10 +386,30 @@ struct Route {
sort_order: Option<u32>,
}

fn remove_stop_zones(model: Model) -> Result<Collections> {
let mut collections = model.into_collections();
collections
.stop_points
.retain(|sp| sp.stop_type != StopType::Zone);
let stop_point_ids: Vec<&Idx<StopPoint>> =
collections.stop_points.get_id_to_idx().values().collect();
let mut vjs = collections.vehicle_journeys.take();
for vj in &mut vjs {
vj.stop_times
.retain(|st| stop_point_ids.contains(&&st.stop_point_idx));
}
vjs.retain(|vj| !vj.stop_times.is_empty());
collections.vehicle_journeys = CollectionWithId::new(vjs)?;
Ok(collections)
}

/// Exports a `Model` to [GTFS](http://gtfs.org/) files
/// in the given directory.
/// see [NTFS to GTFS conversion](https://github.com/CanalTP/navitia_model/blob/master/src/documentation/ntfs2gtfs.md)
pub fn write<P: AsRef<Path>>(model: &Model, path: P) -> Result<()> {
pub fn write<P: AsRef<Path>>(model: Model, path: P) -> Result<()> {
let mut collections = remove_stop_zones(model)?;
collections.sanitize()?;
let model = Model::new(collections)?;
let path = path.as_ref();
info!("Writing GTFS to {:?}", path);

Expand Down
28 changes: 28 additions & 0 deletions tests/ntfs2gtfs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2017 Kisio Digital and/or its affiliates.
//
// This program is free software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see
// <http://www.gnu.org/licenses/>.

use navitia_model;
use navitia_model::test_utils::*;

#[test]
fn test_stop_zones_not_exported_and_cleaned() {
test_in_tmp_dir(|path| {
let input = "./fixtures/ntfs2gtfs/input";
let model = navitia_model::ntfs::read(input).unwrap();
navitia_model::gtfs::write(model, path).unwrap();
compare_output_dir_with_expected(&path, None, "./fixtures/ntfs2gtfs/output");
});
}

0 comments on commit 82dc71c

Please sign in to comment.