Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Added input parameter to disable parsing of GTFS ids in format PREFIX.123 #51

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
@ToString(callSuper=true)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder={"objectIdPrefix",
"splitIdOnDot",
"maxDistanceForConnectionLink",
"maxDistanceForCommercial",
"ignoreEndChars",
Expand All @@ -32,6 +33,10 @@ public class GtfsImportParameters extends AbstractImportParameter {
@XmlElement(name = "object_id_prefix", required=true)
private String objectIdPrefix;

@Getter@Setter
@XmlElement(name = "split_id_on_dot", defaultValue="true")
private boolean splitIdOnDot = true;

@Getter@Setter
@XmlElement(name = "max_distance_for_connection_link", defaultValue="0")
private int maxDistanceForConnectionLink = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.TimeZone;

import mobi.chouette.common.Constant;
import mobi.chouette.exchange.gtfs.importer.GtfsImportParameters;
import mobi.chouette.exchange.gtfs.model.GtfsTime;

import org.apache.log4j.Logger;
Expand Down Expand Up @@ -34,16 +35,19 @@ public static Time getTime(GtfsTime gtfsTime) {
return time;
}

public static String composeObjectId(String prefix, String type, String id, Logger logger) {
public static String composeObjectId(GtfsImportParameters configuration, String type, String id, Logger logger) {

if (id == null || id.isEmpty() ) return "";
String[] tokens = id.split("\\.");
if (tokens.length == 2) {
// id should be produced by Chouette
return tokens[0].trim().replaceAll("[^a-zA-Z_0-9]", "_") + ":" + type + ":"
+ tokens[1].trim().replaceAll("[^a-zA-Z_0-9\\-]", "_");

if(configuration.isSplitIdOnDot()) {
String[] tokens = id.split("\\.");
if (tokens.length == 2) {
// id should be produced by Chouette
return tokens[0].trim().replaceAll("[^a-zA-Z_0-9]", "_") + ":" + type + ":"+ tokens[1].trim().replaceAll("[^a-zA-Z_0-9\\-]", "_");
}
}
return prefix + ":" + type + ":" + id.trim().replaceAll("[^a-zA-Z_0-9\\-]", "_");
return configuration.getObjectIdPrefix() + ":" + type + ":" + id.trim().replaceAll("[^a-zA-Z_0-9\\-]", "_");

}

public static String toString(URL url) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void parse(Context context) throws Exception {
GtfsImportParameters configuration = (GtfsImportParameters) context.get(CONFIGURATION);

for (GtfsAgency gtfsAgency : importer.getAgencyById()) {
String objectId = AbstractConverter.composeObjectId(configuration.getObjectIdPrefix(), Company.COMPANY_KEY,
String objectId = AbstractConverter.composeObjectId(configuration, Company.COMPANY_KEY,
gtfsAgency.getAgencyId(), log);
Company company = ObjectFactory.getCompany(referential, objectId);
convert(context, gtfsAgency, company);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public void parse(Context context) throws Exception {
if (importer.hasCalendarImporter()) {
for (GtfsCalendar gtfsCalendar : importer.getCalendarByService()) {

String objectId = AbstractConverter.composeObjectId(configuration.getObjectIdPrefix(),
String objectId = AbstractConverter.composeObjectId(configuration,
Timetable.TIMETABLE_KEY, gtfsCalendar.getServiceId(), log);
Timetable timetable = ObjectFactory.getTimetable(referential, objectId);
convert(context, gtfsCalendar, timetable);
Expand All @@ -199,7 +199,7 @@ public void parse(Context context) throws Exception {

for (String serviceId : importer.getCalendarDateByService().keys()) {

String objectId = AbstractConverter.composeObjectId(configuration.getObjectIdPrefix(),
String objectId = AbstractConverter.composeObjectId(configuration,
Timetable.TIMETABLE_KEY, serviceId, log);

Timetable timetable = referential.getTimetables().get(objectId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public void parse(Context context) throws Exception {
Index<GtfsRoute> routes = importer.getRouteById();
GtfsRoute gtfsRoute = routes.getValue(gtfsRouteId);

String lineId = AbstractConverter.composeObjectId(configuration.getObjectIdPrefix(), Line.LINE_KEY,
String lineId = AbstractConverter.composeObjectId(configuration, Line.LINE_KEY,
gtfsRouteId, log);
Line line = ObjectFactory.getLine(referential, lineId);
convert(context, gtfsRoute, line);
Expand All @@ -199,7 +199,7 @@ public void parse(Context context) throws Exception {

// Company
if (gtfsRoute.getAgencyId() != null) {
String companyId = AbstractConverter.composeObjectId(configuration.getObjectIdPrefix(),
String companyId = AbstractConverter.composeObjectId(configuration,
Company.COMPANY_KEY, gtfsRoute.getAgencyId(), log);
Company company = ObjectFactory.getCompany(referential, companyId);
line.setCompany(company);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void parse(Context context) throws Exception {
for (GtfsStop gtfsStop : importer.getStopById()) {
if (gtfsStop.getLocationType() != GtfsStop.LocationType.Access) {

String objectId = AbstractConverter.composeObjectId(configuration.getObjectIdPrefix(),
String objectId = AbstractConverter.composeObjectId(configuration,
StopArea.STOPAREA_KEY, gtfsStop.getStopId(), log);

StopArea stopArea = ObjectFactory.getStopArea(referential, objectId);
Expand Down Expand Up @@ -149,7 +149,7 @@ protected void convert(Context context, GtfsStop gtfsStop, StopArea stopArea) {
}
stopArea.setAreaType(ChouetteAreaEnum.BoardingPosition);
if (gtfsStop.getParentStation() != null) {
String parenId = AbstractConverter.composeObjectId(configuration.getObjectIdPrefix(),
String parenId = AbstractConverter.composeObjectId(configuration,
StopArea.STOPAREA_KEY, gtfsStop.getParentStation(), log);
StopArea parent = ObjectFactory.getStopArea(referential, parenId);
stopArea.setParent(parent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void parse(Context context) throws Exception {

for (GtfsTransfer gtfsTransfer : importer.getTransferByFromStop()) {

String objectId = AbstractConverter.composeObjectId(configuration.getObjectIdPrefix(),
String objectId = AbstractConverter.composeObjectId(configuration,
ConnectionLink.CONNECTIONLINK_KEY, gtfsTransfer.getFromStopId() + "_" + gtfsTransfer.getToStopId(),
log);
ConnectionLink connectionLink = ObjectFactory.getConnectionLink(referential, objectId);
Expand All @@ -119,10 +119,10 @@ protected void convert(Context context, GtfsTransfer gtfsTransfer, ConnectionLin
GtfsImportParameters configuration = (GtfsImportParameters) context.get(CONFIGURATION);

StopArea startOfLink = ObjectFactory.getStopArea(referential, AbstractConverter.composeObjectId(
configuration.getObjectIdPrefix(), StopArea.STOPAREA_KEY, gtfsTransfer.getFromStopId(), log));
configuration, StopArea.STOPAREA_KEY, gtfsTransfer.getFromStopId(), log));
connectionLink.setStartOfLink(startOfLink);
StopArea endOfLink = ObjectFactory.getStopArea(referential, AbstractConverter.composeObjectId(
configuration.getObjectIdPrefix(), StopArea.STOPAREA_KEY, gtfsTransfer.getToStopId(), log));
configuration, StopArea.STOPAREA_KEY, gtfsTransfer.getToStopId(), log));
connectionLink.setEndOfLink(endOfLink);
connectionLink.setCreationTime(Calendar.getInstance().getTime());
connectionLink.setLinkType(ConnectionLinkTypeEnum.Overground);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ public void parse(Context context) throws Exception {
if (!hasTimes)
continue;

String objectId = AbstractConverter.composeObjectId(configuration.getObjectIdPrefix(),
String objectId = AbstractConverter.composeObjectId(configuration,
VehicleJourney.VEHICLEJOURNEY_KEY, gtfsTrip.getTripId(), log);
VehicleJourney vehicleJourney = ObjectFactory.getVehicleJourney(referential, objectId);
convert(context, gtfsTrip, vehicleJourney);
Expand All @@ -537,7 +537,7 @@ public void parse(Context context) throws Exception {
Collections.sort(vehicleJourney.getVehicleJourneyAtStops(), VEHICLE_JOURNEY_AT_STOP_COMPARATOR);

// Timetable
String timetableId = AbstractConverter.composeObjectId(configuration.getObjectIdPrefix(),
String timetableId = AbstractConverter.composeObjectId(configuration,
Timetable.TIMETABLE_KEY, gtfsTrip.getServiceId(), log);
if (afterMidnight) {
timetableId += GtfsCalendarParser.AFTER_MIDNIGHT_SUFFIX;
Expand Down Expand Up @@ -588,7 +588,7 @@ private void createJourneyFrequencies(Context context, Referential referential,
for (GtfsFrequency frequency : importer.getFrequencyByTrip().values(gtfsTrip.getTripId())) {
vehicleJourney.setJourneyCategory(JourneyCategoryEnum.Frequency);

String timeBandObjectId = AbstractConverter.composeObjectId(configuration.getObjectIdPrefix(),
String timeBandObjectId = AbstractConverter.composeObjectId(configuration,
Timeband.TIMETABLE_KEY, gtfsTrip.getTripId() + "-" + count++, log);
Timeband timeband = ObjectFactory.getTimeband(referential, timeBandObjectId);
timeband.setName(getTimebandName(frequency));
Expand Down Expand Up @@ -813,14 +813,14 @@ private List<RouteSection> createRouteSections(Context context, Referential refe
* @return
*/
private Route createRoute(Referential referential, GtfsImportParameters configuration, GtfsTrip gtfsTrip) {
String lineId = AbstractConverter.composeObjectId(configuration.getObjectIdPrefix(), Line.LINE_KEY,
String lineId = AbstractConverter.composeObjectId(configuration, Line.LINE_KEY,
gtfsTrip.getRouteId(), log);
Line line = ObjectFactory.getLine(referential, lineId);
String routeKey = gtfsTrip.getRouteId() + "_" + gtfsTrip.getDirectionId().ordinal();
if (gtfsTrip.getShapeId() != null && !gtfsTrip.getShapeId().isEmpty())
routeKey += "_" + gtfsTrip.getShapeId();
routeKey += "_" + line.getRoutes().size();
String routeId = AbstractConverter.composeObjectId(configuration.getObjectIdPrefix(), Route.ROUTE_KEY,
String routeId = AbstractConverter.composeObjectId(configuration, Route.ROUTE_KEY,
routeKey, log);

Route route = ObjectFactory.getRoute(referential, routeId);
Expand Down Expand Up @@ -910,7 +910,7 @@ private void createStopPoint(Route route, JourneyPattern journeyPattern, List<Ve

StopPoint stopPoint = ObjectFactory.getStopPoint(referential, stopKey);

String stopAreaId = AbstractConverter.composeObjectId(configuration.getObjectIdPrefix(),
String stopAreaId = AbstractConverter.composeObjectId(configuration,
StopArea.STOPAREA_KEY, wrapper.stopId, log);
StopArea stopArea = ObjectFactory.getStopArea(referential, stopAreaId);
stopPoint.setContainedInStopArea(stopArea);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private DataLocation buildDataLocation(Context context, DataLocation loc, String
if (gtfsRouteId != null)
{
GtfsImportParameters configuration = (GtfsImportParameters) context.get(CONFIGURATION);
String lineId = AbstractConverter.composeObjectId(configuration.getObjectIdPrefix(), Line.LINE_KEY,
String lineId = AbstractConverter.composeObjectId(configuration, Line.LINE_KEY,
gtfsRouteId, log);
loc.getPath().add(loc.new Path(Line.class.getSimpleName(),lineId));
}
Expand All @@ -117,7 +117,7 @@ else if (fileName.equals(TripById.FILENAME))
if (gtfsRouteId != null)
{
GtfsImportParameters configuration = (GtfsImportParameters) context.get(CONFIGURATION);
String lineId = AbstractConverter.composeObjectId(configuration.getObjectIdPrefix(), Line.LINE_KEY,
String lineId = AbstractConverter.composeObjectId(configuration, Line.LINE_KEY,
gtfsRouteId, log);
loc.getPath().add(loc.new Path(Line.class.getSimpleName(),lineId));
}
Expand All @@ -133,7 +133,7 @@ else if (fileName.equals(ShapeById.FILENAME))
if (gtfsRouteId != null)
{
GtfsImportParameters configuration = (GtfsImportParameters) context.get(CONFIGURATION);
String lineId = AbstractConverter.composeObjectId(configuration.getObjectIdPrefix(), Line.LINE_KEY,
String lineId = AbstractConverter.composeObjectId(configuration, Line.LINE_KEY,
gtfsRouteId, log);
loc.getPath().add(loc.new Path(Line.class.getSimpleName(),lineId));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package mobi.chouette.exchange.gtfs.parser;

import org.testng.Assert;
import org.testng.annotations.Test;

import mobi.chouette.exchange.gtfs.importer.GtfsImportParameters;

public class AbstractConverterTest {
@Test
public void testCreateIdWithParsingDots() {
GtfsImportParameters configuration = new GtfsImportParameters();
configuration.setObjectIdPrefix("PRE");
String id = AbstractConverter.composeObjectId(configuration, "VehicleJourney", "ABC.123", null);

Assert.assertEquals(id, "ABC:VehicleJourney:123");
}

@Test
public void testCreateIdWithoutParsingDots() {
GtfsImportParameters configuration = new GtfsImportParameters();
configuration.setSplitIdOnDot(false);
configuration.setObjectIdPrefix("PRE");
String id = AbstractConverter.composeObjectId(configuration, "VehicleJourney", "ABC.TestType.123", null);

Assert.assertEquals(id, "PRE:VehicleJourney:ABC_TestType_123");
}
}