Skip to content

Commit

Permalink
catch non existing network nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow committed Dec 16, 2024
1 parent 47b1292 commit 67ae2b8
Showing 1 changed file with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.matsim.application.analysis.traffic.traveltime;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.TransportMode;
import org.matsim.api.core.v01.network.Link;
Expand Down Expand Up @@ -47,6 +49,8 @@
)
public class TravelTimeComparison implements MATSimAppCommand {

private static final Logger log = LogManager.getLogger(TravelTimeComparison.class);

@CommandLine.Mixin
private InputOptions input = InputOptions.ofCommand(TravelTimeComparison.class);

Expand Down Expand Up @@ -90,6 +94,13 @@ public Integer call() throws Exception {

for (Row row : data) {
LeastCostPathCalculator.Path congested = computePath(network, congestedRouter, row);

// Skip if path is not found
if (congested == null) {
row.setDouble("simulated", Double.NaN);
continue;
}

double dist = congested.links.stream().mapToDouble(Link::getLength).sum();
double speed = 3.6 * dist / congested.travelTime;

Expand All @@ -102,6 +113,8 @@ public Integer call() throws Exception {
row.setDouble("free_flow", speed);
}

data = data.dropWhere(data.doubleColumn("simulated").isMissing());

data.addColumns(
data.doubleColumn("simulated").subtract(data.doubleColumn("mean")).setName("bias")
);
Expand Down Expand Up @@ -129,6 +142,16 @@ private LeastCostPathCalculator.Path computePath(Network network, LeastCostPathC
Node fromNode = network.getNodes().get(Id.createNodeId(row.getString("from_node")));
Node toNode = network.getNodes().get(Id.createNodeId(row.getString("to_node")));

if (fromNode == null) {
log.error("Node {} not found in network", row.getString("from_node"));
return null;
}

if (toNode == null) {
log.error("Node {} not found in network", row.getString("to_node"));
return null;
}

return router.calcLeastCostPath(fromNode, toNode, row.getInt("hour") * 3600, null, null);
}

Expand Down

0 comments on commit 67ae2b8

Please sign in to comment.