-
Notifications
You must be signed in to change notification settings - Fork 406
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1142 from GIScience/gh-update-dijkstramatrix
Gh update dijkstramatrix
- Loading branch information
Showing
2 changed files
with
195 additions
and
122 deletions.
There are no files selected for viewing
149 changes: 72 additions & 77 deletions
149
...vice/src/main/java/org/heigit/ors/matrix/algorithms/dijkstra/DijkstraMatrixAlgorithm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,91 @@ | ||
/* This file is part of Openrouteservice. | ||
* | ||
* Openrouteservice is free software; you can redistribute it and/or modify it under the terms of the | ||
* GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 | ||
* Openrouteservice is free software; you can redistribute it and/or modify it under the terms of the | ||
* GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 | ||
* of the License, or (at your option) any later version. | ||
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU Lesser General Public License for more details. | ||
* You should have received a copy of the GNU Lesser General Public License along with this library; | ||
* if not, see <https://www.gnu.org/licenses/>. | ||
* You should have received a copy of the GNU Lesser General Public License along with this library; | ||
* if not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package org.heigit.ors.matrix.algorithms.dijkstra; | ||
|
||
import com.graphhopper.GraphHopper; | ||
import com.graphhopper.routing.SPTEntry; | ||
import com.graphhopper.routing.util.FlagEncoder; | ||
import com.graphhopper.routing.util.TraversalMode; | ||
import com.graphhopper.routing.weighting.Weighting; | ||
import com.graphhopper.storage.Graph; | ||
import com.graphhopper.routing.SPTEntry; | ||
|
||
import org.heigit.ors.matrix.MatrixMetricsType; | ||
import org.heigit.ors.matrix.MatrixRequest; | ||
import org.heigit.ors.matrix.MatrixResult; | ||
import org.heigit.ors.matrix.MatrixLocations; | ||
import org.heigit.ors.matrix.PathMetricsExtractor; | ||
import org.heigit.ors.config.MatrixServiceSettings; | ||
import org.heigit.ors.matrix.*; | ||
import org.heigit.ors.matrix.algorithms.AbstractMatrixAlgorithm; | ||
import org.heigit.ors.routing.algorithms.DijkstraOneToManyAlgorithm; | ||
import org.heigit.ors.config.MatrixServiceSettings; | ||
|
||
public class DijkstraMatrixAlgorithm extends AbstractMatrixAlgorithm { | ||
private PathMetricsExtractor pathMetricsExtractor; | ||
|
||
@Override | ||
public void init(MatrixRequest req, GraphHopper gh, Graph graph, FlagEncoder encoder, Weighting weighting) { | ||
super.init(req, gh, graph, encoder, weighting); | ||
|
||
pathMetricsExtractor = new PathMetricsExtractor(req.getMetrics(), this.graph, this.encoder, this.weighting, req.getUnits()); | ||
} | ||
|
||
@Override | ||
public MatrixResult compute(MatrixLocations srcData, MatrixLocations dstData, int metrics) throws Exception { | ||
MatrixResult mtxResult = new MatrixResult(srcData.getLocations(), dstData.getLocations()); | ||
|
||
float[] times = null; | ||
float[] distances = null; | ||
float[] weights = null; | ||
|
||
int tableSize = srcData.size() * dstData.size(); | ||
if (MatrixMetricsType.isSet(metrics, MatrixMetricsType.DURATION)) | ||
times = new float[tableSize]; | ||
if (MatrixMetricsType.isSet(metrics, MatrixMetricsType.DISTANCE)) | ||
distances = new float[tableSize]; | ||
if (MatrixMetricsType.isSet(metrics, MatrixMetricsType.WEIGHT)) | ||
weights = new float[tableSize]; | ||
|
||
if (!srcData.hasValidNodes() || !dstData.hasValidNodes()) { | ||
for (int srcIndex = 0; srcIndex < srcData.size(); srcIndex++) | ||
pathMetricsExtractor.setEmptyValues(srcIndex, dstData, times, distances, weights); | ||
} else { | ||
DijkstraOneToManyAlgorithm algorithm = new DijkstraOneToManyAlgorithm(graph, weighting, TraversalMode.NODE_BASED); | ||
algorithm.prepare(srcData.getNodeIds(), dstData.getNodeIds()); | ||
algorithm.setMaxVisitedNodes(MatrixServiceSettings.getMaximumVisitedNodes()); | ||
int sourceId = -1; | ||
|
||
for (int srcIndex = 0; srcIndex < srcData.size(); srcIndex++) { | ||
sourceId = srcData.getNodeId(srcIndex); | ||
|
||
if (sourceId == -1) { | ||
pathMetricsExtractor.setEmptyValues(srcIndex, dstData, times, distances, weights); | ||
} else { | ||
algorithm.reset(); | ||
SPTEntry[] targets = algorithm.calcPaths(sourceId, dstData.getNodeIds()); | ||
|
||
if (algorithm.getFoundTargets() != algorithm.getTargetsCount()) | ||
throw new Exception("Search exceeds the limit of visited nodes."); | ||
|
||
if (targets != null) { | ||
pathMetricsExtractor.calcValues(srcIndex, targets, dstData, times, distances, weights); | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (MatrixMetricsType.isSet(metrics, MatrixMetricsType.DURATION)) | ||
mtxResult.setTable(MatrixMetricsType.DURATION, times); | ||
if (MatrixMetricsType.isSet(metrics, MatrixMetricsType.DISTANCE)) | ||
mtxResult.setTable(MatrixMetricsType.DISTANCE, distances); | ||
if (MatrixMetricsType.isSet(metrics, MatrixMetricsType.WEIGHT)) | ||
mtxResult.setTable(MatrixMetricsType.WEIGHT, weights); | ||
|
||
return mtxResult; | ||
} | ||
private PathMetricsExtractor pathMetricsExtractor; | ||
|
||
@Override | ||
public void init(MatrixRequest req, GraphHopper gh, Graph graph, FlagEncoder encoder, Weighting weighting) { | ||
super.init(req, gh, graph, encoder, weighting); | ||
|
||
pathMetricsExtractor = new PathMetricsExtractor(req.getMetrics(), this.graph, this.encoder, this.weighting, req.getUnits()); | ||
} | ||
|
||
@Override | ||
public MatrixResult compute(MatrixLocations srcData, MatrixLocations dstData, int metrics) throws Exception { | ||
MatrixResult mtxResult = new MatrixResult(srcData.getLocations(), dstData.getLocations()); | ||
|
||
float[] times = null; | ||
float[] distances = null; | ||
float[] weights = null; | ||
|
||
int tableSize = srcData.size() * dstData.size(); | ||
if (MatrixMetricsType.isSet(metrics, MatrixMetricsType.DURATION)) | ||
times = new float[tableSize]; | ||
if (MatrixMetricsType.isSet(metrics, MatrixMetricsType.DISTANCE)) | ||
distances = new float[tableSize]; | ||
if (MatrixMetricsType.isSet(metrics, MatrixMetricsType.WEIGHT)) | ||
weights = new float[tableSize]; | ||
|
||
if (!srcData.hasValidNodes() || !dstData.hasValidNodes()) { | ||
for (int srcIndex = 0; srcIndex < srcData.size(); srcIndex++) | ||
pathMetricsExtractor.setEmptyValues(srcIndex, dstData, times, distances, weights); | ||
} else { | ||
DijkstraOneToManyAlgorithm algorithm = new DijkstraOneToManyAlgorithm(graph, weighting, TraversalMode.NODE_BASED); | ||
algorithm.prepare(srcData.getNodeIds(), dstData.getNodeIds()); | ||
algorithm.setMaxVisitedNodes(MatrixServiceSettings.getMaximumVisitedNodes()); | ||
|
||
int sourceId = -1; | ||
|
||
for (int srcIndex = 0; srcIndex < srcData.size(); srcIndex++) { | ||
sourceId = srcData.getNodeId(srcIndex); | ||
|
||
if (sourceId == -1) { | ||
pathMetricsExtractor.setEmptyValues(srcIndex, dstData, times, distances, weights); | ||
} else { | ||
algorithm.reset(); | ||
SPTEntry[] targets = algorithm.calcPaths(sourceId, dstData.getNodeIds()); | ||
|
||
if (algorithm.getFoundTargets() != algorithm.getTargetsCount()) | ||
throw new Exception("Search exceeds the limit of visited nodes."); | ||
|
||
if (targets != null) { | ||
pathMetricsExtractor.calcValues(srcIndex, targets, dstData, times, distances, weights); | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (MatrixMetricsType.isSet(metrics, MatrixMetricsType.DURATION)) | ||
mtxResult.setTable(MatrixMetricsType.DURATION, times); | ||
if (MatrixMetricsType.isSet(metrics, MatrixMetricsType.DISTANCE)) | ||
mtxResult.setTable(MatrixMetricsType.DISTANCE, distances); | ||
if (MatrixMetricsType.isSet(metrics, MatrixMetricsType.WEIGHT)) | ||
mtxResult.setTable(MatrixMetricsType.WEIGHT, weights); | ||
|
||
return mtxResult; | ||
} | ||
} |
Oops, something went wrong.