Beispiel #1
0
  public void ensureIncreasingTime() {
    if (getPositionCount() < 2) return;

    long completeTime = getTime(); // ms
    double completeDistance = getDistance(); // m
    double averageSpeed = completeTime > 0 ? completeDistance / completeTime * 1000 : 1.0; // m/s

    List<P> positions = getPositions();
    P first = positions.get(0);
    if (!first.hasTime()) first.setTime(fromCalendar(Calendar.getInstance(UTC)));

    P previous = first;
    for (int i = 1; i < positions.size(); i++) {
      P next = positions.get(i);
      CompactCalendar time = next.getTime();
      if (time == null || time.equals(previous.getTime())) {
        Double distance = next.calculateDistance(previous);
        Long millis = distance != null ? (long) (distance / averageSpeed * 1000) : null;
        if (millis == null || millis < 1000) millis = 1000L;
        next.setTime(
            fromMillisAndTimeZone(
                previous.getTime().getTimeInMillis() + millis, previous.getTime().getTimeZoneId()));
      }
      previous = next;
    }
  }
Beispiel #2
0
 private void move(int index, int upOrDown) {
   List<P> positions = getPositions();
   P move = positions.get(index);
   P replace = positions.get(index + upOrDown);
   positions.set(index + upOrDown, move);
   positions.set(index, replace);
 }
Beispiel #3
0
 public double getElevationDelta(int index) {
   List<P> positions = getPositions();
   NavigationPosition previous = index > 0 ? positions.get(index - 1) : null;
   NavigationPosition current = index < positions.size() ? positions.get(index) : null;
   if (previous != null && current != null) {
     Double elevation = previous.calculateElevation(current);
     if (elevation != null) return elevation;
   }
   return 0;
 }
Beispiel #4
0
 public int[] getPositionsWithinDistanceToPredecessor(double distance) {
   List<Integer> result = new ArrayList<>();
   List<P> positions = getPositions();
   if (positions.size() <= 2) return new int[0];
   P previous = positions.get(0);
   for (int i = 1; i < positions.size() - 1; i++) {
     P next = positions.get(i);
     if (!next.hasCoordinates() || next.calculateDistance(previous) <= distance) result.add(i);
     else previous = next;
   }
   return toArray(result);
 }
Beispiel #5
0
 public double[] getDistancesFromStart(int startIndex, int endIndex) {
   double[] result = new double[endIndex - startIndex + 1];
   List<P> positions = getPositions();
   int index = 0;
   double distance = 0.0;
   NavigationPosition previous = positions.size() > 0 ? positions.get(0) : null;
   while (index <= endIndex) {
     NavigationPosition next = positions.get(index);
     if (previous != null) {
       Double delta = previous.calculateDistance(next);
       if (delta != null) distance += delta;
       if (index >= startIndex) result[index - startIndex] = distance;
     }
     index++;
     previous = next;
   }
   return result;
 }
Beispiel #6
0
 public int[] getContainedPositions(BoundingBox boundingBox) {
   List<Integer> result = new ArrayList<>();
   List<P> positions = getPositions();
   for (int i = 0; i < positions.size(); i++) {
     P position = positions.get(i);
     if (position.hasCoordinates() && boundingBox.contains(position)) result.add(i);
   }
   return toArray(result);
 }
Beispiel #7
0
 /**
  * Removes duplicate adjacent {@link #getPositions() positions} from this route, leaving only
  * distinct neighbours
  */
 public void removeDuplicates() {
   List<P> positions = getPositions();
   P previous = null;
   int index = 0;
   while (index < positions.size()) {
     P next = positions.get(index);
     if (previous != null && (!next.hasCoordinates() || next.calculateDistance(previous) <= 0.0)) {
       positions.remove(index);
     } else index++;
     previous = next;
   }
 }
Beispiel #8
0
 public double getElevationDescend(int startIndex, int endIndex) {
   double result = 0;
   List<P> positions = getPositions();
   NavigationPosition previous = null;
   for (int i = startIndex; i <= endIndex; i++) {
     NavigationPosition next = positions.get(i);
     if (previous != null) {
       Double elevation = previous.calculateElevation(next);
       if (elevation != null && elevation < 0) result += abs(elevation);
     }
     previous = next;
   }
   return result;
 }
Beispiel #9
0
 public double getDistance(int startIndex, int endIndex) {
   double result = 0;
   List<P> positions = getPositions();
   NavigationPosition previous = null;
   for (int i = startIndex; i <= endIndex; i++) {
     NavigationPosition next = positions.get(i);
     if (previous != null) {
       Double distance = previous.calculateDistance(next);
       if (distance != null) result += distance;
     }
     previous = next;
   }
   return result;
 }
Beispiel #10
0
  public double[] getDistancesFromStart(int[] indices) {
    double[] result = new double[indices.length];
    if (indices.length > 0 && getPositionCount() > 0) {
      Arrays.sort(indices);
      int endIndex = min(indices[indices.length - 1], getPositionCount() - 1);

      int index = 0;
      double distance = 0.0;
      List<P> positions = getPositions();
      NavigationPosition previous = positions.get(0);
      while (index <= endIndex) {
        NavigationPosition next = positions.get(index);
        if (previous != null) {
          Double delta = previous.calculateDistance(next);
          if (delta != null) distance += delta;
          int indexInIndices = binarySearch(indices, index);
          if (indexInIndices >= 0) result[indexInIndices] = distance;
        }
        index++;
        previous = next;
      }
    }
    return result;
  }
Beispiel #11
0
  public int getClosestPosition(double longitude, double latitude, double threshold) {
    int closestIndex = -1;
    double closestDistance = MAX_VALUE;

    List<P> positions = getPositions();
    for (int i = 0; i < positions.size(); ++i) {
      P point = positions.get(i);
      Double distance = point.calculateDistance(longitude, latitude);
      if (distance != null && distance < closestDistance && distance < threshold) {
        closestDistance = distance;
        closestIndex = i;
      }
    }
    return closestIndex;
  }
Beispiel #12
0
 public void order(List<P> positions) {
   List<P> existing = getPositions();
   for (int i = 0; i < positions.size(); i++) {
     existing.set(i, positions.get(i));
   }
 }
Beispiel #13
0
 public P getSuccessor(P position) {
   List<P> positions = getPositions();
   int index = positions.indexOf(position);
   return index != -1 && index < positions.size() - 1 ? positions.get(index + 1) : null;
 }