From f1717555514102d7e6f073473c441e054be19e24 Mon Sep 17 00:00:00 2001 From: tschlenther Date: Fri, 1 Sep 2023 15:41:34 +0200 Subject: [PATCH] move readPRStationFile to PRStation class --- .../run/replaceCarByDRT/DrtStopsWriter.java | 2 +- .../DrtVehicleCreatorForBanScenario.java | 2 +- .../matsim/run/replaceCarByDRT/PRStation.java | 36 +++++++++++++++++++ .../PrActivityEventHandler.java | 2 +- .../run/replaceCarByDRT/ReplaceCarByDRT.java | 30 +--------------- .../PrActivityEventHandlerTest.java | 2 +- .../replaceCarByDRT/ReplaceCarByDRTTest.java | 2 +- 7 files changed, 42 insertions(+), 34 deletions(-) diff --git a/src/main/java/org/matsim/run/replaceCarByDRT/DrtStopsWriter.java b/src/main/java/org/matsim/run/replaceCarByDRT/DrtStopsWriter.java index cb73158..42069ef 100644 --- a/src/main/java/org/matsim/run/replaceCarByDRT/DrtStopsWriter.java +++ b/src/main/java/org/matsim/run/replaceCarByDRT/DrtStopsWriter.java @@ -111,7 +111,7 @@ private void writeTransitStops(String outputFile) throws IOException { writeStop(csvWriter, link); } //write a stop for each P+R link - Set prStations = ReplaceCarByDRT.readPRStationFile(url2PRStations); + Set prStations = PRStation.readPRStationFile(url2PRStations); for (PRStation prStation : prStations) { writeStop(csvWriter, network.getLinks().get(prStation.getLinkId())); } diff --git a/src/main/java/org/matsim/run/replaceCarByDRT/DrtVehicleCreatorForBanScenario.java b/src/main/java/org/matsim/run/replaceCarByDRT/DrtVehicleCreatorForBanScenario.java index f6c13fe..ce9033c 100644 --- a/src/main/java/org/matsim/run/replaceCarByDRT/DrtVehicleCreatorForBanScenario.java +++ b/src/main/java/org/matsim/run/replaceCarByDRT/DrtVehicleCreatorForBanScenario.java @@ -93,7 +93,7 @@ public static void main(String[] args) { int seats = 8; DrtVehicleCreatorForBanScenario vehicleCreator = new DrtVehicleCreatorForBanScenario(networkFile, drtServiceAreaShapeFile, ct); - Set prStations = ReplaceCarByDRT.readPRStationFile(prStationsFileURL); + Set prStations = PRStation.readPRStationFile(prStationsFileURL); for (int numberOfVehicles: numbersOfVehicles) { List vehicles = new ArrayList<>(); diff --git a/src/main/java/org/matsim/run/replaceCarByDRT/PRStation.java b/src/main/java/org/matsim/run/replaceCarByDRT/PRStation.java index b96052e..4ab5be0 100644 --- a/src/main/java/org/matsim/run/replaceCarByDRT/PRStation.java +++ b/src/main/java/org/matsim/run/replaceCarByDRT/PRStation.java @@ -20,14 +20,26 @@ package org.matsim.run.replaceCarByDRT; +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVParser; +import org.apache.log4j.Logger; import org.matsim.api.core.v01.Coord; import org.matsim.api.core.v01.Id; import org.matsim.api.core.v01.network.Link; +import org.matsim.core.utils.io.IOUtils; + +import java.io.IOException; +import java.net.URL; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + class PRStation { private final String name; private final Id linkId; private final Coord coord; + private static final Logger LOG = Logger.getLogger(PRStation.class); PRStation(String name, Id linkId, Coord coord) { this.name = name; @@ -35,6 +47,30 @@ class PRStation { this.coord = coord; } + /** + * + * @param url2PRStations a .tsv input file with the following columns (and a header row): 'name', 'x', 'y' and 'linkId'. The order should not matter. + * @return + */ + static Set readPRStationFile(URL url2PRStations) { + LOG.info("read input file for P+R stations"); + Set prStations = new HashSet<>(); + //assume tsv with a header and linkId in the last column + try { + CSVParser parser = CSVParser.parse(IOUtils.getBufferedReader(url2PRStations), CSVFormat.DEFAULT.withDelimiter('\t').withFirstRecordAsHeader()); + Map headerMap = parser.getHeaderMap(); + parser.getRecords().forEach(record -> { + String name = record.get(headerMap.get("name")); + Id linkId = Id.createLinkId(record.get(headerMap.get("linkId"))); + Coord coord = new Coord(Double.parseDouble(record.get(headerMap.get("x"))), Double.parseDouble(record.get(headerMap.get("y")))); + prStations.add(new PRStation(name, linkId, coord)); + }); + } catch (IOException e) { + e.printStackTrace(); + } + return prStations; + } + protected Coord getCoord() { return coord; } diff --git a/src/main/java/org/matsim/run/replaceCarByDRT/PrActivityEventHandler.java b/src/main/java/org/matsim/run/replaceCarByDRT/PrActivityEventHandler.java index e9e59d9..8ed2e76 100644 --- a/src/main/java/org/matsim/run/replaceCarByDRT/PrActivityEventHandler.java +++ b/src/main/java/org/matsim/run/replaceCarByDRT/PrActivityEventHandler.java @@ -41,7 +41,7 @@ class PrActivityEventHandler implements ActivityEndEventHandler, PersonDeparture public PrActivityEventHandler(URL url2PRStations) { - Set prStations = ReplaceCarByDRT.readPRStationFile(url2PRStations); + Set prStations = PRStation.readPRStationFile(url2PRStations); for (PRStation station : prStations){ this.activitiesPerPRStation.put(station, new MutableInt(0)); this.carsInPrStationPerMinute.put(station, new int[36*60]); diff --git a/src/main/java/org/matsim/run/replaceCarByDRT/ReplaceCarByDRT.java b/src/main/java/org/matsim/run/replaceCarByDRT/ReplaceCarByDRT.java index 181bb0d..e39ac9f 100644 --- a/src/main/java/org/matsim/run/replaceCarByDRT/ReplaceCarByDRT.java +++ b/src/main/java/org/matsim/run/replaceCarByDRT/ReplaceCarByDRT.java @@ -21,8 +21,6 @@ package org.matsim.run.replaceCarByDRT; import com.google.common.base.Preconditions; -import org.apache.commons.csv.CSVFormat; -import org.apache.commons.csv.CSVParser; import org.apache.commons.lang3.mutable.MutableInt; import org.apache.log4j.Logger; import org.locationtech.jts.geom.prep.PreparedGeometry; @@ -41,11 +39,9 @@ import org.matsim.core.router.MainModeIdentifier; import org.matsim.core.router.TripRouter; import org.matsim.core.router.TripStructureUtils; -import org.matsim.core.utils.io.IOUtils; import org.matsim.facilities.FacilitiesUtils; import org.matsim.utils.gis.shp2matsim.ShpGeometryUtils; -import java.io.IOException; import java.net.URL; import java.util.*; import java.util.stream.Collectors; @@ -135,7 +131,7 @@ static void prepareInputPlansForCarProhibitionWithPRLogic(Scenario scenario, Preconditions.checkArgument(carFreeGeoms.size() == 1, "you have to provide a shape file that features exactly one geometry."); Preconditions.checkArgument(prStationChoice.equals(PRStationChoice.closestToOutSideActivity) || prStationChoice.equals(PRStationChoice.closestToInsideActivity), "do not know what to do with " + prStationChoice); Preconditions.checkArgument(replacingModes.size() > 0); - Set prStations = readPRStationFile(url2PRStations); + Set prStations = PRStation.readPRStationFile(url2PRStations); log.info("start modifying input plans...."); PopulationFactory fac = scenario.getPopulation().getFactory(); @@ -407,30 +403,6 @@ private static void replaceTripsWithPtTrips(List trips, } } - /** - * - * @param url2PRStations a .tsv input file with the following columns (and a header row): 'name', 'x', 'y' and 'linkId'. The order should not matter. - * @return - */ - static Set readPRStationFile(URL url2PRStations) { - log.info("read input file for P+R stations"); - Set prStations = new HashSet<>(); - //assume tsv with a header and linkId in the last column - try { - CSVParser parser = CSVParser.parse(IOUtils.getBufferedReader(url2PRStations), CSVFormat.DEFAULT.withDelimiter('\t').withFirstRecordAsHeader()); - Map headerMap = parser.getHeaderMap(); - parser.getRecords().forEach(record -> { - String name = record.get(headerMap.get("name")); - Id linkId = Id.createLinkId(record.get(headerMap.get("linkId"))); - Coord coord = new Coord(Double.parseDouble(record.get(headerMap.get("x"))), Double.parseDouble(record.get(headerMap.get("y")))); - prStations.add(new PRStation(name, linkId, coord)); - }); - } catch (IOException e) { - e.printStackTrace(); - } - return prStations; - } - /** * retrieves the trips in the plan of modesToBeReplaced and attributes them with the corresponding TripType * @param plan diff --git a/src/test/java/org/matsim/run/replaceCarByDRT/PrActivityEventHandlerTest.java b/src/test/java/org/matsim/run/replaceCarByDRT/PrActivityEventHandlerTest.java index cdea069..9a9a816 100644 --- a/src/test/java/org/matsim/run/replaceCarByDRT/PrActivityEventHandlerTest.java +++ b/src/test/java/org/matsim/run/replaceCarByDRT/PrActivityEventHandlerTest.java @@ -20,7 +20,7 @@ import java.util.Map; import java.util.Set; -import static org.matsim.run.replaceCarByDRT.ReplaceCarByDRT.readPRStationFile; +import static org.matsim.run.replaceCarByDRT.PRStation.readPRStationFile; public class PrActivityEventHandlerTest { diff --git a/src/test/java/org/matsim/run/replaceCarByDRT/ReplaceCarByDRTTest.java b/src/test/java/org/matsim/run/replaceCarByDRT/ReplaceCarByDRTTest.java index a0157c0..559725a 100644 --- a/src/test/java/org/matsim/run/replaceCarByDRT/ReplaceCarByDRTTest.java +++ b/src/test/java/org/matsim/run/replaceCarByDRT/ReplaceCarByDRTTest.java @@ -23,8 +23,8 @@ import java.util.*; import java.util.stream.Collectors; +import static org.matsim.run.replaceCarByDRT.PRStation.readPRStationFile; import static org.matsim.run.replaceCarByDRT.ReplaceCarByDRT.PR_ACTIVITY_TYPE; -import static org.matsim.run.replaceCarByDRT.ReplaceCarByDRT.readPRStationFile; import static org.matsim.run.replaceCarByDRT.RunBerlinNoInnerCarTripsScenario.URL_2_PR_STATIONS; /**