Skip to content

Commit

Permalink
move readPRStationFile to PRStation class
Browse files Browse the repository at this point in the history
  • Loading branch information
tschlenther committed Sep 1, 2023
1 parent 1144653 commit f171755
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private void writeTransitStops(String outputFile) throws IOException {
writeStop(csvWriter, link);
}
//write a stop for each P+R link
Set<PRStation> prStations = ReplaceCarByDRT.readPRStationFile(url2PRStations);
Set<PRStation> prStations = PRStation.readPRStationFile(url2PRStations);
for (PRStation prStation : prStations) {
writeStop(csvWriter, network.getLinks().get(prStation.getLinkId()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static void main(String[] args) {
int seats = 8;

DrtVehicleCreatorForBanScenario vehicleCreator = new DrtVehicleCreatorForBanScenario(networkFile, drtServiceAreaShapeFile, ct);
Set<PRStation> prStations = ReplaceCarByDRT.readPRStationFile(prStationsFileURL);
Set<PRStation> prStations = PRStation.readPRStationFile(prStationsFileURL);

for (int numberOfVehicles: numbersOfVehicles) {
List<DvrpVehicleSpecification> vehicles = new ArrayList<>();
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/org/matsim/run/replaceCarByDRT/PRStation.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,57 @@

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<Link> linkId;
private final Coord coord;
private static final Logger LOG = Logger.getLogger(PRStation.class);

PRStation(String name, Id<Link> linkId, Coord coord) {
this.name = name;
this.linkId = linkId;
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<PRStation> readPRStationFile(URL url2PRStations) {
LOG.info("read input file for P+R stations");
Set<PRStation> 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<String, Integer> headerMap = parser.getHeaderMap();
parser.getRecords().forEach(record -> {
String name = record.get(headerMap.get("name"));
Id<Link> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class PrActivityEventHandler implements ActivityEndEventHandler, PersonDeparture


public PrActivityEventHandler(URL url2PRStations) {
Set<PRStation> prStations = ReplaceCarByDRT.readPRStationFile(url2PRStations);
Set<PRStation> prStations = PRStation.readPRStationFile(url2PRStations);
for (PRStation station : prStations){
this.activitiesPerPRStation.put(station, new MutableInt(0));
this.carsInPrStationPerMinute.put(station, new int[36*60]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<PRStation> prStations = readPRStationFile(url2PRStations);
Set<PRStation> prStations = PRStation.readPRStationFile(url2PRStations);

log.info("start modifying input plans....");
PopulationFactory fac = scenario.getPopulation().getFactory();
Expand Down Expand Up @@ -407,30 +403,6 @@ private static void replaceTripsWithPtTrips(List<TripStructureUtils.Trip> 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<PRStation> readPRStationFile(URL url2PRStations) {
log.info("read input file for P+R stations");
Set<PRStation> 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<String, Integer> headerMap = parser.getHeaderMap();
parser.getRecords().forEach(record -> {
String name = record.get(headerMap.get("name"));
Id<Link> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down

0 comments on commit f171755

Please sign in to comment.