indexOfFromStart method

LinearLocation indexOfFromStart(
  1. Coordinate inputPt,
  2. LinearLocation? minIndex
)

Implementation

LinearLocation indexOfFromStart(
    Coordinate inputPt, LinearLocation? minIndex) {
  double minDistance = double.maxFinite;
  int minComponentIndex = 0;
  int minSegmentIndex = 0;
  double minFrac = -1.0;

  LineSegment seg = new LineSegment.empty();
  for (LinearIterator it = new LinearIterator(linearGeom);
      it.hasNext();
      it.next()) {
    if (!it.isEndOfLine()) {
      seg.p0 = it.getSegmentStart();
      seg.p1 = it.getSegmentEnd()!;
      double segDistance = seg.distanceCoord(inputPt);
      double segFrac = seg.segmentFraction(inputPt);

      int candidateComponentIndex = it.getComponentIndex();
      int candidateSegmentIndex = it.getVertexIndex();
      if (segDistance < minDistance) {
        // ensure after minLocation, if any
        if (minIndex == null ||
            minIndex.compareLocationValues(
                    candidateComponentIndex, candidateSegmentIndex, segFrac) <
                0) {
          // otherwise, save this as new minimum
          minComponentIndex = candidateComponentIndex;
          minSegmentIndex = candidateSegmentIndex;
          minFrac = segFrac;
          minDistance = segDistance;
        }
      }
    }
  }
  if (minDistance == double.maxFinite) {
    // no minimum was found past minLocation, so return it
    return new LinearLocation.fromLocation(minIndex!);
  }
  // otherwise, return computed location
  LinearLocation loc = new LinearLocation.fromComponentSegmentIndexFraction(
      minComponentIndex, minSegmentIndex, minFrac);
  return loc;
}