-
Notifications
You must be signed in to change notification settings - Fork 376
/
MatrixSearchContextBuilder.java
188 lines (157 loc) 路 7.57 KB
/
MatrixSearchContextBuilder.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
/* 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.routing.ev.Subnetwork;
import com.graphhopper.routing.querygraph.QueryGraph;
import com.graphhopper.routing.querygraph.QueryRoutingCHGraph;
import com.graphhopper.routing.util.DefaultSnapFilter;
import com.graphhopper.routing.util.EdgeFilter;
import com.graphhopper.routing.weighting.Weighting;
import com.graphhopper.storage.Graph;
import com.graphhopper.storage.GraphHopperStorage;
import com.graphhopper.storage.RoutingCHGraph;
import com.graphhopper.storage.index.LocationIndex;
import com.graphhopper.storage.index.Snap;
import com.graphhopper.util.shapes.BBox;
import com.graphhopper.util.shapes.GHPoint3D;
import org.heigit.ors.exceptions.PointNotFoundException;
import org.locationtech.jts.geom.Coordinate;
import java.util.*;
public class MatrixSearchContextBuilder {
private final boolean resolveNames;
private final LocationIndex locIndex;
private final EdgeFilter edgeFilter;
private Map<Coordinate, LocationEntry> locationCache;
private final GraphHopperStorage graphHopperStorage;
private Weighting weighting;
public MatrixSearchContextBuilder(GraphHopperStorage graphHopperStorage, LocationIndex index, EdgeFilter edgeFilter, boolean resolveNames) {
locIndex = index;
this.edgeFilter = edgeFilter;
this.resolveNames = resolveNames;
this.graphHopperStorage = graphHopperStorage;
}
public MatrixSearchContext create(Graph graph, RoutingCHGraph chGraph, Weighting weighting, String profileName, Coordinate[] sources, Coordinate[] destinations, double maxSearchRadius) throws Exception {
if (locationCache == null)
locationCache = new HashMap<>();
else
locationCache.clear();
checkBounds(graph.getBounds(), sources, destinations);
this.weighting = weighting;
List<Snap> snaps = new ArrayList<>(sources.length + destinations.length);
resolveLocations(profileName, sources, snaps, maxSearchRadius);
resolveLocations(profileName, destinations, snaps, maxSearchRadius);
QueryGraph queryGraph = QueryGraph.create(graph, snaps);
RoutingCHGraph routingCHGraph = null;
if (chGraph != null) {
routingCHGraph = new QueryRoutingCHGraph(chGraph, queryGraph);
}
MatrixLocations mlSources = createLocations(sources);
MatrixLocations mlDestinations = createLocations(destinations);
return new MatrixSearchContext(queryGraph, routingCHGraph, mlSources, mlDestinations);
}
private void checkBounds(BBox bounds, Coordinate[] sources, Coordinate[] destinations) throws PointNotFoundException {
String[] messages = new String[2];
messages[0] = constructPointOutOfBoundsMessage("Source", bounds, sources);
messages[1] = constructPointOutOfBoundsMessage("Destination", bounds, destinations);
String exceptionMessage = messages[0];
if (!exceptionMessage.isEmpty() && !messages[1].isEmpty())
exceptionMessage += ". ";
exceptionMessage += messages[1];
if (!exceptionMessage.isEmpty())
throw new PointNotFoundException(exceptionMessage, MatrixErrorCodes.POINT_NOT_FOUND);
}
private String constructPointOutOfBoundsMessage(String pointsType, BBox bounds, Coordinate[] coords) {
int[] pointIds = pointIdsOutOfBounds(bounds, coords);
String message = "";
if (pointIds.length > 0) {
String idString = Arrays.toString(pointIds);
StringBuilder coordsString = new StringBuilder();
for (int id : pointIds) {
if (coordsString.length() > 0) {
coordsString.append("; ");
}
coordsString.append(coords[id].y).append(",").append(coords[id].x);
}
message = pointsType + " point(s) " + idString + " out of bounds: " + coordsString;
}
return message;
}
private int[] pointIdsOutOfBounds(BBox bounds, Coordinate[] coords) {
List<Integer> ids = new ArrayList<>();
for (int i = 0; i < coords.length; i++) {
Coordinate c = coords[i];
if (!bounds.contains(c.y, c.x)) {
ids.add(i);
}
}
int[] idsArray = new int[ids.size()];
for (int i = 0; i < ids.size(); i++) {
idsArray[i] = ids.get(i);
}
return idsArray;
}
private void resolveLocations(String profileName, Coordinate[] coords, List<Snap> queryResults, double maxSearchRadius) {
for (Coordinate p : coords) {
LocationEntry ld = locationCache.get(p);
if (ld == null) {
Snap qr = locIndex.findClosest(p.y, p.x, getSnapFilter(profileName));
ld = new LocationEntry();
ld.snap = qr;
if (qr.isValid() && qr.getQueryDistance() < maxSearchRadius) {
GHPoint3D pt = qr.getSnappedPoint();
ld.nodeId = qr.getClosestNode();
ld.location = new ResolvedLocation(new Coordinate(pt.getLon(), pt.getLat()), resolveNames ? qr.getClosestEdge().getName() : null, qr.getQueryDistance());
queryResults.add(qr);
} else {
ld.nodeId = -1;
}
locationCache.put(p, ld);
}
}
}
protected EdgeFilter getSnapFilter(String profileName) {
EdgeFilter defaultSnapFilter = new DefaultSnapFilter(weighting, this.graphHopperStorage.getEncodingManager().getBooleanEncodedValue(Subnetwork.key(profileName)));
//TODO when Matrix supports additional parameters such as avoidables in the future, the corresponding filters need to be added here for snapping
// if (edgeFilterFactory != null)
// return edgeFilterFactory.createEdgeFilter(request.getAdditionalHints(), weighting.getFlagEncoder(), ghStorage, defaultSnapFilter);
return defaultSnapFilter;
}
private MatrixLocations createLocations(Coordinate[] coords) throws Exception {
MatrixLocations mlRes = new MatrixLocations(coords.length);
for (int i = 0; i < coords.length; i++) {
Coordinate p = coords[i];
LocationEntry ld = locationCache.get(p);
if (ld != null)
mlRes.setData(i, ld.nodeId == -1 ? -1 : ld.snap.getClosestNode(), ld.location);
else
throw new Exception("Oops!");
}
return mlRes;
}
static class LocationEntry {
private int nodeId;
private ResolvedLocation location;
private Snap snap;
public int getNodeId() {
return nodeId;
}
public void setNodeId(int nodeId) {
this.nodeId = nodeId;
}
public ResolvedLocation getLocation() {
return location;
}
public void setLocation(ResolvedLocation location) {
this.location = location;
}
}
}