1 package tim.prune.data;
5 import tim.prune.UpdateMessageBroker;
6 import tim.prune.function.edit.FieldEdit;
7 import tim.prune.function.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 boolean _scaled = false;
23 private int _numPoints = 0;
24 private boolean _hasTrackpoint = false;
25 private boolean _hasWaypoint = false;
27 private FieldList _masterFieldList = null;
29 private DoubleRange _latRange = null, _longRange = null;
30 private DoubleRange _xRange = null, _yRange = null;
34 * Constructor for empty track
39 _masterFieldList = new FieldList(null);
40 // make empty DataPoint array
41 _dataPoints = new DataPoint[0];
48 * Constructor using fields and points from another Track
49 * @param inFieldList Field list from another Track object
50 * @param inPoints (edited) point array
52 public Track(FieldList inFieldList, DataPoint[] inPoints)
54 _masterFieldList = inFieldList;
55 _dataPoints = inPoints;
56 if (_dataPoints == null) _dataPoints = new DataPoint[0];
57 _numPoints = _dataPoints.length;
62 * Load method, for initialising and reinitialising data
63 * @param inFieldArray array of Field objects describing fields
64 * @param inPointArray 2d object array containing data
65 * @param inOptions load options such as units
67 public void load(Field[] inFieldArray, Object[][] inPointArray, PointCreateOptions inOptions)
69 if (inFieldArray == null || inPointArray == null)
75 _masterFieldList = new FieldList(inFieldArray);
76 // make DataPoint object from each point in inPointList
77 _dataPoints = new DataPoint[inPointArray.length];
78 String[] dataArray = null;
80 for (int p=0; p < inPointArray.length; p++)
82 dataArray = (String[]) inPointArray[p];
83 // Convert to DataPoint objects
84 DataPoint point = new DataPoint(dataArray, _masterFieldList, inOptions);
87 _dataPoints[pointIndex] = point;
92 // TODO: Maybe report this somehow?
93 // System.out.println("point is not valid!");
96 _numPoints = pointIndex;
97 // Set first track point to be start of segment
98 DataPoint firstTrackPoint = getNextTrackPoint(0);
99 if (firstTrackPoint != null) {
100 firstTrackPoint.setSegmentStart(true);
102 // needs to be scaled
108 * Load the track by transferring the contents from a loaded Track object
109 * @param inOther Track object containing loaded data
111 public void load(Track inOther)
113 _numPoints = inOther._numPoints;
114 _masterFieldList = inOther._masterFieldList;
115 _dataPoints = inOther._dataPoints;
116 // needs to be scaled
121 * Request that a rescale be done to recalculate derived values
123 public void requestRescale()
129 * Extend the track's field list with the given additional fields
130 * @param inFieldList list of fields to be added
132 public void extendFieldList(FieldList inFieldList)
134 _masterFieldList = _masterFieldList.merge(inFieldList);
137 ////////////////// Modification methods //////////////////////
141 * Combine this Track with new data
142 * @param inOtherTrack other track to combine
144 public void combine(Track inOtherTrack)
147 _masterFieldList = _masterFieldList.merge(inOtherTrack._masterFieldList);
148 // expand data array and add other track's data points
149 int totalPoints = getNumPoints() + inOtherTrack.getNumPoints();
150 DataPoint[] mergedPoints = new DataPoint[totalPoints];
151 System.arraycopy(_dataPoints, 0, mergedPoints, 0, getNumPoints());
152 System.arraycopy(inOtherTrack._dataPoints, 0, mergedPoints, getNumPoints(), inOtherTrack.getNumPoints());
153 _dataPoints = mergedPoints;
154 // combine point count
155 _numPoints = totalPoints;
156 // needs to be scaled again
159 UpdateMessageBroker.informSubscribers();
164 * Crop the track to the given size - subsequent points are not (yet) deleted
165 * @param inNewSize new number of points in track
167 public void cropTo(int inNewSize)
169 if (inNewSize >= 0 && inNewSize < getNumPoints())
171 _numPoints = inNewSize;
172 // needs to be scaled again
174 UpdateMessageBroker.informSubscribers();
180 * Delete the points marked for deletion
181 * @param inSplitSegments true to split segments at deleted points
182 * @return number of points deleted
184 public int deleteMarkedPoints(boolean inSplitSegments)
187 // Copy selected points into a new point array
188 DataPoint[] newPointArray = new DataPoint[_numPoints];
189 boolean prevPointDeleted = false;
190 for (int i=0; i<_numPoints; i++)
192 DataPoint point = _dataPoints[i];
193 // Don't delete photo points
194 if (point.hasMedia() || !point.getDeleteFlag())
196 if (prevPointDeleted && inSplitSegments) {
197 point.setSegmentStart(true);
199 newPointArray[numCopied] = point;
201 prevPointDeleted = false;
204 prevPointDeleted = true;
208 // Copy array references
209 int numDeleted = _numPoints - numCopied;
212 _dataPoints = new DataPoint[numCopied];
213 System.arraycopy(newPointArray, 0, _dataPoints, 0, numCopied);
214 _numPoints = _dataPoints.length;
222 * Delete the specified point
223 * @param inIndex point index
224 * @return true if successful
226 public boolean deletePoint(int inIndex)
228 boolean answer = deleteRange(inIndex, inIndex);
234 * Delete the specified range of points from the Track
235 * @param inStart start of range (inclusive)
236 * @param inEnd end of range (inclusive)
237 * @return true if successful
239 public boolean deleteRange(int inStart, int inEnd)
241 if (inStart < 0 || inEnd < 0 || inEnd < inStart)
243 // no valid range selected so can't delete
246 // check through range to be deleted, and see if any new segment flags present
247 boolean hasSegmentStart = false;
248 DataPoint nextTrackPoint = getNextTrackPoint(inEnd+1);
249 if (nextTrackPoint != null) {
250 for (int i=inStart; i<=inEnd && !hasSegmentStart; i++) {
251 hasSegmentStart |= _dataPoints[i].getSegmentStart();
253 // If segment break found, make sure next trackpoint also has break
254 if (hasSegmentStart) {nextTrackPoint.setSegmentStart(true);}
256 // valid range, let's delete it
257 int numToDelete = inEnd - inStart + 1;
258 DataPoint[] newPointArray = new DataPoint[_numPoints - numToDelete];
259 // Copy points before the selected range
262 System.arraycopy(_dataPoints, 0, newPointArray, 0, inStart);
264 // Copy points after the deleted one(s)
265 if (inEnd < (_numPoints - 1))
267 System.arraycopy(_dataPoints, inEnd + 1, newPointArray, inStart,
268 _numPoints - inEnd - 1);
270 // Copy points over original array
271 _dataPoints = newPointArray;
272 _numPoints -= numToDelete;
273 // needs to be scaled again
280 * Reverse the specified range of points
281 * @param inStart start index
282 * @param inEnd end index
283 * @return true if successful, false otherwise
285 public boolean reverseRange(int inStart, int inEnd)
287 if (inStart < 0 || inEnd < 0 || inStart >= inEnd || inEnd >= _numPoints)
291 // calculate how many point swaps are required
292 int numPointsToReverse = (inEnd - inStart + 1) / 2;
294 for (int i=0; i<numPointsToReverse; i++)
296 // swap pairs of points
297 p = _dataPoints[inStart + i];
298 _dataPoints[inStart + i] = _dataPoints[inEnd - i];
299 _dataPoints[inEnd - i] = p;
301 // adjust segment starts
302 shiftSegmentStarts(inStart, inEnd);
303 // Find first track point and following track point, and set segment starts to true
304 DataPoint firstTrackPoint = getNextTrackPoint(inStart);
305 if (firstTrackPoint != null) {firstTrackPoint.setSegmentStart(true);}
306 DataPoint nextTrackPoint = getNextTrackPoint(inEnd+1);
307 if (nextTrackPoint != null) {nextTrackPoint.setSegmentStart(true);}
308 // needs to be scaled again
310 UpdateMessageBroker.informSubscribers();
316 * Add the given time offset to the specified range
317 * @param inStart start of range
318 * @param inEnd end of range
319 * @param inOffset offset to add (-ve to subtract)
320 * @param inUndo true for undo operation
321 * @return true on success
323 public boolean addTimeOffsetSeconds(int inStart, int inEnd, long inOffset, boolean inUndo)
326 if (inStart < 0 || inEnd < 0 || inStart >= inEnd || inEnd >= _numPoints) {
329 boolean foundTimestamp = false;
330 // Loop over all points within range
331 for (int i=inStart; i<=inEnd; i++)
333 DataPoint p = _dataPoints[i];
334 if (p != null && p.hasTimestamp())
336 // This point has a timestamp so add the offset to it
337 foundTimestamp = true;
338 p.addTimeOffsetSeconds(inOffset);
339 p.setModified(inUndo);
342 return foundTimestamp;
346 * Add the given altitude offset to the specified range
347 * @param inStart start of range
348 * @param inEnd end of range
349 * @param inOffset offset to add (-ve to subtract)
350 * @param inUnit altitude unit of offset
351 * @param inDecimals number of decimal places in offset
352 * @return true on success
354 public boolean addAltitudeOffset(int inStart, int inEnd, double inOffset,
355 Unit inUnit, int inDecimals)
358 if (inStart < 0 || inEnd < 0 || inStart >= inEnd || inEnd >= _numPoints) {
361 boolean foundAlt = false;
362 // Loop over all points within range
363 for (int i=inStart; i<=inEnd; i++)
365 DataPoint p = _dataPoints[i];
366 if (p != null && p.hasAltitude())
368 // This point has an altitude so add the offset to it
370 p.addAltitudeOffset(inOffset, inUnit, inDecimals);
371 p.setModified(false);
374 // needs to be scaled again
381 * Remove altitudes from the specified range
382 * @param inStart start of range
383 * @param inEnd end of range
385 public boolean removeAltitudes(int inStart, int inEnd)
388 if (inStart < 0 || inEnd < 0 || inStart >= inEnd || inEnd >= _numPoints) {
392 boolean anyRemoved = false;
393 for (int i=inStart; i<=inEnd; i++)
395 DataPoint p = _dataPoints[i];
396 if (p != null && p.hasAltitude())
406 * Interleave all waypoints by each nearest track point
407 * @return true if successful, false if no change
409 public boolean interleaveWaypoints()
411 // Separate waypoints and find nearest track point
412 int numWaypoints = 0;
413 DataPoint[] waypoints = new DataPoint[_numPoints];
414 int[] pointIndices = new int[_numPoints];
415 DataPoint point = null;
417 for (i=0; i<_numPoints; i++)
419 point = _dataPoints[i];
420 if (point.isWaypoint())
422 waypoints[numWaypoints] = point;
423 pointIndices[numWaypoints] = getNearestPointIndex(
424 _xValues[i], _yValues[i], -1.0, true);
428 // Exit if data not mixed
429 if (numWaypoints == 0 || numWaypoints == _numPoints)
432 // Loop round points copying to correct order
433 DataPoint[] dataCopy = new DataPoint[_numPoints];
435 for (i=0; i<_numPoints; i++)
437 point = _dataPoints[i];
438 // if it's a track point, copy it
439 if (!point.isWaypoint())
441 dataCopy[copyIndex] = point;
444 // check for waypoints with this index
445 for (int j=0; j<numWaypoints; j++)
447 if (pointIndices[j] == i)
449 dataCopy[copyIndex] = waypoints[j];
454 // Copy data back to track
455 _dataPoints = dataCopy;
456 // needs to be scaled again to recalc x, y
458 UpdateMessageBroker.informSubscribers();
464 * Cut and move the specified section
465 * @param inSectionStart start index of section
466 * @param inSectionEnd end index of section
467 * @param inMoveTo index of move to point
468 * @return true if move successful
470 public boolean cutAndMoveSection(int inSectionStart, int inSectionEnd, int inMoveTo)
472 // TODO: Move cut/move into separate function?
473 // Check that indices make sense
474 if (inSectionStart >= 0 && inSectionEnd > inSectionStart && inMoveTo >= 0
475 && (inMoveTo < inSectionStart || inMoveTo > (inSectionEnd+1)))
477 // do the cut and move
478 DataPoint[] newPointArray = new DataPoint[_numPoints];
479 // System.out.println("Cut/move section (" + inSectionStart + " - " + inSectionEnd + ") to before point " + inMoveTo);
480 // Is it a forward copy or a backward copy?
481 if (inSectionStart > inMoveTo)
483 int sectionLength = inSectionEnd - inSectionStart + 1;
484 // move section to earlier point
486 System.arraycopy(_dataPoints, 0, newPointArray, 0, inMoveTo); // unchanged points before
488 System.arraycopy(_dataPoints, inSectionStart, newPointArray, inMoveTo, sectionLength); // moved bit
489 // after insertion point, before moved bit
490 System.arraycopy(_dataPoints, inMoveTo, newPointArray, inMoveTo + sectionLength, inSectionStart - inMoveTo);
492 if (inSectionEnd < (_numPoints - 1)) {
493 System.arraycopy(_dataPoints, inSectionEnd+1, newPointArray, inSectionEnd+1, _numPoints - inSectionEnd - 1);
498 // Move section to later point
499 if (inSectionStart > 0) {
500 System.arraycopy(_dataPoints, 0, newPointArray, 0, inSectionStart); // unchanged points before
502 // from end of section to move to point
503 if (inMoveTo > (inSectionEnd + 1)) {
504 System.arraycopy(_dataPoints, inSectionEnd+1, newPointArray, inSectionStart, inMoveTo - inSectionEnd - 1);
507 System.arraycopy(_dataPoints, inSectionStart, newPointArray, inSectionStart + inMoveTo - inSectionEnd - 1,
508 inSectionEnd - inSectionStart + 1);
509 // unchanged bit after
510 if (inSectionEnd < (_numPoints - 1)) {
511 System.arraycopy(_dataPoints, inMoveTo, newPointArray, inMoveTo, _numPoints - inMoveTo);
514 // Copy array references
515 _dataPoints = newPointArray;
524 * Interpolate extra points between two selected ones
525 * @param inStartIndex start index of interpolation
526 * @param inNumPoints num points to insert
527 * @return true if successful
529 public boolean interpolate(int inStartIndex, int inNumPoints)
532 if (inStartIndex < 0 || inStartIndex >= _numPoints || inNumPoints <= 0)
535 // get start and end points
536 DataPoint startPoint = getPoint(inStartIndex);
537 DataPoint endPoint = getPoint(inStartIndex + 1);
539 // Make array of points to insert
540 DataPoint[] insertedPoints = startPoint.interpolate(endPoint, inNumPoints);
542 // Insert points into track
543 return insertRange(insertedPoints, inStartIndex + 1);
548 * Average selected points
549 * @param inStartIndex start index of selection
550 * @param inEndIndex end index of selection
551 * @return true if successful
553 public boolean average(int inStartIndex, int inEndIndex)
556 if (inStartIndex < 0 || inStartIndex >= _numPoints || inEndIndex <= inStartIndex)
559 DataPoint startPoint = getPoint(inStartIndex);
560 double firstLatitude = startPoint.getLatitude().getDouble();
561 double firstLongitude = startPoint.getLongitude().getDouble();
562 double latitudeDiff = 0.0, longitudeDiff = 0.0;
563 double totalAltitude = 0;
564 int numAltitudes = 0;
566 // loop between start and end points
567 for (int i=inStartIndex; i<= inEndIndex; i++)
569 DataPoint currPoint = getPoint(i);
570 latitudeDiff += (currPoint.getLatitude().getDouble() - firstLatitude);
571 longitudeDiff += (currPoint.getLongitude().getDouble() - firstLongitude);
572 if (currPoint.hasAltitude())
574 totalAltitude += currPoint.getAltitude().getValue(altUnit);
575 // Use altitude format of first valid altitude
577 altUnit = currPoint.getAltitude().getUnit();
581 int numPoints = inEndIndex - inStartIndex + 1;
582 double meanLatitude = firstLatitude + (latitudeDiff / numPoints);
583 double meanLongitude = firstLongitude + (longitudeDiff / numPoints);
584 Altitude meanAltitude = null;
585 if (numAltitudes > 0) {
586 meanAltitude = new Altitude((int) (totalAltitude / numAltitudes), altUnit);
589 DataPoint insertedPoint = new DataPoint(new Latitude(meanLatitude, Coordinate.FORMAT_DECIMAL_FORCE_POINT),
590 new Longitude(meanLongitude, Coordinate.FORMAT_DECIMAL_FORCE_POINT), meanAltitude);
591 // Make into singleton
592 insertedPoint.setSegmentStart(true);
593 DataPoint nextPoint = getNextTrackPoint(inEndIndex+1);
594 if (nextPoint != null) {nextPoint.setSegmentStart(true);}
595 // Insert points into track
596 return insertRange(new DataPoint[] {insertedPoint}, inEndIndex + 1);
601 * Append the specified points to the end of the track
602 * @param inPoints DataPoint objects to add
604 public void appendPoints(DataPoint[] inPoints)
606 // Insert points into track
607 if (inPoints != null && inPoints.length > 0)
609 insertRange(inPoints, _numPoints);
611 // needs to be scaled again to recalc x, y
613 UpdateMessageBroker.informSubscribers();
617 //////// information methods /////////////
621 * Get the point at the given index
622 * @param inPointNum index number, starting at 0
623 * @return DataPoint object, or null if out of range
625 public DataPoint getPoint(int inPointNum)
627 if (inPointNum > -1 && inPointNum < getNumPoints())
629 return _dataPoints[inPointNum];
635 * @return the number of (valid) points in the track
637 public int getNumPoints()
643 * @return The range of x values as a DoubleRange object
645 public DoubleRange getXRange()
647 if (!_scaled) {scalePoints();}
652 * @return The range of y values as a DoubleRange object
654 public DoubleRange getYRange()
656 if (!_scaled) {scalePoints();}
661 * @return The range of lat values as a DoubleRange object
663 public DoubleRange getLatRange()
665 if (!_scaled) {scalePoints();}
669 * @return The range of lon values as a DoubleRange object
671 public DoubleRange getLonRange()
673 if (!_scaled) {scalePoints();}
678 * @param inPointNum point index, starting at 0
679 * @return scaled x value of specified point
681 public double getX(int inPointNum)
683 if (!_scaled) {scalePoints();}
684 return _xValues[inPointNum];
688 * @param inPointNum point index, starting at 0
689 * @return scaled y value of specified point
691 public double getY(int inPointNum)
693 if (!_scaled) {scalePoints();}
694 return _yValues[inPointNum];
698 * @return the master field list
700 public FieldList getFieldList()
702 return _masterFieldList;
707 * Checks if any data exists for the specified field
708 * @param inField Field to examine
709 * @return true if data exists for this field
711 public boolean hasData(Field inField)
713 // Don't use this method for altitudes
714 if (inField.equals(Field.ALTITUDE)) {return hasAltitudeData();}
715 return hasData(inField, 0, _numPoints-1);
720 * Checks if any data exists for the specified field in the specified range
721 * @param inField Field to examine
722 * @param inStart start of range to check
723 * @param inEnd end of range to check (inclusive)
724 * @return true if data exists for this field
726 public boolean hasData(Field inField, int inStart, int inEnd)
728 // Loop over selected point range
729 for (int i=inStart; i<=inEnd; i++)
731 if (_dataPoints[i].getFieldValue(inField) != null)
733 // Check altitudes and timestamps
734 if ((inField != Field.ALTITUDE || _dataPoints[i].getAltitude().isValid())
735 && (inField != Field.TIMESTAMP || _dataPoints[i].getTimestamp().isValid()))
745 * @return true if track has altitude data
747 public boolean hasAltitudeData()
749 for (int i=0; i<_numPoints; i++) {
750 if (_dataPoints[i].hasAltitude()) {return true;}
756 * @return true if track contains at least one trackpoint
758 public boolean hasTrackPoints()
760 if (!_scaled) {scalePoints();}
761 return _hasTrackpoint;
765 * @return true if track contains waypoints
767 public boolean hasWaypoints()
769 if (!_scaled) {scalePoints();}
774 * @return true if track contains any points marked for deletion
776 public boolean hasMarkedPoints()
778 if (_numPoints < 1) {
781 // Loop over points looking for any marked for deletion
782 for (int i=0; i<=_numPoints-1; i++)
784 if (_dataPoints[i] != null && _dataPoints[i].getDeleteFlag()) {
793 * Clear all the deletion markers
795 public void clearDeletionMarkers()
797 for (int i=0; i<_numPoints; i++)
799 _dataPoints[i].setMarkedForDeletion(false);
804 * Collect all the waypoints into the given List
805 * @param inList List to fill with waypoints
807 public void getWaypoints(List<DataPoint> inList)
811 // loop over points and copy all waypoints into list
812 for (int i=0; i<=_numPoints-1; i++)
814 if (_dataPoints[i] != null && _dataPoints[i].isWaypoint())
816 inList.add(_dataPoints[i]);
822 * Collect all segment starts into the given list
823 * @param inList List to fill with waypoints
825 public void getSegmentStarts(List<DataPoint> inList)
829 // loop over points and copy all segment starts into list
830 for (int i=0; i<=_numPoints-1; i++)
832 if (_dataPoints[i] != null && _dataPoints[i].getSegmentStart())
834 inList.add(_dataPoints[i]);
841 * Search for the given Point in the track and return the index
842 * @param inPoint Point to look for
843 * @return index of Point, if any or -1 if not found
845 public int getPointIndex(DataPoint inPoint)
849 // Loop over points in track
850 for (int i=0; i<=_numPoints-1; i++)
852 if (_dataPoints[i] == inPoint)
863 ///////// Internal processing methods ////////////////
867 * Scale all the points in the track to gain x and y values
870 private synchronized void scalePoints()
872 // Loop through all points in track, to see limits of lat, long
873 _longRange = new DoubleRange();
874 _latRange = new DoubleRange();
876 _hasWaypoint = false; _hasTrackpoint = false;
877 for (p=0; p < getNumPoints(); p++)
879 DataPoint point = getPoint(p);
880 if (point != null && point.isValid())
882 _longRange.addValue(point.getLongitude().getDouble());
883 _latRange.addValue(point.getLatitude().getDouble());
884 if (point.isWaypoint())
887 _hasTrackpoint = true;
891 // Loop over points and calculate scales
892 _xValues = new double[getNumPoints()];
893 _yValues = new double[getNumPoints()];
894 _xRange = new DoubleRange();
895 _yRange = new DoubleRange();
896 for (p=0; p < getNumPoints(); p++)
898 DataPoint point = getPoint(p);
901 _xValues[p] = MapUtils.getXFromLongitude(point.getLongitude().getDouble());
902 _xRange.addValue(_xValues[p]);
903 _yValues[p] = MapUtils.getYFromLatitude(point.getLatitude().getDouble());
904 _yRange.addValue(_yValues[p]);
912 * Find the nearest point to the specified x and y coordinates
913 * or -1 if no point is within the specified max distance
914 * @param inX x coordinate
915 * @param inY y coordinate
916 * @param inMaxDist maximum distance from selected coordinates
917 * @param inJustTrackPoints true if waypoints should be ignored
918 * @return index of nearest point or -1 if not found
920 public int getNearestPointIndex(double inX, double inY, double inMaxDist, boolean inJustTrackPoints)
922 int nearestPoint = 0;
923 double nearestDist = -1.0;
925 for (int i=0; i < getNumPoints(); i++)
927 if (!inJustTrackPoints || !_dataPoints[i].isWaypoint())
929 yDist = Math.abs(_yValues[i] - inY);
930 if (yDist < nearestDist || nearestDist < 0.0)
932 // y dist is within range, so check x too
933 mDist = yDist + getMinXDist(_xValues[i] - inX);
934 if (mDist < nearestDist || nearestDist < 0.0)
942 // Check whether it's within required distance
943 if (nearestDist > inMaxDist && inMaxDist > 0.0)
951 * @param inX x value of point
952 * @return minimum wrapped value
954 private static final double getMinXDist(double inX)
956 // TODO: Should be abs(mod(inX-0.5,1)-0.5) - means two adds, one mod, one abs instead of two adds, 3 abss and two compares
957 return Math.min(Math.min(Math.abs(inX), Math.abs(inX-1.0)), Math.abs(inX+1.0));
961 * Get the next track point starting from the given index
962 * @param inStartIndex index to start looking from
963 * @return next track point, or null if end of data reached
965 public DataPoint getNextTrackPoint(int inStartIndex)
967 return getNextTrackPoint(inStartIndex, _numPoints, true);
971 * Get the next track point in the given range
972 * @param inStartIndex index to start looking from
973 * @param inEndIndex index to stop looking
974 * @return next track point, or null if end of data reached
976 public DataPoint getNextTrackPoint(int inStartIndex, int inEndIndex)
978 return getNextTrackPoint(inStartIndex, inEndIndex, true);
982 * Get the previous track point starting from the given index
983 * @param inStartIndex index to start looking from
984 * @return next track point, or null if end of data reached
986 public DataPoint getPreviousTrackPoint(int inStartIndex)
988 // end index is given as _numPoints but actually it just counts down to -1
989 return getNextTrackPoint(inStartIndex, _numPoints, false);
993 * Get the next track point starting from the given index
994 * @param inStartIndex index to start looking from
995 * @param inEndIndex index to stop looking (inclusive)
996 * @param inCountUp true for next, false for previous
997 * @return next track point, or null if end of data reached
999 private DataPoint getNextTrackPoint(int inStartIndex, int inEndIndex, boolean inCountUp)
1001 // Loop forever over points
1002 int increment = inCountUp?1:-1;
1003 for (int i=inStartIndex; i<=inEndIndex; i+=increment)
1005 DataPoint point = getPoint(i);
1006 // Exit if end of data reached - there wasn't a track point
1007 if (point == null) {return null;}
1008 if (point.isValid() && !point.isWaypoint()) {
1009 // next track point found
1017 * Shift all the segment start flags in the given range by 1
1018 * Method used by reverse range and its undo
1019 * @param inStartIndex start of range, inclusive
1020 * @param inEndIndex end of range, inclusive
1022 public void shiftSegmentStarts(int inStartIndex, int inEndIndex)
1024 boolean prevFlag = true;
1025 boolean currFlag = true;
1026 for (int i=inStartIndex; i<= inEndIndex; i++)
1028 DataPoint point = getPoint(i);
1029 if (point != null && !point.isWaypoint())
1032 currFlag = point.getSegmentStart();
1034 point.setSegmentStart(prevFlag);
1035 prevFlag = currFlag;
1040 ////////////////// Cloning and replacing ///////////////////
1043 * Clone the array of DataPoints
1044 * @return shallow copy of DataPoint objects
1046 public DataPoint[] cloneContents()
1048 DataPoint[] clone = new DataPoint[getNumPoints()];
1049 System.arraycopy(_dataPoints, 0, clone, 0, getNumPoints());
1055 * Clone the specified range of data points
1056 * @param inStart start index (inclusive)
1057 * @param inEnd end index (inclusive)
1058 * @return shallow copy of DataPoint objects
1060 public DataPoint[] cloneRange(int inStart, int inEnd)
1062 int numSelected = 0;
1063 if (inEnd >= 0 && inEnd >= inStart)
1065 numSelected = inEnd - inStart + 1;
1067 DataPoint[] result = new DataPoint[numSelected>0?numSelected:0];
1068 if (numSelected > 0)
1070 System.arraycopy(_dataPoints, inStart, result, 0, numSelected);
1077 * Re-insert the specified point at the given index
1078 * @param inPoint point to insert
1079 * @param inIndex index at which to insert the point
1080 * @return true if it worked, false otherwise
1082 public boolean insertPoint(DataPoint inPoint, int inIndex)
1084 if (inIndex > _numPoints || inPoint == null)
1088 // Make new array to copy points over to
1089 DataPoint[] newPointArray = new DataPoint[_numPoints + 1];
1092 System.arraycopy(_dataPoints, 0, newPointArray, 0, inIndex);
1094 newPointArray[inIndex] = inPoint;
1095 if (inIndex < _numPoints)
1097 System.arraycopy(_dataPoints, inIndex, newPointArray, inIndex+1, _numPoints - inIndex);
1099 // Change over to new array
1100 _dataPoints = newPointArray;
1102 // needs to be scaled again
1104 UpdateMessageBroker.informSubscribers();
1110 * Re-insert the specified point range at the given index
1111 * @param inPoints point array to insert
1112 * @param inIndex index at which to insert the points
1113 * @return true if it worked, false otherwise
1115 public boolean insertRange(DataPoint[] inPoints, int inIndex)
1117 if (inIndex > _numPoints || inPoints == null)
1121 // Make new array to copy points over to
1122 DataPoint[] newPointArray = new DataPoint[_numPoints + inPoints.length];
1125 System.arraycopy(_dataPoints, 0, newPointArray, 0, inIndex);
1127 System.arraycopy(inPoints, 0, newPointArray, inIndex, inPoints.length);
1128 if (inIndex < _numPoints)
1130 System.arraycopy(_dataPoints, inIndex, newPointArray, inIndex+inPoints.length, _numPoints - inIndex);
1132 // Change over to new array
1133 _dataPoints = newPointArray;
1134 _numPoints += inPoints.length;
1135 // needs to be scaled again
1137 UpdateMessageBroker.informSubscribers();
1143 * Replace the track contents with the given point array
1144 * @param inContents array of DataPoint objects
1145 * @return true on success
1147 public boolean replaceContents(DataPoint[] inContents)
1149 // master field array stays the same
1150 // (would need to store field array too if we wanted to redo a load)
1151 // replace data array
1152 _dataPoints = inContents;
1153 _numPoints = _dataPoints.length;
1155 UpdateMessageBroker.informSubscribers();
1161 * Edit the specified point
1162 * @param inPoint point to edit
1163 * @param inEditList list of edits to make
1164 * @param inUndo true if undo operation, false otherwise
1165 * @return true if successful
1167 public boolean editPoint(DataPoint inPoint, FieldEditList inEditList, boolean inUndo)
1169 if (inPoint != null && inEditList != null && inEditList.getNumEdits() > 0)
1171 // remember if coordinates have changed
1172 boolean coordsChanged = false;
1173 // go through edits one by one
1174 int numEdits = inEditList.getNumEdits();
1175 for (int i=0; i<numEdits; i++)
1177 FieldEdit edit = inEditList.getEdit(i);
1178 Field editField = edit.getField();
1179 inPoint.setFieldValue(editField, edit.getValue(), inUndo);
1180 // Check that master field list has this field already (maybe point name has been added)
1181 if (!_masterFieldList.contains(editField)) {
1182 _masterFieldList.extendList(editField);
1184 // check coordinates
1185 coordsChanged |= (editField.equals(Field.LATITUDE)
1186 || editField.equals(Field.LONGITUDE) || editField.equals(Field.ALTITUDE));
1188 // set photo status if coordinates have changed
1189 if (inPoint.getPhoto() != null && coordsChanged)
1191 inPoint.getPhoto().setCurrentStatus(Photo.Status.CONNECTED);
1193 // point possibly needs to be scaled again
1195 // trigger listeners
1196 UpdateMessageBroker.informSubscribers();
1203 * @param inPoint point to check
1204 * @return true if this track contains the given point
1206 public boolean containsPoint(DataPoint inPoint)
1208 if (inPoint == null) return false;
1209 for (int i=0; i < getNumPoints(); i++)
1211 if (getPoint(i) == inPoint) return true;
1213 return false; // not found