Permalink
Browse files

fix #947

improved_logic_in_the_prune_isolated_islands_considering_islands_with_stops
  • Loading branch information...
1 parent 2dbc0a6 commit dcfe6b51873c51f5700cd795dbd2d1f861394634 @benmoovit committed Dec 31, 2012
@@ -34,8 +34,10 @@
</bean>
<bean id="floatingIslands" class="org.opentripplanner.graph_builder.impl.PruneFloatingIslands">
- <property name="maxIslandSize" value="5"/>
- <property name="islandLogFile" value="c:\isolatedTest"/>
+ <property name="maxIslandSize" value="1000 "/>
+ <property name="islandLogFile" value="{graphPath}/island.csv"/>
+ <property name="islandWithStopMaxSize" value="20"/>
+ <property name="transitToStreetNetwork" ref ="transitStreetLink"/>
</bean>
<bean id="transitStreetLink" class="org.opentripplanner.graph_builder.impl.TransitToStreetNetworkGraphBuilderImpl" />
@@ -48,9 +50,11 @@
<!-- GTFS comes before OSM, because we use the loaded set of stops to determine our OSM coverage -->
<ref bean="gtfsBuilder" />
<ref bean="osmBuilder" />
+ <!-- the transmit link should became before the floating islands in order to find islands with stops-->
<ref bean="transitStreetLink" />
- <ref bean="optimizeTransit" />
<ref bean="floatingIslands" />
+ <ref bean="optimizeTransit" />
+
</list>
</property>
</bean>
@@ -26,20 +26,25 @@
import org.opentripplanner.common.StreetUtils;
import org.opentripplanner.graph_builder.services.GraphBuilder;
import org.opentripplanner.routing.graph.Graph;
+import org.opentripplanner.routing.services.StreetVertexIndexService;
import org.slf4j.*;
public class PruneFloatingIslands implements GraphBuilder {
private static org.slf4j.Logger _log = LoggerFactory.getLogger(PruneFloatingIslands.class);
- @Getter
@Setter
private int maxIslandSize = 40;
- @Getter
+ @Setter
+ private int islandWithStopMaxSize = 5;
+
@Setter
private String islandLogFile = "";
+ @Setter
+ private TransitToStreetNetworkGraphBuilderImpl transitToStreetNetwork;
+
public List<String> provides() {
return Collections.emptyList();
}
@@ -50,7 +55,12 @@
@Override
public void buildGraph(Graph graph, HashMap<Class<?>, Object> extra) {
- StreetUtils.pruneFloatingIslands(graph, maxIslandSize, createLogger());
+ if(graph.getService(StreetVertexIndexService.class) == null) {
+ //TODO:log and throw error
+ }
+ StreetUtils.pruneFloatingIslands(graph, maxIslandSize, islandWithStopMaxSize, createLogger());
+ //reconnect stops on small islands (that removed)
+ transitToStreetNetwork.buildGraph(graph,extra);
}
@Override
@@ -69,7 +79,6 @@ private String createLogger() {
return null;
}
return "islands";
-
}
}
@@ -13,52 +13,53 @@
package org.opentripplanner.common;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.Map;
-import java.util.Queue;
+import java.util.*;
import com.vividsolutions.jts.algorithm.ConvexHull;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
+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.services.StreetVertexIndexService;
+import org.opentripplanner.routing.services.TransitIndexService;
import org.opentripplanner.routing.vertextype.StreetVertex;
+import org.opentripplanner.routing.vertextype.TransitStop;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class StreetUtils {
private static Logger _log = LoggerFactory.getLogger(StreetUtils.class);
- //private static Logger _islandLog = LoggerFactory.getLogger("islands");
private static int islandCounter = 0;
- public static void pruneFloatingIslands(Graph graph, int maxIslandSize, String islandLogName) {
+ public static void pruneFloatingIslands(Graph graph, int maxIslandSize, int islandWithStopMaxSize, String islandLogName) {
_log.debug("pruning");
+ StreetVertexIndexService streetVertexIndex = graph.getService(StreetVertexIndexService.class);
+
Logger islandLog = null;
if(islandLogName != null){
islandLog = LoggerFactory.getLogger(islandLogName);
- islandLog.info(String.format("%s\t%s\t%s","id","size","wkt"));
+ islandLog.info(String.format("%s\t%s\t%s\t%s\t%s","id","stopCount", "streetCount","wkt" ,"hadRemoved"));
}
- Map<Vertex, HashSet<Vertex>> subgraphs = new HashMap<Vertex, HashSet<Vertex>>();
+ 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, TraverseMode.TRANSIT));
+ RoutingRequest options = new RoutingRequest(new TraverseModeSet(TraverseMode.WALK));
for (Vertex gv : graph.getVertices()) {
if (!(gv instanceof StreetVertex)) {
@@ -67,7 +68,7 @@ public static void pruneFloatingIslands(Graph graph, int maxIslandSize, String i
State s0 = new State(gv, options);
for (Edge e : gv.getOutgoing()) {
Vertex in = gv;
- if (!(e instanceof StreetEdge)) {
+ if (!(e instanceof StreetEdge || e instanceof StreetTransitLink)) {
continue;
}
State s1 = e.traverse(s0);
@@ -92,7 +93,7 @@ public static void pruneFloatingIslands(Graph graph, int maxIslandSize, String i
}
}
- ArrayList<HashSet<Vertex>> islands = new ArrayList<HashSet<Vertex>>();
+ ArrayList<Subgraph> islands = new ArrayList<Subgraph>();
/* associate each node with a subgraph */
for (Vertex gv : graph.getVertices()) {
if (!(gv instanceof StreetVertex)) {
@@ -105,30 +106,45 @@ public static void pruneFloatingIslands(Graph graph, int maxIslandSize, String i
if (!neighborsForVertex.containsKey(vertex)) {
continue;
}
- HashSet<Vertex> subgraph = computeConnectedSubgraph(neighborsForVertex, vertex);
- for (Vertex subnode : subgraph) {
- subgraphs.put(subnode, subgraph);
+ 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);
}
- islands.add(subgraph);
}
_log.info(islands.size() + " sub graphs found");
- /* remove all tiny subgraphs */
- for (HashSet<Vertex> island : islands) {
- if (islandLog != null) {
- WriteNodesInSubGraph(island, islandLog);
+ /* 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 (island.size() < maxIslandSize) {
- _log.warn(graph.addBuilderAnnotation(new GraphConnectivity(island.iterator().next(), island.size())));
- depedestrianizeOrRemove(graph, island);
+ 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, Collection<Vertex> vertices) {
- for (Vertex v : vertices) {
+ 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) {
@@ -144,48 +160,51 @@ private static void depedestrianizeOrRemove(Graph graph, Collection<Vertex> vert
}
}
}
- for (Vertex v : vertices) {
+
+ 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 HashSet<Vertex> computeConnectedSubgraph(
+ private static Subgraph computeConnectedSubgraph(
Map<Vertex, ArrayList<Vertex>> neighborsForVertex, Vertex startVertex) {
- HashSet<Vertex> subgraph = new HashSet<Vertex>();
+ 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.add(neighbor);
+ subgraph.addVertex(neighbor);
q.add(neighbor);
}
}
}
return subgraph;
+// if(subgraph.size()>1) return subgraph;
+// return null;
}
- private static void WriteNodesInSubGraph(HashSet<Vertex> subgraph, Logger islandLog){
-
- Coordinate[] subGraphCoor = new Coordinate[subgraph.size()];
- int i = 0;
- for(Vertex v:subgraph){
-
- subGraphCoor[i] = v.getCoordinate();
- i++;
- }
- i++;
- ConvexHull convexHull = new ConvexHull(subGraphCoor, new GeometryFactory());
- Geometry convexHullGeom = convexHull.getConvexHull();
- if(convexHullGeom.getGeometryType().equalsIgnoreCase("LINESTRING")){
+ 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%s",
- islandCounter,subgraph.size(),convexHullGeom));
+ islandLog.info(String.format("%d\t%d\t%d\t%s\t%b",
+ islandCounter,subgraph.stopSize(), subgraph.streetSize(), convexHullGeom, hadRemoved));
islandCounter++;
}
-
}
@@ -0,0 +1,85 @@
+package org.opentripplanner.common.geometry;
+
+import com.vividsolutions.jts.algorithm.ConvexHull;
+import com.vividsolutions.jts.geom.Coordinate;
+import com.vividsolutions.jts.geom.Geometry;
+import com.vividsolutions.jts.geom.GeometryFactory;
+import lombok.Getter;
+import org.opentripplanner.routing.graph.Vertex;
+import org.opentripplanner.routing.vertextype.TransitVertex;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+/**
+ * Created with IntelliJ IDEA.
+ * User: Ben
+ * Date: 27/12/12
+ * Time: 16:32
+ * To change this template use File | Settings | File Templates.
+ */
+public class Subgraph {
+
+ private Set<Vertex> streetVertexSet;
+ private Set<Vertex> stopsVertexSet;
+ private ConvexHull convexHull = null;
+ private ArrayList<Coordinate> vertexCoord;
+ private Geometry convexHullAsGeom = null;
+ private boolean addNewVertex = true;
+
+ public Subgraph(){
+ streetVertexSet = new HashSet<Vertex>();
+ stopsVertexSet = new HashSet<Vertex>();
+ vertexCoord = new ArrayList<Coordinate>();
+ }
+
+ public void addVertex(Vertex vertex){
+ if(vertex instanceof TransitVertex){
+ stopsVertexSet.add(vertex);
+ }else{
+ streetVertexSet.add(vertex);
+ }
+ addNewVertex = true;
+ vertexCoord.add(vertex.getCoordinate());
+ }
+
+ public boolean contains(Vertex vertex){
+ return (streetVertexSet.contains(vertex) || stopsVertexSet.contains(vertex));
+ }
+
+ public boolean containsStreet(Vertex vertex){
+ return streetVertexSet.contains(vertex);
+ }
+
+ public int streetSize(){
+ return streetVertexSet.size();
+ }
+
+ public int stopSize(){
+ return stopsVertexSet.size();
+ }
+
+ public Vertex getRepresentativeVertex(){
+ //TODO this is not very smart but good enough at the moment
+ return streetVertexSet.iterator().next();
+ }
+
+ public Iterator<Vertex> streetIterator() {
+ return streetVertexSet.iterator();
+ }
+
+ public Iterator<Vertex> stopIterator() {
+ return stopsVertexSet.iterator();
+ }
+
+ public Geometry getConvexHull(){
+ if(addNewVertex) {
+ convexHull = new ConvexHull(vertexCoord.toArray(new Coordinate[vertexCoord.size()]), new GeometryFactory());
+ convexHullAsGeom = convexHull.getConvexHull();
+ addNewVertex = false;
+ }
+ return convexHullAsGeom;
+ }
+}
Oops, something went wrong.

0 comments on commit dcfe6b5

Please sign in to comment.