-
Notifications
You must be signed in to change notification settings - Fork 376
/
MatrixRequest.java
287 lines (244 loc) 路 12.3 KB
/
MatrixRequest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* 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
* 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.
* 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/>.
*/
package org.heigit.ors.matrix;
import com.graphhopper.GraphHopper;
import com.graphhopper.routing.util.AccessFilter;
import com.graphhopper.routing.util.EdgeFilter;
import com.graphhopper.routing.util.FlagEncoder;
import com.graphhopper.routing.weighting.Weighting;
import com.graphhopper.storage.Graph;
import com.graphhopper.storage.RoutingCHGraph;
import com.graphhopper.util.PMap;
import org.heigit.ors.common.DistanceUnit;
import org.heigit.ors.common.ServiceRequest;
import org.heigit.ors.exceptions.InternalServerException;
import org.heigit.ors.exceptions.MaxVisitedNodesExceededException;
import org.heigit.ors.exceptions.PointNotFoundException;
import org.heigit.ors.matrix.algorithms.core.CoreMatrixAlgorithm;
import org.heigit.ors.matrix.algorithms.dijkstra.DijkstraMatrixAlgorithm;
import org.heigit.ors.matrix.algorithms.rphast.RPHASTMatrixAlgorithm;
import org.heigit.ors.routing.RouteSearchContext;
import org.heigit.ors.routing.RoutingProfile;
import org.heigit.ors.routing.RoutingProfileType;
import org.heigit.ors.routing.WeightingMethod;
import org.heigit.ors.routing.graphhopper.extensions.ORSEdgeFilterFactory;
import org.heigit.ors.routing.graphhopper.extensions.ORSGraphHopperStorage;
import org.heigit.ors.routing.graphhopper.extensions.ORSWeightingFactory;
import org.heigit.ors.util.ProfileTools;
import org.locationtech.jts.geom.Coordinate;
public class MatrixRequest extends ServiceRequest {
private int profileType = -1;
private Coordinate[] sources;
private Coordinate[] destinations;
private int metrics = MatrixMetricsType.DURATION;
private int weightingMethod;
private DistanceUnit units = DistanceUnit.METERS;
private boolean resolveLocations = false;
private boolean flexibleMode = false;
private String algorithm;
private MatrixSearchParameters searchParameters;
private double maximumSearchRadius;
private int maximumVisitedNodes;
private boolean hasInfiniteUTurnCosts;
public MatrixRequest(double maximumSearchRadius, int maximumVisitedNodes, double uTurnCost) {
this.maximumSearchRadius = maximumSearchRadius;
this.maximumVisitedNodes = maximumVisitedNodes;
this.hasInfiniteUTurnCosts = uTurnCost == Weighting.INFINITE_U_TURN_COSTS;
}
public Coordinate[] getSources() {
return sources;
}
public void setSources(Coordinate[] sources) {
this.sources = sources;
}
public Coordinate[] getDestinations() {
return destinations;
}
public void setDestinations(Coordinate[] destinations) {
this.destinations = destinations;
}
public int getMetrics() {
return metrics;
}
public void setMetrics(int metrics) {
this.metrics = metrics;
}
public boolean getResolveLocations() {
return resolveLocations;
}
public void setResolveLocations(boolean resolveLocations) {
this.resolveLocations = resolveLocations;
}
public int getProfileType() {
return profileType;
}
public void setProfileType(int profile) {
profileType = profile;
}
public DistanceUnit getUnits() {
return units;
}
public void setUnits(DistanceUnit units) {
this.units = units;
}
public int getTotalNumberOfLocations() {
return destinations.length * sources.length;
}
public int getWeightingMethod() {
return weightingMethod;
}
/**
* Return weightingMethod or sane default if method is UNKNOWN.
*/
public int getWeightingMethodOrDefault() {
return weightingMethod == WeightingMethod.UNKNOWN ? WeightingMethod.RECOMMENDED : weightingMethod;
}
public void setWeightingMethod(int weightingMethod) {
this.weightingMethod = weightingMethod;
}
public boolean getFlexibleMode() {
return flexibleMode;
}
public void setFlexibleMode(boolean flexibleMode) {
this.flexibleMode = flexibleMode;
}
public String getAlgorithm() {
return algorithm;
}
public void setAlgorithm(String algorithm) {
this.algorithm = algorithm;
}
public double getMaximumSearchRadius() {
return maximumSearchRadius;
}
public void setMaximumSearchRadius(double radius) {
this.maximumSearchRadius = radius;
}
public int getMaximumVisitedNodes() {
return maximumVisitedNodes;
}
public void setMaximumVisitedNodes(int maximumVisitedNodes) {
this.maximumVisitedNodes = maximumVisitedNodes;
}
public void setSearchParameters(MatrixSearchParameters searchParameters) {
this.searchParameters = searchParameters;
}
public MatrixSearchParameters getSearchParameters() {
return searchParameters;
}
public boolean isValid() {
return !(sources == null && destinations == null);
}
public boolean hasInfiniteUTurnCosts() {
return hasInfiniteUTurnCosts;
}
public void setInfiniteUTurnCosts(boolean hasInfiniteUTurnCosts) {
this.hasInfiniteUTurnCosts = hasInfiniteUTurnCosts;
}
/**
* Compute a NxM matrix from a request using any of the three available approaches.
* For performance reasons, RPHAST is preferred over CoreMatrix, which is preferred over DijkstraMatrix, depending on request conditions.
*
* @param routingProfile@return A MatrixResult object, possibly with both time and distance values for all combinations of N and M input locations
* @throws Exception
*/
public MatrixResult computeMatrix(RoutingProfile routingProfile) throws Exception {
GraphHopper gh = routingProfile.getGraphhopper();
String encoderName = RoutingProfileType.getEncoderName(getProfileType());
FlagEncoder flagEncoder = gh.getEncodingManager().getEncoder(encoderName);
PMap hintsMap = new PMap();
ProfileTools.setWeightingMethod(hintsMap, getWeightingMethodOrDefault(), getProfileType(), false);
ProfileTools.setWeighting(hintsMap, getWeightingMethodOrDefault(), getProfileType(), false);
String chProfileName = ProfileTools.makeProfileName(encoderName, hintsMap.getString("weighting", ""), false);
String coreProfileName = ProfileTools.makeProfileName(encoderName, hintsMap.getString("weighting", ""), true);
//TODO Refactoring : probably remove MatrixAlgorithmFactory alltogether as the checks for algorithm choice have to be performed here again. Or combine in a single check nicely
try {
// RPHAST
if (!getFlexibleMode() && gh.getCHPreparationHandler().isEnabled() && routingProfile.hasCHProfile(chProfileName)) {
return computeRPHASTMatrix(gh, flagEncoder, chProfileName);
}
// Core
else if (getSearchParameters().getDynamicSpeeds() && routingProfile.getGraphhopper().isCoreAvailable(coreProfileName)) {
return computeCoreMatrix(gh, flagEncoder, hintsMap, coreProfileName, routingProfile);
}
// Dijkstra
else {
// use CHProfileName (w/o turn costs) since Dijkstra is node-based so turn restrictions are not used.
return computeDijkstraMatrix(gh, flagEncoder, hintsMap, chProfileName);
}
} catch (PointNotFoundException e) {
throw e;
} catch (MaxVisitedNodesExceededException e) {
throw new InternalServerException(MatrixErrorCodes.MAX_VISITED_NODES_EXCEEDED, "Unable to compute a distance/duration matrix: " + e.getMessage());
} catch (Exception ex) {
throw new InternalServerException(MatrixErrorCodes.UNKNOWN, "Unable to compute a distance/duration matrix: " + ex.getMessage());
}
}
/**
* Compute a matrix based on a contraction hierarchies graph using the RPHAST algorithm. This is fast, but inflexible.
*
* @param gh
* @param flagEncoder
* @param profileName
* @return
* @throws Exception
*/
private MatrixResult computeRPHASTMatrix(GraphHopper gh, FlagEncoder flagEncoder, String profileName) throws Exception {
RoutingCHGraph routingCHGraph = gh.getGraphHopperStorage().getRoutingCHGraph(profileName);
MatrixSearchContextBuilder builder = new MatrixSearchContextBuilder(gh.getGraphHopperStorage(), gh.getLocationIndex(), AccessFilter.allEdges(flagEncoder.getAccessEnc()), getResolveLocations());
MatrixSearchContext mtxSearchCntx = builder.create(routingCHGraph.getBaseGraph(), routingCHGraph, routingCHGraph.getWeighting(), profileName, getSources(), getDestinations(), getMaximumSearchRadius());
RPHASTMatrixAlgorithm algo = new RPHASTMatrixAlgorithm();
algo.init(this, gh, mtxSearchCntx.getRoutingCHGraph(), flagEncoder, routingCHGraph.getWeighting());
return algo.compute(mtxSearchCntx.getSources(), mtxSearchCntx.getDestinations(), getMetrics());
}
/**
* Compute a matrix based on the normal graph. Slow, but highly flexible in terms of request parameters.
*
* @param gh
* @param flagEncoder
* @param hintsMap
* @param profileName
* @return
* @throws Exception
*/
private MatrixResult computeDijkstraMatrix(GraphHopper gh, FlagEncoder flagEncoder, PMap hintsMap, String profileName) throws Exception {
Graph graph = gh.getGraphHopperStorage().getBaseGraph();
Weighting weighting = new ORSWeightingFactory(gh.getGraphHopperStorage(), gh.getEncodingManager()).createWeighting(gh.getProfile(profileName), hintsMap, false);
MatrixSearchContextBuilder builder = new MatrixSearchContextBuilder(gh.getGraphHopperStorage(), gh.getLocationIndex(), AccessFilter.allEdges(flagEncoder.getAccessEnc()), getResolveLocations());
MatrixSearchContext mtxSearchCntx = builder.create(graph, null, weighting, profileName, getSources(), getDestinations(), getMaximumSearchRadius());
DijkstraMatrixAlgorithm algo = new DijkstraMatrixAlgorithm();
algo.init(this, gh, mtxSearchCntx.getGraph(), flagEncoder, weighting);
return algo.compute(mtxSearchCntx.getSources(), mtxSearchCntx.getDestinations(), getMetrics());
}
/**
* Compute a matrix based on a core contracted graph, which is slower than RPHAST, but offers all the flexibility of the core
*
* @param gh
* @param flagEncoder
* @param hintsMap
* @param profileName
* @param routingProfile
* @return
*/
private MatrixResult computeCoreMatrix(GraphHopper gh, FlagEncoder flagEncoder, PMap hintsMap, String profileName, RoutingProfile routingProfile) throws Exception {
Weighting weighting = new ORSWeightingFactory(gh.getGraphHopperStorage(), gh.getEncodingManager()).createWeighting(gh.getProfile(profileName), hintsMap, false);
RoutingCHGraph graph = ((ORSGraphHopperStorage) gh.getGraphHopperStorage()).getCoreGraph(profileName);
RouteSearchContext searchCntx = routingProfile.createSearchContext(getSearchParameters());
PMap additionalHints = searchCntx.getProperties();
EdgeFilter edgeFilter = new ORSEdgeFilterFactory().createEdgeFilter(additionalHints, flagEncoder, gh.getGraphHopperStorage());
MatrixSearchContextBuilder builder = new MatrixSearchContextBuilder(gh.getGraphHopperStorage(), gh.getLocationIndex(), edgeFilter, getResolveLocations());
MatrixSearchContext mtxSearchCntx = builder.create(graph.getBaseGraph(), graph, weighting, profileName, getSources(), getDestinations(), getMaximumSearchRadius());
CoreMatrixAlgorithm algo = new CoreMatrixAlgorithm();
algo.init(this, gh, mtxSearchCntx.getRoutingCHGraph(), flagEncoder, weighting, edgeFilter);
return algo.compute(mtxSearchCntx.getSources(), mtxSearchCntx.getDestinations(), getMetrics());
}
}