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; } }
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); }
/** * 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; } }
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; }