From 83e6ca5f83bfe682617e43e98c91a4e858e13d58 Mon Sep 17 00:00:00 2001 From: Jakob Rehmann Date: Thu, 23 May 2024 16:14:05 +0200 Subject: [PATCH] added test osm facil list --- .../org/matsim/run/RunKelheimScenario.java | 46 ++++++++++++++++--- testOsmFacilitiesList.csv | 2 + 2 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 testOsmFacilitiesList.csv diff --git a/src/main/java/org/matsim/run/RunKelheimScenario.java b/src/main/java/org/matsim/run/RunKelheimScenario.java index 2f29976..1755cc7 100644 --- a/src/main/java/org/matsim/run/RunKelheimScenario.java +++ b/src/main/java/org/matsim/run/RunKelheimScenario.java @@ -4,6 +4,9 @@ import ch.sbb.matsim.routing.pt.raptor.SwissRailRaptorModule; import com.google.common.collect.Sets; import com.google.inject.Inject; +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVParser; +import org.apache.commons.csv.CSVRecord; import org.matsim.analysis.KelheimMainModeIdentifier; import org.matsim.analysis.ModeChoiceCoverageControlerListener; import org.matsim.analysis.personMoney.PersonMoneyEventsAnalysisModule; @@ -74,6 +77,11 @@ import playground.vsp.scoring.IncomeDependentUtilityOfMoneyPersonScoringParameters; import javax.annotation.Nullable; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.List; import java.util.Set; import java.util.SplittableRandom; @@ -335,13 +343,39 @@ protected void prepareScenario(Scenario scenario) { if (acc) { // add opportunity facility - double trainStationX = 715041.71; - double trainStationY = 5420617.28; ActivityFacilitiesFactory af = scenario.getActivityFacilities().getFactory(); - ActivityFacility fac1 = af.createActivityFacility(Id.create("xxx", ActivityFacility.class), new Coord(trainStationX, trainStationY)); - ActivityOption ao = af.createActivityOption("train station"); - fac1.addActivityOption(ao); - scenario.getActivityFacilities().addActivityFacility(fac1); + + + // Use this method if reading facilities from a csv. + Path filePath = Path.of("testOsmFacilitiesList.csv"); + try (CSVParser parser = new CSVParser(new BufferedReader(new InputStreamReader(Files.newInputStream(filePath))), + CSVFormat.DEFAULT.withDelimiter(',').withFirstRecordAsHeader())) { + + for (CSVRecord record : parser) { + + String id = record.get("id"); + double x = Double.parseDouble(record.get("x")); + double y = Double.parseDouble(record.get("y")); + String type = record.get("type"); + ActivityFacility fac = af.createActivityFacility(Id.create(id, ActivityFacility.class), new Coord(x, y)); + ActivityOption ao = af.createActivityOption(type); + fac.addActivityOption(ao); + + } + } catch (IOException e) { + throw new RuntimeException(e); + } + + +// {// set facility manually +// double trainStationX = 715041.71; +// double trainStationY = 5420617.28; +// ActivityFacility fac1 = af.createActivityFacility(Id.create("xxx", ActivityFacility.class), new Coord(trainStationX, trainStationY)); +// ActivityOption ao = af.createActivityOption("train station"); +// fac1.addActivityOption(ao); +// scenario.getActivityFacilities().addActivityFacility(fac1); +// } + } } diff --git a/testOsmFacilitiesList.csv b/testOsmFacilitiesList.csv new file mode 100644 index 0000000..606c357 --- /dev/null +++ b/testOsmFacilitiesList.csv @@ -0,0 +1,2 @@ +id,x,y,type +1,715041.71,5420617.28,'train station' \ No newline at end of file