Skip to content

Commit 4671f87

Browse files
authored
Merge pull request #138 from moia-oss/master
Update MATSim CW 11
2 parents 7b43642 + 2acd683 commit 4671f87

File tree

50 files changed

+1511
-1895
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1511
-1895
lines changed

contribs/application/src/main/java/org/matsim/application/prepare/freight/dataProcessing/GenerateLookupTable.java

+15-11
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
import org.matsim.application.MATSimAppCommand;
1111
import org.matsim.application.options.ShpOptions;
1212
import org.matsim.core.utils.geometry.geotools.MGC;
13+
import org.matsim.core.utils.io.IOUtils;
1314
import picocli.CommandLine;
1415

16+
import java.io.BufferedReader;
1517
import java.io.FileWriter;
1618
import java.nio.charset.StandardCharsets;
17-
import java.nio.file.Files;
1819
import java.nio.file.Path;
1920
import java.util.ArrayList;
2021
import java.util.HashMap;
@@ -54,8 +55,9 @@ public Integer call() throws Exception {
5455
// Read german lookup table
5556
Map<String, String> german2006To2021Transformation =
5657
new GermanNutsTransformation(german2006shp, nuts2021shp).getNuts2006To2021Mapping();
57-
try (CSVParser parser = new CSVParser(Files.newBufferedReader(germanTable, StandardCharsets.UTF_8),
58-
CSVFormat.DEFAULT.withDelimiter(';').withFirstRecordAsHeader())) {
58+
try (BufferedReader reader = IOUtils.getBufferedReader(IOUtils.getFileUrl(germanTable.toString()), StandardCharsets.UTF_8)) {
59+
CSVParser parser = CSVFormat.Builder.create(CSVFormat.DEFAULT).setDelimiter(';').setHeader()
60+
.setSkipHeaderRecord(true).build().parse(reader);
5961
for (CSVRecord record : parser) {
6062
String verkerhszelle = record.get(0);
6163
String name = record.get(1);
@@ -66,8 +68,9 @@ public Integer call() throws Exception {
6668
}
6769

6870
// Read international lookup table
69-
try (CSVParser parser = new CSVParser(Files.newBufferedReader(internationalTable, StandardCharsets.UTF_8),
70-
CSVFormat.DEFAULT.withDelimiter(',').withFirstRecordAsHeader())) {
71+
try (BufferedReader reader = IOUtils.getBufferedReader(IOUtils.getFileUrl(internationalTable.toString()), StandardCharsets.UTF_8)) {
72+
CSVParser parser = CSVFormat.Builder.create(CSVFormat.DEFAULT).setDelimiter(',').setHeader()
73+
.setSkipHeaderRecord(true).build().parse(reader);
7174
for (CSVRecord record : parser) {
7275
String verkerhszelle = record.get(0);
7376
String name = record.get(1);
@@ -81,8 +84,9 @@ public Integer call() throws Exception {
8184
List<SimpleFeature> featuresNuts2021 = nuts2021shp.readFeatures();
8285
CSVPrinter tsvWriter = new CSVPrinter(new FileWriter(output.toString()), CSVFormat.TDF);
8386
tsvWriter.printRecord("verkehrszelle", "name", "NUTS_2006", "NUTS_2021", "NUTS_2021_name", "coord_x", "coord_y");
84-
try (CSVParser parser = new CSVParser(Files.newBufferedReader(input, StandardCharsets.ISO_8859_1),
85-
CSVFormat.DEFAULT.withDelimiter(';').withFirstRecordAsHeader())) {
87+
try (BufferedReader reader = IOUtils.getBufferedReader(IOUtils.getFileUrl(input.toString()), StandardCharsets.ISO_8859_1)) {
88+
CSVParser parser = CSVFormat.Builder.create(CSVFormat.DEFAULT).setDelimiter(';').setHeader()
89+
.setSkipHeaderRecord(true).build().parse(reader);
8690
List<String[]> incompleteCellLists = new ArrayList<>();
8791
for (CSVRecord record : parser) {
8892
String verkerhszelle = record.get(0);
@@ -91,7 +95,7 @@ public Integer call() throws Exception {
9195
incompleteCellLists.add(new String[]{verkerhszelle, name});
9296
} else {
9397
TrafficCellCoreData coreData = coreDataLookupTable.get(verkerhszelle);
94-
if (coreData.getNuts2021().equals("")) {
98+
if (coreData.getNuts2021().isEmpty()) {
9599
incompleteCellLists.add(new String[]{verkerhszelle, name});
96100
} else {
97101
String nuts2006 = coreData.getNuts2006();
@@ -103,9 +107,9 @@ public Integer call() throws Exception {
103107
}
104108
}
105109

106-
for (String[] verkerhszelleAndName : incompleteCellLists) {
107-
String verkehrszelle = verkerhszelleAndName[0];
108-
String name = verkerhszelleAndName[1];
110+
for (String[] verkehrszelleAndName : incompleteCellLists) {
111+
String verkehrszelle = verkehrszelleAndName[0];
112+
String name = verkehrszelleAndName[1];
109113
String nuts2006 = coreDataLookupTable.getOrDefault(verkehrszelle, new TrafficCellCoreData(verkehrszelle, name)).getNuts2006();
110114
tsvWriter.printRecord(verkehrszelle, name, nuts2006);
111115
}

contribs/application/src/main/java/org/matsim/application/prepare/freight/optimization/DetermineAverageTruckLoad.java

+287-302
Large diffs are not rendered by default.

contribs/application/src/main/java/org/matsim/application/prepare/freight/optimization/PrepareCountingData.java

+92-91
Original file line numberDiff line numberDiff line change
@@ -12,109 +12,110 @@
1212
import org.matsim.application.options.CrsOptions;
1313
import org.matsim.core.network.NetworkUtils;
1414
import org.matsim.core.utils.geometry.CoordUtils;
15+
import org.matsim.core.utils.io.IOUtils;
1516
import org.matsim.counts.Count;
1617
import org.matsim.counts.Counts;
1718
import org.matsim.counts.CountsWriter;
1819
import picocli.CommandLine;
1920

21+
import java.io.BufferedReader;
2022
import java.io.FileWriter;
2123
import java.nio.charset.StandardCharsets;
22-
import java.nio.file.Files;
2324
import java.nio.file.Path;
2425
import java.util.ArrayList;
2526
import java.util.List;
2627

2728
@CommandLine.Command(
28-
name = "prepare-freight-count",
29-
description = "Prepare the freight traffic count data",
30-
showDefaultValues = true
29+
name = "prepare-freight-count",
30+
description = "Prepare the freight traffic count data",
31+
showDefaultValues = true
3132
)
3233
public class PrepareCountingData implements MATSimAppCommand {
33-
@CommandLine.Option(names = "--data", description = "Path to the raw data", required = true)
34-
private Path rawDataPath;
35-
36-
@CommandLine.Option(names = "--network", description = "Path to desired network file", required = true)
37-
private Path networkPath;
38-
39-
@CommandLine.Option(names = "--output", description = "Path to the output folder", required = true)
40-
private Path outputFolder;
41-
42-
@CommandLine.Mixin
43-
private CrsOptions crs = new CrsOptions(); // Current setup: input EPSG:25832, target EPSG:5677
44-
45-
public static void main(String[] args) {
46-
new PrepareCountingData().execute(args);
47-
}
48-
49-
@Override
50-
public Integer call() throws Exception {
51-
Network network = NetworkUtils.readNetwork(networkPath.toString());
52-
53-
Counts counts = new Counts();
54-
counts.setName("BASt Automatische Zählstellen 2019");
55-
counts.setYear(2019);
56-
List<Id<Link>> processed = new ArrayList<>();
57-
58-
String tsvOutputPath = outputFolder.toString() + "/freight-count-data.tsv";
59-
CSVPrinter tsvWriter = new CSVPrinter(new FileWriter(tsvOutputPath), CSVFormat.TDF);
60-
tsvWriter.printRecord("nearest_link_id", "total_count", "count_direction_1",
61-
"count_direction_2", "road_name", "road_type", "link_to_node_x", "link_to_node_y",
62-
"station_x", "station_y");
63-
64-
try (CSVParser parser = new CSVParser(Files.newBufferedReader(rawDataPath, StandardCharsets.ISO_8859_1),
65-
CSVFormat.DEFAULT.withDelimiter(';').withFirstRecordAsHeader())) {
66-
for (CSVRecord record : parser) {
67-
String totalCountString = record.get(37).replace(".", "");
68-
if (!totalCountString.equals("") && !totalCountString.equals("0")) {
69-
String countDirection1String = record.get(38).replace(".", "");
70-
String countDirection2String = record.get(39).replace(".", "");
71-
String xString = record.get(156).replace(".", "");
72-
String yString = record.get(157).replace(".", "");
73-
String roadName = record.get(2);
74-
String roadType = record.get(5);
75-
76-
int totalCount = Integer.parseInt(totalCountString);
77-
int count1 = Integer.parseInt(countDirection1String);
78-
int count2 = Integer.parseInt(countDirection2String);
79-
Coord originalCoord = new Coord(Double.parseDouble(xString), Double.parseDouble(yString));
80-
Coord coord = crs.getTransformation().transform(originalCoord);
81-
82-
Link link = NetworkUtils.getNearestLink(network, coord);
83-
assert link != null;
84-
double distance = CoordUtils.distancePointLinesegment
85-
(link.getFromNode().getCoord(), link.getToNode().getCoord(), coord);
86-
87-
if (distance > 1000 || processed.contains(link.getId())) {
88-
continue;
89-
}
90-
91-
processed.add(link.getId());
92-
Count count = counts.createAndAddCount(link.getId(), roadName);
93-
double hourlyValue = Math.floor(totalCount / 24.0 + 0.5);
94-
for (int i = 1; i < 25; i++) {
95-
count.createVolume(i, hourlyValue);
96-
}
97-
98-
List<String> outputRow = new ArrayList<>();
99-
outputRow.add(link.getId().toString());
100-
outputRow.add(Integer.toString(totalCount));
101-
outputRow.add(Integer.toString(count1));
102-
outputRow.add(Integer.toString(count2));
103-
outputRow.add(roadName);
104-
outputRow.add(roadType);
105-
outputRow.add(Double.toString(link.getToNode().getCoord().getX()));
106-
outputRow.add(Double.toString(link.getToNode().getCoord().getY()));
107-
outputRow.add(Double.toString(coord.getX()));
108-
outputRow.add(Double.toString(coord.getY()));
109-
110-
tsvWriter.printRecord(outputRow);
111-
}
112-
}
113-
}
114-
tsvWriter.close();
115-
CountsWriter writer = new CountsWriter(counts);
116-
writer.write(outputFolder.toString() + "/count.xml");
117-
118-
return 0;
119-
}
34+
@CommandLine.Option(names = "--data", description = "Path to the raw data", required = true)
35+
private Path rawDataPath;
36+
37+
@CommandLine.Option(names = "--network", description = "Path to desired network file", required = true)
38+
private Path networkPath;
39+
40+
@CommandLine.Option(names = "--output", description = "Path to the output folder", required = true)
41+
private Path outputFolder;
42+
43+
@CommandLine.Mixin
44+
private CrsOptions crs = new CrsOptions(); // Current setup: input EPSG:25832, target EPSG:5677
45+
46+
public static void main(String[] args) {
47+
new PrepareCountingData().execute(args);
48+
}
49+
50+
@Override
51+
public Integer call() throws Exception {
52+
Network network = NetworkUtils.readNetwork(networkPath.toString());
53+
54+
Counts counts = new Counts();
55+
counts.setName("BASt Automatische Zählstellen 2019");
56+
counts.setYear(2019);
57+
List<Id<Link>> processed = new ArrayList<>();
58+
59+
String tsvOutputPath = outputFolder.toString() + "/freight-count-data.tsv";
60+
CSVPrinter tsvWriter = new CSVPrinter(new FileWriter(tsvOutputPath), CSVFormat.TDF);
61+
tsvWriter.printRecord("nearest_link_id", "total_count", "count_direction_1",
62+
"count_direction_2", "road_name", "road_type", "link_to_node_x", "link_to_node_y",
63+
"station_x", "station_y");
64+
try (BufferedReader reader = IOUtils.getBufferedReader(IOUtils.getFileUrl(rawDataPath.toString()), StandardCharsets.ISO_8859_1)) {
65+
CSVParser parser = CSVFormat.Builder.create(CSVFormat.DEFAULT).setDelimiter(';').setHeader()
66+
.setSkipHeaderRecord(true).build().parse(reader);
67+
for (CSVRecord record : parser) {
68+
String totalCountString = record.get(37).replace(".", "");
69+
if (!totalCountString.isEmpty() && !totalCountString.equals("0")) {
70+
String countDirection1String = record.get(38).replace(".", "");
71+
String countDirection2String = record.get(39).replace(".", "");
72+
String xString = record.get(156).replace(".", "");
73+
String yString = record.get(157).replace(".", "");
74+
String roadName = record.get(2);
75+
String roadType = record.get(5);
76+
77+
int totalCount = Integer.parseInt(totalCountString);
78+
int count1 = Integer.parseInt(countDirection1String);
79+
int count2 = Integer.parseInt(countDirection2String);
80+
Coord originalCoord = new Coord(Double.parseDouble(xString), Double.parseDouble(yString));
81+
Coord coord = crs.getTransformation().transform(originalCoord);
82+
83+
Link link = NetworkUtils.getNearestLink(network, coord);
84+
assert link != null;
85+
double distance = CoordUtils.distancePointLinesegment
86+
(link.getFromNode().getCoord(), link.getToNode().getCoord(), coord);
87+
88+
if (distance > 1000 || processed.contains(link.getId())) {
89+
continue;
90+
}
91+
92+
processed.add(link.getId());
93+
Count count = counts.createAndAddCount(link.getId(), roadName);
94+
double hourlyValue = Math.floor(totalCount / 24.0 + 0.5);
95+
for (int i = 1; i < 25; i++) {
96+
count.createVolume(i, hourlyValue);
97+
}
98+
99+
List<String> outputRow = new ArrayList<>();
100+
outputRow.add(link.getId().toString());
101+
outputRow.add(Integer.toString(totalCount));
102+
outputRow.add(Integer.toString(count1));
103+
outputRow.add(Integer.toString(count2));
104+
outputRow.add(roadName);
105+
outputRow.add(roadType);
106+
outputRow.add(Double.toString(link.getToNode().getCoord().getX()));
107+
outputRow.add(Double.toString(link.getToNode().getCoord().getY()));
108+
outputRow.add(Double.toString(coord.getX()));
109+
outputRow.add(Double.toString(coord.getY()));
110+
111+
tsvWriter.printRecord(outputRow);
112+
}
113+
}
114+
}
115+
tsvWriter.close();
116+
CountsWriter writer = new CountsWriter(counts);
117+
writer.write(outputFolder.toString() + "/count.xml");
118+
119+
return 0;
120+
}
120121
}

0 commit comments

Comments
 (0)