Skip to content

Commit

Permalink
Merge pull request #149 from nberard/issue-760-stop-location-type-2
Browse files Browse the repository at this point in the history
handle stop.location_type = 2 for ntfs
  • Loading branch information
datanel committed Nov 22, 2018
2 parents 4091850 + ee1ec67 commit e2e02e6
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 12 deletions.
2 changes: 2 additions & 0 deletions fixtures/minimal_ntfs/stops.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ CHA,Châtelet,48.858137,2.348145,1,
CHAM,Châtelet (Metro),48.858137,2.348145,0,CHA
MTP,Montparnasse,48.842481,2.321783,1,
MTPB,Montparnasse (Bus),48.842481,2.321783,0,MTP
MTPZ,Montparnasse Zone,48.842481,2.321783,2,
CDGZ,Charles de Gaulle Zone,48.842481,2.321783,2,
21 changes: 21 additions & 0 deletions fixtures/ntfs2ntfs/stops.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
stop_id,stop_name,visible,fare_zone_id,stop_lon,stop_lat,location_type,parent_station,stop_timezone,geometry_id,equipment_id
GDLR,Gare de Lyon (RER),1,,2.372987,48.844746,0,GDL,,,
GDLM,Gare de Lyon (Metro),1,,2.372987,48.844746,0,GDL,,,
GDLB,Gare de Lyon (Bus),1,,2.372987,48.844746,0,GDL,,,
NATR,Nation (RER),1,,2.396497,48.84849,0,NAT,,,
NATM,Nation (Metro),1,,2.396497,48.84849,0,NAT,,,
CDGR,Charles de Gaulle (RER),1,,2.295354,48.873965,0,CDG,,,
CDGM,Charles de Gaulle (Metro),1,,2.295354,48.873965,0,CDG,,,
DEFR,La Défense (RER),1,,2.238964,48.891737,0,DEF,,,
CHAM,Châtelet (Metro),1,,2.348145,48.858137,0,CHA,,,
MTPB,Montparnasse (Bus),1,,2.321783,48.842481,0,MTP,,,
MTPZ,Montparnasse Zone,1,,2.321783,48.842481,2,Navitia:MTPZ,,,
CDGZ,Charles de Gaulle Zone,1,,2.321783,48.842481,2,Navitia:CDGZ,,,
GDL,Gare de Lyon,1,,2.372987,48.844746,1,,,,
NAT,Nation,1,,2.396497,48.84849,1,,,,
CDG,Charles de Gaulle,1,,2.295354,48.873965,1,,,,
DEF,La Défense,1,,2.238964,48.891737,1,,,,
CHA,Châtelet,1,,2.348145,48.858137,1,,,,
MTP,Montparnasse,1,,2.321783,48.842481,1,,,,
Navitia:MTPZ,Montparnasse Zone,1,,2.321783,48.842481,1,,,,
Navitia:CDGZ,Charles de Gaulle Zone,1,,2.321783,48.842481,1,,,,
2 changes: 1 addition & 1 deletion src/gtfs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ enum StopLocationType {
#[serde(rename = "1")]
StopArea,
#[serde(rename = "2")]
StopEntrace,
StopEntrance,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
Expand Down
9 changes: 5 additions & 4 deletions src/gtfs/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use csv;
use failure::ResultExt;
use geo_types::{LineString, Point};
use model::Collections;
use objects::{self, CommentLinksT, Contributor, Coord, KeysValues, TransportType};
use objects::{self, CommentLinksT, Contributor, Coord, KeysValues, StopType, TransportType};
use read_utils;
use std::collections::{BTreeSet, HashMap, HashSet};
use std::fs::File;
Expand Down Expand Up @@ -122,6 +122,7 @@ impl From<Stop> for objects::StopPoint {
geometry_id: None,
equipment_id: None,
fare_zone_id: None,
stop_type: StopType::Point,
}
}
}
Expand Down Expand Up @@ -461,7 +462,7 @@ pub fn read_stops<P: AsRef<path::Path>>(
stop.parent_station = Some(new_stop_area.id.clone());
stop_areas.push(objects::StopArea::from(new_stop_area));
}
let mut stop_point = objects::StopPoint::from(stop);
let mut stop_point = <objects::StopPoint>::from(stop);
stop_point.comment_links = comment_links;
stop_point.equipment_id = equipment_id;
stop_points.push(stop_point);
Expand All @@ -472,9 +473,9 @@ pub fn read_stops<P: AsRef<path::Path>>(
stop_area.equipment_id = equipment_id;
stop_areas.push(stop_area);
}
StopLocationType::StopEntrace => warn!(
StopLocationType::StopEntrance => warn!(
"stop location type {:?} not handled for the moment, skipping",
StopLocationType::StopEntrace
StopLocationType::StopEntrance
),
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/gtfs/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ mod tests {
geometry_id: None,
equipment_id: Some("1".to_string()),
fare_zone_id: Some("1".to_string()),
stop_type: StopType::Point,
};

let expected = Stop {
Expand Down Expand Up @@ -640,6 +641,7 @@ mod tests {
geometry_id: None,
equipment_id: None,
fare_zone_id: None,
stop_type: StopType::Point,
};

let expected = Stop {
Expand Down Expand Up @@ -763,6 +765,7 @@ mod tests {
geometry_id: None,
equipment_id: None,
fare_zone_id: Some("1".to_string()),
stop_type: StopType::Point,
},
objects::StopPoint {
id: "OIF:SP:36:2127".to_string(),
Expand All @@ -780,6 +783,7 @@ mod tests {
geometry_id: None,
equipment_id: None,
fare_zone_id: None,
stop_type: StopType::Point,
},
]).unwrap();
let routes = CollectionWithId::new(vec![objects::Route {
Expand Down Expand Up @@ -906,6 +910,7 @@ mod tests {
geometry_id: None,
equipment_id: None,
fare_zone_id: None,
stop_type: StopType::Point,
}]).unwrap();
let tmp_dir = TempDir::new("navitia_model_tests").expect("create temp dir");
write_stop_extensions(tmp_dir.path(), &stop_points, &stop_areas).unwrap();
Expand Down Expand Up @@ -1049,6 +1054,7 @@ mod tests {
equipment_id: None,
stop_area_id: "sa_1".to_string(),
fare_zone_id: None,
stop_type: StopType::Point,
}]).unwrap();
let stop_times_vec = vec![
StopTime {
Expand Down
5 changes: 5 additions & 0 deletions src/ntfs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ mod tests {
geometry_id: None,
equipment_id: None,
fare_zone_id: Some("1".to_string()),
stop_type: StopType::Point,
},
StopPoint {
id: "OIF:SP:36:2127".to_string(),
Expand All @@ -462,6 +463,7 @@ mod tests {
geometry_id: None,
equipment_id: None,
fare_zone_id: None,
stop_type: StopType::Point,
},
]).unwrap();
let vehicle_journeys = CollectionWithId::new(vec![
Expand Down Expand Up @@ -682,6 +684,7 @@ mod tests {
equipment_id: Some("equipment_1".to_string()),
stop_area_id: "sa_1".to_string(),
fare_zone_id: Some("1".to_string()),
stop_type: StopType::Point,
},
// stop point with no parent station
StopPoint {
Expand All @@ -700,6 +703,7 @@ mod tests {
equipment_id: None,
stop_area_id: "Navitia:sa_2".to_string(),
fare_zone_id: None,
stop_type: StopType::Point,
},
]).unwrap();

Expand Down Expand Up @@ -796,6 +800,7 @@ mod tests {
equipment_id: None,
stop_area_id: "sa_1".to_string(),
fare_zone_id: None,
stop_type: StopType::Point,
}]).unwrap();

let stop_areas = CollectionWithId::new(vec![StopArea {
Expand Down
14 changes: 10 additions & 4 deletions src/ntfs/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ impl From<Stop> for StopArea {
}
}
}
impl From<Stop> for StopPoint {
fn from(stop: Stop) -> StopPoint {
impl StopPoint {
fn from_with_type(stop: Stop, stop_type: StopType) -> StopPoint {
let id = stop.id;
StopPoint {
id,
Expand All @@ -64,6 +64,7 @@ impl From<Stop> for StopPoint {
geometry_id: stop.geometry_id,
equipment_id: stop.equipment_id,
fare_zone_id: stop.fare_zone_id,
stop_type,
}
}
}
Expand All @@ -77,14 +78,19 @@ pub fn manage_stops(collections: &mut Collections, path: &path::Path) -> Result<
for stop in rdr.deserialize() {
let mut stop: Stop = stop.with_context(ctx_from_path!(path))?;
match stop.location_type {
0 => {
0 | 2 => {
if stop.parent_station.is_none() {
let mut new_stop_area = stop.clone();
new_stop_area.id = format!("Navitia:{}", new_stop_area.id);
stop.parent_station = Some(new_stop_area.id.clone());
stop_areas.push(StopArea::from(new_stop_area));
}
stop_points.push(StopPoint::from(stop));
let stop_type = if stop.location_type == 0 {
StopType::Point
} else {
StopType::Zone
};
stop_points.push(StopPoint::from_with_type(stop, stop_type));
}
1 => stop_areas.push(StopArea::from(stop)),
i => warn!("stop.location_type = {} not yet supported, skipping.", i),
Expand Down
6 changes: 5 additions & 1 deletion src/ntfs/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,18 @@ pub fn write_stops(
let path = path.join("stops.txt");
let mut wtr = csv::Writer::from_path(&path).with_context(ctx_from_path!(path))?;
for st in stop_points.values() {
let location_type = match st.stop_type {
StopType::Point => 0,
StopType::Zone => 2,
};
wtr.serialize(Stop {
id: st.id.clone(),
visible: st.visible,
name: st.name.clone(),
lat: st.coord.lat,
lon: st.coord.lon,
fare_zone_id: st.fare_zone_id.clone(),
location_type: 0,
location_type: location_type,
parent_station: stop_areas.get(&st.stop_area_id).map(|sa| sa.id.clone()),
timezone: st.timezone.clone(),
equipment_id: st.equipment_id.clone(),
Expand Down
9 changes: 9 additions & 0 deletions src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,13 @@ impl GetObjectType for StopArea {
ObjectType::StopArea
}
}
#[derivative(Default)]
#[derive(Derivative, Debug, PartialEq)]
pub enum StopType {
#[derivative(Default)]
Point,
Zone,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct StopPoint {
Expand All @@ -927,6 +934,8 @@ pub struct StopPoint {
pub geometry_id: Option<String>,
pub equipment_id: Option<String>,
pub fare_zone_id: Option<String>,
#[serde(skip)]
pub stop_type: StopType,
}

impl Id<StopPoint> for StopPoint {
Expand Down
6 changes: 6 additions & 0 deletions src/transfers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ mod tests {
equipment_id: None,
stop_area_id: "sa_1".to_string(),
fare_zone_id: None,
stop_type: StopType::Point,
},
StopPoint {
id: "sp_2".to_string(),
Expand All @@ -306,6 +307,7 @@ mod tests {
equipment_id: None,
stop_area_id: "sa_1".to_string(),
fare_zone_id: None,
stop_type: StopType::Point,
},
StopPoint {
id: "sp_3".to_string(),
Expand All @@ -323,6 +325,7 @@ mod tests {
equipment_id: None,
stop_area_id: "sa_1".to_string(),
fare_zone_id: None,
stop_type: StopType::Point,
},
]).unwrap();

Expand Down Expand Up @@ -418,6 +421,7 @@ mod tests {
equipment_id: None,
stop_area_id: "sa_1".to_string(),
fare_zone_id: None,
stop_type: StopType::Point,
},
StopPoint {
id: "sp_2".to_string(),
Expand All @@ -435,6 +439,7 @@ mod tests {
equipment_id: None,
stop_area_id: "sa_1".to_string(),
fare_zone_id: None,
stop_type: StopType::Point,
},
StopPoint {
id: "sp_3".to_string(),
Expand All @@ -452,6 +457,7 @@ mod tests {
equipment_id: None,
stop_area_id: "sa_1".to_string(),
fare_zone_id: None,
stop_type: StopType::Point,
},
]).unwrap();

Expand Down
27 changes: 25 additions & 2 deletions tests/read_ntfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use navitia_model::collection::{CollectionWithId, Id, Idx};
use navitia_model::model::{GetCorresponding, Model};
use navitia_model::objects::*;
use navitia_model::relations::IdxSet;
use navitia_model::test_utils::*;
use tempdir::TempDir;

fn get<T, U>(idx: Idx<T>, collection: &CollectionWithId<U>, objects: &Model) -> Vec<String>
Expand All @@ -38,8 +39,8 @@ where
fn minimal() {
let ntm = navitia_model::ntfs::read("fixtures/minimal_ntfs/").unwrap();

assert_eq!(6, ntm.stop_areas.len());
assert_eq!(10, ntm.stop_points.len());
assert_eq!(8, ntm.stop_areas.len());
assert_eq!(12, ntm.stop_points.len());
assert_eq!(3, ntm.commercial_modes.len());
assert_eq!(3, ntm.lines.len());
assert_eq!(6, ntm.routes.len());
Expand Down Expand Up @@ -87,6 +88,28 @@ fn minimal() {
);
}

#[test]
fn ntfs_stop_zones() {
let ntm = navitia_model::ntfs::read("fixtures/minimal_ntfs/").unwrap();
let stop_zone_1 = ntm.stop_points.get("MTPZ").unwrap();
assert_eq!(stop_zone_1.stop_type, StopType::Zone);
let stop_zone_2 = ntm.stop_points.get("CDGZ").unwrap();
assert_eq!(stop_zone_2.stop_type, StopType::Zone);
}

#[test]
fn ntfs_stops_output() {
let ntm = navitia_model::ntfs::read("fixtures/minimal_ntfs/").unwrap();
test_in_tmp_dir(|output_dir| {
navitia_model::ntfs::write(&ntm, output_dir).unwrap();
compare_output_dir_with_expected(
output_dir,
&["stops.txt".to_string()],
"fixtures/ntfs2ntfs".to_string(),
);
});
}

#[test]
fn ntfs() {
let pt_objects = navitia_model::ntfs::read("fixtures/ntfs/").unwrap();
Expand Down

0 comments on commit e2e02e6

Please sign in to comment.