-
Notifications
You must be signed in to change notification settings - Fork 1k
/
StreetUtils.java
202 lines (183 loc) · 8.29 KB
/
StreetUtils.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
/* This program 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 3 of
the License, or (at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package org.opentripplanner.common;
import java.util.*;
import com.vividsolutions.jts.geom.Geometry;
import org.opentripplanner.common.geometry.Subgraph;
import org.opentripplanner.gbannotation.GraphConnectivity;
import org.opentripplanner.routing.core.RoutingRequest;
import org.opentripplanner.routing.core.State;
import org.opentripplanner.routing.core.TraverseMode;
import org.opentripplanner.routing.core.TraverseModeSet;
import org.opentripplanner.routing.edgetype.PlainStreetEdge;
import org.opentripplanner.routing.edgetype.StreetEdge;
import org.opentripplanner.routing.edgetype.StreetTransitLink;
import org.opentripplanner.routing.edgetype.StreetTraversalPermission;
import org.opentripplanner.routing.graph.Edge;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.routing.graph.Vertex;
import org.opentripplanner.routing.vertextype.StreetVertex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class StreetUtils {
private static Logger _log = LoggerFactory.getLogger(StreetUtils.class);
private static int islandCounter = 0;
public static void pruneFloatingIslands(Graph graph, int maxIslandSize, int islandWithStopMaxSize, String islandLogName) {
_log.debug("pruning");
Logger islandLog = null;
if(islandLogName != null){
islandLog = LoggerFactory.getLogger(islandLogName);
islandLog.info(String.format("%s\t%s\t%s\t%s\t%s","id","stopCount", "streetCount","wkt" ,"hadRemoved"));
}
Map<Vertex, Subgraph> subgraphs = new HashMap<Vertex, Subgraph>();
Map<Vertex, ArrayList<Vertex>> neighborsForVertex = new HashMap<Vertex, ArrayList<Vertex>>();
// RoutingRequest options = new RoutingRequest(new TraverseModeSet(TraverseMode.WALK, TraverseMode.TRANSIT));
RoutingRequest options = new RoutingRequest(new TraverseModeSet(TraverseMode.WALK));
for (Vertex gv : graph.getVertices()) {
if (!(gv instanceof StreetVertex)) {
continue;
}
State s0 = new State(gv, options);
for (Edge e : gv.getOutgoing()) {
Vertex in = gv;
if (!(e instanceof StreetEdge || e instanceof StreetTransitLink)) {
continue;
}
State s1 = e.traverse(s0);
if (s1 == null) {
continue;
}
Vertex out = s1.getVertex();
ArrayList<Vertex> vertexList = neighborsForVertex.get(in);
if (vertexList == null) {
vertexList = new ArrayList<Vertex>();
neighborsForVertex.put(in, vertexList);
}
vertexList.add(out);
vertexList = neighborsForVertex.get(out);
if (vertexList == null) {
vertexList = new ArrayList<Vertex>();
neighborsForVertex.put(out, vertexList);
}
vertexList.add(in);
}
}
ArrayList<Subgraph> islands = new ArrayList<Subgraph>();
/* associate each node with a subgraph */
for (Vertex gv : graph.getVertices()) {
if (!(gv instanceof StreetVertex)) {
continue;
}
Vertex vertex = gv;
if (subgraphs.containsKey(vertex)) {
continue;
}
if (!neighborsForVertex.containsKey(vertex)) {
continue;
}
Subgraph subgraph = computeConnectedSubgraph(neighborsForVertex, vertex);
if (subgraph != null){
for (Iterator<Vertex> vIter = subgraph.streetIterator(); vIter.hasNext();) {
Vertex subnode = vIter.next();
subgraphs.put(subnode, subgraph);
}
islands.add(subgraph);
}
}
_log.info(islands.size() + " sub graphs found");
/* remove all tiny subgraphs and large subgraphs without stops */
for (Subgraph island : islands) {
boolean hadRemoved = false;
if(island.stopSize() > 0){
//for islands with stops
if (island.streetSize() < islandWithStopMaxSize) {
depedestrianizeOrRemove(graph, island);
hadRemoved = true;
}
}else{
//for islands without stops
if (island.streetSize() < maxIslandSize) {
depedestrianizeOrRemove(graph, island);
hadRemoved = true;
}
}
if (islandLog != null) {
WriteNodesInSubGraph(island, islandLog, hadRemoved);
}
}
if (graph.removeEdgelessVertices() > 0) {
_log.warn("Removed edgeless vertices after pruning islands.");
}
}
private static void depedestrianizeOrRemove(Graph graph, Subgraph island) {
//iterate over the street vertex of the subgraph
for (Iterator<Vertex> vIter = island.streetIterator(); vIter.hasNext();) {
Vertex v = vIter.next();
Collection<Edge> outgoing = new ArrayList<Edge>(v.getOutgoing());
for (Edge e : outgoing) {
if (e instanceof PlainStreetEdge) {
PlainStreetEdge pse = (PlainStreetEdge) e;
StreetTraversalPermission permission = pse.getPermission();
permission = permission.remove(StreetTraversalPermission.PEDESTRIAN);
permission = permission.remove(StreetTraversalPermission.BICYCLE);
if (permission == StreetTraversalPermission.NONE) {
pse.detach();
} else {
pse.setPermission(permission);
}
}
}
}
for (Iterator<Vertex> vIter = island.streetIterator(); vIter.hasNext();) {
Vertex v = vIter.next();
if (v.getDegreeOut() + v.getDegreeIn() == 0) {
graph.remove(v);
}
}
//remove street conncetion form
for (Iterator<Vertex> vIter = island.stopIterator(); vIter.hasNext();) {
Vertex v = vIter.next();
Collection<Edge> outgoing = new ArrayList<Edge>(v.getOutgoing());
for (Edge e : outgoing) {
if (e instanceof StreetTransitLink) {
}
}
}
_log.warn(graph.addBuilderAnnotation(new GraphConnectivity(island.getRepresentativeVertex(), island.streetSize())));
}
private static Subgraph computeConnectedSubgraph(
Map<Vertex, ArrayList<Vertex>> neighborsForVertex, Vertex startVertex) {
Subgraph subgraph = new Subgraph();
Queue<Vertex> q = new LinkedList<Vertex>();
q.add(startVertex);
while (!q.isEmpty()) {
Vertex vertex = q.poll();
for (Vertex neighbor : neighborsForVertex.get(vertex)) {
if (!subgraph.contains(neighbor)) {
subgraph.addVertex(neighbor);
q.add(neighbor);
}
}
}
return subgraph;
// if(subgraph.size()>1) return subgraph;
// return null;
}
private static void WriteNodesInSubGraph(Subgraph subgraph, Logger islandLog, boolean hadRemoved){
Geometry convexHullGeom = subgraph.getConvexHull();
if(!convexHullGeom.getGeometryType().equalsIgnoreCase("POLYGON")){
convexHullGeom = convexHullGeom.buffer(0.0001,5);
}
islandLog.info(String.format("%d\t%d\t%d\t%s\t%b",
islandCounter,subgraph.stopSize(), subgraph.streetSize(), convexHullGeom, hadRemoved));
islandCounter++;
}
}