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 * @return true on success
297 public boolean addTimeOffset(int inStart, int inEnd, long inOffset)
300 if (inStart < 0 || inEnd < 0 || inStart >= inEnd || inEnd >= _numPoints) {
303 boolean foundTimestamp = false;
304 // Loop over all points within range
305 for (int i=inStart; i<=inEnd; i++)
307 Timestamp timestamp = _dataPoints[i].getTimestamp();
308 if (timestamp != null)
310 // This point has a timestamp so add the offset to it
311 foundTimestamp = true;
312 timestamp.addOffset(inOffset);
315 return foundTimestamp;
319 * Add the given altitude offset to the specified range
320 * @param inStart start of range
321 * @param inEnd end of range
322 * @param inOffset offset to add (-ve to subtract)
323 * @param inFormat altitude format of offset
324 * @param inDecimals number of decimal places in offset
325 * @return true on success
327 public boolean addAltitudeOffset(int inStart, int inEnd, double inOffset,
328 Altitude.Format inFormat, int inDecimals)
331 if (inStart < 0 || inEnd < 0 || inStart >= inEnd || inEnd >= _numPoints) {
334 boolean foundAlt = false;
335 // Loop over all points within range
336 for (int i=inStart; i<=inEnd; i++)
338 Altitude alt = _dataPoints[i].getAltitude();
339 if (alt != null && alt.isValid())
341 // This point has an altitude so add the offset to it
343 alt.addOffset(inOffset, inFormat, inDecimals);
346 // needs to be scaled again
353 * Collect all waypoints to the start or end of the track
354 * @param inAtStart true to collect at start, false for end
355 * @return true if successful, false if no change
357 public boolean collectWaypoints(boolean inAtStart)
359 // Check for mixed data, numbers of waypoints & nons
360 int numWaypoints = 0, numNonWaypoints = 0;
361 boolean wayAfterNon = false, nonAfterWay = false;
362 DataPoint[] waypoints = new DataPoint[_numPoints];
363 DataPoint[] nonWaypoints = new DataPoint[_numPoints];
364 DataPoint point = null;
365 for (int i=0; i<_numPoints; i++)
367 point = _dataPoints[i];
368 if (point.isWaypoint())
370 waypoints[numWaypoints] = point;
372 wayAfterNon |= (numNonWaypoints > 0);
376 nonWaypoints[numNonWaypoints] = point;
378 nonAfterWay |= (numWaypoints > 0);
381 // Exit if the data is already in the specified order
382 if (numWaypoints == 0 || numNonWaypoints == 0
383 || (inAtStart && !wayAfterNon && nonAfterWay)
384 || (!inAtStart && wayAfterNon && !nonAfterWay))
389 // Copy the arrays back into _dataPoints in the specified order
392 System.arraycopy(waypoints, 0, _dataPoints, 0, numWaypoints);
393 System.arraycopy(nonWaypoints, 0, _dataPoints, numWaypoints, numNonWaypoints);
397 System.arraycopy(nonWaypoints, 0, _dataPoints, 0, numNonWaypoints);
398 System.arraycopy(waypoints, 0, _dataPoints, numNonWaypoints, numWaypoints);
400 // needs to be scaled again
402 UpdateMessageBroker.informSubscribers();
408 * Interleave all waypoints by each nearest track point
409 * @return true if successful, false if no change
411 public boolean interleaveWaypoints()
413 // Separate waypoints and find nearest track point
414 int numWaypoints = 0;
415 DataPoint[] waypoints = new DataPoint[_numPoints];
416 int[] pointIndices = new int[_numPoints];
417 DataPoint point = null;
419 for (i=0; i<_numPoints; i++)
421 point = _dataPoints[i];
422 if (point.isWaypoint())
424 waypoints[numWaypoints] = point;
425 pointIndices[numWaypoints] = getNearestPointIndex(
426 _xValues[i], _yValues[i], -1.0, true);
430 // Exit if data not mixed
431 if (numWaypoints == 0 || numWaypoints == _numPoints)
434 // Loop round points copying to correct order
435 DataPoint[] dataCopy = new DataPoint[_numPoints];
437 for (i=0; i<_numPoints; i++)
439 point = _dataPoints[i];
440 // if it's a track point, copy it
441 if (!point.isWaypoint())
443 dataCopy[copyIndex] = point;
446 // check for waypoints with this index
447 for (int j=0; j<numWaypoints; j++)
449 if (pointIndices[j] == i)
451 dataCopy[copyIndex] = waypoints[j];
456 // Copy data back to track
457 _dataPoints = dataCopy;
458 // needs to be scaled again to recalc x, y
460 UpdateMessageBroker.informSubscribers();
466 * Cut and move the specified section
467 * @param inSectionStart start index of section
468 * @param inSectionEnd end index of section
469 * @param inMoveTo index of move to point
470 * @return true if move successful
472 public boolean cutAndMoveSection(int inSectionStart, int inSectionEnd, int inMoveTo)
474 // TODO: Move cut/move into separate function?
475 // Check that indices make sense
476 if (inSectionStart > 0 && inSectionEnd > inSectionStart && inMoveTo >= 0
477 && (inMoveTo < inSectionStart || inMoveTo > (inSectionEnd+1)))
479 // do the cut and move
480 DataPoint[] newPointArray = new DataPoint[_numPoints];
481 // System.out.println("Cut/move section (" + inSectionStart + " - " + inSectionEnd + ") to before point " + inMoveTo);
482 // Is it a forward copy or a backward copy?
483 if (inSectionStart > inMoveTo)
485 int sectionLength = inSectionEnd - inSectionStart + 1;
486 // move section to earlier point
488 System.arraycopy(_dataPoints, 0, newPointArray, 0, inMoveTo); // unchanged points before
490 System.arraycopy(_dataPoints, inSectionStart, newPointArray, inMoveTo, sectionLength); // moved bit
491 // after insertion point, before moved bit
492 System.arraycopy(_dataPoints, inMoveTo, newPointArray, inMoveTo + sectionLength, inSectionStart - inMoveTo);
494 if (inSectionEnd < (_numPoints - 1)) {
495 System.arraycopy(_dataPoints, inSectionEnd+1, newPointArray, inSectionEnd+1, _numPoints - inSectionEnd - 1);
500 // Move section to later point
501 if (inSectionStart > 0) {
502 System.arraycopy(_dataPoints, 0, newPointArray, 0, inSectionStart); // unchanged points before
504 // from end of section to move to point
505 if (inMoveTo > (inSectionEnd + 1)) {
506 System.arraycopy(_dataPoints, inSectionEnd+1, newPointArray, inSectionStart, inMoveTo - inSectionEnd - 1);
509 System.arraycopy(_dataPoints, inSectionStart, newPointArray, inSectionStart + inMoveTo - inSectionEnd - 1,
510 inSectionEnd - inSectionStart + 1);
511 // unchanged bit after
512 if (inSectionEnd < (_numPoints - 1)) {
513 System.arraycopy(_dataPoints, inMoveTo, newPointArray, inMoveTo, _numPoints - inMoveTo);
516 // Copy array references
517 _dataPoints = newPointArray;
526 * Interpolate extra points between two selected ones
527 * @param inStartIndex start index of interpolation
528 * @param inNumPoints num points to insert
529 * @return true if successful
531 public boolean interpolate(int inStartIndex, int inNumPoints)
534 if (inStartIndex < 0 || inStartIndex >= _numPoints || inNumPoints <= 0)
537 // get start and end points
538 DataPoint startPoint = getPoint(inStartIndex);
539 DataPoint endPoint = getPoint(inStartIndex + 1);
541 // Make array of points to insert
542 DataPoint[] insertedPoints = startPoint.interpolate(endPoint, inNumPoints);
544 // Insert points into track
545 return insertRange(insertedPoints, inStartIndex + 1);
550 * Average selected points
551 * @param inStartIndex start index of selection
552 * @param inEndIndex end index of selection
553 * @return true if successful
555 public boolean average(int inStartIndex, int inEndIndex)
558 if (inStartIndex < 0 || inStartIndex >= _numPoints || inEndIndex <= inStartIndex)
561 DataPoint startPoint = getPoint(inStartIndex);
562 double firstLatitude = startPoint.getLatitude().getDouble();
563 double firstLongitude = startPoint.getLongitude().getDouble();
564 double latitudeDiff = 0.0, longitudeDiff = 0.0;
565 double totalAltitude = 0;
566 int numAltitudes = 0;
567 Altitude.Format altFormat = Config.getConfigBoolean(Config.KEY_METRIC_UNITS)?Altitude.Format.METRES:Altitude.Format.FEET;
568 // loop between start and end points
569 for (int i=inStartIndex; i<= inEndIndex; i++)
571 DataPoint currPoint = getPoint(i);
572 latitudeDiff += (currPoint.getLatitude().getDouble() - firstLatitude);
573 longitudeDiff += (currPoint.getLongitude().getDouble() - firstLongitude);
574 if (currPoint.hasAltitude()) {
575 totalAltitude += currPoint.getAltitude().getValue(altFormat);
579 int numPoints = inEndIndex - inStartIndex + 1;
580 double meanLatitude = firstLatitude + (latitudeDiff / numPoints);
581 double meanLongitude = firstLongitude + (longitudeDiff / numPoints);
582 Altitude meanAltitude = null;
583 if (numAltitudes > 0) {meanAltitude = new Altitude((int) (totalAltitude / numAltitudes), altFormat);}
585 DataPoint insertedPoint = new DataPoint(new Latitude(meanLatitude, Coordinate.FORMAT_NONE),
586 new Longitude(meanLongitude, Coordinate.FORMAT_NONE), meanAltitude);
587 // Make into singleton
588 insertedPoint.setSegmentStart(true);
589 DataPoint nextPoint = getNextTrackPoint(inEndIndex+1);
590 if (nextPoint != null) {nextPoint.setSegmentStart(true);}
591 // Insert points into track
592 return insertRange(new DataPoint[] {insertedPoint}, inEndIndex + 1);
597 * Append the specified points to the end of the track
598 * @param inPoints DataPoint objects to add
600 public void appendPoints(DataPoint[] inPoints)
602 // Insert points into track
603 if (inPoints != null && inPoints.length > 0)
605 insertRange(inPoints, _numPoints);
607 // needs to be scaled again to recalc x, y
609 UpdateMessageBroker.informSubscribers();
613 //////// information methods /////////////
617 * Get the point at the given index
618 * @param inPointNum index number, starting at 0
619 * @return DataPoint object, or null if out of range
621 public DataPoint getPoint(int inPointNum)
623 if (inPointNum > -1 && inPointNum < getNumPoints())
625 return _dataPoints[inPointNum];
632 * @return altitude range of points as AltitudeRange object
634 public AltitudeRange getAltitudeRange()
636 if (!_scaled) scalePoints();
637 return _altitudeRange;
641 * @return the number of (valid) points in the track
643 public int getNumPoints()
649 * @return The range of x values as a DoubleRange object
651 public DoubleRange getXRange()
653 if (!_scaled) scalePoints();
658 * @return The range of y values as a DoubleRange object
660 public DoubleRange getYRange()
662 if (!_scaled) scalePoints();
667 * @return The range of lat values as a DoubleRange object
669 public DoubleRange getLatRange()
671 if (!_scaled) scalePoints();
675 * @return The range of lon values as a DoubleRange object
677 public DoubleRange getLonRange()
679 if (!_scaled) scalePoints();
684 * @param inPointNum point index, starting at 0
685 * @return scaled x value of specified point
687 public double getX(int inPointNum)
689 if (!_scaled) scalePoints();
690 return _xValues[inPointNum];
694 * @param inPointNum point index, starting at 0
695 * @return scaled y value of specified point
697 public double getY(int inPointNum)
699 if (!_scaled) scalePoints();
700 return _yValues[inPointNum];
704 * @return the master field list
706 public FieldList getFieldList()
708 return _masterFieldList;
713 * Checks if any data exists for the specified field
714 * @param inField Field to examine
715 * @return true if data exists for this field
717 public boolean hasData(Field inField)
719 // Don't use this method for altitudes
720 if (inField.equals(Field.ALTITUDE)) {return hasAltitudeData();}
721 return hasData(inField, 0, _numPoints-1);
726 * Checks if any data exists for the specified field in the specified range
727 * @param inField Field to examine
728 * @param inStart start of range to check
729 * @param inEnd end of range to check (inclusive)
730 * @return true if data exists for this field
732 public boolean hasData(Field inField, int inStart, int inEnd)
734 // Loop over selected point range
735 for (int i=inStart; i<=inEnd; i++)
737 if (_dataPoints[i].getFieldValue(inField) != null)
739 // Check altitudes and timestamps
740 if ((inField != Field.ALTITUDE || _dataPoints[i].getAltitude().isValid())
741 && (inField != Field.TIMESTAMP || _dataPoints[i].getTimestamp().isValid()))
751 * @return true if track has altitude data
753 public boolean hasAltitudeData()
755 for (int i=0; i<_numPoints; i++) {
756 if (_dataPoints[i].hasAltitude()) {return true;}
762 * @return true if track contains at least one trackpoint
764 public boolean hasTrackPoints()
766 if (!_scaled) scalePoints();
767 return _hasTrackpoint;
771 * @return true if track contains waypoints
773 public boolean hasWaypoints()
775 if (!_scaled) scalePoints();
780 * @return true if track contains any points marked for deletion
782 public boolean hasMarkedPoints()
784 if (_numPoints < 1) {
787 // Loop over points looking for any marked for deletion
788 for (int i=0; i<=_numPoints-1; i++)
790 if (_dataPoints[i] != null && _dataPoints[i].getDeleteFlag()) {
799 * Clear all the deletion markers
801 public void clearDeletionMarkers()
803 for (int i=0; i<_numPoints; i++)
805 _dataPoints[i].setMarkedForDeletion(false);
810 * Collect all the waypoints into the given List
811 * @param inList List to fill with waypoints
813 public void getWaypoints(List<DataPoint> inList)
817 // loop over points and copy all waypoints into list
818 for (int i=0; i<=_numPoints-1; i++)
820 if (_dataPoints[i] != null && _dataPoints[i].isWaypoint())
822 inList.add(_dataPoints[i]);
829 * Search for the given Point in the track and return the index
830 * @param inPoint Point to look for
831 * @return index of Point, if any or -1 if not found
833 public int getPointIndex(DataPoint inPoint)
837 // Loop over points in track
838 for (int i=0; i<=_numPoints-1; i++)
840 if (_dataPoints[i] == inPoint)
851 ///////// Internal processing methods ////////////////
855 * Scale all the points in the track to gain x and y values
858 private void scalePoints()
860 // Loop through all points in track, to see limits of lat, long and altitude
861 _longRange = new DoubleRange();
862 _latRange = new DoubleRange();
863 _altitudeRange = new AltitudeRange();
865 _hasWaypoint = false; _hasTrackpoint = false;
866 for (p=0; p < getNumPoints(); p++)
868 DataPoint point = getPoint(p);
869 if (point != null && point.isValid())
871 _longRange.addValue(point.getLongitude().getDouble());
872 _latRange.addValue(point.getLatitude().getDouble());
873 if (point.getAltitude().isValid())
875 _altitudeRange.addValue(point.getAltitude());
877 if (point.isWaypoint())
880 _hasTrackpoint = true;
884 // Loop over points and calculate scales
885 _xValues = new double[getNumPoints()];
886 _yValues = new double[getNumPoints()];
887 _xRange = new DoubleRange();
888 _yRange = new DoubleRange();
889 for (p=0; p < getNumPoints(); p++)
891 DataPoint point = getPoint(p);
894 _xValues[p] = MapUtils.getXFromLongitude(point.getLongitude().getDouble());
895 _xRange.addValue(_xValues[p]);
896 _yValues[p] = MapUtils.getYFromLatitude(point.getLatitude().getDouble());
897 _yRange.addValue(_yValues[p]);
905 * Find the nearest point to the specified x and y coordinates
906 * or -1 if no point is within the specified max distance
907 * @param inX x coordinate
908 * @param inY y coordinate
909 * @param inMaxDist maximum distance from selected coordinates
910 * @param inJustTrackPoints true if waypoints should be ignored
911 * @return index of nearest point or -1 if not found
913 public int getNearestPointIndex(double inX, double inY, double inMaxDist, boolean inJustTrackPoints)
915 int nearestPoint = 0;
916 double nearestDist = -1.0;
918 for (int i=0; i < getNumPoints(); i++)
920 if (!inJustTrackPoints || !_dataPoints[i].isWaypoint())
922 currDist = Math.abs(_xValues[i] - inX) + Math.abs(_yValues[i] - inY);
923 if (currDist < nearestDist || nearestDist < 0.0)
926 nearestDist = currDist;
930 // Check whether it's within required distance
931 if (nearestDist > inMaxDist && inMaxDist > 0.0)
939 * Get the next track point starting from the given index
940 * @param inStartIndex index to start looking from
941 * @return next track point, or null if end of data reached
943 public DataPoint getNextTrackPoint(int inStartIndex)
945 return getNextTrackPoint(inStartIndex, _numPoints, true);
949 * Get the next track point in the given range
950 * @param inStartIndex index to start looking from
951 * @param inEndIndex index to stop looking
952 * @return next track point, or null if end of data reached
954 public DataPoint getNextTrackPoint(int inStartIndex, int inEndIndex)
956 return getNextTrackPoint(inStartIndex, inEndIndex, true);
960 * Get the previous track point starting from the given index
961 * @param inStartIndex index to start looking from
962 * @return next track point, or null if end of data reached
964 public DataPoint getPreviousTrackPoint(int inStartIndex)
966 return getNextTrackPoint(inStartIndex, _numPoints, false);
970 * Get the next track point starting from the given index
971 * @param inStartIndex index to start looking from
972 * @param inEndIndex index to stop looking (inclusive)
973 * @param inCountUp true for next, false for previous
974 * @return next track point, or null if end of data reached
976 private DataPoint getNextTrackPoint(int inStartIndex, int inEndIndex, boolean inCountUp)
978 // Loop forever over points
979 int increment = inCountUp?1:-1;
980 for (int i=inStartIndex; i<=inEndIndex; i+=increment)
982 DataPoint point = getPoint(i);
983 // Exit if end of data reached - there wasn't a track point
984 if (point == null) {return null;}
985 if (point.isValid() && !point.isWaypoint()) {
986 // next track point found
994 * Shift all the segment start flags in the given range by 1
995 * Method used by reverse range and its undo
996 * @param inStartIndex start of range, inclusive
997 * @param inEndIndex end of range, inclusive
999 public void shiftSegmentStarts(int inStartIndex, int inEndIndex)
1001 boolean prevFlag = true;
1002 boolean currFlag = true;
1003 for (int i=inStartIndex; i<= inEndIndex; i++)
1005 DataPoint point = getPoint(i);
1006 if (point != null && !point.isWaypoint())
1009 currFlag = point.getSegmentStart();
1011 point.setSegmentStart(prevFlag);
1012 prevFlag = currFlag;
1017 ////////////////// Cloning and replacing ///////////////////
1020 * Clone the array of DataPoints
1021 * @return shallow copy of DataPoint objects
1023 public DataPoint[] cloneContents()
1025 DataPoint[] clone = new DataPoint[getNumPoints()];
1026 System.arraycopy(_dataPoints, 0, clone, 0, getNumPoints());
1032 * Clone the specified range of data points
1033 * @param inStart start index (inclusive)
1034 * @param inEnd end index (inclusive)
1035 * @return shallow copy of DataPoint objects
1037 public DataPoint[] cloneRange(int inStart, int inEnd)
1039 int numSelected = 0;
1040 if (inEnd >= 0 && inEnd >= inStart)
1042 numSelected = inEnd - inStart + 1;
1044 DataPoint[] result = new DataPoint[numSelected>0?numSelected:0];
1045 if (numSelected > 0)
1047 System.arraycopy(_dataPoints, inStart, result, 0, numSelected);
1054 * Re-insert the specified point at the given index
1055 * @param inPoint point to insert
1056 * @param inIndex index at which to insert the point
1057 * @return true if it worked, false otherwise
1059 public boolean insertPoint(DataPoint inPoint, int inIndex)
1061 if (inIndex > _numPoints || inPoint == null)
1065 // Make new array to copy points over to
1066 DataPoint[] newPointArray = new DataPoint[_numPoints + 1];
1069 System.arraycopy(_dataPoints, 0, newPointArray, 0, inIndex);
1071 newPointArray[inIndex] = inPoint;
1072 if (inIndex < _numPoints)
1074 System.arraycopy(_dataPoints, inIndex, newPointArray, inIndex+1, _numPoints - inIndex);
1076 // Change over to new array
1077 _dataPoints = newPointArray;
1079 // needs to be scaled again
1081 UpdateMessageBroker.informSubscribers();
1087 * Re-insert the specified point range at the given index
1088 * @param inPoints point array to insert
1089 * @param inIndex index at which to insert the points
1090 * @return true if it worked, false otherwise
1092 public boolean insertRange(DataPoint[] inPoints, int inIndex)
1094 if (inIndex > _numPoints || inPoints == null)
1098 // Make new array to copy points over to
1099 DataPoint[] newPointArray = new DataPoint[_numPoints + inPoints.length];
1102 System.arraycopy(_dataPoints, 0, newPointArray, 0, inIndex);
1104 System.arraycopy(inPoints, 0, newPointArray, inIndex, inPoints.length);
1105 if (inIndex < _numPoints)
1107 System.arraycopy(_dataPoints, inIndex, newPointArray, inIndex+inPoints.length, _numPoints - inIndex);
1109 // Change over to new array
1110 _dataPoints = newPointArray;
1111 _numPoints += inPoints.length;
1112 // needs to be scaled again
1114 UpdateMessageBroker.informSubscribers();
1120 * Replace the track contents with the given point array
1121 * @param inContents array of DataPoint objects
1122 * @return true on success
1124 public boolean replaceContents(DataPoint[] inContents)
1126 // master field array stays the same
1127 // (would need to store field array too if we wanted to redo a load)
1128 // replace data array
1129 _dataPoints = inContents;
1130 _numPoints = _dataPoints.length;
1132 UpdateMessageBroker.informSubscribers();
1138 * Edit the specified point
1139 * @param inPoint point to edit
1140 * @param inEditList list of edits to make
1141 * @param inUndo true if undo operation, false otherwise
1142 * @return true if successful
1144 public boolean editPoint(DataPoint inPoint, FieldEditList inEditList, boolean inUndo)
1146 if (inPoint != null && inEditList != null && inEditList.getNumEdits() > 0)
1148 // remember if coordinates have changed
1149 boolean coordsChanged = false;
1150 // go through edits one by one
1151 int numEdits = inEditList.getNumEdits();
1152 for (int i=0; i<numEdits; i++)
1154 FieldEdit edit = inEditList.getEdit(i);
1155 Field editField = edit.getField();
1156 inPoint.setFieldValue(editField, edit.getValue(), inUndo);
1157 // Check that master field list has this field already (maybe point name has been added)
1158 if (!_masterFieldList.contains(editField)) {
1159 _masterFieldList.extendList(editField);
1161 // check coordinates
1162 coordsChanged |= (editField.equals(Field.LATITUDE)
1163 || editField.equals(Field.LONGITUDE) || editField.equals(Field.ALTITUDE));
1165 // set photo status if coordinates have changed
1166 if (inPoint.getPhoto() != null && coordsChanged)
1168 inPoint.getPhoto().setCurrentStatus(Photo.Status.CONNECTED);
1170 // point possibly needs to be scaled again
1172 // trigger listeners
1173 UpdateMessageBroker.informSubscribers();