Skip to content

Commit

Permalink
improve prepare script
Browse files Browse the repository at this point in the history
  • Loading branch information
ikaddoura committed Apr 6, 2024
1 parent 8b28148 commit ded5a32
Showing 1 changed file with 39 additions and 19 deletions.
58 changes: 39 additions & 19 deletions src/main/java/ch/sbb/prepare/GenerateRailsimInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.net.MalformedURLException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.locationtech.jts.geom.prep.PreparedGeometry;
import org.matsim.core.config.Config;
Expand All @@ -48,7 +49,9 @@ public final class GenerateRailsimInput {
// input files
private static final String INPUT_OSM_FILE = "original_data/osm/switzerland_railways.osm";
private static final String INPUT_GTFS_FILE = "original_data/gtfs/gtfs_fp2024_2024-03-20_04-15.zip";
private static final String areaShpFile = "original_data/shp/olten/olten.shp";
private static final String areaShpFileForTrimming = null;
// private static final String areaShpFileForTrimming = "original_data/shp/olten/olten.shp";
private static final Set<String> transitLineNamePrefixesToKeep = CollectionUtils.stringToSet("IC,IR,RE");
private static final String EPSG2056 = "EPSG:2056";

// final matsim input files
Expand Down Expand Up @@ -92,18 +95,20 @@ public static void main(String[] args) throws MalformedURLException {
private static void trimSchedule() throws MalformedURLException {
TransitSchedule schedule = ScheduleTools.readTransitSchedule(SCHEDULE_GTFS_FILTERED);

List<PreparedGeometry> geometries = ShpGeometryUtils.loadPreparedGeometries(new File(areaShpFile).toURI().toURL());
if (areaShpFileForTrimming != null) {
List<PreparedGeometry> geometries = ShpGeometryUtils.loadPreparedGeometries(new File(areaShpFileForTrimming).toURI().toURL());

for (TransitLine transitLine : new HashSet<>(schedule.getTransitLines().values())) {
for(TransitRoute transitRoute : new HashSet<>(transitLine.getRoutes().values())) {
if (routeHasStopInArea(transitRoute, geometries)) {
// keep
} else {
// remove
transitLine.removeRoute(transitRoute);
for (TransitLine transitLine : new HashSet<>(schedule.getTransitLines().values())) {
for(TransitRoute transitRoute : new HashSet<>(transitLine.getRoutes().values())) {
if (routeHasStopInArea(transitRoute, geometries)) {
// keep
} else {
// remove
transitLine.removeRoute(transitRoute);
}
}
}

}
}

ScheduleCleaner.removeNotUsedStopFacilities(schedule);
Expand All @@ -122,6 +127,7 @@ private static boolean routeHasStopInArea(TransitRoute route, List<PreparedGeome
public static void filterSchedule() {
TransitSchedule schedule = ScheduleTools.readTransitSchedule(SCHEDULE_GTFS);

// remove non-rail transit lines, e.g. buses, light-rail, ...
for(TransitLine transitLine : new HashSet<>(schedule.getTransitLines().values())) {
for(TransitRoute transitRoute : new HashSet<>(transitLine.getRoutes().values())) {
if(!transitRoute.getTransportMode().equals("rail")) {
Expand All @@ -130,18 +136,30 @@ public static void filterSchedule() {
}
}

for(TransitLine transitLine : new HashSet<>(schedule.getTransitLines().values())) {
if(transitLine.getName().startsWith("IC") ||
transitLine.getName().startsWith("IR") ||
transitLine.getName().startsWith("RE")) {
// keep
} else {
// remove
for(TransitRoute transitRoute : new HashSet<>(transitLine.getRoutes().values())) {
transitLine.removeRoute(transitRoute);
if (transitLineNamePrefixesToKeep == null || transitLineNamePrefixesToKeep.isEmpty()) {
// do not filter by line name prefix

} else {
for(TransitLine transitLine : new HashSet<>(schedule.getTransitLines().values())) {
boolean lineToKeep = false;
for (String prefix : transitLineNamePrefixesToKeep) {
if (transitLine.getName().startsWith(prefix)) {
lineToKeep = true;
break;
}
}

if(lineToKeep) {
// keep
} else {
// remove
for(TransitRoute transitRoute : new HashSet<>(transitLine.getRoutes().values())) {
transitLine.removeRoute(transitRoute);
}
}
}
}

ScheduleCleaner.removeNotUsedStopFacilities(schedule);
ScheduleTools.writeTransitSchedule(schedule, SCHEDULE_GTFS_FILTERED);
}
Expand Down Expand Up @@ -203,8 +221,10 @@ public static void createMapperConfigFile(String configFile) {
ptmConfig.setOutputScheduleFile(SCHEDULE_FINAL);
// ptmConfig.setOutputStreetNetworkFile(MATSIM_INPUT + "network_streets_final.xml.gz");
ptmConfig.setInputScheduleFile(SCHEDULE_GTFS_FILTERED_TRIMMED);
ptmConfig.setModesToKeepOnCleanUp(CollectionUtils.stringToSet("rail"));
ptmConfig.setScheduleFreespeedModes(CollectionUtils.stringToSet("rail, light_rail"));
new ConfigWriter(config).write(configFile);

}

}

0 comments on commit ded5a32

Please sign in to comment.