1 package tim.prune.data;
5 import tim.prune.UpdateMessageBroker;
6 import tim.prune.edit.FieldEdit;
7 import tim.prune.edit.FieldEditList;
8 import tim.prune.gui.map.MapUtils;
12 * Class to hold all track information,
13 * including track points and waypoints
18 private DataPoint[] _dataPoints = null;
20 private double[] _xValues = null;
21 private double[] _yValues = null;
22 private double[] _xValuesNew = null;
23 private double[] _yValuesNew = null;
24 private boolean _scaled = false;
25 private int _numPoints = 0;
26 private boolean _mixedData = 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, int 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);
91 ////////////////// Modification methods //////////////////////
95 * Combine this Track with new data
96 * @param inOtherTrack other track to combine
98 public void combine(Track inOtherTrack)
101 _masterFieldList = _masterFieldList.merge(inOtherTrack._masterFieldList);
102 // expand data array and add other track's data points
103 int totalPoints = getNumPoints() + inOtherTrack.getNumPoints();
104 DataPoint[] mergedPoints = new DataPoint[totalPoints];
105 System.arraycopy(_dataPoints, 0, mergedPoints, 0, getNumPoints());
106 System.arraycopy(inOtherTrack._dataPoints, 0, mergedPoints, getNumPoints(), inOtherTrack.getNumPoints());
107 _dataPoints = mergedPoints;
108 // combine point count
109 _numPoints = totalPoints;
110 // needs to be scaled again
113 UpdateMessageBroker.informSubscribers();
118 * Crop the track to the given size - subsequent points are not (yet) deleted
119 * @param inNewSize new number of points in track
121 public void cropTo(int inNewSize)
123 if (inNewSize >= 0 && inNewSize < getNumPoints())
125 _numPoints = inNewSize;
126 // needs to be scaled again
128 UpdateMessageBroker.informSubscribers();
134 * Compress the track to the given resolution
135 * @param inResolution resolution
136 * @return number of points deleted
138 public int compress(int inResolution)
140 // (maybe should be separate thread?)
141 // (maybe should be in separate class?)
142 // (maybe should be based on subtended angles instead of distances?)
143 // Suggestion: Find last track point, don't delete it (or maybe preserve first and last of each segment?)
145 if (inResolution <= 0) return 0;
147 // Establish range of track and minimum range between points
149 double wholeScale = _xRange.getMaximum() - _xRange.getMinimum();
150 double yscale = _yRange.getMaximum() - _yRange.getMinimum();
151 if (yscale > wholeScale) wholeScale = yscale;
152 double minDist = wholeScale / inResolution;
154 // Keep track of segment start flags of the deleted points
155 boolean setSegment = false;
156 // Copy selected points
157 DataPoint[] newPointArray = new DataPoint[_numPoints];
158 int[] pointIndices = new int[_numPoints];
159 for (int i=0; i<_numPoints; i++)
161 DataPoint point = _dataPoints[i];
162 boolean keepPoint = true;
163 // Don't delete waypoints or photo points
164 if (!point.isWaypoint() && point.getPhoto() == null)
166 // go through newPointArray to check for range
167 for (int j=0; j<numCopied && keepPoint; j++)
169 // calculate distance between point j and current point
170 double pointDist = Math.abs(_xValues[i] - _xValues[pointIndices[j]])
171 + Math.abs(_yValues[i] - _yValues[pointIndices[j]]);
172 if (pointDist < minDist)
178 newPointArray[numCopied] = point;
179 pointIndices[numCopied] = i;
181 // set segment flag if it's the first track point
182 if (setSegment && !point.isWaypoint())
184 point.setSegmentStart(true);
190 // point will be removed, so check segment flag
191 if (point.getSegmentStart()) {setSegment = true;}
195 // Copy array references
196 int numDeleted = _numPoints - numCopied;
199 _dataPoints = new DataPoint[numCopied];
200 System.arraycopy(newPointArray, 0, _dataPoints, 0, numCopied);
201 _numPoints = _dataPoints.length;
209 * Halve the track by deleting alternate points
210 * @return number of points deleted
214 if (_numPoints < 100) return 0;
215 int newSize = _numPoints / 2;
216 int numDeleted = _numPoints - newSize;
217 DataPoint[] newPointArray = new DataPoint[newSize];
218 // Delete alternate points
219 for (int i=0; i<newSize; i++)
220 newPointArray[i] = _dataPoints[i*2];
221 // Copy array references
222 _dataPoints = newPointArray;
223 _numPoints = _dataPoints.length;
225 UpdateMessageBroker.informSubscribers();
231 * Delete the specified point
232 * @param inIndex point index
233 * @return true if successful
235 public boolean deletePoint(int inIndex)
237 boolean answer = deleteRange(inIndex, inIndex);
243 * Delete the specified range of points from the Track
244 * @param inStart start of range (inclusive)
245 * @param inEnd end of range (inclusive)
246 * @return true if successful
248 public boolean deleteRange(int inStart, int inEnd)
250 if (inStart < 0 || inEnd < 0 || inEnd < inStart)
252 // no valid range selected so can't delete
255 // check through range to be deleted, and see if any new segment flags present
256 boolean hasSegmentStart = false;
257 DataPoint nextTrackPoint = getNextTrackPoint(inEnd+1);
258 if (nextTrackPoint != null) {
259 for (int i=inStart; i<=inEnd && !hasSegmentStart; i++) {
260 hasSegmentStart |= _dataPoints[i].getSegmentStart();
262 // If segment break found, make sure next trackpoint also has break
263 if (hasSegmentStart) {nextTrackPoint.setSegmentStart(true);}
265 // valid range, let's delete it
266 int numToDelete = inEnd - inStart + 1;
267 DataPoint[] newPointArray = new DataPoint[_numPoints - numToDelete];
268 // Copy points before the selected range
271 System.arraycopy(_dataPoints, 0, newPointArray, 0, inStart);
273 // Copy points after the deleted one(s)
274 if (inEnd < (_numPoints - 1))
276 System.arraycopy(_dataPoints, inEnd + 1, newPointArray, inStart,
277 _numPoints - inEnd - 1);
279 // Copy points over original array
280 _dataPoints = newPointArray;
281 _numPoints -= numToDelete;
282 // needs to be scaled again
289 * Delete all the duplicate points in the track
290 * @return number of points deleted
292 public int deleteDuplicates()
294 // loop through Track counting duplicates first
295 boolean[] dupes = new boolean[_numPoints];
298 for (i=1; i<_numPoints; i++)
300 DataPoint p1 = _dataPoints[i];
301 // Loop through all points before this one
302 for (j=0; j<i && !dupes[i]; j++)
304 DataPoint p2 = _dataPoints[j];
305 if (p1.isDuplicate(p2))
314 // Make new resized array and copy DataPoints over
315 DataPoint[] newPointArray = new DataPoint[_numPoints - numDupes];
317 for (i=0; i<_numPoints; i++)
321 newPointArray[j] = _dataPoints[i];
325 // Copy array references
326 _dataPoints = newPointArray;
327 _numPoints = _dataPoints.length;
335 * Reverse the specified range of points
336 * @param inStart start index
337 * @param inEnd end index
338 * @return true if successful, false otherwise
340 public boolean reverseRange(int inStart, int inEnd)
342 if (inStart < 0 || inEnd < 0 || inStart >= inEnd || inEnd >= _numPoints)
346 // calculate how many point swaps are required
347 int numPointsToReverse = (inEnd - inStart + 1) / 2;
349 for (int i=0; i<numPointsToReverse; i++)
351 // swap pairs of points
352 p = _dataPoints[inStart + i];
353 _dataPoints[inStart + i] = _dataPoints[inEnd - i];
354 _dataPoints[inEnd - i] = p;
356 // adjust segment starts
357 shiftSegmentStarts(inStart, inEnd);
358 // Find first track point and following track point, and set segment starts to true
359 DataPoint firstTrackPoint = getNextTrackPoint(inStart);
360 if (firstTrackPoint != null) {firstTrackPoint.setSegmentStart(true);}
361 DataPoint nextTrackPoint = getNextTrackPoint(inEnd+1);
362 if (nextTrackPoint != null) {nextTrackPoint.setSegmentStart(true);}
363 // needs to be scaled again
365 UpdateMessageBroker.informSubscribers();
371 * Add the given time offset to the specified range
372 * @param inStart start of range
373 * @param inEnd end of range
374 * @param inOffset offset to add (-ve to subtract)
375 * @return true on success
377 public boolean addTimeOffset(int inStart, int inEnd, long inOffset)
380 if (inStart < 0 || inEnd < 0 || inStart >= inEnd || inEnd >= _numPoints) {
383 boolean foundTimestamp = false;
384 // Loop over all points within range
385 for (int i=inStart; i<=inEnd; i++)
387 Timestamp timestamp = _dataPoints[i].getTimestamp();
388 if (timestamp != null)
390 // This point has a timestamp so add the offset to it
391 foundTimestamp = true;
392 timestamp.addOffset(inOffset);
395 return foundTimestamp;
400 * Merge the track segments within the given range
401 * @param inStart start index
402 * @param inEnd end index
403 * @return true if successful
405 public boolean mergeTrackSegments(int inStart, int inEnd)
407 boolean firstTrackPoint = true;
408 // Loop between start and end
409 for (int i=inStart; i<=inEnd; i++) {
410 DataPoint point = getPoint(i);
411 // Set all segments to false apart from first track point
412 if (point != null && !point.isWaypoint()) {
413 point.setSegmentStart(firstTrackPoint);
414 firstTrackPoint = false;
417 // Find following track point, if any
418 DataPoint nextPoint = getNextTrackPoint(inEnd+1);
419 if (nextPoint != null) {nextPoint.setSegmentStart(true);}
420 UpdateMessageBroker.informSubscribers();
425 * Collect all waypoints to the start or end of the track
426 * @param inAtStart true to collect at start, false for end
427 * @return true if successful, false if no change
429 public boolean collectWaypoints(boolean inAtStart)
431 // Check for mixed data, numbers of waypoints & nons
432 int numWaypoints = 0, numNonWaypoints = 0;
433 boolean wayAfterNon = false, nonAfterWay = false;
434 DataPoint[] waypoints = new DataPoint[_numPoints];
435 DataPoint[] nonWaypoints = new DataPoint[_numPoints];
436 DataPoint point = null;
437 for (int i=0; i<_numPoints; i++)
439 point = _dataPoints[i];
440 if (point.isWaypoint())
442 waypoints[numWaypoints] = point;
444 wayAfterNon |= (numNonWaypoints > 0);
448 nonWaypoints[numNonWaypoints] = point;
450 nonAfterWay |= (numWaypoints > 0);
453 // Exit if the data is already in the specified order
454 if (numWaypoints == 0 || numNonWaypoints == 0
455 || (inAtStart && !wayAfterNon && nonAfterWay)
456 || (!inAtStart && wayAfterNon && !nonAfterWay))
461 // Copy the arrays back into _dataPoints in the specified order
464 System.arraycopy(waypoints, 0, _dataPoints, 0, numWaypoints);
465 System.arraycopy(nonWaypoints, 0, _dataPoints, numWaypoints, numNonWaypoints);
469 System.arraycopy(nonWaypoints, 0, _dataPoints, 0, numNonWaypoints);
470 System.arraycopy(waypoints, 0, _dataPoints, numNonWaypoints, numWaypoints);
472 // needs to be scaled again
474 UpdateMessageBroker.informSubscribers();
480 * Interleave all waypoints by each nearest track point
481 * @return true if successful, false if no change
483 public boolean interleaveWaypoints()
485 // Separate waypoints and find nearest track point
486 int numWaypoints = 0;
487 DataPoint[] waypoints = new DataPoint[_numPoints];
488 int[] pointIndices = new int[_numPoints];
489 DataPoint point = null;
491 for (i=0; i<_numPoints; i++)
493 point = _dataPoints[i];
494 if (point.isWaypoint())
496 waypoints[numWaypoints] = point;
497 pointIndices[numWaypoints] = getNearestPointIndex(
498 _xValues[i], _yValues[i], -1.0, true);
502 // Exit if data not mixed
503 if (numWaypoints == 0 || numWaypoints == _numPoints)
506 // Loop round points copying to correct order
507 DataPoint[] dataCopy = new DataPoint[_numPoints];
509 for (i=0; i<_numPoints; i++)
511 point = _dataPoints[i];
512 // if it's a track point, copy it
513 if (!point.isWaypoint())
515 dataCopy[copyIndex] = point;
518 // check for waypoints with this index
519 for (int j=0; j<numWaypoints; j++)
521 if (pointIndices[j] == i)
523 dataCopy[copyIndex] = waypoints[j];
528 // Copy data back to track
529 _dataPoints = dataCopy;
530 // needs to be scaled again to recalc x, y
532 UpdateMessageBroker.informSubscribers();
538 * Cut and move the specified section
539 * @param inSectionStart start index of section
540 * @param inSectionEnd end index of section
541 * @param inMoveTo index of move to point
542 * @return true if move successful
544 public boolean cutAndMoveSection(int inSectionStart, int inSectionEnd, int inMoveTo)
546 // Check that indices make sense
547 if (inSectionStart > 0 && inSectionEnd > inSectionStart && inMoveTo > 0
548 && (inMoveTo < inSectionStart || inMoveTo > (inSectionEnd+1)))
550 // do the cut and move
551 DataPoint[] newPointArray = new DataPoint[_numPoints];
552 // System.out.println("Cut/move section (" + inSectionStart + " - " + inSectionEnd + ") to before point " + inMoveTo);
553 // Is it a forward copy or a backward copy?
554 if (inSectionStart > inMoveTo)
556 int sectionLength = inSectionEnd - inSectionStart + 1;
557 // move section to earlier point
559 System.arraycopy(_dataPoints, 0, newPointArray, 0, inMoveTo); // unchanged points before
560 System.arraycopy(_dataPoints, inSectionStart, newPointArray, inMoveTo, sectionLength); // moved bit
561 // after insertion point, before moved bit
562 if (inSectionStart > (inMoveTo + 1))
563 System.arraycopy(_dataPoints, inMoveTo, newPointArray, inMoveTo + sectionLength, inSectionStart - inMoveTo);
565 if (inSectionEnd < (_numPoints - 1))
566 System.arraycopy(_dataPoints, inSectionEnd+1, newPointArray, inSectionEnd+1, _numPoints - inSectionEnd - 1);
570 // Move section to later point
571 if (inSectionStart > 0)
572 System.arraycopy(_dataPoints, 0, newPointArray, 0, inSectionStart); // unchanged points before
573 // from end of section to move to point
574 if (inMoveTo > (inSectionEnd + 1))
575 System.arraycopy(_dataPoints, inSectionEnd+1, newPointArray, inSectionStart, inMoveTo - inSectionEnd - 1);
577 System.arraycopy(_dataPoints, inSectionStart, newPointArray, inSectionStart + inMoveTo - inSectionEnd - 1,
578 inSectionEnd - inSectionStart + 1);
579 // unchanged bit after
580 if (inSectionEnd < (_numPoints - 1))
581 System.arraycopy(_dataPoints, inMoveTo, newPointArray, inMoveTo, _numPoints - inMoveTo);
583 // Copy array references
584 _dataPoints = newPointArray;
593 * Interpolate extra points between two selected ones
594 * @param inStartIndex start index of interpolation
595 * @param inNumPoints num points to insert
596 * @return true if successful
598 public boolean interpolate(int inStartIndex, int inNumPoints)
601 if (inStartIndex < 0 || inStartIndex >= _numPoints || inNumPoints <= 0)
604 // get start and end points
605 DataPoint startPoint = getPoint(inStartIndex);
606 DataPoint endPoint = getPoint(inStartIndex + 1);
608 // Make array of points to insert
609 DataPoint[] insertedPoints = startPoint.interpolate(endPoint, inNumPoints);
611 // Insert points into track
612 return insertRange(insertedPoints, inStartIndex + 1);
617 * Append the specified points to the end of the track
618 * @param inPoints DataPoint objects to add
620 public void appendPoints(DataPoint[] inPoints)
622 // Insert points into track
623 if (inPoints != null && inPoints.length > 0)
625 insertRange(inPoints, _numPoints);
627 // needs to be scaled again to recalc x, y
629 UpdateMessageBroker.informSubscribers();
633 //////// information methods /////////////
637 * Get the point at the given index
638 * @param inPointNum index number, starting at 0
639 * @return DataPoint object, or null if out of range
641 public DataPoint getPoint(int inPointNum)
643 if (inPointNum > -1 && inPointNum < getNumPoints())
645 return _dataPoints[inPointNum];
652 * @return altitude range of points as AltitudeRange object
654 public AltitudeRange getAltitudeRange()
656 if (!_scaled) scalePoints();
657 return _altitudeRange;
660 * @return the number of (valid) points in the track
662 public int getNumPoints()
668 * @return The range of x values as a DoubleRange object
670 public DoubleRange getXRange()
672 if (!_scaled) scalePoints();
677 * @return The range of y values as a DoubleRange object
679 public DoubleRange getYRange()
681 if (!_scaled) scalePoints();
686 * @return The range of lat values as a DoubleRange object
688 public DoubleRange getLatRange()
690 if (!_scaled) scalePoints();
694 * @return The range of lon values as a DoubleRange object
696 public DoubleRange getLonRange()
698 if (!_scaled) scalePoints();
703 * @param inPointNum point index, starting at 0
704 * @return scaled x value of specified point
706 public double getX(int inPointNum)
708 if (!_scaled) scalePoints();
709 return _xValues[inPointNum];
713 * @param inPointNum point index, starting at 0
714 * @return scaled y value of specified point
716 public double getY(int inPointNum)
718 if (!_scaled) scalePoints();
719 return _yValues[inPointNum];
723 * @param inPointNum point index, starting at 0
724 * @return scaled x value of specified point
726 public double getXNew(int inPointNum)
728 if (!_scaled) scalePoints();
729 return _xValuesNew[inPointNum];
733 * @param inPointNum point index, starting at 0
734 * @return scaled y value of specified point
736 public double getYNew(int inPointNum)
738 if (!_scaled) scalePoints();
739 return _yValuesNew[inPointNum];
743 * @return the master field list
745 public FieldList getFieldList()
747 return _masterFieldList;
752 * Checks if any data exists for the specified field
753 * @param inField Field to examine
754 * @return true if data exists for this field
756 public boolean hasData(Field inField)
758 return hasData(inField, 0, _numPoints-1);
763 * Checks if any data exists for the specified field in the specified range
764 * @param inField Field to examine
765 * @param inStart start of range to check
766 * @param inEnd end of range to check (inclusive)
767 * @return true if data exists for this field
769 public boolean hasData(Field inField, int inStart, int inEnd)
771 for (int i=inStart; i<=inEnd; i++)
773 if (_dataPoints[i].getFieldValue(inField) != null)
783 * @return true if track contains waypoints and trackpoints
785 public boolean hasMixedData()
787 if (!_scaled) scalePoints();
793 * Collect all the waypoints into the given List
794 * @param inList List to fill with waypoints
796 public void getWaypoints(List inList)
800 // loop over points and copy all waypoints into list
801 for (int i=0; i<=_numPoints-1; i++)
803 if (_dataPoints[i] != null && _dataPoints[i].isWaypoint())
805 inList.add(_dataPoints[i]);
812 * Search for the given Point in the track and return the index
813 * @param inPoint Point to look for
814 * @return index of Point, if any or -1 if not found
816 public int getPointIndex(DataPoint inPoint)
820 // Loop over points in track
821 for (int i=0; i<=_numPoints-1; i++)
823 if (_dataPoints[i] == inPoint)
834 ///////// Internal processing methods ////////////////
838 * Scale all the points in the track to gain x and y values
841 private void scalePoints()
843 // Loop through all points in track, to see limits of lat, long and altitude
844 _longRange = new DoubleRange();
845 _latRange = new DoubleRange();
846 _altitudeRange = new AltitudeRange();
848 boolean hasWaypoint = false, hasTrackpoint = false;
849 for (p=0; p < getNumPoints(); p++)
851 DataPoint point = getPoint(p);
852 if (point != null && point.isValid())
854 _longRange.addValue(point.getLongitude().getDouble());
855 _latRange.addValue(point.getLatitude().getDouble());
856 if (point.getAltitude().isValid())
858 _altitudeRange.addValue(point.getAltitude());
860 if (point.isWaypoint())
863 hasTrackpoint = true;
866 _mixedData = hasWaypoint && hasTrackpoint;
868 // Use medians to centre at 0
869 double longMedian = (_longRange.getMaximum() + _longRange.getMinimum()) / 2.0;
870 double latMedian = (_latRange.getMaximum() + _latRange.getMinimum()) / 2.0;
871 double longFactor = Math.cos(latMedian / 180.0 * Math.PI); // Function of median latitude
873 // Loop over points and calculate scales
874 _xValues = new double[getNumPoints()];
875 _yValues = new double[getNumPoints()];
876 _xValuesNew = new double[getNumPoints()];
877 _yValuesNew = new double[getNumPoints()];
878 _xRange = new DoubleRange();
879 _yRange = new DoubleRange();
880 for (p=0; p < getNumPoints(); p++)
882 DataPoint point = getPoint(p);
885 _xValues[p] = (point.getLongitude().getDouble() - longMedian) * longFactor;
886 _xRange.addValue(_xValues[p]);
887 _xValuesNew[p] = MapUtils.getXFromLongitude(point.getLongitude().getDouble());
888 _yValues[p] = (point.getLatitude().getDouble() - latMedian);
889 _yRange.addValue(_yValues[p]);
890 _yValuesNew[p] = MapUtils.getYFromLatitude(point.getLatitude().getDouble());
898 * Find the nearest point to the specified x and y coordinates
899 * or -1 if no point is within the specified max distance
900 * @param inX x coordinate
901 * @param inY y coordinate
902 * @param inMaxDist maximum distance from selected coordinates
903 * @param inJustTrackPoints true if waypoints should be ignored
904 * @return index of nearest point or -1 if not found
906 public int getNearestPointIndex(double inX, double inY, double inMaxDist, boolean inJustTrackPoints)
908 int nearestPoint = 0;
909 double nearestDist = -1.0;
911 for (int i=0; i < getNumPoints(); i++)
913 if (!inJustTrackPoints || !_dataPoints[i].isWaypoint())
915 currDist = Math.abs(_xValues[i] - inX) + Math.abs(_yValues[i] - inY);
916 if (currDist < nearestDist || nearestDist < 0.0)
919 nearestDist = currDist;
923 // Check whether it's within required distance
924 if (nearestDist > inMaxDist && inMaxDist > 0.0)
933 * Find the nearest point to the specified x and y coordinates
934 * or -1 if no point is within the specified max distance
935 * @param inX x coordinate
936 * @param inY y coordinate
937 * @param inMaxDist maximum distance from selected coordinates
938 * @param inJustTrackPoints true if waypoints should be ignored
939 * @return index of nearest point or -1 if not found
941 public int getNearestPointIndexNew(double inX, double inY, double inMaxDist, boolean inJustTrackPoints)
943 int nearestPoint = 0;
944 double nearestDist = -1.0;
946 for (int i=0; i < getNumPoints(); i++)
948 if (!inJustTrackPoints || !_dataPoints[i].isWaypoint())
950 currDist = Math.abs(_xValuesNew[i] - inX) + Math.abs(_yValuesNew[i] - inY);
951 if (currDist < nearestDist || nearestDist < 0.0)
954 nearestDist = currDist;
958 // Check whether it's within required distance
959 if (nearestDist > inMaxDist && inMaxDist > 0.0)
967 * Get the next track point starting from the given index
968 * @param inStartIndex index to start looking from
969 * @return next track point, or null if end of data reached
971 public DataPoint getNextTrackPoint(int inStartIndex)
973 return getNextTrackPoint(inStartIndex, _numPoints, true);
977 * Get the next track point in the given range
978 * @param inStartIndex index to start looking from
979 * @param inEndIndex index to stop looking
980 * @return next track point, or null if end of data reached
982 public DataPoint getNextTrackPoint(int inStartIndex, int inEndIndex)
984 return getNextTrackPoint(inStartIndex, inEndIndex, true);
988 * Get the previous track point starting from the given index
989 * @param inStartIndex index to start looking from
990 * @return next track point, or null if end of data reached
992 public DataPoint getPreviousTrackPoint(int inStartIndex)
994 return getNextTrackPoint(inStartIndex, _numPoints, false);
998 * Get the next track point starting from the given index
999 * @param inStartIndex index to start looking from
1000 * @param inEndIndex index to stop looking (inclusive)
1001 * @param inCountUp true for next, false for previous
1002 * @return next track point, or null if end of data reached
1004 private DataPoint getNextTrackPoint(int inStartIndex, int inEndIndex, boolean inCountUp)
1006 // Loop forever over points
1007 int increment = inCountUp?1:-1;
1008 for (int i=inStartIndex; i<=inEndIndex; i+=increment)
1010 DataPoint point = getPoint(i);
1011 // Exit if end of data reached - there wasn't a track point
1012 if (point == null) {return null;}
1013 if (point.isValid() && !point.isWaypoint()) {
1014 // next track point found
1022 * Shift all the segment start flags in the given range by 1
1023 * Method used by reverse range and its undo
1024 * @param inStartIndex start of range, inclusive
1025 * @param inEndIndex end of range, inclusive
1027 public void shiftSegmentStarts(int inStartIndex, int inEndIndex)
1029 boolean prevFlag = true;
1030 boolean currFlag = true;
1031 for (int i=inStartIndex; i<= inEndIndex; i++)
1033 DataPoint point = getPoint(i);
1034 if (point != null && !point.isWaypoint())
1037 currFlag = point.getSegmentStart();
1039 point.setSegmentStart(prevFlag);
1040 prevFlag = currFlag;
1045 ////////////////// Cloning and replacing ///////////////////
1048 * Clone the array of DataPoints
1049 * @return shallow copy of DataPoint objects
1051 public DataPoint[] cloneContents()
1053 DataPoint[] clone = new DataPoint[getNumPoints()];
1054 System.arraycopy(_dataPoints, 0, clone, 0, getNumPoints());
1060 * Clone the specified range of data points
1061 * @param inStart start index (inclusive)
1062 * @param inEnd end index (inclusive)
1063 * @return shallow copy of DataPoint objects
1065 public DataPoint[] cloneRange(int inStart, int inEnd)
1067 int numSelected = 0;
1068 if (inEnd >= 0 && inEnd >= inStart)
1070 numSelected = inEnd - inStart + 1;
1072 DataPoint[] result = new DataPoint[numSelected>0?numSelected:0];
1073 if (numSelected > 0)
1075 System.arraycopy(_dataPoints, inStart, result, 0, numSelected);
1082 * Re-insert the specified point at the given index
1083 * @param inPoint point to insert
1084 * @param inIndex index at which to insert the point
1085 * @return true if it worked, false otherwise
1087 public boolean insertPoint(DataPoint inPoint, int inIndex)
1089 if (inIndex > _numPoints || inPoint == null)
1093 // Make new array to copy points over to
1094 DataPoint[] newPointArray = new DataPoint[_numPoints + 1];
1097 System.arraycopy(_dataPoints, 0, newPointArray, 0, inIndex);
1099 newPointArray[inIndex] = inPoint;
1100 if (inIndex < _numPoints)
1102 System.arraycopy(_dataPoints, inIndex, newPointArray, inIndex+1, _numPoints - inIndex);
1104 // Change over to new array
1105 _dataPoints = newPointArray;
1107 // needs to be scaled again
1109 UpdateMessageBroker.informSubscribers();
1115 * Re-insert the specified point range at the given index
1116 * @param inPoints point array to insert
1117 * @param inIndex index at which to insert the points
1118 * @return true if it worked, false otherwise
1120 public boolean insertRange(DataPoint[] inPoints, int inIndex)
1122 if (inIndex > _numPoints || inPoints == null)
1126 // Make new array to copy points over to
1127 DataPoint[] newPointArray = new DataPoint[_numPoints + inPoints.length];
1130 System.arraycopy(_dataPoints, 0, newPointArray, 0, inIndex);
1132 System.arraycopy(inPoints, 0, newPointArray, inIndex, inPoints.length);
1133 if (inIndex < _numPoints)
1135 System.arraycopy(_dataPoints, inIndex, newPointArray, inIndex+inPoints.length, _numPoints - inIndex);
1137 // Change over to new array
1138 _dataPoints = newPointArray;
1139 _numPoints += inPoints.length;
1140 // needs to be scaled again
1142 UpdateMessageBroker.informSubscribers();
1148 * Replace the track contents with the given point array
1149 * @param inContents array of DataPoint objects
1150 * @return true on success
1152 public boolean replaceContents(DataPoint[] inContents)
1154 // master field array stays the same
1155 // (would need to store field array too if we wanted to redo a load)
1156 // replace data array
1157 _dataPoints = inContents;
1158 _numPoints = _dataPoints.length;
1160 UpdateMessageBroker.informSubscribers();
1166 * Edit the specified point
1167 * @param inPoint point to edit
1168 * @param inEditList list of edits to make
1169 * @return true if successful
1171 public boolean editPoint(DataPoint inPoint, FieldEditList inEditList)
1173 if (inPoint != null && inEditList != null && inEditList.getNumEdits() > 0)
1175 // remember if coordinates have changed
1176 boolean coordsChanged = false;
1177 // go through edits one by one
1178 int numEdits = inEditList.getNumEdits();
1179 for (int i=0; i<numEdits; i++)
1181 FieldEdit edit = inEditList.getEdit(i);
1182 inPoint.setFieldValue(edit.getField(), edit.getValue());
1183 // check coordinates
1184 coordsChanged |= (edit.getField().equals(Field.LATITUDE)
1185 || edit.getField().equals(Field.LONGITUDE) || edit.getField().equals(Field.ALTITUDE));
1187 // set photo status if coordinates have changed
1188 if (inPoint.getPhoto() != null && coordsChanged)
1190 inPoint.getPhoto().setCurrentStatus(PhotoStatus.CONNECTED);
1192 // point possibly needs to be scaled again
1194 // trigger listeners
1195 UpdateMessageBroker.informSubscribers();