Skip to content

Commit

Permalink
- make only one of route_short_name and route_long_name required
Browse files Browse the repository at this point in the history
- implement the optionality of agency if there only is one
  • Loading branch information
Tobias Kohl committed Dec 19, 2024
1 parent a80c736 commit 0bd70ff
Show file tree
Hide file tree
Showing 63 changed files with 657 additions and 10 deletions.
35 changes: 29 additions & 6 deletions src/main/java/org/matsim/pt2matsim/gtfs/GtfsFeedImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,22 @@ protected void loadAgencies() throws IOException {
String[] line = reader.readNext();
while(line != null) {
l++;
String agencyId = line[col.get(GtfsDefinitions.AGENCY_ID)];
String agencyId = col.containsKey(GtfsDefinitions.AGENCY_ID) ?
line[col.get(GtfsDefinitions.AGENCY_ID)] :
line[col.get(GtfsDefinitions.AGENCY_NAME)];
AgencyImpl agency = new AgencyImpl(agencyId, line[col.get(GtfsDefinitions.AGENCY_NAME)], line[col.get(GtfsDefinitions.AGENCY_URL)], line[col.get(GtfsDefinitions.AGENCY_TIMEZONE)]);
agencies.put(agencyId, agency);

line = reader.readNext();
}

reader.close();

if (this.agencies.isEmpty()) {
throw new IllegalArgumentException("agencies file must contain at least one agency!");
} else if (this.agencies.size() > 1 && !col.containsKey(GtfsDefinitions.AGENCY_ID)) {
throw new IllegalArgumentException("agencies file has more than one entry but no id column!");
}
} catch (ArrayIndexOutOfBoundsException e) {
throw new RuntimeException("Line " + l + " in agency.txt is empty or malformed.");
} catch (CsvValidationException e) {
Expand Down Expand Up @@ -442,7 +450,16 @@ protected void loadRoutes() throws IOException {
CSVReader reader = createCSVReader(root + GtfsDefinitions.Files.ROUTES.fileName);
String[] header = reader.readNext();
Map<String, Integer> col = getIndices(header, GtfsDefinitions.Files.ROUTES.columns, GtfsDefinitions.Files.ROUTES.optionalColumns);

if (!col.containsKey(GtfsDefinitions.ROUTE_SHORT_NAME) && !col.containsKey(GtfsDefinitions.ROUTE_LONG_NAME)) {
throw new IllegalArgumentException("at least one of 'route_short_name' or 'route_long_name' is required but the dataset has neither column!");
}
Agency defaultAgency = null;
if (this.agencies.size() > 1 && !col.containsKey(GtfsDefinitions.AGENCY_ID)) {
throw new IllegalArgumentException("there is no column 'agency_id' in the routes file but there are multiple agencies in the agency file");
} else if (this.agencies.size() == 1) {
defaultAgency = this.agencies.values().stream().findAny().get();
}

String[] line = reader.readNext();
while(line != null) {
l++;
Expand All @@ -455,12 +472,18 @@ protected void loadRoutes() throws IOException {
extendedRouteType = ExtendedRouteType.Unknown;
}
String routeId = line[col.get(GtfsDefinitions.ROUTE_ID)];
String shortName = line[col.get(GtfsDefinitions.ROUTE_SHORT_NAME)];
String longName = line[col.get(GtfsDefinitions.ROUTE_LONG_NAME)];
String shortName = col.containsKey(GtfsDefinitions.ROUTE_SHORT_NAME) ?
line[col.get(GtfsDefinitions.ROUTE_SHORT_NAME)] :
line[col.get(GtfsDefinitions.ROUTE_LONG_NAME)];
String longName = col.containsKey(GtfsDefinitions.ROUTE_LONG_NAME) ?
line[col.get(GtfsDefinitions.ROUTE_LONG_NAME)] :
line[col.get(GtfsDefinitions.ROUTE_SHORT_NAME)];

Agency agency = this.agencies.get(line[col.get(GtfsDefinitions.AGENCY_ID)]);
Agency agency = col.containsKey(GtfsDefinitions.AGENCY_ID) ?
this.agencies.get(line[col.get(GtfsDefinitions.AGENCY_ID)]) :
defaultAgency;
if (agency == null) {
throw new RuntimeException("Line " + l + " in routes.txt references unknown agency id " + line[col.get(GtfsDefinitions.AGENCY_ID)]);
throw new IllegalArgumentException("Line " + l + " in routes.txt references unknown agency id " + line[col.get(GtfsDefinitions.AGENCY_ID)]);
}
Route newGtfsRoute = new RouteImpl(routeId, shortName, longName, agency, extendedRouteType);
routes.put(line[col.get(GtfsDefinitions.ROUTE_ID)], newGtfsRoute);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ public final class GtfsDefinitions {
*/
public enum Files {
AGENCY("Agency", "agency.txt",
new String[]{AGENCY_ID, AGENCY_NAME, AGENCY_URL, AGENCY_TIMEZONE},
new String[]{AGENCY_LANG, AGENCY_PHONE, AGENCY_FARE_URL, AGENCY_EMAIL}),
new String[]{AGENCY_NAME, AGENCY_URL, AGENCY_TIMEZONE},
new String[]{AGENCY_ID, AGENCY_LANG, AGENCY_PHONE, AGENCY_FARE_URL, AGENCY_EMAIL}),

STOPS("Stop", "stops.txt",
new String[]{STOP_ID, STOP_LON, STOP_LAT, STOP_NAME},
Expand All @@ -132,8 +132,8 @@ public enum Files {

ROUTES("Route",
"routes.txt",
new String[]{ROUTE_ID, AGENCY_ID, ROUTE_SHORT_NAME, ROUTE_LONG_NAME, ROUTE_TYPE},
new String[]{ROUTE_DESC, ROUTE_URL, ROUTE_COLOR, ROUTE_TEXT_COLOR}),
new String[]{ROUTE_ID, ROUTE_TYPE},
new String[]{AGENCY_ID, ROUTE_SHORT_NAME, ROUTE_LONG_NAME, ROUTE_DESC, ROUTE_URL, ROUTE_COLOR, ROUTE_TEXT_COLOR}),

TRIPS("Trip",
"trips.txt",
Expand Down
74 changes: 74 additions & 0 deletions src/test/java/org/matsim/pt2matsim/gtfs/GtfsMinimalCaseTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* *********************************************************************** *
* project: org.matsim.*
* *********************************************************************** *
* *
* copyright : (C) 2016 by the members listed in the COPYING, *
* LICENSE and WARRANTY file. *
* email : info at matsim dot org *
* *
* *********************************************************************** *
* *
* 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 2 of the License, or *
* (at your option) any later version. *
* See also COPYING, LICENSE and WARRANTY file *
* *
* *********************************************************************** */

package org.matsim.pt2matsim.gtfs;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.matsim.core.utils.geometry.transformations.TransformationFactory;
import org.matsim.pt2matsim.gtfs.lib.GtfsDefinitions;

/**
* This test should check that ftfs datasets, that only do the bare minimum (i.e. do our best
* do not meet the conditions in "conditionally required" and omit the corresponding fields)
* are still converted correctly
*
* @author Tobias Kohl / Senozon
*/
class GtfsMinimalCaseTest {

@Test
void noAgencyId() {
GtfsFeed feed = new GtfsFeedImpl("test/gtfs-feed-min/noAgencyId");
feed.getRoutes().values().forEach(route -> Assertions.assertNotNull(route.getAgency(), "no agency in route " + route.getId()));
Assertions.assertEquals("pt2matsim", feed.getRoutes().get("lineA").getAgency().getAgencyName());
Assertions.assertEquals("https://github.com/matsim-org/pt2matsim", feed.getRoutes().get("lineB").getAgency().getAgencyUrl());
Assertions.assertEquals("Europe/Zurich", feed.getRoutes().get("lineC").getAgency().getAgencyTimeZone());
}

@Test
void noShortName() {
GtfsFeed feed = new GtfsFeedImpl("test/gtfs-feed-min/noShortName");
Assertions.assertEquals("Bus Line A", feed.getRoutes().get("lineA").getShortName());
Assertions.assertEquals("Bus Line A", feed.getRoutes().get("lineA").getLongName());
Assertions.assertEquals(GtfsDefinitions.RouteType.BUS, feed.getRoutes().get("lineA").getRouteType());
Assertions.assertEquals("P2M", feed.getRoutes().get("lineB").getAgency().getId());
}

@Test
void noLongName() {
GtfsFeed feed = new GtfsFeedImpl("test/gtfs-feed-min/noLongName");
Assertions.assertEquals("Line A", feed.getRoutes().get("lineA").getShortName());
Assertions.assertEquals("Line A", feed.getRoutes().get("lineA").getLongName());
Assertions.assertEquals(GtfsDefinitions.RouteType.BUS, feed.getRoutes().get("lineA").getRouteType());
Assertions.assertEquals("P2M", feed.getRoutes().get("lineB").getAgency().getId());
}

@Test
void noNameAtAll() {
Assertions.assertThrows(IllegalArgumentException.class, () -> new GtfsFeedImpl("test/gtfs-feed-min/noNameAtAll"));
}

@Test
void multipleAgencies() {
Assertions.assertThrows(IllegalArgumentException.class, () -> new GtfsFeedImpl("test/gtfs-feed-min/multipleAgencies"));
}
}
2 changes: 2 additions & 0 deletions test/gtfs-feed-min/agency.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
agency_id,agency_name,agency_url,agency_timezone
P2M,pt2matsim,https://github.com/matsim-org/pt2matsim,Europe/Zurich
5 changes: 5 additions & 0 deletions test/gtfs-feed-min/calendar.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
service_id,monday,tuesday,wednesday,thursday,friday,saturday,sunday,start_date,end_date
WEEK,1,1,1,1,1,0,0,20181001,20181007
EXPR,0,0,0,0,0,0,0,20181001,20181007
WEND,0,0,0,0,0,1,1,20181001,20181007
EMPT,1,0,1,1,0,1,0,20181001,20181007
6 changes: 6 additions & 0 deletions test/gtfs-feed-min/calendar_dates.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
service_id,date,exception_type
WEEK,20181006,2
WEEK,20181007,2
EXPR,20181005,1
EMPT,20181002,1
EMPT,20181001,2
4 changes: 4 additions & 0 deletions test/gtfs-feed-min/frequencies.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
trip_id,start_time,end_time,headway_secs,exact_times
routeA1,08:00:00,14:00:01,10800,0
routeA2,09:00:00,15:00:01,10800,0
routeB,07:00:00,16:00:01,10800,0
3 changes: 3 additions & 0 deletions test/gtfs-feed-min/multipleAgencies/agency.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
agency_id,agency_name,agency_url,agency_timezone
P2M,pt2matsim,https://github.com/matsim-org/pt2matsim,Europe/Zurich
S42,Service 42,htpps://google.com,Europe/Berlin
5 changes: 5 additions & 0 deletions test/gtfs-feed-min/multipleAgencies/calendar.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
service_id,monday,tuesday,wednesday,thursday,friday,saturday,sunday,start_date,end_date
WEEK,1,1,1,1,1,0,0,20181001,20181007
EXPR,0,0,0,0,0,0,0,20181001,20181007
WEND,0,0,0,0,0,1,1,20181001,20181007
EMPT,1,0,1,1,0,1,0,20181001,20181007
6 changes: 6 additions & 0 deletions test/gtfs-feed-min/multipleAgencies/calendar_dates.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
service_id,date,exception_type
WEEK,20181006,2
WEEK,20181007,2
EXPR,20181005,1
EMPT,20181002,1
EMPT,20181001,2
4 changes: 4 additions & 0 deletions test/gtfs-feed-min/multipleAgencies/frequencies.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
trip_id,start_time,end_time,headway_secs,exact_times
routeA1,08:00:00,14:00:01,10800,0
routeA2,09:00:00,15:00:01,10800,0
routeB,07:00:00,16:00:01,10800,0
4 changes: 4 additions & 0 deletions test/gtfs-feed-min/multipleAgencies/routes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
route_id,route_short_name,route_long_name,route_type
lineA,Line A,Bus Line A,3
lineB,Line B,Tram Line B,0
lineC,Line C,Something else,907
22 changes: 22 additions & 0 deletions test/gtfs-feed-min/multipleAgencies/shapes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
shape_id,shape_pt_lon,shape_pt_lat,shape_pt_sequence
A1,7.438899946082748,46.95162082278473,1
A1,7.43916266994327,46.95162082188145,2
A1,7.43916266818439,46.951440917895496,3
A1,7.439294030224575,46.95148589321508,4
A1,7.4394253911654555,46.951440916390055,5
A1,7.439425388527151,46.951261012398504,6
A2,7.439451661009937,46.95127900261426,1
A2,7.439451663736189,46.95145890660527,2
A2,7.4393203027733215,46.95150388346022,3
A2,7.439188940667179,46.95145890817091,4
A2,7.439188942514004,46.951638812156304,5
A2,7.438926218565539,46.9516388131198,6
B,7.438899946082748,46.95162082278473,1
B,7.439031308672595,46.95171077439923,2
B,7.43916266994327,46.95162082188145,3
B,7.4394253938037815,46.951620820376,4
B,7.439294030224575,46.95148589321508,5
B,7.43916266818439,46.951440917895496,6
B,7.439162667304952,46.951350965900424,7
B,7.4392940263770475,46.951171061228614,8
B,7.439425388527151,46.951261012398504,9
27 changes: 27 additions & 0 deletions test/gtfs-feed-min/multipleAgencies/stop_times.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
trip_id,arrival_time,departure_time,stop_id,stop_sequence
routeA1,00:00:00,00:00:00,stop1,1
routeA1,00:00:20,00:00:20,stop2,2
routeA1,00:00:40,00:00:40,stop3,3
routeA1,00:01:20,00:01:20,stop4,4
routeA2,00:01:20,00:01:20,stop1,4
routeA2,00:01:00,00:01:00,stop2,3
routeA2,00:00:40,00:00:40,stop3,2
routeA2,00:00:00,00:00:00,stop4,1
routeB,00:00:00,00:00:00,stop1,1
routeB,00:01:20,00:01:20,stop3,2
routeB,00:01:40,00:01:40,stop5,3
routeB,00:02:20,00:02:20,stop6,4
routeB,00:02:40,00:02:40,stop4,5
routeA1_weekend,00:00:00,00:00:00,stop1,1
routeA1_weekend,00:00:30,00:00:20,stop2,2
routeA1_weekend,00:01:00,00:00:40,stop3,3
routeA1_weekend,00:01:30,00:01:20,stop4,4
routeA2_weekend,00:00:00,00:00:00,stop4,1
routeA2_weekend,00:00:30,00:00:30,stop3,2
routeA2_weekend,00:01:00,00:01:00,stop2,3
routeA2_weekend,00:01:30,00:01:30,stop1,4
routeB_weekend,00:00:00,00:00:00,stop1,1
routeB_weekend,00:01:00,00:01:00,stop3,2
routeB_weekend,00:02:00,00:02:00,stop5,3
routeB_weekend,00:03:00,00:03:00,stop6,4
routeB_weekend,00:04:00,00:04:00,stop4,5
7 changes: 7 additions & 0 deletions test/gtfs-feed-min/multipleAgencies/stops.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
stop_id,stop_name,stop_lat,stop_lon
stop1,One,46.95162082275386,7.438913082275775
stop2,Two,46.95153086988918,7.439162669063829
stop3,Three,46.95147689809022,7.439280893956796
stop4,Four,46.95127900232747,7.439491069338372
stop5,Five,46.9513959419576,7.439149531606609
stop6,Six,46.95121603683256,7.439359707397135
7 changes: 7 additions & 0 deletions test/gtfs-feed-min/multipleAgencies/transfers.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from_stop_id,to_stop_id,transfer_type,min_transfer_time
stop2,stop3,0,-99
stop3,stop2,1,-99
stop3,stop4,2,10
stop4,stop3,2,10
stop4,stop1,3,
stop1,stop4,3,
7 changes: 7 additions & 0 deletions test/gtfs-feed-min/multipleAgencies/trips.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
route_id,service_id,trip_id,shape_id
lineA,WEEK,routeA1,shapeA1
lineA,WEEK,routeA2,shapeA2
lineB,EXPR,routeB,shapeB
lineA,WEND,routeA1_weekend,shapeA1
lineA,WEND,routeA2_weekend,shapeA2
lineB,WEND,routeB_weekend,shapeB
2 changes: 2 additions & 0 deletions test/gtfs-feed-min/noAgencyId/agency.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
agency_id,agency_name,agency_url,agency_timezone
P2M,pt2matsim,https://github.com/matsim-org/pt2matsim,Europe/Zurich
5 changes: 5 additions & 0 deletions test/gtfs-feed-min/noAgencyId/calendar.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
service_id,monday,tuesday,wednesday,thursday,friday,saturday,sunday,start_date,end_date
WEEK,1,1,1,1,1,0,0,20181001,20181007
EXPR,0,0,0,0,0,0,0,20181001,20181007
WEND,0,0,0,0,0,1,1,20181001,20181007
EMPT,1,0,1,1,0,1,0,20181001,20181007
6 changes: 6 additions & 0 deletions test/gtfs-feed-min/noAgencyId/calendar_dates.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
service_id,date,exception_type
WEEK,20181006,2
WEEK,20181007,2
EXPR,20181005,1
EMPT,20181002,1
EMPT,20181001,2
4 changes: 4 additions & 0 deletions test/gtfs-feed-min/noAgencyId/frequencies.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
trip_id,start_time,end_time,headway_secs,exact_times
routeA1,08:00:00,14:00:01,10800,0
routeA2,09:00:00,15:00:01,10800,0
routeB,07:00:00,16:00:01,10800,0
4 changes: 4 additions & 0 deletions test/gtfs-feed-min/noAgencyId/routes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
route_id,route_short_name,route_long_name,route_type
lineA,Line A,Bus Line A,3
lineB,Line B,Tram Line B,0
lineC,Line C,Something else,907
22 changes: 22 additions & 0 deletions test/gtfs-feed-min/noAgencyId/shapes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
shape_id,shape_pt_lon,shape_pt_lat,shape_pt_sequence
A1,7.438899946082748,46.95162082278473,1
A1,7.43916266994327,46.95162082188145,2
A1,7.43916266818439,46.951440917895496,3
A1,7.439294030224575,46.95148589321508,4
A1,7.4394253911654555,46.951440916390055,5
A1,7.439425388527151,46.951261012398504,6
A2,7.439451661009937,46.95127900261426,1
A2,7.439451663736189,46.95145890660527,2
A2,7.4393203027733215,46.95150388346022,3
A2,7.439188940667179,46.95145890817091,4
A2,7.439188942514004,46.951638812156304,5
A2,7.438926218565539,46.9516388131198,6
B,7.438899946082748,46.95162082278473,1
B,7.439031308672595,46.95171077439923,2
B,7.43916266994327,46.95162082188145,3
B,7.4394253938037815,46.951620820376,4
B,7.439294030224575,46.95148589321508,5
B,7.43916266818439,46.951440917895496,6
B,7.439162667304952,46.951350965900424,7
B,7.4392940263770475,46.951171061228614,8
B,7.439425388527151,46.951261012398504,9
27 changes: 27 additions & 0 deletions test/gtfs-feed-min/noAgencyId/stop_times.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
trip_id,arrival_time,departure_time,stop_id,stop_sequence
routeA1,00:00:00,00:00:00,stop1,1
routeA1,00:00:20,00:00:20,stop2,2
routeA1,00:00:40,00:00:40,stop3,3
routeA1,00:01:20,00:01:20,stop4,4
routeA2,00:01:20,00:01:20,stop1,4
routeA2,00:01:00,00:01:00,stop2,3
routeA2,00:00:40,00:00:40,stop3,2
routeA2,00:00:00,00:00:00,stop4,1
routeB,00:00:00,00:00:00,stop1,1
routeB,00:01:20,00:01:20,stop3,2
routeB,00:01:40,00:01:40,stop5,3
routeB,00:02:20,00:02:20,stop6,4
routeB,00:02:40,00:02:40,stop4,5
routeA1_weekend,00:00:00,00:00:00,stop1,1
routeA1_weekend,00:00:30,00:00:20,stop2,2
routeA1_weekend,00:01:00,00:00:40,stop3,3
routeA1_weekend,00:01:30,00:01:20,stop4,4
routeA2_weekend,00:00:00,00:00:00,stop4,1
routeA2_weekend,00:00:30,00:00:30,stop3,2
routeA2_weekend,00:01:00,00:01:00,stop2,3
routeA2_weekend,00:01:30,00:01:30,stop1,4
routeB_weekend,00:00:00,00:00:00,stop1,1
routeB_weekend,00:01:00,00:01:00,stop3,2
routeB_weekend,00:02:00,00:02:00,stop5,3
routeB_weekend,00:03:00,00:03:00,stop6,4
routeB_weekend,00:04:00,00:04:00,stop4,5
7 changes: 7 additions & 0 deletions test/gtfs-feed-min/noAgencyId/stops.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
stop_id,stop_name,stop_lat,stop_lon
stop1,One,46.95162082275386,7.438913082275775
stop2,Two,46.95153086988918,7.439162669063829
stop3,Three,46.95147689809022,7.439280893956796
stop4,Four,46.95127900232747,7.439491069338372
stop5,Five,46.9513959419576,7.439149531606609
stop6,Six,46.95121603683256,7.439359707397135
Loading

0 comments on commit 0bd70ff

Please sign in to comment.