コード例 #1
0
  /**
   * 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);
  }