|
12 | 12 | import org.matsim.application.options.CrsOptions;
|
13 | 13 | import org.matsim.core.network.NetworkUtils;
|
14 | 14 | import org.matsim.core.utils.geometry.CoordUtils;
|
| 15 | +import org.matsim.core.utils.io.IOUtils; |
15 | 16 | import org.matsim.counts.Count;
|
16 | 17 | import org.matsim.counts.Counts;
|
17 | 18 | import org.matsim.counts.CountsWriter;
|
18 | 19 | import picocli.CommandLine;
|
19 | 20 |
|
| 21 | +import java.io.BufferedReader; |
20 | 22 | import java.io.FileWriter;
|
21 | 23 | import java.nio.charset.StandardCharsets;
|
22 |
| -import java.nio.file.Files; |
23 | 24 | import java.nio.file.Path;
|
24 | 25 | import java.util.ArrayList;
|
25 | 26 | import java.util.List;
|
26 | 27 |
|
27 | 28 | @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 |
31 | 32 | )
|
32 | 33 | 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 | + } |
120 | 121 | }
|
0 commit comments