/** * Adds an Edge, DirectedEdges, and Nodes for the given LineString representation of an edge. * Snaps all vertices according to GraphParams.GLOBAL_SNAP_DIST * * @param lineString * @param id : the id coming out of OSM */ public void addLineString( LineString lineString, int id, short envType, short cykType, double groenM) { distancesCalculated = false; if (lineString.isEmpty()) { return; } if (lineString.getCoordinates().length < 2) { System.exit(1); } Coordinate[] coordinates = lineString.getCoordinates(); modifyEnvelope(coordinates); if (GraphParams.getInstance().getSnap()) { double sd = GraphParams.getInstance().getSnapDistance(); for (Coordinate c : coordinates) { c.x = c.x - (c.x % sd); c.y = c.y - (c.y % sd); } } Edge edge = getLineMergeGraphH4cked().addEdge(lineString); edgeId__edge.put(id, edge); if (edge != null) { // edge might not have been added because of coinciding coordinates if (lineString.getUserData() == null) lineString.setUserData(new HashMap<String, Object>(3)); @SuppressWarnings("unchecked") HashMap<String, Object> userdata = (HashMap<String, Object>) lineString.getUserData(); // HashMap<String, Object> hm = new HashMap<String, Object>(); userdata.put("id", id); userdata.put("et", envType); userdata.put("ct", cykType); userdata.put("gm", groenM); // groenM userdata.put("geom", lineString); edge.setData(userdata); // add edge data to the spatial index } }
/* * removes an edge */ public void removeEdge(Edge edge) { if (!edge.isRemoved()) { getLineMergeGraphH4cked().remove(edge); } }
/** * Split the graph by splitting an Edge at a point into 2 Edges * * @param c coordinates at which to split the graph * @param iSplit split index (the i-th split point, used to create the new edge IDs) */ public void splitGraphAtPoint(Coordinate c, int iSplit) { Edge nearestEdge = null; float nearestDist = kNearestEdgeDistance; while (nearestEdge == null && nearestDist / kNearestEdgeDistance < 20.) { nearestEdge = findNearestEdge(c, nearestDist); if (nearestEdge == null) { nearestDist *= 2.; logger.info("Increasing kNearestEdgeDistance: " + nearestDist); } } // get the projected point on the nearest edge PointPairDistance ppd = new PointPairDistance(); @SuppressWarnings("rawtypes") LineString nearestLineString = (LineString) ((HashMap) nearestEdge.getData()).get("geom"); EuclideanDistanceToPoint.computeDistance(nearestLineString, c, ppd); Coordinate resultcoord = null; for (Coordinate cc : ppd.getCoordinates()) { // System.out.println(cc); if (cc.equals(c) == false) { resultcoord = cc; } } // let us loop through the vertices Coordinate[] nearestLineStringCoordinates = nearestLineString.getCoordinates(); ArrayList<Coordinate> fromVertices = new ArrayList<Coordinate>(); ArrayList<Coordinate> toVertices = new ArrayList<Coordinate>(); fromVertices.add(nearestLineStringCoordinates[0]); boolean isAfter = false; for (int i = 1; i < nearestLineStringCoordinates.length; i++) { Coordinate pt0 = nearestLineStringCoordinates[i - 1]; Coordinate pt1 = nearestLineStringCoordinates[i]; // let us build a segment between 2 nodes Coordinate[] segmentCoords = {pt0, pt1}; LineString segment = fact.createLineString(segmentCoords); // let us check whether the projected point (resultcoord) is on the node if (fact.createPoint(resultcoord).distance(segment) < SPLITSNAPDISTANCE) { // point within, let us split fromVertices.add(resultcoord); toVertices.add(resultcoord); isAfter = true; } else { if (!isAfter) { fromVertices.add(nearestLineStringCoordinates[i]); } else { toVertices.add(nearestLineStringCoordinates[i]); } } } // if (nearestLineStringCoordinates.length==2) { toVertices.add(nearestLineStringCoordinates[nearestLineStringCoordinates.length - 1]); // } // TODO: generate nice edge ids here int id1 = -2 * iSplit - 1; int id2 = -2 * iSplit - 2; // replace straight lines with lines including vertices Coordinate[] fromArray = new Coordinate[fromVertices.size()]; Coordinate[] toArray = new Coordinate[toVertices.size()]; fromVertices.toArray(fromArray); toVertices.toArray(toArray); addLineString(fact.createLineString(fromArray), id1); addLineString(fact.createLineString(toArray), id2); this.removeEdge(nearestEdge); System.out.println("Graph split @ " + resultcoord); }