Skip to content

Commit

Permalink
Merge pull request #18 from matsim-org/feature/update-to-matsim12
Browse files Browse the repository at this point in the history
Feature/update to matsim12
  • Loading branch information
jfbischoff authored Jun 29, 2020
2 parents 8aa04d1 + c09b04a commit 9cdef70
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 42 deletions.
15 changes: 10 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.matsim.contrib</groupId>
<artifactId>matsim-gtfs</artifactId>
<version>11.1-SNAPSHOT</version>
<version>12.1-SNAPSHOT</version>

<repositories>
<repository>
<!-- Geotools is not on Maven central -->
<id>osgeo</id>
<name>Geotools repository</name>
<url>http://download.osgeo.org/webdav/geotools</url>
<name>OSGeo Release Repository</name>
<url>https://repo.osgeo.org/repository/release/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
<repository>
<id>matsim</id>
Expand Down Expand Up @@ -47,7 +52,7 @@
<dependency>
<groupId>org.matsim</groupId>
<artifactId>matsim</artifactId>
<version>11.0</version>
<version>12.0</version>
</dependency>
<dependency>
<groupId>com.conveyal</groupId>
Expand Down
16 changes: 7 additions & 9 deletions src/main/java/org/matsim/contrib/gtfs/GtfsConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,17 @@ private void convertTrips(List<Trip> trips) {
for(StopTime stopTime : feed.getInterpolatedStopTimesForTrip(trip.trip_id)) {
Id<TransitStopFacility> stopId = Id.create(stopTime.stop_id, TransitStopFacility.class);
TransitStopFacility stop = ts.getFacilities().get(stopId);
double arrivalOffset;

TransitRouteStop.Builder builder = ts.getFactory().createTransitRouteStopBuilder(stop);
if (stopTime.arrival_time != Integer.MIN_VALUE) {
arrivalOffset = Time.parseTime(String.valueOf(stopTime.arrival_time)) - departureTime;
} else {
arrivalOffset = Time.UNDEFINED_TIME;
double arrivalOffset = Time.parseTime(String.valueOf(stopTime.arrival_time)) - departureTime;
builder.arrivalOffset(arrivalOffset);
}
double departureOffset;
if (stopTime.departure_time != Integer.MIN_VALUE) {
departureOffset = Time.parseTime(String.valueOf(stopTime.departure_time)) - departureTime;
} else {
departureOffset = Time.UNDEFINED_TIME;
double departureOffset = Time.parseTime(String.valueOf(stopTime.departure_time)) - departureTime;
builder.departureOffset(departureOffset);
}
TransitRouteStop routeStop = ts.getFactory().createTransitRouteStop(stop, arrivalOffset, departureOffset);
TransitRouteStop routeStop = builder.build();
routeStop.setAwaitDepartureTime(true);
stops.add(routeStop);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/matsim/contrib/gtfs/RouteType.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ public enum RouteType {
SIGHTSEEING_TRAM_SERVICE(904, "Sightseeing Tram Service", TRAM),
SHUTTLE_TRAM_SERVICE(905, "Shuttle Tram Service", TRAM),
ALL_TRAM_SERVICE(906, "All Tram Services", TRAM),

OTHER_TRAM_SERVICE(907, "Other Tram Services", TRAM),

WATER_TRANSPORT_SERVICE(1000, "Water Transport Service", FERRY),
INTERNATIONAL_CAR_FERRY_SERVICE(1001, "International Car Ferry Service", FERRY),
NATIONAL_CAR_FERRY_SERVICE(1002, "National Car Ferry Service", FERRY),
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/matsim/contrib/gtfs/RunGTFS2MATSim.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
import org.matsim.core.scenario.ScenarioUtils;
import org.matsim.core.utils.geometry.CoordinateTransformation;
import org.matsim.core.utils.geometry.transformations.IdentityTransformation;
import org.matsim.core.utils.geometry.transformations.TransformationFactory;
import org.matsim.pt.transitSchedule.api.TransitScheduleWriter;

import com.conveyal.gtfs.GTFSFeed;
import com.conveyal.gtfs.model.Route;
import org.matsim.pt.utils.CreatePseudoNetwork;
import org.matsim.pt.utils.CreateVehiclesForSchedule;

/**
* @author NKuehnel
Expand Down Expand Up @@ -52,6 +55,23 @@ public static void convertGtfs(String fromFile, String toFile, LocalDate date, C
System.out.println("Done.");
}

public static void convertGTFSandAddToScenario(Scenario scenario, String gtfsZip, LocalDate date, CoordinateTransformation coordinateTransformation, boolean createNetworkAndVehicles) {
GTFSFeed feed = GTFSFeed.fromFile(gtfsZip);
feed.feedInfo.values().stream().findFirst().ifPresent((feedInfo) -> {
System.out.println("Feed start date: " + feedInfo.feed_start_date);
System.out.println("Feed end date: " + feedInfo.feed_end_date);
});
GtfsConverter converter = new GtfsConverter(feed, scenario, coordinateTransformation, false);
converter.setDate(date);
converter.convert();
TransitSchedulePostProcessTools.copyLateDeparturesToStartOfDay(scenario.getTransitSchedule(), 86400.0, "copied", false);
TransitSchedulePostProcessTools.copyEarlyDeparturesToFollowingNight(scenario.getTransitSchedule(), 21600.0, "copied");
if (createNetworkAndVehicles) {
(new CreatePseudoNetwork(scenario.getTransitSchedule(), scenario.getNetwork(), "pt_")).createNetwork();
(new CreateVehiclesForSchedule(scenario.getTransitSchedule(), scenario.getTransitVehicles())).run();
}
}

public static void main(String[] args) {
String inputZipFile = args[0];
String outputFile = args[1];
Expand Down
35 changes: 13 additions & 22 deletions src/main/java/org/matsim/contrib/gtfs/RunGTFS2MATSimExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,54 +34,45 @@
import org.matsim.pt.transitSchedule.api.TransitScheduleWriter;
import org.matsim.pt.utils.CreatePseudoNetwork;
import org.matsim.pt.utils.CreateVehiclesForSchedule;
import org.matsim.vehicles.MatsimVehicleWriter;
import org.matsim.vehicles.VehicleWriterV1;

/**
* @author jbischoff
* This is an example script that utilizes GTFS2MATSim and creates a pseudo network and vehicles using MATSim standard API functionality.
*/

public class RunGTFS2MATSimExample {
public final class RunGTFS2MATSimExample {

private RunGTFS2MATSimExample() {
}


public static void main(String[] args) {

//this was tested for the latest VBB GTFS, available at
// http://www.vbb.de/de/article/fahrplan/webservices/datensaetze/1186967.html

//input data
String gtfsZipFile = "631760.zip";
String gtfsZipFile = "";
CoordinateTransformation ct = TransformationFactory.getCoordinateTransformation(TransformationFactory.WGS84, "EPSG:25833");
LocalDate date = LocalDate.parse("2017-11-10");
LocalDate date = LocalDate.parse("2020-06-25");

//output files
String scheduleFile = "transitSchedule.xml.gz";
String networkFile = "network.xml.gz";
String transitVehiclesFile ="transitVehicles.xml.gz";

//Convert GTFS
RunGTFS2MATSim.convertGtfs(gtfsZipFile, scheduleFile, date, ct, false);

//Parse the schedule again
Scenario scenario = ScenarioUtils.createScenario(ConfigUtils.createConfig());
new TransitScheduleReader(scenario).readFile(scheduleFile);


//Convert GTFS
RunGTFS2MATSim.convertGTFSandAddToScenario(scenario,gtfsZipFile,date,ct,true);

// copy late/early departures to have at complete schedule from ca. 0:00 to ca. 30:00
TransitSchedulePostProcessTools.copyLateDeparturesToStartOfDay(scenario.getTransitSchedule(), 24 * 3600, "copied", false);
TransitSchedulePostProcessTools.copyEarlyDeparturesToFollowingNight(scenario.getTransitSchedule(), 6 * 3600, "copied");

//if neccessary, parse in an existing network file here:
// new MatsimNetworkReader(scenario.getNetwork()).readFile("network.xml");

//Create a network around the schedule
new CreatePseudoNetwork(scenario.getTransitSchedule(),scenario.getNetwork(),"pt_").createNetwork();

//Create simple transit vehicles
new CreateVehiclesForSchedule(scenario.getTransitSchedule(), scenario.getTransitVehicles()).run();


//Write out network, vehicles and schedule
new NetworkWriter(scenario.getNetwork()).write(networkFile);
new TransitScheduleWriter(scenario.getTransitSchedule()).writeFile(scheduleFile);
new VehicleWriterV1(scenario.getTransitVehicles()).writeFile(transitVehiclesFile);
new MatsimVehicleWriter(scenario.getTransitVehicles()).writeFile(transitVehiclesFile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static void copyLateDeparturesToStartOfDay(TransitSchedule schedule, doub
for (Departure dep: route.getDepartures().values()) {
double oldDepartureTime = dep.getDepartureTime();
// do not copy Departures which arrive before midnight ()
double arrivalAtLastStop = dep.getDepartureTime() + route.getStops().get(route.getStops().size() - 1).getArrivalOffset();
double arrivalAtLastStop = dep.getDepartureTime() + route.getStops().get(route.getStops().size() - 1).getArrivalOffset().seconds();
if (oldDepartureTime > startTimeOfCopying &&
(departureExclusionMarker== null || !dep.getId().toString().contains(departureExclusionMarker)) &&
(copyDespiteArrivalBeforeMidnight || arrivalAtLastStop >= 24*3600) ) {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/matsim/contrib/gtfs/GtfsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ private void compareTransitSchedules(MutableScenario sc1, MutableScenario sc2) {
Assert.assertEquals(tr1.getTransportMode(), tr2.getTransportMode());
for(TransitRouteStop trStop: tr1.getStops()){
Assert.assertEquals(trStop.isAwaitDepartureTime(), tr2.getStops().get(tr1.getStops().indexOf(trStop)).isAwaitDepartureTime());
Assert.assertEquals(trStop.getDepartureOffset(), tr2.getStops().get(tr1.getStops().indexOf(trStop)).getDepartureOffset(), 0.0);
Assert.assertEquals(trStop.getArrivalOffset(), tr2.getStops().get(tr1.getStops().indexOf(trStop)).getArrivalOffset(), 0.0);
Assert.assertEquals(trStop.getDepartureOffset().seconds(), tr2.getStops().get(tr1.getStops().indexOf(trStop)).getDepartureOffset().seconds(), 0.0);
Assert.assertEquals(trStop.getArrivalOffset().seconds(), tr2.getStops().get(tr1.getStops().indexOf(trStop)).getArrivalOffset().seconds(), 0.0);
}
Assert.assertEquals(tr1.getDepartures().size(), tr2.getDepartures().size());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ public DepartureCopyingFixture() {

NetworkRoute networkRoute = RouteUtils.createLinkNetworkRouteImpl(firstStopLinkId, new ArrayList<Id<Link>>(), lastStopLinkId);
List<TransitRouteStop> stopsRed = new ArrayList<>(2);
stopsRed.add(sf.createTransitRouteStop(firstStop, Time.getUndefinedTime(), 0.0));
stopsRed.add(sf.createTransitRouteStop(lastStop, 600, Time.getUndefinedTime()));
stopsRed.add(sf.createTransitRouteStopBuilder(firstStop).departureOffset(0.0).build());
stopsRed.add(sf.createTransitRouteStopBuilder(lastStop).arrivalOffset(600).build());
TransitRoute redABRoute = sf.createTransitRoute(Id.create("redFirstToLast", TransitRoute.class), networkRoute, stopsRed, "hoovercraft");
redABRoute.addDeparture(sf.createDeparture(Id.create("early", Departure.class), 6.0*3600));
redABRoute.addDeparture(sf.createDeparture(Id.create("midday", Departure.class), 12.0*3600));
Expand Down

0 comments on commit 9cdef70

Please sign in to comment.