-
Notifications
You must be signed in to change notification settings - Fork 1k
/
OsmBoardingLocationsModule.java
215 lines (196 loc) · 8.57 KB
/
OsmBoardingLocationsModule.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
package org.opentripplanner.graph_builder.module;
import jakarta.inject.Inject;
import java.util.List;
import java.util.stream.Collectors;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Envelope;
import org.opentripplanner.framework.geometry.GeometryUtils;
import org.opentripplanner.framework.geometry.SphericalDistanceLibrary;
import org.opentripplanner.framework.i18n.LocalizedString;
import org.opentripplanner.graph_builder.model.GraphBuilderModule;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.routing.graph.index.StreetIndex;
import org.opentripplanner.routing.linking.LinkingDirection;
import org.opentripplanner.routing.linking.VertexLinker;
import org.opentripplanner.street.model.StreetTraversalPermission;
import org.opentripplanner.street.model.edge.AreaEdge;
import org.opentripplanner.street.model.edge.BoardingLocationToStopLink;
import org.opentripplanner.street.model.edge.Edge;
import org.opentripplanner.street.model.edge.NamedArea;
import org.opentripplanner.street.model.edge.StreetEdge;
import org.opentripplanner.street.model.edge.StreetEdgeBuilder;
import org.opentripplanner.street.model.edge.StreetTransitStopLink;
import org.opentripplanner.street.model.vertex.OsmBoardingLocationVertex;
import org.opentripplanner.street.model.vertex.StreetVertex;
import org.opentripplanner.street.model.vertex.TransitStopVertex;
import org.opentripplanner.street.model.vertex.VertexFactory;
import org.opentripplanner.street.search.TraverseMode;
import org.opentripplanner.street.search.TraverseModeSet;
import org.opentripplanner.transit.service.TransitModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This module takes advantage of the fact that in some cities, an authoritative linking location
* for GTFS stops is provided by tags in the OSM data.
* <p>
* When OSM data is being loaded, certain entities that represent transit stops are made into
* {@link OsmBoardingLocationVertex} instances. In some cities, these nodes have a ref=* tag which
* gives the corresponding GTFS stop ID for the stop but the exact tag name is configurable. See
* <a href="https://wiki.openstreetmap.org/wiki/Key:public_transport">the OSM wiki page</a>.
* <p>
* This module will attempt to link all transit stops and platforms to such nodes or way centroids
* in the OSM data, based on the stop ID or stop code and ref tag. It is run before the main transit
* stop linker, and if no linkage was created here, the main linker should create one based on
* distance or other heuristics.
*/
public class OsmBoardingLocationsModule implements GraphBuilderModule {
private static final Logger LOG = LoggerFactory.getLogger(OsmBoardingLocationsModule.class);
private final double searchRadiusDegrees = SphericalDistanceLibrary.metersToDegrees(250);
private final Graph graph;
private final TransitModel transitModel;
private final VertexFactory vertexFactory;
private VertexLinker linker;
@Inject
public OsmBoardingLocationsModule(Graph graph, TransitModel transitModel) {
this.graph = graph;
this.transitModel = transitModel;
this.vertexFactory = new VertexFactory(graph);
}
@Override
public void buildGraph() {
LOG.info("Improving boarding locations by checking OSM entities...");
StreetIndex streetIndex = graph.getStreetIndexSafe(transitModel.getStopModel());
this.linker = streetIndex.getVertexLinker();
int successes = 0;
for (TransitStopVertex ts : graph.getVerticesOfType(TransitStopVertex.class)) {
// if the street is already linked there is no need to linked it again,
// could happened if using the prune isolated island
boolean alreadyLinked = false;
for (Edge e : ts.getOutgoing()) {
if (e instanceof StreetTransitStopLink) {
alreadyLinked = true;
break;
}
}
if (alreadyLinked) continue;
// only connect transit stops that are not part of a pathway network
if (!ts.hasPathways()) {
if (!connectVertexToStop(ts, streetIndex)) {
LOG.debug("Could not connect {} at {}", ts.getStop().getCode(), ts.getCoordinate());
} else {
successes++;
}
}
}
LOG.info("Found {} OSM references which match a stop's id or code", successes);
}
@Override
public void checkInputs() {
//no inputs
}
private boolean connectVertexToStop(TransitStopVertex ts, StreetIndex index) {
var stopCode = ts.getStop().getCode();
var stopId = ts.getStop().getId().getId();
Envelope envelope = new Envelope(ts.getCoordinate());
double xscale = Math.cos(ts.getCoordinate().y * Math.PI / 180);
envelope.expandBy(searchRadiusDegrees / xscale, searchRadiusDegrees);
// if the boarding location is an OSM node it's generated in the OSM processing step but we need
// link it here
var nearbyBoardingLocations = index
.getVerticesForEnvelope(envelope)
.stream()
.filter(OsmBoardingLocationVertex.class::isInstance)
.map(OsmBoardingLocationVertex.class::cast)
.collect(Collectors.toSet());
for (var boardingLocation : nearbyBoardingLocations) {
if (
(stopCode != null && boardingLocation.references.contains(stopCode)) ||
boardingLocation.references.contains(stopId)
) {
if (!boardingLocation.isConnectedToStreetNetwork()) {
linker.linkVertexPermanently(
boardingLocation,
new TraverseModeSet(TraverseMode.WALK),
LinkingDirection.BOTH_WAYS,
(osmBoardingLocationVertex, splitVertex) -> {
if (osmBoardingLocationVertex == splitVertex) {
return List.of();
}
// the OSM boarding location vertex is not connected to the street network, so we
// need to link it first
return List.of(
linkBoardingLocationToStreetNetwork(boardingLocation, splitVertex),
linkBoardingLocationToStreetNetwork(splitVertex, boardingLocation)
);
}
);
}
linkBoardingLocationToStop(ts, stopCode, boardingLocation);
return true;
}
}
// if the boarding location is an OSM way (an area) then we are generating the vertex here and
// use the AreaEdgeList to link it to the correct vertices of the platform edge
var nearbyEdgeLists = index
.getEdgesForEnvelope(envelope)
.stream()
.filter(AreaEdge.class::isInstance)
.map(AreaEdge.class::cast)
.map(AreaEdge::getArea)
.collect(Collectors.toSet());
// Iterate over all nearby areas representing transit stops in OSM, linking to them if they have a stop code or id
// in their ref= tag that matches the GTFS stop code of this StopVertex.
for (var edgeList : nearbyEdgeLists) {
if (
(stopCode != null && edgeList.references.contains(stopCode)) ||
edgeList.references.contains(stopId)
) {
var name = edgeList
.getAreas()
.stream()
.findFirst()
.map(NamedArea::getName)
.orElse(new LocalizedString("name.platform"));
var label = "platform-centroid/%s".formatted(ts.getStop().getId().toString());
var centroid = edgeList.getGeometry().getCentroid();
var boardingLocation = vertexFactory.osmBoardingLocation(
new Coordinate(centroid.getX(), centroid.getY()),
label,
edgeList.references,
name
);
linker.addPermanentAreaVertex(boardingLocation, edgeList);
linkBoardingLocationToStop(ts, stopCode, boardingLocation);
return true;
}
}
return false;
}
private StreetEdge linkBoardingLocationToStreetNetwork(StreetVertex from, StreetVertex to) {
var line = GeometryUtils.makeLineString(List.of(from.getCoordinate(), to.getCoordinate()));
return new StreetEdgeBuilder<>()
.withFromVertex(from)
.withToVertex(to)
.withGeometry(line)
.withName(new LocalizedString("name.platform"))
.withMeterLength(SphericalDistanceLibrary.length(line))
.withPermission(StreetTraversalPermission.PEDESTRIAN_AND_BICYCLE)
.withBack(false)
.buildAndConnect();
}
private void linkBoardingLocationToStop(
TransitStopVertex ts,
String stopCode,
OsmBoardingLocationVertex boardingLocation
) {
BoardingLocationToStopLink.createBoardingLocationToStopLink(ts, boardingLocation);
BoardingLocationToStopLink.createBoardingLocationToStopLink(boardingLocation, ts);
LOG.debug(
"Connected {} ({}) to {} at {}",
ts,
stopCode,
boardingLocation.getLabel(),
boardingLocation.getCoordinate()
);
}
}