From 4616b1ea68870e2cec4f5fa5e9c6ce7b185c5257 Mon Sep 17 00:00:00 2001 From: Ricardo Ewert Date: Wed, 20 Mar 2024 09:34:06 +0100 Subject: [PATCH] make the path of input data configurable --- ...rateSmallScaleCommercialTrafficDemand.java | 12 +++++-- .../LanduseBuildingAnalysis.java | 31 ++++++++++--------- .../LanduseBuildingAnalysisTest.java | 14 +++++---- ...nerateSmallScaleCommercialTrafficTest.java | 9 ++++-- .../TrafficVolumeGenerationTest.java | 24 +++++++++----- .../TripDistributionMatrixTest.java | 12 ++++--- 6 files changed, 64 insertions(+), 38 deletions(-) diff --git a/contribs/small-scale-traffic-generation/src/main/java/org/matsim/smallScaleCommercialTrafficGeneration/GenerateSmallScaleCommercialTrafficDemand.java b/contribs/small-scale-traffic-generation/src/main/java/org/matsim/smallScaleCommercialTrafficGeneration/GenerateSmallScaleCommercialTrafficDemand.java index 1a2191b1542..e5b6a27b774 100644 --- a/contribs/small-scale-traffic-generation/src/main/java/org/matsim/smallScaleCommercialTrafficGeneration/GenerateSmallScaleCommercialTrafficDemand.java +++ b/contribs/small-scale-traffic-generation/src/main/java/org/matsim/smallScaleCommercialTrafficGeneration/GenerateSmallScaleCommercialTrafficDemand.java @@ -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; @@ -244,7 +250,6 @@ 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); @@ -252,9 +257,10 @@ public Integer call() throws Exception { indexInvestigationAreaRegions = SmallScaleCommercialTrafficUtils.getIndexRegions(shapeFileRegionsPath, shapeCRS, regionsShapeRegionColumn); Map> resultingDataPerZone = LanduseBuildingAnalysis - .createInputDataDistribution(output, landuseCategoriesAndDataConnection, inputDataDirectory, + .createInputDataDistribution(output, landuseCategoriesAndDataConnection, usedLanduseConfiguration.toString(), indexLanduse, indexZones, - indexBuildings, indexInvestigationAreaRegions, shapeFileZoneNameColumn, buildingsPerZone); + indexBuildings, indexInvestigationAreaRegions, shapeFileZoneNameColumn, buildingsPerZone, pathToInvestigationAreaData, + pathToExistingDataDistributionToZones); Map, Link>> linksPerZone = filterLinksForZones(scenario, indexZones, buildingsPerZone); switch (usedSmallScaleCommercialTrafficType) { diff --git a/contribs/small-scale-traffic-generation/src/main/java/org/matsim/smallScaleCommercialTrafficGeneration/LanduseBuildingAnalysis.java b/contribs/small-scale-traffic-generation/src/main/java/org/matsim/smallScaleCommercialTrafficGeneration/LanduseBuildingAnalysis.java index b0af262a7b8..7e26668438e 100644 --- a/contribs/small-scale-traffic-generation/src/main/java/org/matsim/smallScaleCommercialTrafficGeneration/LanduseBuildingAnalysis.java +++ b/contribs/small-scale-traffic-generation/src/main/java/org/matsim/smallScaleCommercialTrafficGeneration/LanduseBuildingAnalysis.java @@ -54,11 +54,14 @@ public class LanduseBuildingAnalysis { * used OSM data. */ static Map> createInputDataDistribution(Path output, - Map> landuseCategoriesAndDataConnection, Path inputDataDirectory, + Map> landuseCategoriesAndDataConnection, String usedLanduseConfiguration, Index indexLanduse, Index indexZones, Index indexBuildings, Index indexInvestigationAreaRegions, - String shapeFileZoneNameColumn, Map>> buildingsPerZone) - throws IOException { + String shapeFileZoneNameColumn, + Map>> buildingsPerZone, + Path pathToInvestigationAreaData, + Path pathToExistingDataDistributionToZones) + throws IOException { Map> resultingDataPerZone = new HashMap<>(); Map zoneIdRegionConnection = new HashMap<>(); @@ -81,13 +84,12 @@ static Map> 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); @@ -101,8 +103,8 @@ static Map> 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 { @@ -115,7 +117,7 @@ static Map> createInputDataDistribution(Path ou buildingsPerZone, shapeFileZoneNameColumn, zoneIdRegionConnection); Map> investigationAreaData = new HashMap<>(); - readAreaData(investigationAreaData, inputDataDirectory); + readAreaData(investigationAreaData, pathToInvestigationAreaData); createResultingDataForLanduseInZones(landuseCategoriesPerZone, investigationAreaData, resultingDataPerZone, landuseCategoriesAndDataConnection, zoneIdRegionConnection); @@ -287,14 +289,13 @@ private static void createLanduseDistribution(Map> areaData, Path inputDataDirectory) + private static void readAreaData(Map> 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) { diff --git a/contribs/small-scale-traffic-generation/src/test/java/org/matsim/smallScaleCommercialTrafficGeneration/LanduseBuildingAnalysisTest.java b/contribs/small-scale-traffic-generation/src/test/java/org/matsim/smallScaleCommercialTrafficGeneration/LanduseBuildingAnalysisTest.java index 7762b156cc5..fa640744c92 100644 --- a/contribs/small-scale-traffic-generation/src/test/java/org/matsim/smallScaleCommercialTrafficGeneration/LanduseBuildingAnalysisTest.java +++ b/contribs/small-scale-traffic-generation/src/test/java/org/matsim/smallScaleCommercialTrafficGeneration/LanduseBuildingAnalysisTest.java @@ -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> 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); @@ -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> 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); diff --git a/contribs/small-scale-traffic-generation/src/test/java/org/matsim/smallScaleCommercialTrafficGeneration/RunGenerateSmallScaleCommercialTrafficTest.java b/contribs/small-scale-traffic-generation/src/test/java/org/matsim/smallScaleCommercialTrafficGeneration/RunGenerateSmallScaleCommercialTrafficTest.java index a521917e8de..c664ba46440 100644 --- a/contribs/small-scale-traffic-generation/src/test/java/org/matsim/smallScaleCommercialTrafficGeneration/RunGenerateSmallScaleCommercialTrafficTest.java +++ b/contribs/small-scale-traffic-generation/src/test/java/org/matsim/smallScaleCommercialTrafficGeneration/RunGenerateSmallScaleCommercialTrafficTest.java @@ -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; @@ -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"; @@ -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, diff --git a/contribs/small-scale-traffic-generation/src/test/java/org/matsim/smallScaleCommercialTrafficGeneration/TrafficVolumeGenerationTest.java b/contribs/small-scale-traffic-generation/src/test/java/org/matsim/smallScaleCommercialTrafficGeneration/TrafficVolumeGenerationTest.java index cfac585157a..b680e568ffb 100644 --- a/contribs/small-scale-traffic-generation/src/test/java/org/matsim/smallScaleCommercialTrafficGeneration/TrafficVolumeGenerationTest.java +++ b/contribs/small-scale-traffic-generation/src/test/java/org/matsim/smallScaleCommercialTrafficGeneration/TrafficVolumeGenerationTest.java @@ -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> 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"; @@ -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> 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.; @@ -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 modesORvehTypes = new ArrayList<>( Arrays.asList("vehTyp1", "vehTyp2", "vehTyp3", "vehTyp4", "vehTyp5")); @@ -525,9 +531,9 @@ void testReducingDemandAfterAddingExistingScenarios_goods() throws Exception { Map> 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> trafficVolumePerTypeAndZone_start = TrafficVolumeGeneration .createTrafficVolume_start(resultingDataPerZone, output, sample, modesORvehTypes, usedTrafficType); @@ -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 modesORvehTypes = new ArrayList<>( List.of("total")); @@ -683,9 +691,9 @@ void testReducingDemandAfterAddingExistingScenarios_commercialPersonTraffic() th Map> 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> trafficVolumePerTypeAndZone_start = TrafficVolumeGeneration .createTrafficVolume_start(resultingDataPerZone, output, sample, modesORvehTypes, usedTrafficType); diff --git a/contribs/small-scale-traffic-generation/src/test/java/org/matsim/smallScaleCommercialTrafficGeneration/TripDistributionMatrixTest.java b/contribs/small-scale-traffic-generation/src/test/java/org/matsim/smallScaleCommercialTrafficGeneration/TripDistributionMatrixTest.java index 76f6c0b6cd8..7190ba7350d 100644 --- a/contribs/small-scale-traffic-generation/src/test/java/org/matsim/smallScaleCommercialTrafficGeneration/TripDistributionMatrixTest.java +++ b/contribs/small-scale-traffic-generation/src/test/java/org/matsim/smallScaleCommercialTrafficGeneration/TripDistributionMatrixTest.java @@ -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> 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.; @@ -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> 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.;