From 4812e2e702adf368597d2e4ea9d0c283d3b57feb Mon Sep 17 00:00:00 2001 From: Thomas Gran Date: Fri, 25 Aug 2017 10:02:50 +0200 Subject: [PATCH] Import GTFS using onebusaway library and map into OTP model. Find the best place to map between the to models. (Refactor OTP to have new OTP classes to replace the OBA GTFS classes #2494) --- pom.xml | 9 +- .../onebusaway2/gtfs/impl/GtfsDaoImpl.java | 15 +- .../gtfs/impl/GtfsRelationalDaoImpl.java | 53 +- .../onebusaway2/gtfs/impl/StopTimeArray.java | 3 +- .../org/onebusaway2/gtfs/model/Agency.java | 1 - .../onebusaway2/gtfs/services/MockGtfs.java | 476 +++--------------- .../calendar/CalendarServiceDataFactory.java | 2 +- .../impl/CalendarServiceDataFactoryImpl.java | 3 +- .../graph_builder/module/GtfsModule.java | 48 +- .../org/opentripplanner/gtfs/BikeAccess.java | 56 +++ .../org/opentripplanner/gtfs/GtfsImport.java | 52 ++ .../org/opentripplanner/gtfs/GtfsLibrary.java | 24 +- .../gtfs/mapping/AgencyAndIdMapper.java | 22 + .../gtfs/mapping/AgencyMapper.java | 48 ++ .../gtfs/mapping/FareAttributeMapper.java | 50 ++ .../gtfs/mapping/FareRuleMapper.java | 54 ++ .../gtfs/mapping/FeedInfoMapper.java | 49 ++ .../gtfs/mapping/FrequencyMapper.java | 53 ++ .../gtfs/mapping/MapCollection.java | 27 + .../gtfs/mapping/ModelMapper.java | 75 +++ .../gtfs/mapping/PathwayMapper.java | 52 ++ .../gtfs/mapping/RouteMapper.java | 60 +++ .../mapping/ServiceCalendarDateMapper.java | 45 ++ .../gtfs/mapping/ServiceCalendarMapper.java | 53 ++ .../gtfs/mapping/ServiceDateMapper.java | 23 + .../gtfs/mapping/ShapePointMapper.java | 54 ++ .../gtfs/mapping/StopMapper.java | 57 +++ .../gtfs/mapping/StopTimeMapper.java | 73 +++ .../gtfs/mapping/TransferMapper.java | 64 +++ .../gtfs/mapping/TripMapper.java | 62 +++ .../factory/GTFSPatternHopFactoryTest.java | 6 +- 31 files changed, 1187 insertions(+), 482 deletions(-) create mode 100644 src/main/java/org/opentripplanner/gtfs/GtfsImport.java create mode 100644 src/main/java/org/opentripplanner/gtfs/mapping/AgencyAndIdMapper.java create mode 100644 src/main/java/org/opentripplanner/gtfs/mapping/AgencyMapper.java create mode 100644 src/main/java/org/opentripplanner/gtfs/mapping/FareAttributeMapper.java create mode 100644 src/main/java/org/opentripplanner/gtfs/mapping/FareRuleMapper.java create mode 100644 src/main/java/org/opentripplanner/gtfs/mapping/FeedInfoMapper.java create mode 100644 src/main/java/org/opentripplanner/gtfs/mapping/FrequencyMapper.java create mode 100644 src/main/java/org/opentripplanner/gtfs/mapping/MapCollection.java create mode 100644 src/main/java/org/opentripplanner/gtfs/mapping/ModelMapper.java create mode 100644 src/main/java/org/opentripplanner/gtfs/mapping/PathwayMapper.java create mode 100644 src/main/java/org/opentripplanner/gtfs/mapping/RouteMapper.java create mode 100644 src/main/java/org/opentripplanner/gtfs/mapping/ServiceCalendarDateMapper.java create mode 100644 src/main/java/org/opentripplanner/gtfs/mapping/ServiceCalendarMapper.java create mode 100644 src/main/java/org/opentripplanner/gtfs/mapping/ServiceDateMapper.java create mode 100644 src/main/java/org/opentripplanner/gtfs/mapping/ShapePointMapper.java create mode 100644 src/main/java/org/opentripplanner/gtfs/mapping/StopMapper.java create mode 100644 src/main/java/org/opentripplanner/gtfs/mapping/StopTimeMapper.java create mode 100644 src/main/java/org/opentripplanner/gtfs/mapping/TransferMapper.java create mode 100644 src/main/java/org/opentripplanner/gtfs/mapping/TripMapper.java diff --git a/pom.xml b/pom.xml index a20e25107c7..b02c594cffc 100644 --- a/pom.xml +++ b/pom.xml @@ -641,15 +641,10 @@ 1 - - - - - org.onebusaway - onebusaway-csv-entities - 1.1.5-SNAPSHOT + onebusaway-gtfs + 1.3.5-conveyal-SNAPSHOT-2 diff --git a/src/main/java/org/onebusaway2/gtfs/impl/GtfsDaoImpl.java b/src/main/java/org/onebusaway2/gtfs/impl/GtfsDaoImpl.java index 60773978103..06b7fd78bd9 100644 --- a/src/main/java/org/onebusaway2/gtfs/impl/GtfsDaoImpl.java +++ b/src/main/java/org/onebusaway2/gtfs/impl/GtfsDaoImpl.java @@ -49,21 +49,24 @@ public class GtfsDaoImpl extends GenericDaoImpl implements GtfsMutableDao { private boolean packShapePoints = false; + public boolean isPackStopTimes() { return packStopTimes; } - public void setPackStopTimes(boolean packStopTimes) { - this.packStopTimes = packStopTimes; - } + // TODO TGR - The mapping from GTFS to OTP model do not support this - ok? +// public void setPackStopTimes(boolean packStopTimes) { +// this.packStopTimes = packStopTimes; +// } public boolean isPackShapePoints() { return packShapePoints; } - public void setPackShapePoints(boolean packShapePoints) { - this.packShapePoints = packShapePoints; - } + // TODO TGR - The mapping from GTFS to OTP model do not support this - ok? +// public void setPackShapePoints(boolean packShapePoints) { +// this.packShapePoints = packShapePoints; +// } /*** * {@link GtfsDao} Interface diff --git a/src/main/java/org/onebusaway2/gtfs/impl/GtfsRelationalDaoImpl.java b/src/main/java/org/onebusaway2/gtfs/impl/GtfsRelationalDaoImpl.java index e6315e94e74..f89a2574674 100644 --- a/src/main/java/org/onebusaway2/gtfs/impl/GtfsRelationalDaoImpl.java +++ b/src/main/java/org/onebusaway2/gtfs/impl/GtfsRelationalDaoImpl.java @@ -29,18 +29,7 @@ import org.onebusaway.csv_entities.exceptions.EntityInstantiationException; import org.onebusaway.csv_entities.schema.BeanWrapper; import org.onebusaway.csv_entities.schema.BeanWrapperFactory; -import org.onebusaway2.gtfs.model.Agency; -import org.onebusaway2.gtfs.model.AgencyAndId; -import org.onebusaway2.gtfs.model.FareAttribute; -import org.onebusaway2.gtfs.model.FareRule; -import org.onebusaway2.gtfs.model.Frequency; -import org.onebusaway2.gtfs.model.Route; -import org.onebusaway2.gtfs.model.ServiceCalendar; -import org.onebusaway2.gtfs.model.ServiceCalendarDate; -import org.onebusaway2.gtfs.model.ShapePoint; -import org.onebusaway2.gtfs.model.Stop; -import org.onebusaway2.gtfs.model.StopTime; -import org.onebusaway2.gtfs.model.Trip; +import org.onebusaway2.gtfs.model.*; import org.onebusaway2.gtfs.services.GtfsMutableRelationalDao; /** @@ -82,7 +71,45 @@ public class GtfsRelationalDaoImpl extends GtfsDaoImpl implements private Map> _fareRulesByFareAttribute = null; - public void clearAllCaches() { + public GtfsRelationalDaoImpl( + Collection agencies, + Collection calendarDates, + Collection calendars, + Collection fareAttributes, + Collection fareRules, + Collection feedInfos, + Collection frequencies, + Collection pathways, + Collection routes, + Collection shapePoints, + Collection stops, + Collection stopTimes, + Collection transfers, + Collection trips + ) { + saveAll(agencies); + saveAll(calendarDates); + saveAll(calendars); + saveAll(fareAttributes); + saveAll(fareRules); + saveAll(feedInfos); + saveAll(frequencies); + saveAll(pathways); + saveAll(routes); + saveAll(shapePoints); + saveAll(stops); + saveAll(stopTimes); + saveAll(transfers); + saveAll(trips); + } + + private void saveAll(Collection entities) { + for (Object it : entities) { + saveEntity(it); + } + } + + public void clearAllCaches() { _tripAgencyIdsByServiceId = clearMap(_tripAgencyIdsByServiceId); _routesByAgency = clearMap(_routesByAgency); _stopsByStation = clearMap(_stopsByStation); diff --git a/src/main/java/org/onebusaway2/gtfs/impl/StopTimeArray.java b/src/main/java/org/onebusaway2/gtfs/impl/StopTimeArray.java index 138d7758c90..2d948db1823 100644 --- a/src/main/java/org/onebusaway2/gtfs/impl/StopTimeArray.java +++ b/src/main/java/org/onebusaway2/gtfs/impl/StopTimeArray.java @@ -156,7 +156,8 @@ public void remove() { } } - private class StopTimeProxyImpl implements StopTimeProxy { + private class StopTimeProxyImpl implements + StopTimeProxy { private final int index; diff --git a/src/main/java/org/onebusaway2/gtfs/model/Agency.java b/src/main/java/org/onebusaway2/gtfs/model/Agency.java index 23fa543494a..19f3482b5f2 100644 --- a/src/main/java/org/onebusaway2/gtfs/model/Agency.java +++ b/src/main/java/org/onebusaway2/gtfs/model/Agency.java @@ -47,7 +47,6 @@ public final class Agency extends IdentityBean { private String brandingUrl; public Agency() { - } public Agency(Agency a) { diff --git a/src/main/java/org/onebusaway2/gtfs/services/MockGtfs.java b/src/main/java/org/onebusaway2/gtfs/services/MockGtfs.java index bc3d66cb72f..6f57b8ef9ff 100644 --- a/src/main/java/org/onebusaway2/gtfs/services/MockGtfs.java +++ b/src/main/java/org/onebusaway2/gtfs/services/MockGtfs.java @@ -1,461 +1,115 @@ -/** - * Copyright (C) 2011 Google, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ package org.onebusaway2.gtfs.services; -import java.io.ByteArrayOutputStream; +import org.onebusaway2.gtfs.model.AgencyAndId; +import org.opentripplanner.gtfs.mapping.AgencyAndIdMapper; +import org.opentripplanner.gtfs.mapping.ModelMapper; + import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Calendar; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.zip.ZipEntry; -import java.util.zip.ZipOutputStream; - -import org.onebusaway2.gtfs.impl.GtfsRelationalDaoImpl; -import org.onebusaway2.gtfs.model.AgencyAndId; -import org.onebusaway2.gtfs.model.calendar.ServiceDate; -import org.onebusaway2.gtfs.serialization.GtfsReader; -import org.onebusaway2.gtfs.serialization.mappings.StopTimeFieldMappingFactory; public class MockGtfs { - private final File _path; - - private Map _contentByFileName = new HashMap(); - - public MockGtfs(File path) { - _path = path; - } - - public static MockGtfs create() throws IOException { - File tmpFile = File.createTempFile("MockGtfs-", ".zip"); - tmpFile.deleteOnExit(); - return new MockGtfs(tmpFile); - } - - public File getPath() { - return _path; - } - - public void putFile(String fileName, String content) { - _contentByFileName.put(fileName, content.getBytes()); - updateZipFile(); - } + private final org.onebusaway.gtfs.services.MockGtfs gtfsDelegate; - public void putFile(String fileName, File file) throws IOException { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - byte[] buffer = new byte[1024]; - FileInputStream in = new FileInputStream(file); - while (true) { - int rc = in.read(buffer); - if (rc == -1) { - break; - } - out.write(buffer, 0, rc); + private MockGtfs(org.onebusaway.gtfs.services.MockGtfs gtfsDelegate) { + this.gtfsDelegate = gtfsDelegate; } - in.close(); - _contentByFileName.put(fileName, out.toByteArray()); - updateZipFile(); - } - public void putLines(String fileName, String... rows) { - StringBuilder b = new StringBuilder(); - for (String row : rows) { - b.append(row); - b.append('\n'); + public MockGtfs(File path) { + this(new org.onebusaway.gtfs.services.MockGtfs(path)); } - putFile(fileName, b.toString()); - } - public GtfsMutableRelationalDao read() throws IOException { - GtfsReader reader = new GtfsReader(); - return read(reader); - } - - public GtfsMutableRelationalDao read(GtfsReader reader) throws IOException { - reader.setInputLocation(_path); - GtfsRelationalDaoImpl dao = new GtfsRelationalDaoImpl(); - reader.setEntityStore(dao); - try { - reader.run(); - } finally { - reader.close(); + public static MockGtfs create() throws IOException { + return new MockGtfs(org.onebusaway.gtfs.services.MockGtfs.create()); } - return dao; - } - - public void putMinimal() { - putAgencies(1); - putStops(0); - putRoutes(0); - putTrips(0, "", ""); - putStopTimes("", ""); - } - - public void putAgencies(int numberOfRows, String... columns) { - TableBuilder b = new TableBuilder(numberOfRows); - b.addColumnSpec("agency_id", "a$0"); - b.addColumnSpec("agency_name", "Agency $0"); - b.addColumnSpec("agency_url", "http://agency-$0.gov/"); - b.addColumnSpec("agency_timezone", "America/Los_Angeles"); - b.addColumnSpec("agency_lang", "en"); - b.addColumnSpecs(columns); - putFile("agency.txt", b.build()); - } - - public void putDefaultAgencies() { - putLines("agency.txt", "agency_id,agency_name,agency_url,agency_timezone", - "1,Metro,http://metro.gov/,America/Los_Angeles"); - } - public void putRoutes(int numberOfRows, String... columns) { - TableBuilder b = new TableBuilder(numberOfRows); - b.addColumnSpec("route_id", "r$0"); - b.addColumnSpec("route_short_name", "$0"); - b.addColumnSpec("route_long_name", "Route $0"); - b.addColumnSpec("route_type", "3"); - b.addColumnSpecs(columns); - putFile("routes.txt", b.build()); - } - - public void putDefaultRoutes() { - putDefaultAgencies(); - putLines("routes.txt", - "route_id,route_short_name,route_long_name,route_type", - "R10,10,The Ten,3"); - } - - public void putStops(int numberOfRows, String... columns) { - TableBuilder b = new TableBuilder(numberOfRows); - b.addColumnSpec("stop_id", "s$0"); - b.addColumnSpec("stop_name", "Stop $0"); - - List stopLats = new ArrayList(); - List stopLons = new ArrayList(); - for (int i = 0; i < numberOfRows; ++i) { - double lat = 47.65383950857904 + 0.004 * i; - double lon = -122.30782950811766; - stopLats.add(Double.toString(lat)); - stopLons.add(Double.toString(lon)); + public File getPath() { + return gtfsDelegate.getPath(); } - b.addColumnSpec("stop_lat", stopLats); - b.addColumnSpec("stop_lon", stopLons); - - b.addColumnSpecs(columns); - putFile("stops.txt", b.build()); - } - public void putDefaultStops() { - putDefaultAgencies(); - putLines("stops.txt", "stop_id,stop_name,stop_lat,stop_lon", - "100,The Stop,47.654403,-122.305211", - "200,The Other Stop,47.656303,-122.315436"); - } - - public void putCalendars(int numberOfServiceIds, String... columns) { - TableBuilder b = new TableBuilder(numberOfServiceIds); - b.addColumnSpec("service_id", "sid$0"); - - Calendar c = Calendar.getInstance(); - ServiceDate startDate = new ServiceDate(c); - b.addColumnSpec("start_date", startDate.getAsString()); - c.add(Calendar.MONTH, 3); - ServiceDate endDate = new ServiceDate(c); - b.addColumnSpec("end_date", endDate.getAsString()); - b.addColumnSpec("start_date", startDate.getAsString()); - - String[] days = { - "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", - "sunday"}; - for (String day : days) { - b.addColumnSpec(day, "1"); + public void putFile(String fileName, String content) { + gtfsDelegate.putFile(fileName, content); } - List mask = new ArrayList(); - columns = b.removeColumn("mask", columns, mask); - if (!mask.isEmpty()) { - Map> valuesByDay = new HashMap>(); - - for (String day : days) { - valuesByDay.put(day, new ArrayList()); - } - for (String maskRow : mask) { - if (maskRow.length() != days.length) { - throw new IllegalArgumentException("invalid calendar.txt mask=" - + maskRow); - } - for (int i = 0; i < maskRow.length(); ++i) { - String day = days[i]; - valuesByDay.get(day).add(maskRow.substring(i, i + 1)); - } - } - for (String day : days) { - b.addColumnSpec(day, valuesByDay.get(day)); - } + public void putFile(String fileName, File file) throws IOException { + gtfsDelegate.putFile(fileName, file); } - b.addColumnSpecs(columns); - putFile("calendar.txt", b.build()); - } - - public void putDefaultCalendar() { - putLines( - "calendars.txt", - "service_id,monday,tuesday,wednesday,thursday,friday,saturday,sunday,start_date,end_date", - "WEEK,1,1,1,1,1,0,0,20110101,20111231"); - } - public void putCalendarDates(String... specs) { - List serviceIds = new ArrayList(); - List serviceDates = new ArrayList(); - List exceptionTypes = new ArrayList(); - for (String spec : specs) { - int index = spec.indexOf('='); - if (index == -1) { - throw new IllegalArgumentException("invalid calendar date spec=" + spec); - } - String serviceId = spec.substring(0, index); - String dates = spec.substring(index + 1); - for (String date : dates.split(",")) { - int exceptionType = 1; - if (date.startsWith("-")) { - exceptionType = 2; - date = date.substring(1); - } - serviceIds.add(serviceId); - serviceDates.add(date); - exceptionTypes.add(Integer.toString(exceptionType)); - } + public void putLines(String fileName, String... rows) { + gtfsDelegate.putLines(fileName, rows); } - TableBuilder b = new TableBuilder(serviceIds.size()); - b.addColumnSpec("service_id", serviceIds); - b.addColumnSpec("date", serviceDates); - b.addColumnSpec("exception_type", exceptionTypes); - putFile("calendar_dates.txt", b.build()); - - } - - public void putTrips(int numberOfRows, String routeIds, String serviceIds, - String... columns) { - TableBuilder b = new TableBuilder(numberOfRows); - b.addColumnSpec("trip_id", "t$0"); - b.addColumnSpec("route_id", routeIds); - b.addColumnSpec("service_id", serviceIds); - b.addColumnSpecs(columns); - putFile("trips.txt", b.build()); - } - - public void putDefaultTrips() { - putDefaultRoutes(); - putDefaultCalendar(); - putLines("trips.txt", "route_id,service_id,trip_id", "R10,WEEK,T10-0"); - } - - public void putStopTimes(String tripIds, String stopIds) { - List tripIdColumn = new ArrayList(); - List stopIdColumn = new ArrayList(); - List arrivalTimeColumn = new ArrayList(); - List departureTimeColumn = new ArrayList(); - List stopSequenceColumn = new ArrayList(); - String[] expandedTripIds = tripIds.isEmpty() ? new String[0] - : tripIds.split(","); - List> expandedStopIds = new ArrayList>(); - if (!stopIds.isEmpty()) { - for (String stopIdsEntry : stopIds.split("\\|")) { - expandedStopIds.add(Arrays.asList(stopIdsEntry.split(","))); - } - } - if (expandedStopIds.size() != 1 - && expandedStopIds.size() != expandedTripIds.length) { - throw new IllegalArgumentException("given " + expandedTripIds.length - + " trip_id values, expected either 1 or " + expandedTripIds.length - + " stop_id lists, but instead found " + expandedStopIds.size()); - } - int startTime = 9 * 60 * 60; - for (int i = 0; i < expandedTripIds.length; ++i) { - String tripId = expandedTripIds[i]; - List specificStopIds = expandedStopIds.get(expandedStopIds.size() == 1 - ? 0 : i); - int t = startTime; - for (int stopSequence = 0; stopSequence < specificStopIds.size(); stopSequence++) { - String stopId = specificStopIds.get(stopSequence); - tripIdColumn.add(tripId); - stopIdColumn.add(stopId); - stopSequenceColumn.add(Integer.toString(stopSequence)); - String timeString = StopTimeFieldMappingFactory.getSecondsAsString(t); - arrivalTimeColumn.add(timeString); - departureTimeColumn.add(timeString); - t += 5 * 60; - } - startTime += 30 * 60; + public GtfsMutableRelationalDao read() throws IOException { + return ModelMapper.mapDao(gtfsDelegate.read()); } - TableBuilder b = new TableBuilder(tripIdColumn.size()); - b.addColumnSpec("trip_id", tripIdColumn); - b.addColumnSpec("stop_id", stopIdColumn); - b.addColumnSpec("arrival_time", arrivalTimeColumn); - b.addColumnSpec("departure_time", departureTimeColumn); - b.addColumnSpec("stop_sequence", stopSequenceColumn); - putFile("stop_times.txt", b.build()); - } + public GtfsMutableRelationalDao read(org.onebusaway.gtfs.serialization.GtfsReader reader) + throws IOException { + return ModelMapper.mapDao(gtfsDelegate.read(reader)); + } - public void putDefaultStopTimes() { - putDefaultTrips(); - putDefaultStops(); - putLines("stop_times.txt", - "trip_id,stop_id,stop_sequence,arrival_time,departure_time", - "T10-0,100,0,08:00:00,08:00:00", "T10-0,200,1,09:00:00,09:00:00"); - } + public void putMinimal() { + gtfsDelegate.putMinimal(); + } - /** - * - * @param id - * @return a full id with the default agency id ("a0") for the feed. - */ - public AgencyAndId id(String id) { - return new AgencyAndId("a0", id); - } + public void putAgencies(int numberOfRows, String... columns) { + gtfsDelegate.putAgencies(numberOfRows, columns); + } - private void updateZipFile() { - try { - if (_path.exists()) { - _path.delete(); - } - ZipOutputStream out = new ZipOutputStream(new FileOutputStream(_path)); - for (Map.Entry entry : _contentByFileName.entrySet()) { - String fileName = entry.getKey(); - byte[] content = entry.getValue(); - ZipEntry zipEntry = new ZipEntry(fileName); - out.putNextEntry(zipEntry); - out.write(content); - out.closeEntry(); - } - out.close(); - } catch (IOException ex) { - throw new IllegalStateException(ex); + public void putDefaultAgencies() { + gtfsDelegate.putDefaultAgencies(); } - } - private static class TableBuilder { + public void putRoutes(int numberOfRows, String... columns) { + gtfsDelegate.putRoutes(numberOfRows, columns); + } - private final LinkedHashMap> _columnsAndValues = new LinkedHashMap>(); + public void putDefaultRoutes() { + gtfsDelegate.putDefaultRoutes(); + } - private final int _numberOfRows; + public void putStops(int numberOfRows, String... columns) { + gtfsDelegate.putStops(numberOfRows, columns); + } - public TableBuilder(int numberOfRows) { - _numberOfRows = numberOfRows; + public void putDefaultStops() { + gtfsDelegate.putDefaultStops(); } - public void addColumnSpec(String columnName, List values) { - if (values.size() != 1 && values.size() != _numberOfRows) { - throw new IllegalArgumentException("expected 1 or " + _numberOfRows - + " values but found " + values.size()); - } - _columnsAndValues.put(columnName, values); + public void putCalendars(int numberOfServiceIds, String... columns) { + gtfsDelegate.putCalendars(numberOfServiceIds, columns); } - public void addColumnSpec(String columnName, String values) { - addColumnSpec(columnName, expand(values)); + public void putDefaultCalendar() { + gtfsDelegate.putDefaultCalendar(); } - public void addColumnSpecs(String[] columns) { - for (String spec : columns) { - int index = spec.indexOf('='); - if (index == -1) { - throw new IllegalArgumentException("invalid column spec=" + spec); - } - addColumnSpec(spec.substring(0, index), spec.substring(index + 1)); - } + public void putCalendarDates(String... specs) { + gtfsDelegate.putCalendarDates(specs); } - public String[] removeColumn(String name, String[] columns, - List values) { - List filtered = new ArrayList(); - for (String columnSpec : columns) { - int index = columnSpec.indexOf('='); - if (index == -1) { - throw new IllegalArgumentException("invalid column spec=" - + columnSpec); - } - String columnName = columnSpec.substring(0, index); - if (columnName.equals(name)) { - String columnValue = columnSpec.substring(index + 1); - values.clear(); - values.addAll(expand(columnValue)); - } else { - filtered.add(columnSpec); - } - } - return filtered.toArray(new String[filtered.size()]); + public void putTrips(int numberOfRows, String routeIds, String serviceIds, String... columns) { + gtfsDelegate.putTrips(numberOfRows, routeIds, serviceIds, columns); } - public String build() { - StringBuilder b = new StringBuilder(); - buildHeader(b); - buildValues(b); - return b.toString(); + public void putDefaultTrips() { + gtfsDelegate.putDefaultTrips(); } - private void buildHeader(StringBuilder b) { - boolean addComma = false; - for (String columnName : _columnsAndValues.keySet()) { - if (addComma) { - b.append(","); - } - b.append(columnName); - addComma = true; - } - b.append("\n"); + public void putStopTimes(String tripIds, String stopIds) { + gtfsDelegate.putStopTimes(tripIds, stopIds); } - private void buildValues(StringBuilder b) { - for (int i = 0; i < _numberOfRows; ++i) { - boolean addComma = false; - for (List values : _columnsAndValues.values()) { - if (addComma) { - b.append(","); - } - String value = values.size() > 1 ? values.get(i) : values.get(0); - b.append(value); - addComma = true; - } - b.append("\n"); - } + public void putDefaultStopTimes() { + gtfsDelegate.putDefaultStopTimes(); } - private List expand(String values) { - String[] tokens = values.split(","); - if (tokens.length == 1 && tokens[0].contains("$0")) { - String[] expanded = new String[_numberOfRows]; - for (int i = 0; i < _numberOfRows; ++i) { - expanded[i] = tokens[0].replaceAll("\\$0", Integer.toString(i)); - } - tokens = expanded; - } - if (tokens.length != 1 && tokens.length != _numberOfRows) { - throw new IllegalStateException("expected either 1 or " + _numberOfRows - + " values but found " + tokens.length + " for \"" + values + "\""); - } - return Arrays.asList(tokens); + /** + * + * @param id + * @return a full id with the default agency id ("a0") for the feed. + */ + public AgencyAndId id(String id) { + return AgencyAndIdMapper.mapAgencyAndId(gtfsDelegate.id(id)); } - } } diff --git a/src/main/java/org/onebusaway2/gtfs/services/calendar/CalendarServiceDataFactory.java b/src/main/java/org/onebusaway2/gtfs/services/calendar/CalendarServiceDataFactory.java index 7c7540dbf71..7a4318e3b68 100644 --- a/src/main/java/org/onebusaway2/gtfs/services/calendar/CalendarServiceDataFactory.java +++ b/src/main/java/org/onebusaway2/gtfs/services/calendar/CalendarServiceDataFactory.java @@ -18,5 +18,5 @@ import org.onebusaway2.gtfs.model.calendar.CalendarServiceData; public interface CalendarServiceDataFactory { - public CalendarServiceData createData(); + CalendarServiceData createData(); } diff --git a/src/main/java/org/opentripplanner/calendar/impl/CalendarServiceDataFactoryImpl.java b/src/main/java/org/opentripplanner/calendar/impl/CalendarServiceDataFactoryImpl.java index 5772cb9abdf..7a711954fc6 100644 --- a/src/main/java/org/opentripplanner/calendar/impl/CalendarServiceDataFactoryImpl.java +++ b/src/main/java/org/opentripplanner/calendar/impl/CalendarServiceDataFactoryImpl.java @@ -50,8 +50,7 @@ * * @author bdferris */ -public class CalendarServiceDataFactoryImpl implements - CalendarServiceDataFactory { +public class CalendarServiceDataFactoryImpl implements CalendarServiceDataFactory { private final Logger _log = LoggerFactory.getLogger(CalendarServiceDataFactoryImpl.class); diff --git a/src/main/java/org/opentripplanner/graph_builder/module/GtfsModule.java b/src/main/java/org/opentripplanner/graph_builder/module/GtfsModule.java index dc825af67cd..09ee2b261fd 100644 --- a/src/main/java/org/opentripplanner/graph_builder/module/GtfsModule.java +++ b/src/main/java/org/opentripplanner/graph_builder/module/GtfsModule.java @@ -26,12 +26,11 @@ the License, or (at your option) any later version. import java.util.Set; import org.onebusaway.csv_entities.EntityHandler; -import org.onebusaway2.gtfs.impl.GtfsRelationalDaoImpl; -import org.onebusaway2.gtfs.model.*; -import org.onebusaway2.gtfs.model.calendar.CalendarServiceData; -import org.onebusaway2.gtfs.serialization.GtfsReader; -import org.onebusaway2.gtfs.services.GenericMutableDao; -import org.onebusaway2.gtfs.services.GtfsMutableRelationalDao; +import org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl; +import org.onebusaway.gtfs.model.*; +import org.onebusaway.gtfs.serialization.GtfsReader; +import org.onebusaway.gtfs.services.GenericMutableDao; +import org.onebusaway.gtfs.services.GtfsMutableRelationalDao; import org.opentripplanner.calendar.impl.CalendarServiceDataFactoryImpl; import org.opentripplanner.calendar.impl.MultiCalendarServiceImpl; import org.opentripplanner.graph_builder.model.GtfsBundle; @@ -48,6 +47,8 @@ the License, or (at your option) any later version. import com.google.common.collect.Sets; +import static org.opentripplanner.gtfs.mapping.ModelMapper.mapDao; + public class GtfsModule implements GraphBuilderModule { private static final Logger LOG = LoggerFactory.getLogger(GtfsModule.class); @@ -93,29 +94,35 @@ public void buildGraph(Graph graph, HashMap, Object> extra) { // because the time zone from the first agency is cached graph.clearTimeZone(); - MultiCalendarServiceImpl service = new MultiCalendarServiceImpl(); - GtfsStopContext stopContext = new GtfsStopContext(); + MultiCalendarServiceImpl service = new MultiCalendarServiceImpl(); // OTP + GtfsStopContext stopContext = new GtfsStopContext(); // OTP try { - for (GtfsBundle gtfsBundle : gtfsBundles) { + for (GtfsBundle gtfsBundle : gtfsBundles) { // GTFS // apply global defaults to individual GTFSBundles (if globals have been set) if (cacheDirectory != null && gtfsBundle.cacheDirectory == null) gtfsBundle.cacheDirectory = cacheDirectory; if (useCached != null && gtfsBundle.useCached == null) gtfsBundle.useCached = useCached; - GtfsMutableRelationalDao dao = new GtfsRelationalDaoImpl(); - GtfsContext context = GtfsLibrary.createContext(gtfsBundle.getFeedId(), dao, service); + + + org.onebusaway2.gtfs.services.GtfsMutableRelationalDao dao = mapDao(loadBundle(gtfsBundle)); // OTP + + // Map from gtfs to OTP model here + GtfsContext context = GtfsLibrary.createContext(gtfsBundle.getFeedId(), dao, service); // OTP? GTFSPatternHopFactory hf = new GTFSPatternHopFactory(context); + hf.setStopContext(stopContext); hf.setFareServiceFactory(_fareServiceFactory); hf.setMaxStopToShapeSnapDistance(gtfsBundle.getMaxStopToShapeSnapDistance()); - loadBundle(gtfsBundle, graph, dao); - CalendarServiceDataFactoryImpl csfactory = new CalendarServiceDataFactoryImpl(); + CalendarServiceDataFactoryImpl csfactory = new CalendarServiceDataFactoryImpl(); //OTP csfactory.setGtfsDao(dao); - CalendarServiceData data = csfactory.createData(); - service.addData(data, dao); + org.onebusaway2.gtfs.model.calendar.CalendarServiceData data = csfactory.createData(); + + // Map CalendarServiceData here + service.addData(data, context.getDao()); // OTP hf.subwayAccessTime = gtfsBundle.subwayAccessTime; hf.maxInterlineDistance = gtfsBundle.maxInterlineDistance; @@ -136,9 +143,8 @@ public void buildGraph(Graph graph, HashMap, Object> extra) { } // We need to save the calendar service data so we can use it later - CalendarServiceData data = service.getData(); - graph.putService(CalendarServiceData.class, data); - graph.updateTransitFeedValidity(data); + graph.putService(org.onebusaway2.gtfs.model.calendar.CalendarServiceData.class, service.getData()); + graph.updateTransitFeedValidity(service.getData()); graph.hasTransit = true; graph.calculateTransitCenter(); @@ -149,10 +155,10 @@ public void buildGraph(Graph graph, HashMap, Object> extra) { * Private Methods ****/ - private void loadBundle(GtfsBundle gtfsBundle, Graph graph, GtfsMutableRelationalDao dao) + private GtfsMutableRelationalDao loadBundle(GtfsBundle gtfsBundle) throws IOException { - StoreImpl store = new StoreImpl(dao); + StoreImpl store = new StoreImpl(new GtfsRelationalDaoImpl()); store.open(); LOG.info("reading {}", gtfsBundle.toString()); @@ -227,7 +233,7 @@ private void loadBundle(GtfsBundle gtfsBundle, Graph graph, GtfsMutableRelationa } store.close(); - + return store.dao; } /** diff --git a/src/main/java/org/opentripplanner/gtfs/BikeAccess.java b/src/main/java/org/opentripplanner/gtfs/BikeAccess.java index fc8e7ed5835..53c3a50adb4 100644 --- a/src/main/java/org/opentripplanner/gtfs/BikeAccess.java +++ b/src/main/java/org/opentripplanner/gtfs/BikeAccess.java @@ -98,4 +98,60 @@ public static void setForTrip(Trip trip, BikeAccess access) { break; } } + + /** + * @Improvment This method could be removed, if the logic was not part of the GTFS import, + * but rader applyed after the GTFS model is mapped into OTP. + */ + @SuppressWarnings("deprecation") + public static BikeAccess fromTrip(org.onebusaway.gtfs.model.Trip gtfsTrip) { + switch (gtfsTrip.getBikesAllowed()) { + case 1: + return ALLOWED; + case 2: + return NOT_ALLOWED; + } + switch (gtfsTrip.getTripBikesAllowed()) { + case 1: + return NOT_ALLOWED; + case 2: + return ALLOWED; + } + org.onebusaway.gtfs.model.Route route = gtfsTrip.getRoute(); + switch (route.getBikesAllowed()) { + case 1: + return ALLOWED; + case 2: + return NOT_ALLOWED; + } + switch (route.getRouteBikesAllowed()) { + case 1: + return NOT_ALLOWED; + case 2: + return ALLOWED; + } + return UNKNOWN; + } + + /** + * @Improvment This method could be removed, if the logic was not part of the GTFS import, + * but rader applyed after the GTFS model is mapped into OTP. + */ + @SuppressWarnings("deprecation") + public static void setForTrip(org.onebusaway.gtfs.model.Trip gtfsTrip, BikeAccess access) { + switch (access) { + case ALLOWED: + gtfsTrip.setBikesAllowed(1); + gtfsTrip.setTripBikesAllowed(2); + break; + case NOT_ALLOWED: + gtfsTrip.setBikesAllowed(2); + gtfsTrip.setTripBikesAllowed(1); + break; + case UNKNOWN: + gtfsTrip.setBikesAllowed(0); + gtfsTrip.setTripBikesAllowed(0); + break; + } + } } diff --git a/src/main/java/org/opentripplanner/gtfs/GtfsImport.java b/src/main/java/org/opentripplanner/gtfs/GtfsImport.java new file mode 100644 index 00000000000..961ab8312ab --- /dev/null +++ b/src/main/java/org/opentripplanner/gtfs/GtfsImport.java @@ -0,0 +1,52 @@ +/* This program is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser 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 . */ + +package org.opentripplanner.gtfs; + +import org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl; +import org.onebusaway.gtfs.serialization.GtfsReader; +import org.onebusaway.gtfs.services.GtfsMutableRelationalDao; +import org.opentripplanner.graph_builder.module.GtfsFeedId; + +import java.io.File; +import java.io.IOException; + +class GtfsImport { + + private final GtfsReader reader; + private GtfsFeedId feedId = null; + private GtfsRelationalDaoImpl dao = null; + + GtfsImport(File path) throws IOException { + this.reader = new GtfsReader(); + reader.setInputLocation(path); + } + + /** TODO TGR - This does not need to be mutable? */ + GtfsMutableRelationalDao getDao() throws IOException { + if(dao == null) { + dao = new GtfsRelationalDaoImpl(); + reader.setEntityStore(dao); + reader.setDefaultAgencyId(getFeedId().getId()); + reader.run(); + } + return dao; + } + + GtfsFeedId getFeedId() { + if(feedId == null) { + feedId = new GtfsFeedId.Builder().fromGtfsFeed(reader.getInputSource()).build(); + } + return feedId; + } +} diff --git a/src/main/java/org/opentripplanner/gtfs/GtfsLibrary.java b/src/main/java/org/opentripplanner/gtfs/GtfsLibrary.java index 362300b69d9..f80055acd01 100644 --- a/src/main/java/org/opentripplanner/gtfs/GtfsLibrary.java +++ b/src/main/java/org/opentripplanner/gtfs/GtfsLibrary.java @@ -17,16 +17,16 @@ the License, or (at your option) any later version. import java.io.File; import java.io.IOException; -import org.onebusaway2.gtfs.impl.GtfsRelationalDaoImpl; import org.onebusaway2.gtfs.impl.calendar.CalendarServiceDataFactoryImpl; import org.onebusaway2.gtfs.impl.calendar.CalendarServiceImpl; import org.onebusaway2.gtfs.model.AgencyAndId; import org.onebusaway2.gtfs.model.Route; import org.onebusaway2.gtfs.model.calendar.CalendarServiceData; -import org.onebusaway2.gtfs.serialization.GtfsReader; +import org.onebusaway2.gtfs.services.GtfsMutableRelationalDao; import org.onebusaway2.gtfs.services.GtfsRelationalDao; import org.onebusaway2.gtfs.services.calendar.CalendarService; import org.opentripplanner.graph_builder.module.GtfsFeedId; +import org.opentripplanner.gtfs.mapping.ModelMapper; import org.opentripplanner.routing.core.TraverseMode; public class GtfsLibrary { @@ -43,21 +43,13 @@ public static GtfsContext createContext(GtfsFeedId feedId, GtfsRelationalDao dao } public static GtfsContext readGtfs(File path) throws IOException { - GtfsRelationalDaoImpl dao = new GtfsRelationalDaoImpl(); + GtfsImport gtfsImport = new GtfsImport(path); - GtfsReader reader = new GtfsReader(); - reader.setInputLocation(path); - reader.setEntityStore(dao); + GtfsFeedId feedId = gtfsImport.getFeedId(); + GtfsMutableRelationalDao otpDao = ModelMapper.mapDao(gtfsImport.getDao()); + CalendarService calendarService = createCalendarService(otpDao); - GtfsFeedId feedId = new GtfsFeedId.Builder().fromGtfsFeed(reader.getInputSource()).build(); - - reader.setDefaultAgencyId(feedId.getId()); - - reader.run(); - - CalendarService calendarService = createCalendarService(dao); - - return new GtfsContextImpl(feedId, dao, calendarService); + return new GtfsContextImpl(feedId, otpDao, calendarService); } public static CalendarService createCalendarService(GtfsRelationalDao dao) { @@ -158,7 +150,7 @@ private static class GtfsContextImpl implements GtfsContext { private GtfsRelationalDao _dao; private CalendarService _calendar; - + public GtfsContextImpl(GtfsFeedId feedId, GtfsRelationalDao dao, CalendarService calendar) { _feedId = feedId; _dao = dao; diff --git a/src/main/java/org/opentripplanner/gtfs/mapping/AgencyAndIdMapper.java b/src/main/java/org/opentripplanner/gtfs/mapping/AgencyAndIdMapper.java new file mode 100644 index 00000000000..8a794e4b5eb --- /dev/null +++ b/src/main/java/org/opentripplanner/gtfs/mapping/AgencyAndIdMapper.java @@ -0,0 +1,22 @@ +/* This program is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser 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 . */ + +package org.opentripplanner.gtfs.mapping; + +import org.onebusaway2.gtfs.model.AgencyAndId; + +public class AgencyAndIdMapper { + public static AgencyAndId mapAgencyAndId(org.onebusaway.gtfs.model.AgencyAndId id) { + return id == null ? null : new AgencyAndId(id.getAgencyId(), id.getId()); + } +} diff --git a/src/main/java/org/opentripplanner/gtfs/mapping/AgencyMapper.java b/src/main/java/org/opentripplanner/gtfs/mapping/AgencyMapper.java new file mode 100644 index 00000000000..02c6a712642 --- /dev/null +++ b/src/main/java/org/opentripplanner/gtfs/mapping/AgencyMapper.java @@ -0,0 +1,48 @@ +/* This program is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser 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 . */ + +package org.opentripplanner.gtfs.mapping; + +import org.onebusaway2.gtfs.model.Agency; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +class AgencyMapper { + + private final Map mappedAgencies = new HashMap<>(); + + Collection map(Collection agencies) { + return MapCollection.mapCollection(agencies, this::map); + } + + Agency map(org.onebusaway.gtfs.model.Agency orginal) { + return mappedAgencies.computeIfAbsent(orginal, this::doMap); + } + + private Agency doMap(org.onebusaway.gtfs.model.Agency rhs) { + Agency lhs = new Agency(); + + lhs.setId(rhs.getId()); + lhs.setName(rhs.getName()); + lhs.setUrl(rhs.getUrl()); + lhs.setTimezone(rhs.getTimezone()); + lhs.setLang(rhs.getLang()); + lhs.setPhone(rhs.getPhone()); + lhs.setFareUrl(rhs.getFareUrl()); + lhs.setBrandingUrl(rhs.getBrandingUrl()); + + return lhs; + } +} diff --git a/src/main/java/org/opentripplanner/gtfs/mapping/FareAttributeMapper.java b/src/main/java/org/opentripplanner/gtfs/mapping/FareAttributeMapper.java new file mode 100644 index 00000000000..5297ad68965 --- /dev/null +++ b/src/main/java/org/opentripplanner/gtfs/mapping/FareAttributeMapper.java @@ -0,0 +1,50 @@ +/* This program is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser 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 . */ + +package org.opentripplanner.gtfs.mapping; + +import org.onebusaway2.gtfs.model.FareAttribute; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import static org.opentripplanner.gtfs.mapping.AgencyAndIdMapper.mapAgencyAndId; + +public class FareAttributeMapper { + private Map mappedStops = new HashMap<>(); + + Collection map(Collection allStops) { + return MapCollection.mapCollection(allStops, this::map); + } + + FareAttribute map(org.onebusaway.gtfs.model.FareAttribute orginal) { + return mappedStops.computeIfAbsent(orginal, this::doMap); + } + + private FareAttribute doMap(org.onebusaway.gtfs.model.FareAttribute rhs) { + FareAttribute lhs = new FareAttribute(); + + lhs.setId(mapAgencyAndId(rhs.getId())); + lhs.setPrice(rhs.getPrice()); + lhs.setCurrencyType(rhs.getCurrencyType()); + lhs.setPaymentMethod(rhs.getPaymentMethod()); + lhs.setTransfers(rhs.getTransfers()); + lhs.setTransferDuration(rhs.getTransferDuration()); + lhs.setYouthPrice(rhs.getYouthPrice()); + lhs.setSeniorPrice(rhs.getSeniorPrice()); + lhs.setJourneyDuration(rhs.getJourneyDuration()); + + return lhs; + } +} diff --git a/src/main/java/org/opentripplanner/gtfs/mapping/FareRuleMapper.java b/src/main/java/org/opentripplanner/gtfs/mapping/FareRuleMapper.java new file mode 100644 index 00000000000..b0ae4bd73ef --- /dev/null +++ b/src/main/java/org/opentripplanner/gtfs/mapping/FareRuleMapper.java @@ -0,0 +1,54 @@ +/* This program is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser 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 . */ + +package org.opentripplanner.gtfs.mapping; + +import org.onebusaway2.gtfs.model.FareRule; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +public class FareRuleMapper { + + private final RouteMapper routeMapper; + private final FareAttributeMapper fareAttributeMapper; + + private Map mappedFareRules = new HashMap<>(); + + public FareRuleMapper(RouteMapper routeMapper, FareAttributeMapper fareAttributeMapper) { + this.routeMapper = routeMapper; + this.fareAttributeMapper = fareAttributeMapper; + } + + Collection map(Collection allFareRules) { + return MapCollection.mapCollection(allFareRules, this::map); + } + + FareRule map(org.onebusaway.gtfs.model.FareRule orginal) { + return orginal == null ? null : mappedFareRules.computeIfAbsent(orginal, this::doMap); + } + + private FareRule doMap(org.onebusaway.gtfs.model.FareRule rhs) { + FareRule lhs = new FareRule(); + + lhs.setId(rhs.getId()); + lhs.setFare(fareAttributeMapper.map(rhs.getFare())); + lhs.setRoute(routeMapper.map(rhs.getRoute())); + lhs.setOriginId(rhs.getOriginId()); + lhs.setDestinationId(rhs.getDestinationId()); + lhs.setContainsId(rhs.getContainsId()); + + return lhs; + } +} diff --git a/src/main/java/org/opentripplanner/gtfs/mapping/FeedInfoMapper.java b/src/main/java/org/opentripplanner/gtfs/mapping/FeedInfoMapper.java new file mode 100644 index 00000000000..239e7f17f1a --- /dev/null +++ b/src/main/java/org/opentripplanner/gtfs/mapping/FeedInfoMapper.java @@ -0,0 +1,49 @@ +/* This program is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser 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 . */ + +package org.opentripplanner.gtfs.mapping; + +import org.onebusaway2.gtfs.model.FeedInfo; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import static org.opentripplanner.gtfs.mapping.ServiceDateMapper.mapServiceDate; + +public class FeedInfoMapper { + private Map mappedFeedInfos = new HashMap<>(); + + + Collection map(Collection feedInfos) { + return MapCollection.mapCollection(feedInfos, this::map); + } + + FeedInfo map(org.onebusaway.gtfs.model.FeedInfo orginal) { + return orginal == null ? null : mappedFeedInfos.computeIfAbsent(orginal, this::doMap); + } + + private FeedInfo doMap(org.onebusaway.gtfs.model.FeedInfo rhs) { + FeedInfo lhs = new FeedInfo(); + + lhs.setId(rhs.getId()); + lhs.setPublisherName(rhs.getPublisherName()); + lhs.setPublisherUrl(rhs.getPublisherUrl()); + lhs.setLang(rhs.getLang()); + lhs.setStartDate(mapServiceDate(rhs.getStartDate())); + lhs.setEndDate(mapServiceDate(rhs.getEndDate())); + lhs.setVersion(rhs.getVersion()); + + return lhs; + } +} diff --git a/src/main/java/org/opentripplanner/gtfs/mapping/FrequencyMapper.java b/src/main/java/org/opentripplanner/gtfs/mapping/FrequencyMapper.java new file mode 100644 index 00000000000..eddc3380fa3 --- /dev/null +++ b/src/main/java/org/opentripplanner/gtfs/mapping/FrequencyMapper.java @@ -0,0 +1,53 @@ +/* This program is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser 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 . */ + +package org.opentripplanner.gtfs.mapping; + +import org.onebusaway2.gtfs.model.Frequency; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + + +public class FrequencyMapper { + private final TripMapper tripMapper; + + private Map mappedFrequencys = new HashMap<>(); + + public FrequencyMapper(TripMapper tripMapper) { + this.tripMapper = tripMapper; + } + + Collection map(Collection allFrequencys) { + return MapCollection.mapCollection(allFrequencys, this::map); + } + + Frequency map(org.onebusaway.gtfs.model.Frequency orginal) { + return orginal == null ? null : mappedFrequencys.computeIfAbsent(orginal, this::doMap); + } + + private Frequency doMap(org.onebusaway.gtfs.model.Frequency rhs) { + Frequency lhs = new Frequency(); + + lhs.setId(rhs.getId()); + lhs.setTrip(tripMapper.map(rhs.getTrip())); + lhs.setStartTime(rhs.getStartTime()); + lhs.setEndTime(rhs.getEndTime()); + lhs.setHeadwaySecs(rhs.getHeadwaySecs()); + lhs.setExactTimes(rhs.getExactTimes()); + lhs.setLabelOnly(rhs.getLabelOnly()); + + return lhs; + } +} diff --git a/src/main/java/org/opentripplanner/gtfs/mapping/MapCollection.java b/src/main/java/org/opentripplanner/gtfs/mapping/MapCollection.java new file mode 100644 index 00000000000..c1220e22a6d --- /dev/null +++ b/src/main/java/org/opentripplanner/gtfs/mapping/MapCollection.java @@ -0,0 +1,27 @@ +/* This program is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser 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 . */ + +package org.opentripplanner.gtfs.mapping; + +import java.util.Collection; +import java.util.function.Function; +import java.util.stream.Collectors; + +/** + * @author Thomas Gran (Capra) - tgr@capraconsulting.no (24.08.2017) + */ +public class MapCollection { + static Collection mapCollection(Collection entites, Function mapper) { + return entites == null ? null : entites.stream().map(mapper).collect(Collectors.toList()); + } +} diff --git a/src/main/java/org/opentripplanner/gtfs/mapping/ModelMapper.java b/src/main/java/org/opentripplanner/gtfs/mapping/ModelMapper.java new file mode 100644 index 00000000000..98cd2fd2ce8 --- /dev/null +++ b/src/main/java/org/opentripplanner/gtfs/mapping/ModelMapper.java @@ -0,0 +1,75 @@ +/* This program is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser 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 . */ + +package org.opentripplanner.gtfs.mapping; + +import org.onebusaway2.gtfs.impl.GtfsRelationalDaoImpl; +import org.onebusaway2.gtfs.services.GtfsMutableRelationalDao; + +public class ModelMapper { + private final AgencyMapper agencyMapper = new AgencyMapper(); + private final StopMapper stopMapper = new StopMapper(); + private final FareAttributeMapper fareAttributeMapper = new FareAttributeMapper(); + private final ServiceCalendarDateMapper serviceCalendarDateMapper = new ServiceCalendarDateMapper(); + private final FeedInfoMapper feedInfoMapper = new FeedInfoMapper(); + private final ShapePointMapper shapePointMapper = new ShapePointMapper(); + private final ServiceCalendarMapper serviceCalendarMapper = new ServiceCalendarMapper(); + + private final PathwayMapper pathwayMapper = new PathwayMapper(stopMapper); + private final RouteMapper routeMapper = new RouteMapper(agencyMapper); + private final TripMapper tripMapper = new TripMapper(routeMapper); + private final StopTimeMapper stopTimeMapper = new StopTimeMapper(stopMapper, tripMapper); + + private final FrequencyMapper frequencyMapper = new FrequencyMapper(tripMapper); + private final TransferMapper transferMapper = new TransferMapper(routeMapper, stopMapper, tripMapper); + + private final FareRuleMapper fareRuleMapper = new FareRuleMapper(routeMapper, fareAttributeMapper); + + public static GtfsMutableRelationalDao mapDao( + org.onebusaway.gtfs.services.GtfsMutableRelationalDao data + ) { + return new ModelMapper().map(data); + } + + private GtfsMutableRelationalDao map( + org.onebusaway.gtfs.services.GtfsMutableRelationalDao data + ) { + + // TODO TGR - Remove this if it works + if( + ((org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl)data).isPackShapePoints() || + ((org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl)data).isPackStopTimes() + ) { + System.err.println("!!! ------------------------------------------------------"); + System.err.println("!!! --> Data is packed, not expected: " + data); + System.err.println("!!! ------------------------------------------------------"); + } + + return new GtfsRelationalDaoImpl( + agencyMapper.map(data.getAllAgencies()), + serviceCalendarDateMapper.map(data.getAllCalendarDates()), + serviceCalendarMapper.map(data.getAllCalendars()), + fareAttributeMapper.map(data.getAllFareAttributes()), + fareRuleMapper.map(data.getAllFareRules()), + feedInfoMapper.map(data.getAllFeedInfos()), + frequencyMapper.map(data.getAllFrequencies()), + pathwayMapper.map(data.getAllPathways()), + routeMapper.map(data.getAllRoutes()), + shapePointMapper.map(data.getAllShapePoints()), + stopMapper.map(data.getAllStops()), + stopTimeMapper.map(data.getAllStopTimes()), + transferMapper.map(data.getAllTransfers()), + tripMapper.map(data.getAllTrips()) + ); + } +} diff --git a/src/main/java/org/opentripplanner/gtfs/mapping/PathwayMapper.java b/src/main/java/org/opentripplanner/gtfs/mapping/PathwayMapper.java new file mode 100644 index 00000000000..dd8000cb542 --- /dev/null +++ b/src/main/java/org/opentripplanner/gtfs/mapping/PathwayMapper.java @@ -0,0 +1,52 @@ +/* This program is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser 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 . */ + +package org.opentripplanner.gtfs.mapping; + +import org.onebusaway2.gtfs.model.Pathway; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +public class PathwayMapper { + + private final StopMapper stopMapper; + + private Map mappedPathways = new HashMap<>(); + + public PathwayMapper(StopMapper stopMapper) { + this.stopMapper = stopMapper; + } + + Collection map(Collection allPathways) { + return MapCollection.mapCollection(allPathways, this::map); + } + + Pathway map(org.onebusaway.gtfs.model.Pathway orginal) { + return orginal == null ? null : mappedPathways.computeIfAbsent(orginal, this::doMap); + } + + private Pathway doMap(org.onebusaway.gtfs.model.Pathway rhs) { + Pathway lhs = new Pathway(); + + lhs.setId(AgencyAndIdMapper.mapAgencyAndId(rhs.getId())); + lhs.setPathwayType(rhs.getPathwayType()); + lhs.setFromStop(stopMapper.map(rhs.getFromStop())); + lhs.setToStop(stopMapper.map(rhs.getToStop())); + lhs.setTraversalTime(rhs.getTraversalTime()); + lhs.setWheelchairTraversalTime(rhs.getWheelchairTraversalTime()); + + return lhs; + } +} diff --git a/src/main/java/org/opentripplanner/gtfs/mapping/RouteMapper.java b/src/main/java/org/opentripplanner/gtfs/mapping/RouteMapper.java new file mode 100644 index 00000000000..c0f339f01f3 --- /dev/null +++ b/src/main/java/org/opentripplanner/gtfs/mapping/RouteMapper.java @@ -0,0 +1,60 @@ +/* This program is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser 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 . */ + +package org.opentripplanner.gtfs.mapping; + +import org.onebusaway2.gtfs.model.Route; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +/** + * @author Thomas Gran (Capra) - tgr@capraconsulting.no (24.08.2017) + */ +public class RouteMapper { + private final AgencyMapper agencyMapper; + private final Map mappedRoutes = new HashMap<>(); + + public RouteMapper(AgencyMapper agencyMapper) { + this.agencyMapper = agencyMapper; + } + + Collection map(Collection agencies) { + return MapCollection.mapCollection(agencies, this::map); + } + + Route map(org.onebusaway.gtfs.model.Route orginal) { + return orginal == null ? null : mappedRoutes.computeIfAbsent(orginal, this::doMap); + } + + private Route doMap(org.onebusaway.gtfs.model.Route rhs) { + Route lhs = new Route(); + + lhs.setId(AgencyAndIdMapper.mapAgencyAndId(rhs.getId())); + lhs.setAgency(agencyMapper.map(rhs.getAgency())); + lhs.setShortName(rhs.getShortName()); + lhs.setLongName(rhs.getLongName()); + lhs.setType(rhs.getType()); + lhs.setDesc(rhs.getDesc()); + lhs.setUrl(rhs.getUrl()); + lhs.setColor(rhs.getColor()); + lhs.setTextColor(rhs.getTextColor()); + lhs.setRouteBikesAllowed(rhs.getRouteBikesAllowed()); + lhs.setBikesAllowed(rhs.getBikesAllowed()); + lhs.setSortOrder(rhs.getSortOrder()); + lhs.setBrandingUrl(rhs.getBrandingUrl()); + + return lhs; + } +} diff --git a/src/main/java/org/opentripplanner/gtfs/mapping/ServiceCalendarDateMapper.java b/src/main/java/org/opentripplanner/gtfs/mapping/ServiceCalendarDateMapper.java new file mode 100644 index 00000000000..17e2a53a3ed --- /dev/null +++ b/src/main/java/org/opentripplanner/gtfs/mapping/ServiceCalendarDateMapper.java @@ -0,0 +1,45 @@ +/* This program is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser 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 . */ + +package org.opentripplanner.gtfs.mapping; + +import org.onebusaway2.gtfs.model.ServiceCalendarDate; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +class ServiceCalendarDateMapper { + private Map mappedServiceDates = new HashMap<>(); + + Collection map( + Collection allServiceDates) { + return MapCollection.mapCollection(allServiceDates, this::map); + } + + ServiceCalendarDate map(org.onebusaway.gtfs.model.ServiceCalendarDate orginal) { + return orginal == null ? null : mappedServiceDates.computeIfAbsent(orginal, this::doMap); + } + + private ServiceCalendarDate doMap(org.onebusaway.gtfs.model.ServiceCalendarDate rhs) { + ServiceCalendarDate lhs = new ServiceCalendarDate(); + + lhs.setId(rhs.getId()); + lhs.setServiceId(AgencyAndIdMapper.mapAgencyAndId(rhs.getServiceId())); + lhs.setDate(ServiceDateMapper.mapServiceDate(rhs.getDate())); + lhs.setExceptionType(rhs.getExceptionType()); + + return lhs; + } + +} diff --git a/src/main/java/org/opentripplanner/gtfs/mapping/ServiceCalendarMapper.java b/src/main/java/org/opentripplanner/gtfs/mapping/ServiceCalendarMapper.java new file mode 100644 index 00000000000..06d299c1d42 --- /dev/null +++ b/src/main/java/org/opentripplanner/gtfs/mapping/ServiceCalendarMapper.java @@ -0,0 +1,53 @@ +/* This program is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser 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 . */ + +package org.opentripplanner.gtfs.mapping; + +import org.onebusaway2.gtfs.model.ServiceCalendar; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import static org.opentripplanner.gtfs.mapping.AgencyAndIdMapper.mapAgencyAndId; + +public class ServiceCalendarMapper { + private Map mappedCalendars = new HashMap<>(); + + Collection map( + Collection allServiceCalendars) { + return MapCollection.mapCollection(allServiceCalendars, this::map); + } + + ServiceCalendar map(org.onebusaway.gtfs.model.ServiceCalendar orginal) { + return orginal == null ? null : mappedCalendars.computeIfAbsent(orginal, this::doMap); + } + + private ServiceCalendar doMap(org.onebusaway.gtfs.model.ServiceCalendar rhs) { + ServiceCalendar lhs = new ServiceCalendar(); + + lhs.setId(rhs.getId()); + lhs.setServiceId(mapAgencyAndId(rhs.getServiceId())); + lhs.setMonday(rhs.getMonday()); + lhs.setTuesday(rhs.getTuesday()); + lhs.setWednesday(rhs.getWednesday()); + lhs.setThursday(rhs.getThursday()); + lhs.setFriday(rhs.getFriday()); + lhs.setSaturday(rhs.getSaturday()); + lhs.setSunday(rhs.getSunday()); + lhs.setStartDate(ServiceDateMapper.mapServiceDate(rhs.getStartDate())); + lhs.setEndDate(ServiceDateMapper.mapServiceDate(rhs.getEndDate())); + + return lhs; + } +} diff --git a/src/main/java/org/opentripplanner/gtfs/mapping/ServiceDateMapper.java b/src/main/java/org/opentripplanner/gtfs/mapping/ServiceDateMapper.java new file mode 100644 index 00000000000..a32dde2e0ab --- /dev/null +++ b/src/main/java/org/opentripplanner/gtfs/mapping/ServiceDateMapper.java @@ -0,0 +1,23 @@ +/* This program is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser 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 . */ + +package org.opentripplanner.gtfs.mapping; + +import org.onebusaway2.gtfs.model.calendar.ServiceDate; + + +public class ServiceDateMapper { + static ServiceDate mapServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate orginal) { + return orginal == null ? null : new ServiceDate(orginal.getYear(), orginal.getMonth(), orginal.getDay()); + } +} diff --git a/src/main/java/org/opentripplanner/gtfs/mapping/ShapePointMapper.java b/src/main/java/org/opentripplanner/gtfs/mapping/ShapePointMapper.java new file mode 100644 index 00000000000..60e2779bd3e --- /dev/null +++ b/src/main/java/org/opentripplanner/gtfs/mapping/ShapePointMapper.java @@ -0,0 +1,54 @@ +/* This program is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser 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 . */ + +package org.opentripplanner.gtfs.mapping; + +import org.onebusaway2.gtfs.model.ShapePoint; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import static org.opentripplanner.gtfs.mapping.MapCollection.mapCollection; + +public class ShapePointMapper { + private Map mappedShapePoints = new HashMap<>(); + + Collection map( + Collection allShapePoints) { + return mapCollection(allShapePoints, this::map); + } + + ShapePoint map(org.onebusaway.gtfs.model.ShapePoint orginal) { + return orginal == null ? null : mappedShapePoints.computeIfAbsent(orginal, this::doMap); + } + + private ShapePoint doMap(org.onebusaway.gtfs.model.ShapePoint rhs) { + ShapePoint lhs = new ShapePoint(); + + lhs.setId(rhs.getId()); + lhs.setShapeId(AgencyAndIdMapper.mapAgencyAndId(rhs.getShapeId())); + lhs.setSequence(rhs.getSequence()); + lhs.setLat(rhs.getLat()); + lhs.setLon(rhs.getLon()); + lhs.setDistTraveled(rhs.getDistTraveled()); + + // Skip mapping of proxy + // private transient StopTimeProxy proxy; + if (rhs.getProxy() != null) { + throw new IllegalStateException("Did not expect proxy to be set! Data: " + rhs); + } + + return lhs; + } +} diff --git a/src/main/java/org/opentripplanner/gtfs/mapping/StopMapper.java b/src/main/java/org/opentripplanner/gtfs/mapping/StopMapper.java new file mode 100644 index 00000000000..abcb4a692c2 --- /dev/null +++ b/src/main/java/org/opentripplanner/gtfs/mapping/StopMapper.java @@ -0,0 +1,57 @@ +/* This program is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser 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 . */ + +package org.opentripplanner.gtfs.mapping; + +import org.onebusaway2.gtfs.model.Stop; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import static org.opentripplanner.gtfs.mapping.AgencyAndIdMapper.mapAgencyAndId; +import static org.opentripplanner.gtfs.mapping.MapCollection.mapCollection; + +class StopMapper { + private Map mappedStops = new HashMap<>(); + + Collection map(Collection allStops) { + return mapCollection(allStops, this::map); + } + + Stop map(org.onebusaway.gtfs.model.Stop orginal) { + return orginal == null ? null : mappedStops.computeIfAbsent(orginal, this::doMap); + } + + private Stop doMap(org.onebusaway.gtfs.model.Stop rhs) { + Stop lhs = new Stop(); + + lhs.setId(mapAgencyAndId(rhs.getId())); + lhs.setName(rhs.getName()); + lhs.setLat(rhs.getLat()); + lhs.setLon(rhs.getLon()); + lhs.setCode(rhs.getCode()); + lhs.setDesc(rhs.getDesc()); + lhs.setZoneId(rhs.getZoneId()); + lhs.setUrl(rhs.getUrl()); + lhs.setLocationType(rhs.getLocationType()); + lhs.setParentStation(rhs.getParentStation()); + lhs.setWheelchairBoarding(rhs.getWheelchairBoarding()); + lhs.setDirection(rhs.getDirection()); + lhs.setTimezone(rhs.getTimezone()); + lhs.setVehicleType(rhs.getVehicleType()); + lhs.setPlatformCode(rhs.getPlatformCode()); + + return lhs; + } +} diff --git a/src/main/java/org/opentripplanner/gtfs/mapping/StopTimeMapper.java b/src/main/java/org/opentripplanner/gtfs/mapping/StopTimeMapper.java new file mode 100644 index 00000000000..627b7cbe064 --- /dev/null +++ b/src/main/java/org/opentripplanner/gtfs/mapping/StopTimeMapper.java @@ -0,0 +1,73 @@ +/* This program is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser 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 . */ + +package org.opentripplanner.gtfs.mapping; + +import org.onebusaway2.gtfs.model.StopTime; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +/** + * @author Thomas Gran (Capra) - tgr@capraconsulting.no (24.08.2017) + */ +class StopTimeMapper { + private final StopMapper stopMapper; + + private final TripMapper tripMapper; + + private Map mappedStopTimes = new HashMap<>(); + + StopTimeMapper(StopMapper stopMapper, TripMapper tripMapper) { + this.stopMapper = stopMapper; + this.tripMapper = tripMapper; + } + + Collection map(Collection times) { + return MapCollection.mapCollection(times, this::map); + } + + StopTime map(org.onebusaway.gtfs.model.StopTime orginal) { + return orginal == null ? null : mappedStopTimes.computeIfAbsent(orginal, this::doMap); + } + + private StopTime doMap(org.onebusaway.gtfs.model.StopTime rhs) { + StopTime lhs = new StopTime(); + + lhs.setId(rhs.getId()); + + lhs.setId(rhs.getId()); + lhs.setTrip(tripMapper.map(rhs.getTrip())); + lhs.setStop(stopMapper.map(rhs.getStop())); + lhs.setArrivalTime(rhs.getArrivalTime()); + lhs.setDepartureTime(rhs.getDepartureTime()); + lhs.setTimepoint(rhs.getTimepoint()); + lhs.setStopSequence(rhs.getStopSequence()); + lhs.setStopHeadsign(rhs.getStopHeadsign()); + lhs.setRouteShortName(rhs.getRouteShortName()); + lhs.setPickupType(rhs.getPickupType()); + lhs.setDropOffType(rhs.getDropOffType()); + lhs.setShapeDistTraveled(rhs.getShapeDistTraveled()); + lhs.setFarePeriodId(rhs.getFarePeriodId()); + + // Skip mapping of proxy + // private transient StopTimeProxy proxy; + if (rhs.getProxy() != null) { + throw new IllegalStateException("Did not expect proxy to be set!"); + } + + return lhs; + } + +} diff --git a/src/main/java/org/opentripplanner/gtfs/mapping/TransferMapper.java b/src/main/java/org/opentripplanner/gtfs/mapping/TransferMapper.java new file mode 100644 index 00000000000..09c315c4e9f --- /dev/null +++ b/src/main/java/org/opentripplanner/gtfs/mapping/TransferMapper.java @@ -0,0 +1,64 @@ +/* This program is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser 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 . */ + +package org.opentripplanner.gtfs.mapping; + +import org.onebusaway2.gtfs.model.Transfer; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import static org.opentripplanner.gtfs.mapping.MapCollection.mapCollection; + +class TransferMapper { + private final RouteMapper routeMapper; + + private final StopMapper stopMapper; + + private final TripMapper tripMapper; + + private Map mappedTransfers = new HashMap<>(); + + public TransferMapper(RouteMapper routeMapper, StopMapper stopMapper, TripMapper tripMapper) { + this.routeMapper = routeMapper; + this.stopMapper = stopMapper; + this.tripMapper = tripMapper; + } + + Collection map(Collection allTransfers) { + return mapCollection(allTransfers, this::map); + } + + Transfer map(org.onebusaway.gtfs.model.Transfer orginal) { + return orginal == null ? null : mappedTransfers.computeIfAbsent(orginal, this::doMap); + } + + private Transfer doMap(org.onebusaway.gtfs.model.Transfer rhs) { + Transfer lhs = new Transfer(); + + lhs.setId(rhs.getId()); + + lhs.setId(rhs.getId()); + lhs.setFromStop(stopMapper.map(rhs.getFromStop())); + lhs.setFromRoute(routeMapper.map(rhs.getFromRoute())); + lhs.setFromTrip(tripMapper.map(rhs.getFromTrip())); + lhs.setToStop(stopMapper.map(rhs.getToStop())); + lhs.setToRoute(routeMapper.map(rhs.getToRoute())); + lhs.setToTrip(tripMapper.map(rhs.getToTrip())); + lhs.setTransferType(rhs.getTransferType()); + lhs.setMinTransferTime(rhs.getMinTransferTime()); + + return lhs; + } +} diff --git a/src/main/java/org/opentripplanner/gtfs/mapping/TripMapper.java b/src/main/java/org/opentripplanner/gtfs/mapping/TripMapper.java new file mode 100644 index 00000000000..10d72b36ce8 --- /dev/null +++ b/src/main/java/org/opentripplanner/gtfs/mapping/TripMapper.java @@ -0,0 +1,62 @@ +/* This program is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser 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 . */ + +package org.opentripplanner.gtfs.mapping; + +import org.onebusaway2.gtfs.model.Trip; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import static org.opentripplanner.gtfs.mapping.MapCollection.mapCollection; + +class TripMapper { + + private final RouteMapper routeMapper; + + private Map mappedTrips = new HashMap<>(); + + TripMapper(RouteMapper routeMapper) { + this.routeMapper = routeMapper; + } + + Collection map(Collection trips) { + return mapCollection(trips, this::map); + } + + Trip map(org.onebusaway.gtfs.model.Trip orginal) { + return orginal == null ? null : mappedTrips.computeIfAbsent(orginal, this::doMap); + } + + private Trip doMap(org.onebusaway.gtfs.model.Trip rhs) { + Trip lhs = new Trip(); + + lhs.setId(AgencyAndIdMapper.mapAgencyAndId(rhs.getId())); + lhs.setRoute(routeMapper.map(rhs.getRoute())); + lhs.setServiceId(AgencyAndIdMapper.mapAgencyAndId(rhs.getServiceId())); + lhs.setTripShortName(rhs.getTripShortName()); + lhs.setTripHeadsign(rhs.getTripHeadsign()); + lhs.setRouteShortName(rhs.getRouteShortName()); + lhs.setDirectionId(rhs.getDirectionId()); + lhs.setBlockId(rhs.getBlockId()); + lhs.setShapeId(AgencyAndIdMapper.mapAgencyAndId(rhs.getShapeId())); + lhs.setWheelchairAccessible(rhs.getWheelchairAccessible()); + lhs.setTripBikesAllowed(rhs.getTripBikesAllowed()); + lhs.setBikesAllowed(rhs.getBikesAllowed()); + lhs.setFareId(rhs.getFareId()); + + return lhs; + } + +} diff --git a/src/test/java/org/opentripplanner/routing/edgetype/factory/GTFSPatternHopFactoryTest.java b/src/test/java/org/opentripplanner/routing/edgetype/factory/GTFSPatternHopFactoryTest.java index 10faab41214..eeda0b45c88 100644 --- a/src/test/java/org/opentripplanner/routing/edgetype/factory/GTFSPatternHopFactoryTest.java +++ b/src/test/java/org/opentripplanner/routing/edgetype/factory/GTFSPatternHopFactoryTest.java @@ -13,7 +13,6 @@ the License, or (at your option) any later version. package org.opentripplanner.routing.edgetype.factory; -import static org.junit.Assert.assertTrue; import java.io.IOException; @@ -41,8 +40,9 @@ public void testBikesAllowed() throws IOException { "t0,09:00:00,17:00:00,300"); GtfsFeedId feedId = new GtfsFeedId.Builder().id("FEED").build(); - GTFSPatternHopFactory factory = new GTFSPatternHopFactory(GtfsLibrary.createContext(feedId, gtfs - .read())); + GTFSPatternHopFactory factory = new GTFSPatternHopFactory( + GtfsLibrary.createContext(feedId, gtfs.read()) + ); Graph graph = new Graph(); factory.run(graph);