Skip to content

Commit

Permalink
make the path of input data configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
rewertvsp committed Mar 20, 2024
1 parent cf1ae7b commit 4616b1e
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ private enum SmallScaleCommercialTrafficType {
@CommandLine.Parameters(arity = "1", paramLabel = "INPUT", description = "Path to the config for small scale commercial generation")
private Path configPath;

@CommandLine.Option(names = "--pathToInvestigationAreaData", description = "Path to the investigation area data")
private Path pathToInvestigationAreaData;

@CommandLine.Option(names = "--pathToExistingDataDistributionToZones", description = "Path to the existing data distribution to zones. This is only needed if the option useExistingDataDistribution is selected.")
private Path pathToExistingDataDistributionToZones;

@CommandLine.Option(names = "--sample", description = "Scaling factor of the small scale commercial traffic (0, 1)", required = true)
private double sample;

Expand Down Expand Up @@ -244,17 +250,17 @@ public Integer call() throws Exception {
if (!Files.exists(shapeFileRegionsPath)) {
throw new Exception("Required regions shape file {} not found" + shapeFileRegionsPath.toString());
}
Path inputDataDirectory = Path.of(config.getContext().toURI()).getParent();

indexZones = SmallScaleCommercialTrafficUtils.getIndexZones(shapeFileZonePath, shapeCRS, shapeFileZoneNameColumn);
indexBuildings = SmallScaleCommercialTrafficUtils.getIndexBuildings(shapeFileBuildingsPath, shapeCRS, shapeFileBuildingTypeColumn);
indexLanduse = SmallScaleCommercialTrafficUtils.getIndexLanduse(shapeFileLandusePath, shapeCRS, shapeFileLanduseTypeColumn);
indexInvestigationAreaRegions = SmallScaleCommercialTrafficUtils.getIndexRegions(shapeFileRegionsPath, shapeCRS, regionsShapeRegionColumn);

Map<String, Object2DoubleMap<String>> resultingDataPerZone = LanduseBuildingAnalysis
.createInputDataDistribution(output, landuseCategoriesAndDataConnection, inputDataDirectory,
.createInputDataDistribution(output, landuseCategoriesAndDataConnection,
usedLanduseConfiguration.toString(), indexLanduse, indexZones,
indexBuildings, indexInvestigationAreaRegions, shapeFileZoneNameColumn, buildingsPerZone);
indexBuildings, indexInvestigationAreaRegions, shapeFileZoneNameColumn, buildingsPerZone, pathToInvestigationAreaData,
pathToExistingDataDistributionToZones);
Map<String, Map<Id<Link>, Link>> linksPerZone = filterLinksForZones(scenario, indexZones, buildingsPerZone);

switch (usedSmallScaleCommercialTrafficType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,14 @@ public class LanduseBuildingAnalysis {
* used OSM data.
*/
static Map<String, Object2DoubleMap<String>> createInputDataDistribution(Path output,
Map<String, List<String>> landuseCategoriesAndDataConnection, Path inputDataDirectory,
Map<String, List<String>> landuseCategoriesAndDataConnection,
String usedLanduseConfiguration, Index indexLanduse, Index indexZones,
Index indexBuildings, Index indexInvestigationAreaRegions,
String shapeFileZoneNameColumn, Map<String, Map<String, List<SimpleFeature>>> buildingsPerZone)
throws IOException {
String shapeFileZoneNameColumn,
Map<String, Map<String, List<SimpleFeature>>> buildingsPerZone,
Path pathToInvestigationAreaData,
Path pathToExistingDataDistributionToZones)
throws IOException {

Map<String, Object2DoubleMap<String>> resultingDataPerZone = new HashMap<>();
Map<String, String> zoneIdRegionConnection = new HashMap<>();
Expand All @@ -81,13 +84,12 @@ static Map<String, Object2DoubleMap<String>> createInputDataDistribution(Path ou
Arrays.asList("commercial", "embassy", "foundation", "government", "office", "townhall")));

if (usedLanduseConfiguration.equals("useExistingDataDistribution")) {
Path existingDataDistribution = inputDataDirectory.resolve("dataDistributionPerZone.csv");

if (!Files.exists(existingDataDistribution)) {
log.error("Required data per zone file {} not found", existingDataDistribution);
if (!Files.exists(pathToExistingDataDistributionToZones)) {
log.error("Required data per zone file {} not found", pathToExistingDataDistributionToZones);
}

try (BufferedReader reader = IOUtils.getBufferedReader(existingDataDistribution.toString())) {
try (BufferedReader reader = IOUtils.getBufferedReader(pathToExistingDataDistributionToZones.toString())) {
CSVParser parse = CSVFormat.Builder.create(CSVFormat.DEFAULT).setDelimiter('\t').setHeader()
.setSkipHeaderRecord(true).build().parse(reader);

Expand All @@ -101,8 +103,8 @@ static Map<String, Object2DoubleMap<String>> createInputDataDistribution(Path ou
}
}
log.info("Data distribution for " + resultingDataPerZone.size() + " zones was imported from " +
existingDataDistribution);
Files.copy(existingDataDistribution, outputFileInOutputFolder, StandardCopyOption.COPY_ATTRIBUTES);
pathToExistingDataDistributionToZones);
Files.copy(pathToExistingDataDistributionToZones, outputFileInOutputFolder, StandardCopyOption.COPY_ATTRIBUTES);
}

else {
Expand All @@ -115,7 +117,7 @@ static Map<String, Object2DoubleMap<String>> createInputDataDistribution(Path ou
buildingsPerZone, shapeFileZoneNameColumn, zoneIdRegionConnection);

Map<String, Map<String, Integer>> investigationAreaData = new HashMap<>();
readAreaData(investigationAreaData, inputDataDirectory);
readAreaData(investigationAreaData, pathToInvestigationAreaData);

createResultingDataForLanduseInZones(landuseCategoriesPerZone, investigationAreaData, resultingDataPerZone,
landuseCategoriesAndDataConnection, zoneIdRegionConnection);
Expand Down Expand Up @@ -287,14 +289,13 @@ private static void createLanduseDistribution(Map<String, Object2DoubleMap<Strin
/**
* Reads the input data for certain areas from the csv file.
*/
private static void readAreaData(Map<String, Map<String, Integer>> areaData, Path inputDataDirectory)
private static void readAreaData(Map<String, Map<String, Integer>> areaData, Path pathToInvestigationAreaData)
throws IOException {

Path areaDataPath = inputDataDirectory.resolve("investigationAreaData.csv");
if (!Files.exists(areaDataPath)) {
log.error("Required input data file {} not found", areaDataPath);
if (!Files.exists(pathToInvestigationAreaData)) {
log.error("Required input data file {} not found", pathToInvestigationAreaData);
}
try (CSVParser parser = new CSVParser(Files.newBufferedReader(areaDataPath),
try (CSVParser parser = new CSVParser(Files.newBufferedReader(pathToInvestigationAreaData),
CSVFormat.Builder.create(CSVFormat.TDF).setHeader().setSkipHeaderRecord(true).build())) {

for (CSVRecord record : parser) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ void testReadOfDataDistributionPerZoneAndBuildingAnalysis() throws IOException {
Path inputDataDirectory = Path.of(utils.getPackageInputDirectory());
String usedLanduseConfiguration = "useExistingDataDistribution";
String shapeFileZoneNameColumn = "name";

Path pathToInvestigationAreaData = Path.of(utils.getPackageInputDirectory()).resolve("investigationAreaData.csv");
Path pathToExistingDataDistributionToZones = Path.of(utils.getPackageInputDirectory()).resolve("dataDistributionPerZone.csv");
// Test if the reading of the existing data distribution works correctly

Map<String, Object2DoubleMap<String>> resultingDataPerZone = LanduseBuildingAnalysis
.createInputDataDistribution(output, landuseCategoriesAndDataConnection,
inputDataDirectory, usedLanduseConfiguration,
usedLanduseConfiguration,
SCTUtils.getIndexLanduse(inputDataDirectory), SCTUtils.getZoneIndex(inputDataDirectory), SCTUtils.getIndexBuildings(inputDataDirectory),
SCTUtils.getIndexRegions(inputDataDirectory), shapeFileZoneNameColumn, buildingsPerZone);
SCTUtils.getIndexRegions(inputDataDirectory), shapeFileZoneNameColumn, buildingsPerZone, pathToInvestigationAreaData, pathToExistingDataDistributionToZones);

Assertions.assertEquals(3, resultingDataPerZone.size(), MatsimTestUtils.EPSILON);

Expand Down Expand Up @@ -245,13 +247,13 @@ void testLanduseDistribution() throws IOException {
Path inputDataDirectory = Path.of(utils.getPackageInputDirectory());
String usedLanduseConfiguration = "useOSMBuildingsAndLanduse";
String shapeFileZoneNameColumn = "name";

Path pathToInvestigationAreaData = Path.of(utils.getPackageInputDirectory()).resolve("investigationAreaData.csv");
// Analyze resultingData per zone
Map<String, Object2DoubleMap<String>> resultingDataPerZone = LanduseBuildingAnalysis
.createInputDataDistribution(output, landuseCategoriesAndDataConnection,
inputDataDirectory, usedLanduseConfiguration,
usedLanduseConfiguration,
SCTUtils.getIndexLanduse(inputDataDirectory), SCTUtils.getZoneIndex(inputDataDirectory), SCTUtils.getIndexBuildings(inputDataDirectory),
SCTUtils.getIndexRegions(inputDataDirectory), shapeFileZoneNameColumn, buildingsPerZone);
SCTUtils.getIndexRegions(inputDataDirectory), shapeFileZoneNameColumn, buildingsPerZone, pathToInvestigationAreaData, null);

Assertions.assertEquals(3, resultingDataPerZone.size(), MatsimTestUtils.EPSILON);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand All @@ -60,7 +61,9 @@ public class RunGenerateSmallScaleCommercialTrafficTest {

@Test
void testMainRunAndResults() {
String inputDataDirectory = utils.getPackageInputDirectory() + "config_demand.xml";
String pathToConfig = utils.getPackageInputDirectory() + "config_demand.xml";
Path pathToInvestigationAreaData = Path.of(utils.getPackageInputDirectory()).resolve("investigationAreaData.csv");
Path pathToExistingDataDistributionToZones = Path.of(utils.getPackageInputDirectory()).resolve("dataDistributionPerZone.csv");
String output = utils.getOutputDirectory();
String sample = "0.1";
String jspritIterations = "2";
Expand All @@ -79,7 +82,9 @@ void testMainRunAndResults() {
String resultPopulation = "testPopulation.xml.gz";

new GenerateSmallScaleCommercialTrafficDemand().execute(
inputDataDirectory,
pathToConfig,
"--pathToInvestigationAreaData", pathToInvestigationAreaData.toString(),
"--pathToExistingDataDistributionToZones", pathToExistingDataDistributionToZones.toString(),
"--sample", sample,
"--jspritIterations", jspritIterations,
"--creationOption", creationOption,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ void testTrafficVolumeGenerationCommercialPersonTraffic() throws IOException {
Path inputDataDirectory = Path.of(utils.getPackageInputDirectory());
String usedLanduseConfiguration = "useExistingDataDistribution";
String shapeFileZoneNameColumn = "name";
Path pathToInvestigationAreaData = Path.of(utils.getPackageInputDirectory()).resolve("investigationAreaData.csv");
Path pathToExistingDataDistributionToZones = Path.of(utils.getPackageInputDirectory()).resolve("dataDistributionPerZone.csv");

Map<String, Object2DoubleMap<String>> resultingDataPerZone = LanduseBuildingAnalysis
.createInputDataDistribution(output, landuseCategoriesAndDataConnection,
inputDataDirectory, usedLanduseConfiguration,
usedLanduseConfiguration,
SCTUtils.getIndexLanduse(inputDataDirectory), SCTUtils.getZoneIndex(inputDataDirectory), SCTUtils.getIndexBuildings(inputDataDirectory),
SCTUtils.getIndexRegions(inputDataDirectory), shapeFileZoneNameColumn, buildingsPerZone);
SCTUtils.getIndexRegions(inputDataDirectory), shapeFileZoneNameColumn, buildingsPerZone, pathToInvestigationAreaData, pathToExistingDataDistributionToZones);


String usedTrafficType = "commercialPersonTraffic";
Expand Down Expand Up @@ -189,12 +191,14 @@ void testTrafficVolumeGenerationGoodsTraffic() throws IOException {
Path inputDataDirectory = Path.of(utils.getPackageInputDirectory());
String usedLanduseConfiguration = "useExistingDataDistribution";
String shapeFileZoneNameColumn = "name";
Path pathToInvestigationAreaData = Path.of(utils.getPackageInputDirectory()).resolve("investigationAreaData.csv");
Path pathToExistingDataDistributionToZones = Path.of(utils.getPackageInputDirectory()).resolve("dataDistributionPerZone.csv");

Map<String, Object2DoubleMap<String>> resultingDataPerZone = LanduseBuildingAnalysis
.createInputDataDistribution(output, landuseCategoriesAndDataConnection,
inputDataDirectory, usedLanduseConfiguration,
usedLanduseConfiguration,
SCTUtils.getIndexLanduse(inputDataDirectory), SCTUtils.getZoneIndex(inputDataDirectory), SCTUtils.getIndexBuildings(inputDataDirectory),
SCTUtils.getIndexRegions(inputDataDirectory), shapeFileZoneNameColumn, buildingsPerZone);
SCTUtils.getIndexRegions(inputDataDirectory), shapeFileZoneNameColumn, buildingsPerZone, pathToInvestigationAreaData, pathToExistingDataDistributionToZones);

String usedTrafficType = "goodsTraffic";
double sample = 1.;
Expand Down Expand Up @@ -512,6 +516,8 @@ void testReducingDemandAfterAddingExistingScenarios_goods() throws Exception {
String usedTrafficType = "goodsTraffic";
double sample = 1.;
String shapeFileZoneNameColumn = "name";
Path pathToInvestigationAreaData = Path.of(utils.getPackageInputDirectory()).resolve("investigationAreaData.csv");
Path pathToExistingDataDistributionToZones = Path.of(utils.getPackageInputDirectory()).resolve("dataDistributionPerZone.csv");

ArrayList<String> modesORvehTypes = new ArrayList<>(
Arrays.asList("vehTyp1", "vehTyp2", "vehTyp3", "vehTyp4", "vehTyp5"));
Expand All @@ -525,9 +531,9 @@ void testReducingDemandAfterAddingExistingScenarios_goods() throws Exception {

Map<String, Object2DoubleMap<String>> resultingDataPerZone = LanduseBuildingAnalysis
.createInputDataDistribution(output, landuseCategoriesAndDataConnection,
inputDataDirectory, usedLanduseConfiguration,
usedLanduseConfiguration,
SCTUtils.getIndexLanduse(inputDataDirectory), SCTUtils.getZoneIndex(inputDataDirectory), SCTUtils.getIndexBuildings(inputDataDirectory),
SCTUtils.getIndexRegions(inputDataDirectory), shapeFileZoneNameColumn, buildingsPerZone);
SCTUtils.getIndexRegions(inputDataDirectory), shapeFileZoneNameColumn, buildingsPerZone, pathToInvestigationAreaData, pathToExistingDataDistributionToZones);

Map<TrafficVolumeKey, Object2DoubleMap<Integer>> trafficVolumePerTypeAndZone_start = TrafficVolumeGeneration
.createTrafficVolume_start(resultingDataPerZone, output, sample, modesORvehTypes, usedTrafficType);
Expand Down Expand Up @@ -670,6 +676,8 @@ void testReducingDemandAfterAddingExistingScenarios_commercialPersonTraffic() th
String usedTrafficType = "commercialPersonTraffic";
double sample = 1.;
String shapeFileZoneNameColumn = "name";
Path pathToInvestigationAreaData = Path.of(utils.getPackageInputDirectory()).resolve("investigationAreaData.csv");
Path pathToExistingDataDistributionToZones = Path.of(utils.getPackageInputDirectory()).resolve("dataDistributionPerZone.csv");

ArrayList<String> modesORvehTypes = new ArrayList<>(
List.of("total"));
Expand All @@ -683,9 +691,9 @@ void testReducingDemandAfterAddingExistingScenarios_commercialPersonTraffic() th

Map<String, Object2DoubleMap<String>> resultingDataPerZone = LanduseBuildingAnalysis
.createInputDataDistribution(output, landuseCategoriesAndDataConnection,
inputDataDirectory, usedLanduseConfiguration,
usedLanduseConfiguration,
SCTUtils.getIndexLanduse(inputDataDirectory), SCTUtils.getZoneIndex(inputDataDirectory), SCTUtils.getIndexBuildings(inputDataDirectory),
SCTUtils.getIndexRegions(inputDataDirectory), shapeFileZoneNameColumn, buildingsPerZone);
SCTUtils.getIndexRegions(inputDataDirectory), shapeFileZoneNameColumn, buildingsPerZone, pathToInvestigationAreaData, pathToExistingDataDistributionToZones);

Map<TrafficVolumeKey, Object2DoubleMap<Integer>> trafficVolumePerTypeAndZone_start = TrafficVolumeGeneration
.createTrafficVolume_start(resultingDataPerZone, output, sample, modesORvehTypes, usedTrafficType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ void testTripDistributionCommercialPersonTrafficTraffic() throws IOException {
String networkLocation = "https://raw.githubusercontent.com/matsim-org/matsim-libs/master/examples/scenarios/freight-chessboard-9x9/grid9x9.xml";
Network network = NetworkUtils.readNetwork(networkLocation);
String shapeFileZoneNameColumn = "name";
Path pathToInvestigationAreaData = Path.of(utils.getPackageInputDirectory()).resolve("investigationAreaData.csv");
Path pathToExistingDataDistributionToZones = Path.of(utils.getPackageInputDirectory()).resolve("dataDistributionPerZone.csv");

Map<String, Object2DoubleMap<String>> resultingDataPerZone = LanduseBuildingAnalysis
.createInputDataDistribution(output, landuseCategoriesAndDataConnection,
inputDataDirectory, usedLanduseConfiguration,
usedLanduseConfiguration,
getIndexLanduse(inputDataDirectory), getZoneIndex(inputDataDirectory), getIndexBuildings(inputDataDirectory),
SCTUtils.getIndexRegions(inputDataDirectory), shapeFileZoneNameColumn, buildingsPerZone);
SCTUtils.getIndexRegions(inputDataDirectory), shapeFileZoneNameColumn, buildingsPerZone, pathToInvestigationAreaData, pathToExistingDataDistributionToZones);

String usedTrafficType = "commercialPersonTraffic";
double sample = 1.;
Expand Down Expand Up @@ -150,11 +152,13 @@ void testTripDistributionGoodsTraffic() throws IOException {
String networkLocation = "https://raw.githubusercontent.com/matsim-org/matsim-libs/master/examples/scenarios/freight-chessboard-9x9/grid9x9.xml";
Network network = NetworkUtils.readNetwork(networkLocation);
String shapeFileZoneNameColumn = "name";
Path pathToInvestigationAreaData = Path.of(utils.getPackageInputDirectory()).resolve("investigationAreaData.csv");
Path pathToExistingDataDistributionToZones = Path.of(utils.getPackageInputDirectory()).resolve("dataDistributionPerZone.csv");
Map<String, Object2DoubleMap<String>> resultingDataPerZone = LanduseBuildingAnalysis
.createInputDataDistribution(output, landuseCategoriesAndDataConnection,
inputDataDirectory, usedLanduseConfiguration,
usedLanduseConfiguration,
getIndexLanduse(inputDataDirectory), getZoneIndex(inputDataDirectory), getIndexBuildings(inputDataDirectory),
SCTUtils.getIndexRegions(inputDataDirectory), shapeFileZoneNameColumn, buildingsPerZone);
SCTUtils.getIndexRegions(inputDataDirectory), shapeFileZoneNameColumn, buildingsPerZone, pathToInvestigationAreaData, pathToExistingDataDistributionToZones);

String usedTrafficType = "goodsTraffic";
double sample = 1.;
Expand Down

0 comments on commit 4616b1e

Please sign in to comment.