-
Notifications
You must be signed in to change notification settings - Fork 376
/
ExportRequest.java
148 lines (123 loc) 路 6.02 KB
/
ExportRequest.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
package org.heigit.ors.export;
import com.graphhopper.GraphHopper;
import com.graphhopper.routing.util.AccessFilter;
import com.graphhopper.routing.util.FlagEncoder;
import com.graphhopper.routing.weighting.Weighting;
import com.graphhopper.storage.Graph;
import com.graphhopper.storage.NodeAccess;
import com.graphhopper.storage.index.LocationIndex;
import com.graphhopper.util.EdgeExplorer;
import com.graphhopper.util.EdgeIterator;
import com.graphhopper.util.EdgeIteratorState;
import com.graphhopper.util.PMap;
import com.graphhopper.util.shapes.BBox;
import org.apache.log4j.Logger;
import org.heigit.ors.common.Pair;
import org.heigit.ors.common.ServiceRequest;
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.WheelchairAttributes;
import org.heigit.ors.routing.graphhopper.extensions.storages.GraphStorageUtils;
import org.heigit.ors.routing.graphhopper.extensions.storages.OsmIdGraphStorage;
import org.heigit.ors.routing.graphhopper.extensions.storages.WheelchairAttributesGraphStorage;
import org.heigit.ors.util.ProfileTools;
import org.locationtech.jts.geom.Coordinate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class ExportRequest extends ServiceRequest {
private static final Logger LOGGER = Logger.getLogger(ExportRequest.class);
private BBox boundingBox;
private int profileType = -1;
private boolean debug;
public BBox getBoundingBox() {
return this.boundingBox;
}
public void setBoundingBox(BBox bbox) {
this.boundingBox = bbox;
}
public int getProfileType() {
return profileType;
}
public void setProfileType(int profileType) {
this.profileType = profileType;
}
public void setDebug(boolean debug) {
this.debug = debug;
}
public boolean debug() {
return debug;
}
public ExportResult computeExport(RoutingProfile routingProfile) {
ExportResult res = new ExportResult();
GraphHopper gh = routingProfile.getGraphhopper();
String encoderName = RoutingProfileType.getEncoderName(getProfileType());
Graph graph = gh.getGraphHopperStorage().getBaseGraph();
PMap hintsMap = new PMap();
int weightingMethod = WeightingMethod.FASTEST;
ProfileTools.setWeightingMethod(hintsMap, weightingMethod, getProfileType(), false);
String profileName = ProfileTools.makeProfileName(encoderName, hintsMap.getString("weighting_method", ""), false);
Weighting weighting = gh.createWeighting(gh.getProfile(profileName), hintsMap);
FlagEncoder flagEncoder = gh.getEncodingManager().getEncoder(encoderName);
EdgeExplorer explorer = graph.createEdgeExplorer(AccessFilter.outEdges(flagEncoder.getAccessEnc()));
// filter graph for nodes in Bounding Box
LocationIndex index = gh.getLocationIndex();
NodeAccess nodeAccess = graph.getNodeAccess();
BBox bbox = getBoundingBox();
ArrayList<Integer> nodesInBBox = new ArrayList<>();
index.query(bbox, edgeId -> {
// According to GHUtility.getEdgeFromEdgeKey, edgeIds are calculated as edgeKey/2.
EdgeIteratorState edge = graph.getEdgeIteratorStateForKey(edgeId * 2);
int baseNode = edge.getBaseNode();
int adjNode = edge.getAdjNode();
if (bbox.contains(nodeAccess.getLat(baseNode), nodeAccess.getLon(baseNode))) {
nodesInBBox.add(baseNode);
}
if (bbox.contains(nodeAccess.getLat(adjNode), nodeAccess.getLon(adjNode))) {
nodesInBBox.add(adjNode);
}
});
LOGGER.debug("Found %d nodes in bbox.".formatted(nodesInBBox.size()));
if (nodesInBBox.isEmpty()) {
// without nodes, no export can be calculated
res.setWarning(new ExportWarning(ExportWarning.EMPTY_BBOX));
return res;
}
// calculate node coordinates
for (int from : nodesInBBox) {
Coordinate coord = new Coordinate(nodeAccess.getLon(from), nodeAccess.getLat(from));
res.addLocation(from, coord);
EdgeIterator iter = explorer.setBaseNode(from);
while (iter.next()) {
int to = iter.getAdjNode();
if (nodesInBBox.contains(to)) {
double weight = weighting.calcEdgeWeight(iter, false, EdgeIterator.NO_EDGE);
Pair<Integer, Integer> p = new Pair<>(from, to);
res.addEdge(p, weight);
if (debug()) {
Map<String, Object> extra = new HashMap<>();
extra.put("edge_id", iter.getEdge());
WheelchairAttributesGraphStorage storage = GraphStorageUtils.getGraphExtension(gh.getGraphHopperStorage(), WheelchairAttributesGraphStorage.class);
if (storage != null) {
WheelchairAttributes attributes = new WheelchairAttributes();
byte[] buffer = new byte[WheelchairAttributesGraphStorage.BYTE_COUNT];
storage.getEdgeValues(iter.getEdge(), attributes, buffer);
if (attributes.hasValues()) {
extra.put("incline", attributes.getIncline());
extra.put("surface_quality_known", attributes.isSurfaceQualityKnown());
extra.put("suitable", attributes.isSuitable());
}
}
OsmIdGraphStorage storage2 = GraphStorageUtils.getGraphExtension(gh.getGraphHopperStorage(), OsmIdGraphStorage.class);
if (storage2 != null) {
extra.put("osm_id", storage2.getEdgeValue(iter.getEdge()));
}
res.addEdgeExtra(p, extra);
}
}
}
}
return res;
}
}