Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public final class DistanceMatrix {
public static final double DISTANCE_SNAP_ERROR_VALUE = 1e7;
public static final long TIME_SNAP_ERROR_VALUE = 10000000;

/** Sentinel value (meters) returned when two snapped points have no route between them. */
public static final double DISTANCE_NO_ROUTE_VALUE = (double) Integer.MAX_VALUE;
/** Sentinel value (milliseconds) returned when two snapped points have no route between them. */
public static final long TIME_NO_ROUTE_VALUE = (long) Integer.MAX_VALUE;

private final int numberOfOrigins;
private final int numberOfDestinations;

Expand Down Expand Up @@ -52,6 +57,9 @@ public DistanceMatrix(int numberOfOrigins, int numberOfDestinations, IntArrayLis
distances = new double[numberOfOrigins][numberOfDestinations];
times = new long[numberOfOrigins][numberOfDestinations];

for (double[] row : distances) Arrays.fill(row, DISTANCE_NO_ROUTE_VALUE);
for (long[] row : times) Arrays.fill(row, TIME_NO_ROUTE_VALUE);

this.snapOriginsErrors = originsErrors;
this.snapDestinationErrors = destinationsErros;

Expand Down
18 changes: 18 additions & 0 deletions core/src/test/java/com/graphhopper/GraphHopperMatrixTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package com.graphhopper;

import com.carrotsearch.hppc.IntArrayList;
import com.graphhopper.config.CHProfile;
import com.graphhopper.config.Profile;
import com.graphhopper.routing.MultiplePointsNotFoundException;
Expand All @@ -38,7 +39,9 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.graphhopper.routing.matrix.DistanceMatrix.DISTANCE_NO_ROUTE_VALUE;
import static com.graphhopper.routing.matrix.DistanceMatrix.DISTANCE_SNAP_ERROR_VALUE;
import static com.graphhopper.routing.matrix.DistanceMatrix.TIME_NO_ROUTE_VALUE;
import static com.graphhopper.routing.matrix.DistanceMatrix.TIME_SNAP_ERROR_VALUE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -437,4 +440,19 @@ void testMatrixCarProfile(){
assertTrue(result.getTime(0, 0) == 530105);

}

@Test
void testNoRouteDefaultCellValue() {
IntArrayList noErrors = new IntArrayList();
DistanceMatrix matrix = new DistanceMatrix(2, 2, noErrors, noErrors);

// Cells never written by setCell must return the no-route sentinel
assertEquals(DISTANCE_NO_ROUTE_VALUE, matrix.getDistance(0, 1));
assertEquals(TIME_NO_ROUTE_VALUE, matrix.getTime(0, 1));

// setCell must overwrite the sentinel with real values
matrix.setCell(0, 1, 1500.0, 90000L);
assertEquals(1500.0, matrix.getDistance(0, 1));
assertEquals(90000L, matrix.getTime(0, 1));
}
}
Loading