1 package tim.prune.data;
5 import tim.prune.UpdateMessageBroker;
6 import tim.prune.config.Config;
7 import tim.prune.function.edit.FieldEdit;
8 import tim.prune.function.edit.FieldEditList;
9 import tim.prune.gui.map.MapUtils;
13 * Class to hold all track information,
14 * including track points and waypoints
19 private DataPoint[] _dataPoints = null;
21 private double[] _xValues = null;
22 private double[] _yValues = null;
23 private boolean _scaled = false;
24 private int _numPoints = 0;
25 private boolean _hasTrackpoint = false;
26 private boolean _hasWaypoint = false;
28 private FieldList _masterFieldList = null;
30 private AltitudeRange _altitudeRange = null;
31 private DoubleRange _latRange = null, _longRange = null;
32 private DoubleRange _xRange = null, _yRange = null;
36 * Constructor for empty track
41 _masterFieldList = new FieldList(null);
42 // make empty DataPoint array
43 _dataPoints = new DataPoint[0];
51 * Load method, for initialising and reinitialising data
52 * @param inFieldArray array of Field objects describing fields
53 * @param inPointArray 2d object array containing data
54 * @param inAltFormat altitude format
56 public void load(Field[] inFieldArray, Object[][] inPointArray, Altitude.Format inAltFormat)
58 if (inFieldArray == null || inPointArray == null)
64 _masterFieldList = new FieldList(inFieldArray);
65 // make DataPoint object from each point in inPointList
66 _dataPoints = new DataPoint[inPointArray.length];
67 String[] dataArray = null;
69 for (int p=0; p < inPointArray.length; p++)
71 dataArray = (String[]) inPointArray[p];
72 // Convert to DataPoint objects
73 DataPoint point = new DataPoint(dataArray, _masterFieldList, inAltFormat);
76 _dataPoints[pointIndex] = point;
80 _numPoints = pointIndex;
81 // Set first track point to be start of segment
82 DataPoint firstTrackPoint = getNextTrackPoint(0);
83 if (firstTrackPoint != null) {
84 firstTrackPoint.setSegmentStart(true);
92 * Load the track by transferring the contents from a loaded Track object
93 * @param inOther Track object containing loaded data
95 public void load(Track inOther)
97 _numPoints = inOther._numPoints;
98 _masterFieldList = inOther._masterFieldList;
99 _dataPoints = inOther._dataPoints;
100 // needs to be scaled
105 * Request that a rescale be done to recalculate derived values
107 public void requestRescale()
113 * Extend the track's field list with the given additional fields
114 * @param inFieldList list of fields to be added
116 public void extendFieldList(FieldList inFieldList)
118 _masterFieldList = _masterFieldList.merge(inFieldList);
121 ////////////////// Modification methods //////////////////////
125 * Combine this Track with new data
126 * @param inOtherTrack other track to combine
128 public void combine(Track inOtherTrack)
131 _masterFieldList = _masterFieldList.merge(inOtherTrack._masterFieldList);
132 // expand data array and add other track's data points
133 int totalPoints = getNumPoints() + inOtherTrack.getNumPoints();
134 DataPoint[] mergedPoints = new DataPoint[totalPoints];
135 System.arraycopy(_dataPoints, 0, mergedPoints, 0, getNumPoints());
136 System.arraycopy(inOtherTrack._dataPoints, 0, mergedPoints, getNumPoints(), inOtherTrack.getNumPoints());
137 _dataPoints = mergedPoints;
138 // combine point count
139 _numPoints = totalPoints;
140 // needs to be scaled again
143 UpdateMessageBroker.informSubscribers();
148 * Crop the track to the given size - subsequent points are not (yet) deleted
149 * @param inNewSize new number of points in track
151 public void cropTo(int inNewSize)
153 if (inNewSize >= 0 && inNewSize < getNumPoints())
155 _numPoints = inNewSize;
156 // needs to be scaled again
158 UpdateMessageBroker.informSubscribers();
164 * Delete the points marked for deletion
165 * @return number of points deleted
167 public int deleteMarkedPoints()
170 // Copy selected points
171 DataPoint[] newPointArray = new DataPoint[_numPoints];
172 for (int i=0; i<_numPoints; i++)
174 DataPoint point = _dataPoints[i];
175 // Don't delete photo points
176 if (point.getPhoto() != null || !point.getDeleteFlag())
178 newPointArray[numCopied] = point;
183 // Copy array references
184 int numDeleted = _numPoints - numCopied;
187 _dataPoints = new DataPoint[numCopied];
188 System.arraycopy(newPointArray, 0, _dataPoints, 0, numCopied);
189 _numPoints = _dataPoints.length;
197 * Delete the specified point
198 * @param inIndex point index
199 * @return true if successful
201 public boolean deletePoint(int inIndex)
203 boolean answer = deleteRange(inIndex, inIndex);
209 * Delete the specified range of points from the Track
210 * @param inStart start of range (inclusive)
211 * @param inEnd end of range (inclusive)
212 * @return true if successful
214 public boolean deleteRange(int inStart, int inEnd)
216 if (inStart < 0 || inEnd < 0 || inEnd < inStart)
218 // no valid range selected so can't delete
221 // check through range to be deleted, and see if any new segment flags present
222 boolean hasSegmentStart = false;
223 DataPoint nextTrackPoint = getNextTrackPoint(inEnd+1);
224 if (nextTrackPoint != null) {
225 for (int i=inStart; i<=inEnd && !hasSegmentStart; i++) {
226 hasSegmentStart |= _dataPoints[i].getSegmentStart();
228 // If segment break found, make sure next trackpoint also has break
229 if (hasSegmentStart) {nextTrackPoint.setSegmentStart(true);}
231 // valid range, let's delete it
232 int numToDelete = inEnd - inStart + 1;
233 DataPoint[] newPointArray = new DataPoint[_numPoints - numToDelete];
234 // Copy points before the selected range
237 System.arraycopy(_dataPoints, 0, newPointArray, 0, inStart);
239 // Copy points after the deleted one(s)
240 if (inEnd < (_numPoints - 1))
242 System.arraycopy(_dataPoints, inEnd + 1, newPointArray, inStart,
243 _numPoints - inEnd - 1);
245 // Copy points over original array
246 _dataPoints = newPointArray;
247 _numPoints -= numToDelete;
248 // needs to be scaled again
255 * Reverse the specified range of points
256 * @param inStart start index
257 * @param inEnd end index
258 * @return true if successful, false otherwise
260 public boolean reverseRange(int inStart, int inEnd)
262 if (inStart < 0 || inEnd < 0 || inStart >= inEnd || inEnd >= _numPoints)
266 // calculate how many point swaps are required
267 int numPointsToReverse = (inEnd - inStart + 1) / 2;
269 for (int i=0; i<numPointsToReverse; i++)
271 // swap pairs of points
272 p = _dataPoints[inStart + i];
273 _dataPoints[inStart + i] = _dataPoints[inEnd - i];
274 _dataPoints[inEnd - i] = p;
276 // adjust segment starts
277 shiftSegmentStarts(inStart, inEnd);
278 // Find first track point and following track point, and set segment starts to true
279 DataPoint firstTrackPoint = getNextTrackPoint(inStart);
280 if (firstTrackPoint != null) {firstTrackPoint.setSegmentStart(true);}
281 DataPoint nextTrackPoint = getNextTrackPoint(inEnd+1);
282 if (nextTrackPoint != null) {nextTrackPoint.setSegmentStart(true);}
283 // needs to be scaled again
285 UpdateMessageBroker.informSubscribers();
291 * Add the given time offset to the specified range
292 * @param inStart start of range
293 * @param inEnd end of range
294 * @param inOffset offset to add (-ve to subtract)
295 * @param inUndo true for undo operation
296 * @return true on success
298 public boolean addTimeOffset(int inStart, int inEnd, long inOffset, boolean inUndo)
301 if (inStart < 0 || inEnd < 0 || inStart >= inEnd || inEnd >= _numPoints) {
304 boolean foundTimestamp = false;
305 // Loop over all points within range
306 for (int i=inStart; i<=inEnd; i++)
308 Timestamp timestamp = _dataPoints[i].getTimestamp();
309 if (timestamp != null)
311 // This point has a timestamp so add the offset to it
312 foundTimestamp = true;
313 timestamp.addOffset(inOffset);
314 _dataPoints[i].setModified(inUndo);
317 return foundTimestamp;
321 * Add the given altitude offset to the specified range
322 * @param inStart start of range
323 * @param inEnd end of range
324 * @param inOffset offset to add (-ve to subtract)
325 * @param inFormat altitude format of offset
326 * @param inDecimals number of decimal places in offset
327 * @return true on success
329 public boolean addAltitudeOffset(int inStart, int inEnd, double inOffset,
330 Altitude.Format inFormat, int inDecimals)
333 if (inStart < 0 || inEnd < 0 || inStart >= inEnd || inEnd >= _numPoints) {
336 boolean foundAlt = false;
337 // Loop over all points within range
338 for (int i=inStart; i<=inEnd; i++)
340 Altitude alt = _dataPoints[i].getAltitude();
341 if (alt != null && alt.isValid())
343 // This point has an altitude so add the offset to it
345 alt.addOffset(inOffset, inFormat, inDecimals);
346 _dataPoints[i].setModified(false);
349 // needs to be scaled again
356 * Collect all waypoints to the start or end of the track
357 * @param inAtStart true to collect at start, false for end
358 * @return true if successful, false if no change
360 public boolean collectWaypoints(boolean inAtStart)
362 // Check for mixed data, numbers of waypoints & nons
363 int numWaypoints = 0, numNonWaypoints = 0;
364 boolean wayAfterNon = false, nonAfterWay = false;
365 DataPoint[] waypoints = new DataPoint[_numPoints];
366 DataPoint[] nonWaypoints = new DataPoint[_numPoints];
367 DataPoint point = null;
368 for (int i=0; i<_numPoints; i++)
370 point = _dataPoints[i];
371 if (point.isWaypoint())
373 waypoints[numWaypoints] = point;
375 wayAfterNon |= (numNonWaypoints > 0);
379 nonWaypoints[numNonWaypoints] = point;
381 nonAfterWay |= (numWaypoints > 0);
384 // Exit if the data is already in the specified order
385 if (numWaypoints == 0 || numNonWaypoints == 0
386 || (inAtStart && !wayAfterNon && nonAfterWay)
387 || (!inAtStart && wayAfterNon && !nonAfterWay))
392 // Copy the arrays back into _dataPoints in the specified order
395 System.arraycopy(waypoints, 0, _dataPoints, 0, numWaypoints);
396 System.arraycopy(nonWaypoints, 0, _dataPoints, numWaypoints, numNonWaypoints);
400 System.arraycopy(nonWaypoints, 0, _dataPoints, 0, numNonWaypoints);
401 System.arraycopy(waypoints, 0, _dataPoints, numNonWaypoints, numWaypoints);
403 // needs to be scaled again
405 UpdateMessageBroker.informSubscribers();
411 * Interleave all waypoints by each nearest track point
412 * @return true if successful, false if no change
414 public boolean interleaveWaypoints()
416 // Separate waypoints and find nearest track point
417 int numWaypoints = 0;
418 DataPoint[] waypoints = new DataPoint[_numPoints];
419 int[] pointIndices = new int[_numPoints];
420 DataPoint point = null;
422 for (i=0; i<_numPoints; i++)
424 point = _dataPoints[i];
425 if (point.isWaypoint())
427 waypoints[numWaypoints] = point;
428 pointIndices[numWaypoints] = getNearestPointIndex(
429 _xValues[i], _yValues[i], -1.0, true);
433 // Exit if data not mixed
434 if (numWaypoints == 0 || numWaypoints == _numPoints)
437 // Loop round points copying to correct order
438 DataPoint[] dataCopy = new DataPoint[_numPoints];
440 for (i=0; i<_numPoints; i++)
442 point = _dataPoints[i];
443 // if it's a track point, copy it
444 if (!point.isWaypoint())
446 dataCopy[copyIndex] = point;
449 // check for waypoints with this index
450 for (int j=0; j<numWaypoints; j++)
452 if (pointIndices[j] == i)
454 dataCopy[copyIndex] = waypoints[j];
459 // Copy data back to track
460 _dataPoints = dataCopy;
461 // needs to be scaled again to recalc x, y
463 UpdateMessageBroker.informSubscribers();
469 * Cut and move the specified section
470 * @param inSectionStart start index of section
471 * @param inSectionEnd end index of section
472 * @param inMoveTo index of move to point
473 * @return true if move successful
475 public boolean cutAndMoveSection(int inSectionStart, int inSectionEnd, int inMoveTo)
477 // TODO: Move cut/move into separate function?
478 // Check that indices make sense
479 if (inSectionStart > 0 && inSectionEnd > inSectionStart && inMoveTo >= 0
480 && (inMoveTo < inSectionStart || inMoveTo > (inSectionEnd+1)))
482 // do the cut and move
483 DataPoint[] newPointArray = new DataPoint[_numPoints];
484 // System.out.println("Cut/move section (" + inSectionStart + " - " + inSectionEnd + ") to before point " + inMoveTo);
485 // Is it a forward copy or a backward copy?
486 if (inSectionStart > inMoveTo)
488 int sectionLength = inSectionEnd - inSectionStart + 1;
489 // move section to earlier point
491 System.arraycopy(_dataPoints, 0, newPointArray, 0, inMoveTo); // unchanged points before
493 System.arraycopy(_dataPoints, inSectionStart, newPointArray, inMoveTo, sectionLength); // moved bit
494 // after insertion point, before moved bit
495 System.arraycopy(_dataPoints, inMoveTo, newPointArray, inMoveTo + sectionLength, inSectionStart - inMoveTo);
497 if (inSectionEnd < (_numPoints - 1)) {
498 System.arraycopy(_dataPoints, inSectionEnd+1, newPointArray, inSectionEnd+1, _numPoints - inSectionEnd - 1);
503 // Move section to later point
504 if (inSectionStart > 0) {
505 System.arraycopy(_dataPoints, 0, newPointArray, 0, inSectionStart); // unchanged points before
507 // from end of section to move to point
508 if (inMoveTo > (inSectionEnd + 1)) {
509 System.arraycopy(_dataPoints, inSectionEnd+1, newPointArray, inSectionStart, inMoveTo - inSectionEnd - 1);
512 System.arraycopy(_dataPoints, inSectionStart, newPointArray, inSectionStart + inMoveTo - inSectionEnd - 1,
513 inSectionEnd - inSectionStart + 1);
514 // unchanged bit after
515 if (inSectionEnd < (_numPoints - 1)) {
516 System.arraycopy(_dataPoints, inMoveTo, newPointArray, inMoveTo, _numPoints - inMoveTo);
519 // Copy array references
520 _dataPoints = newPointArray;
529 * Interpolate extra points between two selected ones
530 * @param inStartIndex start index of interpolation
531 * @param inNumPoints num points to insert
532 * @return true if successful
534 public boolean interpolate(int inStartIndex, int inNumPoints)
537 if (inStartIndex < 0 || inStartIndex >= _numPoints || inNumPoints <= 0)
540 // get start and end points
541 DataPoint startPoint = getPoint(inStartIndex);
542 DataPoint endPoint = getPoint(inStartIndex + 1);
544 // Make array of points to insert
545 DataPoint[] insertedPoints = startPoint.interpolate(endPoint, inNumPoints);
547 // Insert points into track
548 return insertRange(insertedPoints, inStartIndex + 1);
553 * Average selected points
554 * @param inStartIndex start index of selection
555 * @param inEndIndex end index of selection
556 * @return true if successful
558 public boolean average(int inStartIndex, int inEndIndex)
561 if (inStartIndex < 0 || inStartIndex >= _numPoints || inEndIndex <= inStartIndex)
564 DataPoint startPoint = getPoint(inStartIndex);
565 double firstLatitude = startPoint.getLatitude().getDouble();
566 double firstLongitude = startPoint.getLongitude().getDouble();
567 double latitudeDiff = 0.0, longitudeDiff = 0.0;
568 double totalAltitude = 0;
569 int numAltitudes = 0;
570 Altitude.Format altFormat = Config.getConfigBoolean(Config.KEY_METRIC_UNITS)?Altitude.Format.METRES:Altitude.Format.FEET;
571 // loop between start and end points
572 for (int i=inStartIndex; i<= inEndIndex; i++)
574 DataPoint currPoint = getPoint(i);
575 latitudeDiff += (currPoint.getLatitude().getDouble() - firstLatitude);
576 longitudeDiff += (currPoint.getLongitude().getDouble() - firstLongitude);
577 if (currPoint.hasAltitude()) {
578 totalAltitude += currPoint.getAltitude().getValue(altFormat);
582 int numPoints = inEndIndex - inStartIndex + 1;
583 double meanLatitude = firstLatitude + (latitudeDiff / numPoints);
584 double meanLongitude = firstLongitude + (longitudeDiff / numPoints);
585 Altitude meanAltitude = null;
586 if (numAltitudes > 0) {meanAltitude = new Altitude((int) (totalAltitude / numAltitudes), altFormat);}
588 DataPoint insertedPoint = new DataPoint(new Latitude(meanLatitude, Coordinate.FORMAT_NONE),
589 new Longitude(meanLongitude, Coordinate.FORMAT_NONE), meanAltitude);
590 // Make into singleton
591 insertedPoint.setSegmentStart(true);
592 DataPoint nextPoint = getNextTrackPoint(inEndIndex+1);
593 if (nextPoint != null) {nextPoint.setSegmentStart(true);}
594 // Insert points into track
595 return insertRange(new DataPoint[] {insertedPoint}, inEndIndex + 1);
600 * Append the specified points to the end of the track
601 * @param inPoints DataPoint objects to add
603 public void appendPoints(DataPoint[] inPoints)
605 // Insert points into track
606 if (inPoints != null && inPoints.length > 0)
608 insertRange(inPoints, _numPoints);
610 // needs to be scaled again to recalc x, y
612 UpdateMessageBroker.informSubscribers();
616 //////// information methods /////////////
620 * Get the point at the given index
621 * @param inPointNum index number, starting at 0
622 * @return DataPoint object, or null if out of range
624 public DataPoint getPoint(int inPointNum)
626 if (inPointNum > -1 && inPointNum < getNumPoints())
628 return _dataPoints[inPointNum];
635 * @return altitude range of points as AltitudeRange object
637 public AltitudeRange getAltitudeRange()
639 if (!_scaled) scalePoints();
640 return _altitudeRange;
644 * @return the number of (valid) points in the track
646 public int getNumPoints()
652 * @return The range of x values as a DoubleRange object
654 public DoubleRange getXRange()
656 if (!_scaled) scalePoints();
661 * @return The range of y values as a DoubleRange object
663 public DoubleRange getYRange()
665 if (!_scaled) scalePoints();
670 * @return The range of lat values as a DoubleRange object
672 public DoubleRange getLatRange()
674 if (!_scaled) scalePoints();
678 * @return The range of lon values as a DoubleRange object
680 public DoubleRange getLonRange()
682 if (!_scaled) scalePoints();
687 * @param inPointNum point index, starting at 0
688 * @return scaled x value of specified point
690 public double getX(int inPointNum)
692 if (!_scaled) scalePoints();
693 return _xValues[inPointNum];
697 * @param inPointNum point index, starting at 0
698 * @return scaled y value of specified point
700 public double getY(int inPointNum)
702 if (!_scaled) scalePoints();
703 return _yValues[inPointNum];
707 * @return the master field list
709 public FieldList getFieldList()
711 return _masterFieldList;
716 * Checks if any data exists for the specified field
717 * @param inField Field to examine
718 * @return true if data exists for this field
720 public boolean hasData(Field inField)
722 // Don't use this method for altitudes
723 if (inField.equals(Field.ALTITUDE)) {return hasAltitudeData();}
724 return hasData(inField, 0, _numPoints-1);
729 * Checks if any data exists for the specified field in the specified range
730 * @param inField Field to examine
731 * @param inStart start of range to check
732 * @param inEnd end of range to check (inclusive)
733 * @return true if data exists for this field
735 public boolean hasData(Field inField, int inStart, int inEnd)
737 // Loop over selected point range
738 for (int i=inStart; i<=inEnd; i++)
740 if (_dataPoints[i].getFieldValue(inField) != null)
742 // Check altitudes and timestamps
743 if ((inField != Field.ALTITUDE || _dataPoints[i].getAltitude().isValid())
744 && (inField != Field.TIMESTAMP || _dataPoints[i].getTimestamp().isValid()))
754 * @return true if track has altitude data
756 public boolean hasAltitudeData()
758 for (int i=0; i<_numPoints; i++) {
759 if (_dataPoints[i].hasAltitude()) {return true;}
765 * @return true if track contains at least one trackpoint
767 public boolean hasTrackPoints()
769 if (!_scaled) scalePoints();
770 return _hasTrackpoint;
774 * @return true if track contains waypoints
776 public boolean hasWaypoints()
778 if (!_scaled) scalePoints();
783 * @return true if track contains any points marked for deletion
785 public boolean hasMarkedPoints()
787 if (_numPoints < 1) {
790 // Loop over points looking for any marked for deletion
791 for (int i=0; i<=_numPoints-1; i++)
793 if (_dataPoints[i] != null && _dataPoints[i].getDeleteFlag()) {
802 * Clear all the deletion markers
804 public void clearDeletionMarkers()
806 for (int i=0; i<_numPoints; i++)
808 _dataPoints[i].setMarkedForDeletion(false);
813 * Collect all the waypoints into the given List
814 * @param inList List to fill with waypoints
816 public void getWaypoints(List<DataPoint> inList)
820 // loop over points and copy all waypoints into list
821 for (int i=0; i<=_numPoints-1; i++)
823 if (_dataPoints[i] != null && _dataPoints[i].isWaypoint())
825 inList.add(_dataPoints[i]);
832 * Search for the given Point in the track and return the index
833 * @param inPoint Point to look for
834 * @return index of Point, if any or -1 if not found
836 public int getPointIndex(DataPoint inPoint)
840 // Loop over points in track
841 for (int i=0; i<=_numPoints-1; i++)
843 if (_dataPoints[i] == inPoint)
854 ///////// Internal processing methods ////////////////
858 * Scale all the points in the track to gain x and y values
861 private void scalePoints()
863 // Loop through all points in track, to see limits of lat, long and altitude
864 _longRange = new DoubleRange();
865 _latRange = new DoubleRange();
866 _altitudeRange = new AltitudeRange();
868 _hasWaypoint = false; _hasTrackpoint = false;
869 for (p=0; p < getNumPoints(); p++)
871 DataPoint point = getPoint(p);
872 if (point != null && point.isValid())
874 _longRange.addValue(point.getLongitude().getDouble());
875 _latRange.addValue(point.getLatitude().getDouble());
876 if (point.getAltitude().isValid())
878 _altitudeRange.addValue(point.getAltitude());
880 if (point.isWaypoint())
883 _hasTrackpoint = true;
887 // Loop over points and calculate scales
888 _xValues = new double[getNumPoints()];
889 _yValues = new double[getNumPoints()];
890 _xRange = new DoubleRange();
891 _yRange = new DoubleRange();
892 for (p=0; p < getNumPoints(); p++)
894 DataPoint point = getPoint(p);
897 _xValues[p] = MapUtils.getXFromLongitude(point.getLongitude().getDouble());
898 _xRange.addValue(_xValues[p]);
899 _yValues[p] = MapUtils.getYFromLatitude(point.getLatitude().getDouble());
900 _yRange.addValue(_yValues[p]);
908 * Find the nearest point to the specified x and y coordinates
909 * or -1 if no point is within the specified max distance
910 * @param inX x coordinate
911 * @param inY y coordinate
912 * @param inMaxDist maximum distance from selected coordinates
913 * @param inJustTrackPoints true if waypoints should be ignored
914 * @return index of nearest point or -1 if not found
916 public int getNearestPointIndex(double inX, double inY, double inMaxDist, boolean inJustTrackPoints)
918 int nearestPoint = 0;
919 double nearestDist = -1.0;
921 for (int i=0; i < getNumPoints(); i++)
923 if (!inJustTrackPoints || !_dataPoints[i].isWaypoint())
925 currDist = Math.abs(_xValues[i] - inX) + Math.abs(_yValues[i] - inY);
926 if (currDist < nearestDist || nearestDist < 0.0)
929 nearestDist = currDist;
933 // Check whether it's within required distance
934 if (nearestDist > inMaxDist && inMaxDist > 0.0)
942 * Get the next track point starting from the given index
943 * @param inStartIndex index to start looking from
944 * @return next track point, or null if end of data reached
946 public DataPoint getNextTrackPoint(int inStartIndex)
948 return getNextTrackPoint(inStartIndex, _numPoints, true);
952 * Get the next track point in the given range
953 * @param inStartIndex index to start looking from
954 * @param inEndIndex index to stop looking
955 * @return next track point, or null if end of data reached
957 public DataPoint getNextTrackPoint(int inStartIndex, int inEndIndex)
959 return getNextTrackPoint(inStartIndex, inEndIndex, true);
963 * Get the previous track point starting from the given index
964 * @param inStartIndex index to start looking from
965 * @return next track point, or null if end of data reached
967 public DataPoint getPreviousTrackPoint(int inStartIndex)
969 return getNextTrackPoint(inStartIndex, _numPoints, false);
973 * Get the next track point starting from the given index
974 * @param inStartIndex index to start looking from
975 * @param inEndIndex index to stop looking (inclusive)
976 * @param inCountUp true for next, false for previous
977 * @return next track point, or null if end of data reached
979 private DataPoint getNextTrackPoint(int inStartIndex, int inEndIndex, boolean inCountUp)
981 // Loop forever over points
982 int increment = inCountUp?1:-1;
983 for (int i=inStartIndex; i<=inEndIndex; i+=increment)
985 DataPoint point = getPoint(i);
986 // Exit if end of data reached - there wasn't a track point
987 if (point == null) {return null;}
988 if (point.isValid() && !point.isWaypoint()) {
989 // next track point found
997 * Shift all the segment start flags in the given range by 1
998 * Method used by reverse range and its undo
999 * @param inStartIndex start of range, inclusive
1000 * @param inEndIndex end of range, inclusive
1002 public void shiftSegmentStarts(int inStartIndex, int inEndIndex)
1004 boolean prevFlag = true;
1005 boolean currFlag = true;
1006 for (int i=inStartIndex; i<= inEndIndex; i++)
1008 DataPoint point = getPoint(i);
1009 if (point != null && !point.isWaypoint())
1012 currFlag = point.getSegmentStart();
1014 point.setSegmentStart(prevFlag);
1015 prevFlag = currFlag;
1020 ////////////////// Cloning and replacing ///////////////////
1023 * Clone the array of DataPoints
1024 * @return shallow copy of DataPoint objects
1026 public DataPoint[] cloneContents()
1028 DataPoint[] clone = new DataPoint[getNumPoints()];
1029 System.arraycopy(_dataPoints, 0, clone, 0, getNumPoints());
1035 * Clone the specified range of data points
1036 * @param inStart start index (inclusive)
1037 * @param inEnd end index (inclusive)
1038 * @return shallow copy of DataPoint objects
1040 public DataPoint[] cloneRange(int inStart, int inEnd)
1042 int numSelected = 0;
1043 if (inEnd >= 0 && inEnd >= inStart)
1045 numSelected = inEnd - inStart + 1;
1047 DataPoint[] result = new DataPoint[numSelected>0?numSelected:0];
1048 if (numSelected > 0)
1050 System.arraycopy(_dataPoints, inStart, result, 0, numSelected);
1057 * Re-insert the specified point at the given index
1058 * @param inPoint point to insert
1059 * @param inIndex index at which to insert the point
1060 * @return true if it worked, false otherwise
1062 public boolean insertPoint(DataPoint inPoint, int inIndex)
1064 if (inIndex > _numPoints || inPoint == null)
1068 // Make new array to copy points over to
1069 DataPoint[] newPointArray = new DataPoint[_numPoints + 1];
1072 System.arraycopy(_dataPoints, 0, newPointArray, 0, inIndex);
1074 newPointArray[inIndex] = inPoint;
1075 if (inIndex < _numPoints)
1077 System.arraycopy(_dataPoints, inIndex, newPointArray, inIndex+1, _numPoints - inIndex);
1079 // Change over to new array
1080 _dataPoints = newPointArray;
1082 // needs to be scaled again
1084 UpdateMessageBroker.informSubscribers();
1090 * Re-insert the specified point range at the given index
1091 * @param inPoints point array to insert
1092 * @param inIndex index at which to insert the points
1093 * @return true if it worked, false otherwise
1095 public boolean insertRange(DataPoint[] inPoints, int inIndex)
1097 if (inIndex > _numPoints || inPoints == null)
1101 // Make new array to copy points over to
1102 DataPoint[] newPointArray = new DataPoint[_numPoints + inPoints.length];
1105 System.arraycopy(_dataPoints, 0, newPointArray, 0, inIndex);
1107 System.arraycopy(inPoints, 0, newPointArray, inIndex, inPoints.length);
1108 if (inIndex < _numPoints)
1110 System.arraycopy(_dataPoints, inIndex, newPointArray, inIndex+inPoints.length, _numPoints - inIndex);
1112 // Change over to new array
1113 _dataPoints = newPointArray;
1114 _numPoints += inPoints.length;
1115 // needs to be scaled again
1117 UpdateMessageBroker.informSubscribers();
1123 * Replace the track contents with the given point array
1124 * @param inContents array of DataPoint objects
1125 * @return true on success
1127 public boolean replaceContents(DataPoint[] inContents)
1129 // master field array stays the same
1130 // (would need to store field array too if we wanted to redo a load)
1131 // replace data array
1132 _dataPoints = inContents;
1133 _numPoints = _dataPoints.length;
1135 UpdateMessageBroker.informSubscribers();
1141 * Edit the specified point
1142 * @param inPoint point to edit
1143 * @param inEditList list of edits to make
1144 * @param inUndo true if undo operation, false otherwise
1145 * @return true if successful
1147 public boolean editPoint(DataPoint inPoint, FieldEditList inEditList, boolean inUndo)
1149 if (inPoint != null && inEditList != null && inEditList.getNumEdits() > 0)
1151 // remember if coordinates have changed
1152 boolean coordsChanged = false;
1153 // go through edits one by one
1154 int numEdits = inEditList.getNumEdits();
1155 for (int i=0; i<numEdits; i++)
1157 FieldEdit edit = inEditList.getEdit(i);
1158 Field editField = edit.getField();
1159 inPoint.setFieldValue(editField, edit.getValue(), inUndo);
1160 // Check that master field list has this field already (maybe point name has been added)
1161 if (!_masterFieldList.contains(editField)) {
1162 _masterFieldList.extendList(editField);
1164 // check coordinates
1165 coordsChanged |= (editField.equals(Field.LATITUDE)
1166 || editField.equals(Field.LONGITUDE) || editField.equals(Field.ALTITUDE));
1168 // set photo status if coordinates have changed
1169 if (inPoint.getPhoto() != null && coordsChanged)
1171 inPoint.getPhoto().setCurrentStatus(Photo.Status.CONNECTED);
1173 // point possibly needs to be scaled again
1175 // trigger listeners
1176 UpdateMessageBroker.informSubscribers();