getLocationForward method

LinearLocation getLocationForward(
  1. double length
)

Implementation

LinearLocation getLocationForward(double length) {
  if (length <= 0.0) return new LinearLocation();

  double totalLength = 0.0;

  LinearIterator it = new LinearIterator(linearGeom);
  while (it.hasNext()) {
    /**
     * Special handling is required for the situation when the
     * length references exactly to a component endpoint.
     * In this case, the endpoint location of the current component
     * is returned,
     * rather than the startpoint location of the next component.
     * This produces consistent behaviour with the project method.
     */
    if (it.isEndOfLine()) {
      if (totalLength == length) {
        int compIndex = it.getComponentIndex();
        int segIndex = it.getVertexIndex();
        return new LinearLocation.fromComponentSegmentIndexFraction(
            compIndex, segIndex, 0.0);
      }
    } else {
      Coordinate p0 = it.getSegmentStart();
      Coordinate p1 = it.getSegmentEnd()!;
      double segLen = p1.distance(p0);
      // length falls in this segment
      if (totalLength + segLen > length) {
        double frac = (length - totalLength) / segLen;
        int compIndex = it.getComponentIndex();
        int segIndex = it.getVertexIndex();
        return new LinearLocation.fromComponentSegmentIndexFraction(
            compIndex, segIndex, frac);
      }
      totalLength += segLen;
    }

    it.next();
  }
  // length is longer than line - return end location
  return LinearLocation.getEndLocation(linearGeom);
}