matsim-org / matsim-code-examples

A repository containing code examples around MATSim
GNU General Public License v3.0
83 stars 180 forks source link

Support for merging multiple public transport schedules (with pt2matsim)? #50

Closed ait-energy closed 5 years ago

ait-energy commented 5 years ago

I need to merge several GTFS feeds because we want to use GTFS data sets from neighboring regions in a single simulation.

After having a look at the pt2matsim process I think the best place to do this is in an additional step before invoking PTMapper, which handles merging the unmapped transitSchedule.xml (and the vehicleDefinitions.xml). A simplistic approach would be to merge the relevant files (e.g. via xslt). A more sophisticated approach could handle duplicate ids (e.g. vehicleDefinitions>vehicle>id can easily be the same in multiple files).

I wanted to ask if someone already did this before / if there already is an established way to achieve this?

polettif commented 5 years ago

I don't know about an established way but there's ScheduleTools.mergeSchedules which, together with ScheduleTools.createVehicles(), could be used to achieve this. Doing this before running PTMapper is the way to go. Combining xml files should work as well I think.

ait-energy commented 5 years ago

Great, thanks for the hint! ScheduleTools does the job well:

static final String COORDINATE_SYTEM = "epsg:XXXXX";
static final List<String> GTFS_DIRS = Arrays.asList("a", "b", "c");
static final String TRANSIT_SCHEDULE_NOT_YET_MAPPED_TO_ROADS = "transit_schedule_not_yet_mapped_to_roads.xml";
static final String TRANSIT_VEHICLES = "transit_vehicles.xml";

public static void prepareTransitSchedules() {
    TransitSchedule transitSchedule = prepareTransitSchedule(GTFS_DIRS.get(0));
    for(String gtfsDir : GTFS_DIRS.subList(1, GTFS_DIRS.size())) {
        TransitSchedule additionalSchedule = prepareTransitSchedule(gtfsDir);
        ScheduleTools.mergeSchedules(transitSchedule, additionalSchedule);
    }

    Vehicles transitVehicles = VehicleUtils.createVehiclesContainer();
    ScheduleTools.createVehicles(transitSchedule, transitVehicles);

    ScheduleTools.writeTransitSchedule(transitSchedule, TRANSIT_SCHEDULE_NOT_YET_MAPPED_TO_ROADS);
    ScheduleTools.writeVehicles(transitVehicles, TRANSIT_VEHICLES);
}

private static TransitSchedule prepareTransitSchedule(String gtfsDir) {
    String param = GtfsConverter.DAY_WITH_MOST_TRIPS;
    GtfsFeed gtfsFeed = new GtfsFeedImpl(gtfsDir);
    GtfsConverter converter = new GtfsConverter(gtfsFeed);
    converter.convert(param, COORDINATE_SYTEM);
    return converter.getSchedule();
}