Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 0 additions & 12 deletions documentation/ntfs_to_gtfs_specs.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ The following additional files are generated only if the corresponding objects a

* [transfers](#transferstxt)
* [shapes](#shapestxt)
* [stop_extensions](#stop_extensionstxt): additional information providing the complementary stop codes used in external systems.
* [object_codes_extension](#object_codes_extensiontxt): additional information providing the complementary codes for various objects (stops, networks, lines, routes, trips, companies) used in external systems.

[GTFS]: https://gtfs.org/reference/static
Expand Down Expand Up @@ -157,17 +156,6 @@ This file is the same as the NTFS calendar_dates.txt file. All dates of service
| shape_pt_lon | yes | geometries.txt | geometry_wkt | Longitude of the stop in the shape |
| shape_pt_sequence | yes | | | Integer starting at 0 and increase by an increment of one for every point in the shape |

### stop_extensions.txt

This file contains the complementary stop codes from the NTFS object_codes.txt file. If no additional stop code is specified, this file is not generated.
If N complementary codes are specified for a stop, there will be N separate lines in the file for the different stop_id/system_name pairs.

| GTFS field | Required | NTFS file | NTFS field | Note |
| ----------- | -------- | ---------------- | ------------- | ----------------------------------------------------------------------------------------------------- |
| stop_id | yes | object_codes.txt | object_id | `stop_id` of the stop having a complementary code specified (link to the [stops.txt](#stopstxt) file) |
| system_name | yes | object_codes.txt | object_system | |
| system_code | yes | object_codes.txt | object_code | |

### object_codes_extension.txt

**Note**: This is a GTFS extension file (not part of the standard GTFS specification).
Expand Down

This file was deleted.

4 changes: 2 additions & 2 deletions ntfs2gtfs/tests/ntfs2gtfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ fn test_ntfs2gtfs_with_pickup_drop_off_windows_stoptimes() {
}

#[test]
fn test_stop_and_object_codes_extension() {
fn test_object_codes_extension() {
let output_dir = TempDir::new().expect("create temp dir failed");
Command::cargo_bin("ntfs2gtfs")
.expect("Failed to find binary 'ntfs2gtfs'")
Expand All @@ -232,7 +232,7 @@ fn test_stop_and_object_codes_extension() {
.success();
compare_output_dir_with_expected(
output_dir,
Some(vec!["stop_extensions.txt", "object_codes_extension.txt"]),
Some(vec!["object_codes_extension.txt"]),
"./tests/fixtures/stop_and_object_codes_extension",
);
}
1 change: 0 additions & 1 deletion src/gtfs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,6 @@ pub fn write<P: AsRef<Path>>(model: Model, path: P, extend_route_type: bool) ->
let gtfs_trips = write::write_trips(path, &model)?;
write::write_attributions(path, &model.companies, gtfs_trips)?;
write::write_routes(path, &model, extend_route_type)?;
write::write_stop_extensions(path, &model.stop_points, &model.stop_areas)?;
write::write_stop_times(
path,
&model.vehicle_journeys,
Expand Down
126 changes: 2 additions & 124 deletions src/gtfs/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ use crate::Result;
use anyhow::Context;
use geo::Geometry as GeoGeometry;
use relational_types::IdxSet;
use serde::{Deserialize, Serialize};
use serde::Serialize;
use std::collections::{HashMap, HashSet};
use std::path;
use tracing::{info, warn};
use typed_index_collection::{Collection, CollectionWithId, Id, Idx};
use typed_index_collection::{Collection, CollectionWithId, Idx};

pub fn write_transfers(path: &path::Path, transfers: &Collection<NtfsTransfer>) -> Result<()> {
if transfers.is_empty() {
Expand Down Expand Up @@ -372,59 +372,6 @@ pub fn write_attributions(
Ok(())
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct StopExtension {
#[serde(rename = "object_id")]
id: String,
#[serde(rename = "object_system")]
name: String,
#[serde(rename = "object_code")]
code: String,
}

fn stop_extensions_from_collection_with_id<T>(
collections: &CollectionWithId<T>,
) -> impl Iterator<Item = StopExtension> + '_
where
T: Id<T> + Codes,
{
collections
.values()
.flat_map(|obj| obj.codes().iter().map(move |c| (obj.id(), c)))
.map(|(id, (name, code))| StopExtension {
id: id.to_string(),
name: name.to_string(),
code: code.to_string(),
})
}

pub fn write_stop_extensions(
path: &path::Path,
stop_points: &CollectionWithId<StopPoint>,
stop_areas: &CollectionWithId<StopArea>,
) -> Result<()> {
let mut stop_extensions = Vec::new();
stop_extensions.extend(stop_extensions_from_collection_with_id(stop_points));
stop_extensions.extend(stop_extensions_from_collection_with_id(stop_areas));
if stop_extensions.is_empty() {
return Ok(());
}
let file = "stop_extensions.txt";
info!(file_name = %file, "Writing");

let path = path.join(file);
let mut wtr =
csv::Writer::from_path(&path).with_context(|| format!("Error reading {path:?}"))?;
for se in stop_extensions {
wtr.serialize(se)
.with_context(|| format!("Error reading {path:?}"))?;
}
wtr.flush()
.with_context(|| format!("Error reading {path:?}"))?;

Ok(())
}

#[derive(Serialize, Debug, Clone, Eq, Hash, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum GTFSObjectType {
Expand Down Expand Up @@ -1282,75 +1229,6 @@ mod tests {
assert_eq!(expected, make_gtfs_trip_from_ntfs_vj(&vj_coach, &model));
}

#[test]
fn ntfs_object_code_to_stop_extensions() {
let mut sa_codes: BTreeSet<(String, String)> = BTreeSet::new();
sa_codes.insert(("sa name 1".to_string(), "sa_code_1".to_string()));
sa_codes.insert(("sa name 2".to_string(), "sa_code_2".to_string()));
let stop_areas = CollectionWithId::from(StopArea {
id: "sa:01".to_string(),
name: "sa:01".to_string(),
codes: sa_codes,
object_properties: PropertiesMap::default(),
comment_links: LinksT::default(),
visible: true,
coord: Coord {
lon: 2.073,
lat: 48.799,
},
timezone: None,
geometry_id: None,
level_id: Some("level0".to_string()),
equipment_id: None,
address_id: None,
});
let mut sp_codes: BTreeSet<(String, String)> = BTreeSet::new();
sp_codes.insert(("sp name 1".to_string(), "sp_code_1".to_string()));
sp_codes.insert(("sp name 2".to_string(), "sp_code_2".to_string()));
sp_codes.insert(("sp name 3".to_string(), "sp_code_3".to_string()));
let stop_points = CollectionWithId::from(StopPoint {
id: "sp:01".to_string(),
name: "sp:01".to_string(),
codes: sp_codes,
visible: true,
coord: Coord {
lon: 2.073,
lat: 48.799,
},
stop_area_id: "sa:01".to_string(),
stop_type: StopType::Point,
..Default::default()
});
let tmp_dir = tempdir().expect("create temp dir");
write_stop_extensions(tmp_dir.path(), &stop_points, &stop_areas).unwrap();
let output_file_path = tmp_dir.path().join("stop_extensions.txt");
let mut output_file = File::open(output_file_path.clone())
.unwrap_or_else(|_| panic!("file {:?} not found", output_file_path));
let mut output_contents = String::new();
output_file.read_to_string(&mut output_contents).unwrap();
assert_eq!(
"object_id,object_system,object_code\n\
sp:01,sp name 1,sp_code_1\n\
sp:01,sp name 2,sp_code_2\n\
sp:01,sp name 3,sp_code_3\n\
sa:01,sa name 1,sa_code_1\n\
sa:01,sa name 2,sa_code_2\n",
output_contents
);
tmp_dir.close().expect("delete temp dir");
}

#[test]
fn ntfs_object_code_to_stop_extensions_nothing_generated() {
let stop_areas = CollectionWithId::default();
let stop_points = CollectionWithId::default();
let tmp_dir = tempdir().expect("create temp dir");
write_stop_extensions(tmp_dir.path(), &stop_points, &stop_areas).unwrap();
let output_file_path = tmp_dir.path().join("stop_extensions.txt");
assert!(!output_file_path.exists());
tmp_dir.close().expect("delete temp dir");
}

#[test]
fn ntfs_geometry_linestring_exported() {
let geo = objects::Geometry {
Expand Down