/** * Extract the major roads from the road network * * @return a connected network of major roads */ public Network extractMajorRoads() { Network majorRoads = new Network(); // go through all nodes for (Object o : roads.getAllNodes()) { GeoNode n = (GeoNode) o; // go through all edges for (Object p : roads.getEdgesOut(n)) { sim.field.network.Edge e = (sim.field.network.Edge) p; // String type = ((MasonGeometry)e.info).getStringAttribute("class"); // save major roads // if(type.equals("major")) majorRoads.addEdge(e.from(), e.to(), e.info); } } // merge the major roads into a connected component NetworkUtilities.attachUnconnectedComponents(majorRoads, roads); return majorRoads; }
/** * Make sure the network doesn't have any problems * * @param n - the network to be tested */ static void testNetworkForIssues(Network n) { System.out.println("testing"); for (Object o : n.allNodes) { GeoNode node = (GeoNode) o; for (Object p : n.getEdgesOut(node)) { sim.field.network.Edge e = (sim.field.network.Edge) p; LineString ls = (LineString) ((MasonGeometry) e.info).geometry; Coordinate c1 = ls.getCoordinateN(0); Coordinate c2 = ls.getCoordinateN(ls.getNumPoints() - 1); GeoNode g1 = (GeoNode) e.getFrom(); GeoNode g2 = (GeoNode) e.getTo(); if (c1.distance(g1.geometry.getCoordinate()) > 1) System.out.println("found you"); if (c2.distance(g2.geometry.getCoordinate()) > 1) System.out.println("found you"); } } }
/** * Connect the GeoNode to the given subnetwork using the complete road network * * @param n - the target node * @param subNetwork - the existing subnetwork */ void connectToMajorNetwork(GeoNode n, Network subNetwork) { try { Bag subNetNodes; subNetNodes = (Bag) subNetwork.allNodes.clone(); // find a path using the whole set of roads in the environment AStar pathfinder = new AStar(); ArrayList<Edge> edges = pathfinder.astarPath(n, new ArrayList<GeoNode>(subNetNodes), roads); if (edges == null) return; // maybe no such path exists! // otherwise, add the edges into the subnetwork for (Edge e : edges) { GeoNode a = (GeoNode) e.getFrom(), b = (GeoNode) e.getTo(); if (!subNetwork.nodeExists(a) || !subNetwork.nodeExists(b)) subNetwork.addEdge(a, b, e.info); } } catch (CloneNotSupportedException e1) { e1.printStackTrace(); } }