Skip to content

Commit aea1f51

Browse files
authored
Merge pull request #111 from moia-oss/moia-deployment-pre
Update MATSim CW34
2 parents 93b4961 + f6df55b commit aea1f51

File tree

44 files changed

+1419
-742
lines changed

Some content is hidden

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

44 files changed

+1419
-742
lines changed

contribs/application/src/main/java/org/matsim/application/analysis/traffic/traveltime/SampleValidationRoutes.java

+67-6
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@
1919
import org.matsim.api.core.v01.network.Node;
2020
import org.matsim.application.CommandSpec;
2121
import org.matsim.application.MATSimAppCommand;
22-
import org.matsim.application.options.CrsOptions;
23-
import org.matsim.application.options.InputOptions;
24-
import org.matsim.application.options.OutputOptions;
25-
import org.matsim.application.options.ShpOptions;
22+
import org.matsim.application.options.*;
2623
import org.matsim.application.prepare.network.SampleNetwork;
2724
import org.matsim.core.network.NetworkUtils;
2825
import org.matsim.core.router.costcalculators.OnlyTimeDependentTravelDisutility;
@@ -32,9 +29,11 @@
3229
import org.matsim.core.trafficmonitoring.FreeSpeedTravelTime;
3330
import org.matsim.core.utils.geometry.CoordUtils;
3431
import org.matsim.core.utils.geometry.transformations.GeotoolsTransformation;
32+
import org.matsim.core.utils.io.IOUtils;
3533
import picocli.CommandLine;
3634

3735
import java.io.IOException;
36+
import java.io.UncheckedIOException;
3837
import java.nio.file.Files;
3938
import java.nio.file.Path;
4039
import java.util.*;
@@ -86,6 +85,8 @@ public class SampleValidationRoutes implements MATSimAppCommand {
8685
@CommandLine.Option(names = "--mode", description = "Mode to validate", defaultValue = TransportMode.car)
8786
private String mode;
8887

88+
@CommandLine.Option(names = "--input-od", description = "Use input fromNode,toNode instead of sampling", required = false)
89+
private String inputOD;
8990

9091
public static void main(String[] args) {
9192
new SampleValidationRoutes().execute(args);
@@ -142,9 +143,15 @@ public Integer call() throws Exception {
142143
OnlyTimeDependentTravelDisutility util = new OnlyTimeDependentTravelDisutility(tt);
143144
LeastCostPathCalculator router = new SpeedyALTFactory().createPathCalculator(network, util, tt);
144145

145-
List<Route> routes = sampleRoutes(network, router, rnd);
146146

147-
log.info("Sampled {} routes in range {}", routes.size(), distRange);
147+
List<Route> routes;
148+
if (inputOD != null) {
149+
log.info("Using input OD file {}", inputOD);
150+
routes = queryRoutes(network, router);
151+
} else {
152+
routes = sampleRoutes(network, router, rnd);
153+
log.info("Sampled {} routes in range {}", routes.size(), distRange);
154+
}
148155

149156
try (CSVPrinter csv = new CSVPrinter(Files.newBufferedWriter(output.getPath()), CSVFormat.DEFAULT)) {
150157
csv.printRecord("from_node", "to_node", "beeline_dist", "dist", "travel_time", "geometry");
@@ -228,6 +235,7 @@ private List<Route> sampleRoutes(Network network, LeastCostPathCalculator router
228235
ShpOptions.Index index = shp.isDefined() ? shp.createIndex(crs, "_") : null;
229236
Predicate<Link> exclude = excludeRoads != null && !excludeRoads.isBlank() ? new Predicate<>() {
230237
final Pattern p = Pattern.compile(excludeRoads, Pattern.CASE_INSENSITIVE);
238+
231239
@Override
232240
public boolean test(Link link) {
233241
return p.matcher(NetworkUtils.getHighwayType(link)).find();
@@ -282,6 +290,59 @@ public boolean test(Link link) {
282290
return result;
283291
}
284292

293+
/**
294+
* Use given od pairs as input for validation.
295+
*/
296+
private List<Route> queryRoutes(Network network, LeastCostPathCalculator router) {
297+
298+
List<Route> result = new ArrayList<>();
299+
String crs = ProjectionUtils.getCRS(network);
300+
301+
if (this.crs.getInputCRS() != null)
302+
crs = this.crs.getInputCRS();
303+
304+
if (crs == null) {
305+
throw new IllegalArgumentException("Input CRS could not be detected. Please specify with --input-crs [EPSG:xxx]");
306+
}
307+
308+
GeotoolsTransformation ct = new GeotoolsTransformation(crs, "EPSG:4326");
309+
310+
try (CSVParser parser = CSVParser.parse(IOUtils.getBufferedReader(inputOD), CSVFormat.DEFAULT.builder().setHeader().setSkipHeaderRecord(true).
311+
setDelimiter(CsvOptions.detectDelimiter(inputOD)).build())) {
312+
313+
List<String> header = parser.getHeaderNames();
314+
if (!header.contains("from_node"))
315+
throw new IllegalArgumentException("Missing 'from_node' column in input file");
316+
if (!header.contains("to_node"))
317+
throw new IllegalArgumentException("Missing 'to_node' column in input file");
318+
319+
for (CSVRecord r : parser) {
320+
Node fromNode = network.getNodes().get(Id.createNodeId(r.get("from_node")));
321+
Node toNode = network.getNodes().get(Id.createNodeId(r.get("to_node")));
322+
323+
if (fromNode == null)
324+
throw new IllegalArgumentException("Node " + r.get("from_node") + " not found");
325+
if (toNode == null)
326+
throw new IllegalArgumentException("Node " + r.get("to_node") + " not found");
327+
328+
LeastCostPathCalculator.Path path = router.calcLeastCostPath(fromNode, toNode, 0, null, null);
329+
result.add(new Route(
330+
fromNode.getId(),
331+
toNode.getId(),
332+
ct.transform(fromNode.getCoord()),
333+
ct.transform(toNode.getCoord()),
334+
path.travelTime,
335+
path.links.stream().mapToDouble(Link::getLength).sum()
336+
));
337+
}
338+
339+
} catch (IOException e) {
340+
throw new UncheckedIOException(e);
341+
}
342+
343+
return result;
344+
}
345+
285346
/**
286347
* Key as pair of from and to node.
287348
*/

0 commit comments

Comments
 (0)