+ /**
+ * Average selected points
+ * @param inStartIndex start index of selection
+ * @param inEndIndex end index of selection
+ * @return true if successful
+ */
+ public boolean average(int inStartIndex, int inEndIndex)
+ {
+ // check parameters
+ if (inStartIndex < 0 || inStartIndex >= _numPoints || inEndIndex <= inStartIndex)
+ return false;
+
+ DataPoint startPoint = getPoint(inStartIndex);
+ double firstLatitude = startPoint.getLatitude().getDouble();
+ double firstLongitude = startPoint.getLongitude().getDouble();
+ double latitudeDiff = 0.0, longitudeDiff = 0.0;
+ double totalAltitude = 0;
+ int numAltitudes = 0;
+ Altitude.Format altFormat = Config.getUseMetricUnits()?Altitude.Format.METRES:Altitude.Format.FEET;
+ // loop between start and end points
+ for (int i=inStartIndex; i<= inEndIndex; i++)
+ {
+ DataPoint currPoint = getPoint(i);
+ latitudeDiff += (currPoint.getLatitude().getDouble() - firstLatitude);
+ longitudeDiff += (currPoint.getLongitude().getDouble() - firstLongitude);
+ if (currPoint.hasAltitude()) {
+ totalAltitude += currPoint.getAltitude().getValue(altFormat);
+ numAltitudes++;
+ }
+ }
+ int numPoints = inEndIndex - inStartIndex + 1;
+ double meanLatitude = firstLatitude + (latitudeDiff / numPoints);
+ double meanLongitude = firstLongitude + (longitudeDiff / numPoints);
+ Altitude meanAltitude = null;
+ if (numAltitudes > 0) {meanAltitude = new Altitude((int) (totalAltitude / numAltitudes), altFormat);}
+
+ DataPoint insertedPoint = new DataPoint(new Latitude(meanLatitude, Coordinate.FORMAT_NONE),
+ new Longitude(meanLongitude, Coordinate.FORMAT_NONE), meanAltitude);
+ // Make into singleton
+ insertedPoint.setSegmentStart(true);
+ DataPoint nextPoint = getNextTrackPoint(inEndIndex+1);
+ if (nextPoint != null) {nextPoint.setSegmentStart(true);}
+ // Insert points into track
+ return insertRange(new DataPoint[] {insertedPoint}, inEndIndex + 1);
+ }
+
+