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;
91 _numPoints = pointIndex;
92 // Set first track point to be start of segment
93 DataPoint firstTrackPoint = getNextTrackPoint(0);
94 if (firstTrackPoint != null) {
95 firstTrackPoint.setSegmentStart(true);
103 * Load the track by transferring the contents from a loaded Track object
104 * @param inOther Track object containing loaded data
106 public void load(Track inOther)
108 _numPoints = inOther._numPoints;
109 _masterFieldList = inOther._masterFieldList;
110 _dataPoints = inOther._dataPoints;
111 // needs to be scaled
116 * Request that a rescale be done to recalculate derived values
118 public void requestRescale()
124 * Extend the track's field list with the given additional fields
125 * @param inFieldList list of fields to be added
127 public void extendFieldList(FieldList inFieldList)
129 _masterFieldList = _masterFieldList.merge(inFieldList);
132 ////////////////// Modification methods //////////////////////
136 * Combine this Track with new data
137 * @param inOtherTrack other track to combine
139 public void combine(Track inOtherTrack)
142 _masterFieldList = _masterFieldList.merge(inOtherTrack._masterFieldList);
143 // expand data array and add other track's data points
144 int totalPoints = getNumPoints() + inOtherTrack.getNumPoints();
145 DataPoint[] mergedPoints = new DataPoint[totalPoints];
146 System.arraycopy(_dataPoints, 0, mergedPoints, 0, getNumPoints());
147 System.arraycopy(inOtherTrack._dataPoints, 0, mergedPoints, getNumPoints(), inOtherTrack.getNumPoints());
148 _dataPoints = mergedPoints;
149 // combine point count
150 _numPoints = totalPoints;
151 // needs to be scaled again
154 UpdateMessageBroker.informSubscribers();
159 * Crop the track to the given size - subsequent points are not (yet) deleted
160 * @param inNewSize new number of points in track
162 public void cropTo(int inNewSize)
164 if (inNewSize >= 0 && inNewSize < getNumPoints())
166 _numPoints = inNewSize;
167 // needs to be scaled again
169 UpdateMessageBroker.informSubscribers();
175 * Delete the points marked for deletion
176 * @return number of points deleted
178 public int deleteMarkedPoints()
181 // Copy selected points
182 DataPoint[] newPointArray = new DataPoint[_numPoints];
183 for (int i=0; i<_numPoints; i++)
185 DataPoint point = _dataPoints[i];
186 // Don't delete photo points
187 if (point.hasMedia() || !point.getDeleteFlag())
189 newPointArray[numCopied] = point;
194 // Copy array references
195 int numDeleted = _numPoints - numCopied;
198 _dataPoints = new DataPoint[numCopied];
199 System.arraycopy(newPointArray, 0, _dataPoints, 0, numCopied);
200 _numPoints = _dataPoints.length;
208 * Delete the specified point
209 * @param inIndex point index
210 * @return true if successful
212 public boolean deletePoint(int inIndex)
214 boolean answer = deleteRange(inIndex, inIndex);
220 * Delete the specified range of points from the Track
221 * @param inStart start of range (inclusive)
222 * @param inEnd end of range (inclusive)
223 * @return true if successful
225 public boolean deleteRange(int inStart, int inEnd)
227 if (inStart < 0 || inEnd < 0 || inEnd < inStart)
229 // no valid range selected so can't delete
232 // check through range to be deleted, and see if any new segment flags present
233 boolean hasSegmentStart = false;
234 DataPoint nextTrackPoint = getNextTrackPoint(inEnd+1);
235 if (nextTrackPoint != null) {
236 for (int i=inStart; i<=inEnd && !hasSegmentStart; i++) {
237 hasSegmentStart |= _dataPoints[i].getSegmentStart();
239 // If segment break found, make sure next trackpoint also has break
240 if (hasSegmentStart) {nextTrackPoint.setSegmentStart(true);}
242 // valid range, let's delete it
243 int numToDelete = inEnd - inStart + 1;
244 DataPoint[] newPointArray = new DataPoint[_numPoints - numToDelete];
245 // Copy points before the selected range
248 System.arraycopy(_dataPoints, 0, newPointArray, 0, inStart);
250 // Copy points after the deleted one(s)
251 if (inEnd < (_numPoints - 1))
253 System.arraycopy(_dataPoints, inEnd + 1, newPointArray, inStart,
254 _numPoints - inEnd - 1);
256 // Copy points over original array
257 _dataPoints = newPointArray;
258 _numPoints -= numToDelete;
259 // needs to be scaled again
266 * Reverse the specified range of points
267 * @param inStart start index
268 * @param inEnd end index
269 * @return true if successful, false otherwise
271 public boolean reverseRange(int inStart, int inEnd)
273 if (inStart < 0 || inEnd < 0 || inStart >= inEnd || inEnd >= _numPoints)
277 // calculate how many point swaps are required
278 int numPointsToReverse = (inEnd - inStart + 1) / 2;
280 for (int i=0; i<numPointsToReverse; i++)
282 // swap pairs of points
283 p = _dataPoints[inStart + i];
284 _dataPoints[inStart + i] = _dataPoints[inEnd - i];
285 _dataPoints[inEnd - i] = p;
287 // adjust segment starts
288 shiftSegmentStarts(inStart, inEnd);
289 // Find first track point and following track point, and set segment starts to true
290 DataPoint firstTrackPoint = getNextTrackPoint(inStart);
291 if (firstTrackPoint != null) {firstTrackPoint.setSegmentStart(true);}
292 DataPoint nextTrackPoint = getNextTrackPoint(inEnd+1);
293 if (nextTrackPoint != null) {nextTrackPoint.setSegmentStart(true);}
294 // needs to be scaled again
296 UpdateMessageBroker.informSubscribers();
302 * Add the given time offset to the specified range
303 * @param inStart start of range
304 * @param inEnd end of range
305 * @param inOffset offset to add (-ve to subtract)
306 * @param inUndo true for undo operation
307 * @return true on success
309 public boolean addTimeOffset(int inStart, int inEnd, long inOffset, boolean inUndo)
312 if (inStart < 0 || inEnd < 0 || inStart >= inEnd || inEnd >= _numPoints) {
315 boolean foundTimestamp = false;
316 // Loop over all points within range
317 for (int i=inStart; i<=inEnd; i++)
319 Timestamp timestamp = _dataPoints[i].getTimestamp();
320 if (timestamp != null)
322 // This point has a timestamp so add the offset to it
323 foundTimestamp = true;
324 timestamp.addOffset(inOffset);
325 _dataPoints[i].setModified(inUndo);
328 return foundTimestamp;
332 * Add the given altitude offset to the specified range
333 * @param inStart start of range
334 * @param inEnd end of range
335 * @param inOffset offset to add (-ve to subtract)
336 * @param inUnit altitude unit of offset
337 * @param inDecimals number of decimal places in offset
338 * @return true on success
340 public boolean addAltitudeOffset(int inStart, int inEnd, double inOffset,
341 Unit inUnit, int inDecimals)
344 if (inStart < 0 || inEnd < 0 || inStart >= inEnd || inEnd >= _numPoints) {
347 boolean foundAlt = false;
348 // Loop over all points within range
349 for (int i=inStart; i<=inEnd; i++)
351 Altitude alt = _dataPoints[i].getAltitude();
352 if (alt != null && alt.isValid())
354 // This point has an altitude so add the offset to it
356 alt.addOffset(inOffset, inUnit, inDecimals);
357 _dataPoints[i].setModified(false);
360 // needs to be scaled again
367 * Collect all waypoints to the start or end of the track
368 * @param inAtStart true to collect at start, false for end
369 * @return true if successful, false if no change
371 public boolean collectWaypoints(boolean inAtStart)
373 // Check for mixed data, numbers of waypoints & nons
374 int numWaypoints = 0, numNonWaypoints = 0;
375 boolean wayAfterNon = false, nonAfterWay = false;
376 DataPoint[] waypoints = new DataPoint[_numPoints];
377 DataPoint[] nonWaypoints = new DataPoint[_numPoints];
378 DataPoint point = null;
379 for (int i=0; i<_numPoints; i++)
381 point = _dataPoints[i];
382 if (point.isWaypoint())
384 waypoints[numWaypoints] = point;
386 wayAfterNon |= (numNonWaypoints > 0);
390 nonWaypoints[numNonWaypoints] = point;
392 nonAfterWay |= (numWaypoints > 0);
395 // Exit if the data is already in the specified order
396 if (numWaypoints == 0 || numNonWaypoints == 0
397 || (inAtStart && !wayAfterNon && nonAfterWay)
398 || (!inAtStart && wayAfterNon && !nonAfterWay))
403 // Copy the arrays back into _dataPoints in the specified order
406 System.arraycopy(waypoints, 0, _dataPoints, 0, numWaypoints);
407 System.arraycopy(nonWaypoints, 0, _dataPoints, numWaypoints, numNonWaypoints);
411 System.arraycopy(nonWaypoints, 0, _dataPoints, 0, numNonWaypoints);
412 System.arraycopy(waypoints, 0, _dataPoints, numNonWaypoints, numWaypoints);
414 // needs to be scaled again
416 UpdateMessageBroker.informSubscribers();
422 * Interleave all waypoints by each nearest track point
423 * @return true if successful, false if no change
425 public boolean interleaveWaypoints()
427 // Separate waypoints and find nearest track point
428 int numWaypoints = 0;
429 DataPoint[] waypoints = new DataPoint[_numPoints];
430 int[] pointIndices = new int[_numPoints];
431 DataPoint point = null;
433 for (i=0; i<_numPoints; i++)
435 point = _dataPoints[i];
436 if (point.isWaypoint())
438 waypoints[numWaypoints] = point;
439 pointIndices[numWaypoints] = getNearestPointIndex(
440 _xValues[i], _yValues[i], -1.0, true);
444 // Exit if data not mixed
445 if (numWaypoints == 0 || numWaypoints == _numPoints)
448 // Loop round points copying to correct order
449 DataPoint[] dataCopy = new DataPoint[_numPoints];
451 for (i=0; i<_numPoints; i++)
453 point = _dataPoints[i];
454 // if it's a track point, copy it
455 if (!point.isWaypoint())
457 dataCopy[copyIndex] = point;
460 // check for waypoints with this index
461 for (int j=0; j<numWaypoints; j++)
463 if (pointIndices[j] == i)
465 dataCopy[copyIndex] = waypoints[j];
470 // Copy data back to track
471 _dataPoints = dataCopy;
472 // needs to be scaled again to recalc x, y
474 UpdateMessageBroker.informSubscribers();
480 * Cut and move the specified section
481 * @param inSectionStart start index of section
482 * @param inSectionEnd end index of section
483 * @param inMoveTo index of move to point
484 * @return true if move successful
486 public boolean cutAndMoveSection(int inSectionStart, int inSectionEnd, int inMoveTo)
488 // TODO: Move cut/move into separate function?
489 // Check that indices make sense
490 if (inSectionStart >= 0 && inSectionEnd > inSectionStart && inMoveTo >= 0
491 && (inMoveTo < inSectionStart || inMoveTo > (inSectionEnd+1)))
493 // do the cut and move
494 DataPoint[] newPointArray = new DataPoint[_numPoints];
495 // System.out.println("Cut/move section (" + inSectionStart + " - " + inSectionEnd + ") to before point " + inMoveTo);
496 // Is it a forward copy or a backward copy?
497 if (inSectionStart > inMoveTo)
499 int sectionLength = inSectionEnd - inSectionStart + 1;
500 // move section to earlier point
502 System.arraycopy(_dataPoints, 0, newPointArray, 0, inMoveTo); // unchanged points before
504 System.arraycopy(_dataPoints, inSectionStart, newPointArray, inMoveTo, sectionLength); // moved bit
505 // after insertion point, before moved bit
506 System.arraycopy(_dataPoints, inMoveTo, newPointArray, inMoveTo + sectionLength, inSectionStart - inMoveTo);
508 if (inSectionEnd < (_numPoints - 1)) {
509 System.arraycopy(_dataPoints, inSectionEnd+1, newPointArray, inSectionEnd+1, _numPoints - inSectionEnd - 1);
514 // Move section to later point
515 if (inSectionStart > 0) {
516 System.arraycopy(_dataPoints, 0, newPointArray, 0, inSectionStart); // unchanged points before
518 // from end of section to move to point
519 if (inMoveTo > (inSectionEnd + 1)) {
520 System.arraycopy(_dataPoints, inSectionEnd+1, newPointArray, inSectionStart, inMoveTo - inSectionEnd - 1);
523 System.arraycopy(_dataPoints, inSectionStart, newPointArray, inSectionStart + inMoveTo - inSectionEnd - 1,
524 inSectionEnd - inSectionStart + 1);
525 // unchanged bit after
526 if (inSectionEnd < (_numPoints - 1)) {
527 System.arraycopy(_dataPoints, inMoveTo, newPointArray, inMoveTo, _numPoints - inMoveTo);
530 // Copy array references
531 _dataPoints = newPointArray;
540 * Interpolate extra points between two selected ones
541 * @param inStartIndex start index of interpolation
542 * @param inNumPoints num points to insert
543 * @return true if successful
545 public boolean interpolate(int inStartIndex, int inNumPoints)
548 if (inStartIndex < 0 || inStartIndex >= _numPoints || inNumPoints <= 0)
551 // get start and end points
552 DataPoint startPoint = getPoint(inStartIndex);
553 DataPoint endPoint = getPoint(inStartIndex + 1);
555 // Make array of points to insert
556 DataPoint[] insertedPoints = startPoint.interpolate(endPoint, inNumPoints);
558 // Insert points into track
559 return insertRange(insertedPoints, inStartIndex + 1);
564 * Average selected points
565 * @param inStartIndex start index of selection
566 * @param inEndIndex end index of selection
567 * @return true if successful
569 public boolean average(int inStartIndex, int inEndIndex)
572 if (inStartIndex < 0 || inStartIndex >= _numPoints || inEndIndex <= inStartIndex)
575 DataPoint startPoint = getPoint(inStartIndex);
576 double firstLatitude = startPoint.getLatitude().getDouble();
577 double firstLongitude = startPoint.getLongitude().getDouble();
578 double latitudeDiff = 0.0, longitudeDiff = 0.0;
579 double totalAltitude = 0;
580 int numAltitudes = 0;
582 // loop between start and end points
583 for (int i=inStartIndex; i<= inEndIndex; i++)
585 DataPoint currPoint = getPoint(i);
586 latitudeDiff += (currPoint.getLatitude().getDouble() - firstLatitude);
587 longitudeDiff += (currPoint.getLongitude().getDouble() - firstLongitude);
588 if (currPoint.hasAltitude())
590 totalAltitude += currPoint.getAltitude().getValue(altUnit);
591 // Use altitude format of first valid altitude
593 altUnit = currPoint.getAltitude().getUnit();
597 int numPoints = inEndIndex - inStartIndex + 1;
598 double meanLatitude = firstLatitude + (latitudeDiff / numPoints);
599 double meanLongitude = firstLongitude + (longitudeDiff / numPoints);
600 Altitude meanAltitude = null;
601 if (numAltitudes > 0) {
602 meanAltitude = new Altitude((int) (totalAltitude / numAltitudes), altUnit);
605 DataPoint insertedPoint = new DataPoint(new Latitude(meanLatitude, Coordinate.FORMAT_NONE),
606 new Longitude(meanLongitude, Coordinate.FORMAT_NONE), meanAltitude);
607 // Make into singleton
608 insertedPoint.setSegmentStart(true);
609 DataPoint nextPoint = getNextTrackPoint(inEndIndex+1);
610 if (nextPoint != null) {nextPoint.setSegmentStart(true);}
611 // Insert points into track
612 return insertRange(new DataPoint[] {insertedPoint}, inEndIndex + 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];
651 * @return the number of (valid) points in the track
653 public int getNumPoints()
659 * @return The range of x values as a DoubleRange object
661 public DoubleRange getXRange()
663 if (!_scaled) {scalePoints();}
668 * @return The range of y values as a DoubleRange object
670 public DoubleRange getYRange()
672 if (!_scaled) {scalePoints();}
677 * @return The range of lat values as a DoubleRange object
679 public DoubleRange getLatRange()
681 if (!_scaled) {scalePoints();}
685 * @return The range of lon values as a DoubleRange object
687 public DoubleRange getLonRange()
689 if (!_scaled) {scalePoints();}
694 * @param inPointNum point index, starting at 0
695 * @return scaled x value of specified point
697 public double getX(int inPointNum)
699 if (!_scaled) {scalePoints();}
700 return _xValues[inPointNum];
704 * @param inPointNum point index, starting at 0
705 * @return scaled y value of specified point
707 public double getY(int inPointNum)
709 if (!_scaled) {scalePoints();}
710 return _yValues[inPointNum];
714 * @return the master field list
716 public FieldList getFieldList()
718 return _masterFieldList;
723 * Checks if any data exists for the specified field
724 * @param inField Field to examine
725 * @return true if data exists for this field
727 public boolean hasData(Field inField)
729 // Don't use this method for altitudes
730 if (inField.equals(Field.ALTITUDE)) {return hasAltitudeData();}
731 return hasData(inField, 0, _numPoints-1);
736 * Checks if any data exists for the specified field in the specified range
737 * @param inField Field to examine
738 * @param inStart start of range to check
739 * @param inEnd end of range to check (inclusive)
740 * @return true if data exists for this field
742 public boolean hasData(Field inField, int inStart, int inEnd)
744 // Loop over selected point range
745 for (int i=inStart; i<=inEnd; i++)
747 if (_dataPoints[i].getFieldValue(inField) != null)
749 // Check altitudes and timestamps
750 if ((inField != Field.ALTITUDE || _dataPoints[i].getAltitude().isValid())
751 && (inField != Field.TIMESTAMP || _dataPoints[i].getTimestamp().isValid()))
761 * @return true if track has altitude data
763 public boolean hasAltitudeData()
765 for (int i=0; i<_numPoints; i++) {
766 if (_dataPoints[i].hasAltitude()) {return true;}
772 * @return true if track contains at least one trackpoint
774 public boolean hasTrackPoints()
776 if (!_scaled) {scalePoints();}
777 return _hasTrackpoint;
781 * @return true if track contains waypoints
783 public boolean hasWaypoints()
785 if (!_scaled) {scalePoints();}
790 * @return true if track contains any points marked for deletion
792 public boolean hasMarkedPoints()
794 if (_numPoints < 1) {
797 // Loop over points looking for any marked for deletion
798 for (int i=0; i<=_numPoints-1; i++)
800 if (_dataPoints[i] != null && _dataPoints[i].getDeleteFlag()) {
809 * Clear all the deletion markers
811 public void clearDeletionMarkers()
813 for (int i=0; i<_numPoints; i++)
815 _dataPoints[i].setMarkedForDeletion(false);
820 * Collect all the waypoints into the given List
821 * @param inList List to fill with waypoints
823 public void getWaypoints(List<DataPoint> inList)
827 // loop over points and copy all waypoints into list
828 for (int i=0; i<=_numPoints-1; i++)
830 if (_dataPoints[i] != null && _dataPoints[i].isWaypoint())
832 inList.add(_dataPoints[i]);
839 * Search for the given Point in the track and return the index
840 * @param inPoint Point to look for
841 * @return index of Point, if any or -1 if not found
843 public int getPointIndex(DataPoint inPoint)
847 // Loop over points in track
848 for (int i=0; i<=_numPoints-1; i++)
850 if (_dataPoints[i] == inPoint)
861 ///////// Internal processing methods ////////////////
865 * Scale all the points in the track to gain x and y values
868 private synchronized void scalePoints()
870 // Loop through all points in track, to see limits of lat, long
871 _longRange = new DoubleRange();
872 _latRange = new DoubleRange();
874 _hasWaypoint = false; _hasTrackpoint = false;
875 for (p=0; p < getNumPoints(); p++)
877 DataPoint point = getPoint(p);
878 if (point != null && point.isValid())
880 _longRange.addValue(point.getLongitude().getDouble());
881 _latRange.addValue(point.getLatitude().getDouble());
882 if (point.isWaypoint())
885 _hasTrackpoint = true;
889 // Loop over points and calculate scales
890 _xValues = new double[getNumPoints()];
891 _yValues = new double[getNumPoints()];
892 _xRange = new DoubleRange();
893 _yRange = new DoubleRange();
894 for (p=0; p < getNumPoints(); p++)
896 DataPoint point = getPoint(p);
899 _xValues[p] = MapUtils.getXFromLongitude(point.getLongitude().getDouble());
900 _xRange.addValue(_xValues[p]);
901 _yValues[p] = MapUtils.getYFromLatitude(point.getLatitude().getDouble());
902 _yRange.addValue(_yValues[p]);
910 * Find the nearest point to the specified x and y coordinates
911 * or -1 if no point is within the specified max distance
912 * @param inX x coordinate
913 * @param inY y coordinate
914 * @param inMaxDist maximum distance from selected coordinates
915 * @param inJustTrackPoints true if waypoints should be ignored
916 * @return index of nearest point or -1 if not found
918 public int getNearestPointIndex(double inX, double inY, double inMaxDist, boolean inJustTrackPoints)
920 int nearestPoint = 0;
921 double nearestDist = -1.0;
923 for (int i=0; i < getNumPoints(); i++)
925 if (!inJustTrackPoints || !_dataPoints[i].isWaypoint())
927 yDist = Math.abs(_yValues[i] - inY);
928 if (yDist < nearestDist || nearestDist < 0.0)
930 // y dist is within range, so check x too
931 mDist = yDist + getMinXDist(_xValues[i] - inX);
932 if (mDist < nearestDist || nearestDist < 0.0)
940 // Check whether it's within required distance
941 if (nearestDist > inMaxDist && inMaxDist > 0.0)
949 * @param inX x value of point
950 * @return minimum wrapped value
952 private static final double getMinXDist(double inX)
954 // TODO: Can use some kind of floor here?
955 return Math.min(Math.min(Math.abs(inX), Math.abs(inX-1.0)), Math.abs(inX+1.0));
959 * Get the next track point starting from the given index
960 * @param inStartIndex index to start looking from
961 * @return next track point, or null if end of data reached
963 public DataPoint getNextTrackPoint(int inStartIndex)
965 return getNextTrackPoint(inStartIndex, _numPoints, true);
969 * Get the next track point in the given range
970 * @param inStartIndex index to start looking from
971 * @param inEndIndex index to stop looking
972 * @return next track point, or null if end of data reached
974 public DataPoint getNextTrackPoint(int inStartIndex, int inEndIndex)
976 return getNextTrackPoint(inStartIndex, inEndIndex, true);
980 * Get the previous track point starting from the given index
981 * @param inStartIndex index to start looking from
982 * @return next track point, or null if end of data reached
984 public DataPoint getPreviousTrackPoint(int inStartIndex)
986 // end index is given as _numPoints but actually it just counts down to -1
987 return getNextTrackPoint(inStartIndex, _numPoints, false);
991 * Get the next track point starting from the given index
992 * @param inStartIndex index to start looking from
993 * @param inEndIndex index to stop looking (inclusive)
994 * @param inCountUp true for next, false for previous
995 * @return next track point, or null if end of data reached
997 private DataPoint getNextTrackPoint(int inStartIndex, int inEndIndex, boolean inCountUp)
999 // Loop forever over points
1000 int increment = inCountUp?1:-1;
1001 for (int i=inStartIndex; i<=inEndIndex; i+=increment)
1003 DataPoint point = getPoint(i);
1004 // Exit if end of data reached - there wasn't a track point
1005 if (point == null) {return null;}
1006 if (point.isValid() && !point.isWaypoint()) {
1007 // next track point found
1015 * Shift all the segment start flags in the given range by 1
1016 * Method used by reverse range and its undo
1017 * @param inStartIndex start of range, inclusive
1018 * @param inEndIndex end of range, inclusive
1020 public void shiftSegmentStarts(int inStartIndex, int inEndIndex)
1022 boolean prevFlag = true;
1023 boolean currFlag = true;
1024 for (int i=inStartIndex; i<= inEndIndex; i++)
1026 DataPoint point = getPoint(i);
1027 if (point != null && !point.isWaypoint())
1030 currFlag = point.getSegmentStart();
1032 point.setSegmentStart(prevFlag);
1033 prevFlag = currFlag;
1038 ////////////////// Cloning and replacing ///////////////////
1041 * Clone the array of DataPoints
1042 * @return shallow copy of DataPoint objects
1044 public DataPoint[] cloneContents()
1046 DataPoint[] clone = new DataPoint[getNumPoints()];
1047 System.arraycopy(_dataPoints, 0, clone, 0, getNumPoints());
1053 * Clone the specified range of data points
1054 * @param inStart start index (inclusive)
1055 * @param inEnd end index (inclusive)
1056 * @return shallow copy of DataPoint objects
1058 public DataPoint[] cloneRange(int inStart, int inEnd)
1060 int numSelected = 0;
1061 if (inEnd >= 0 && inEnd >= inStart)
1063 numSelected = inEnd - inStart + 1;
1065 DataPoint[] result = new DataPoint[numSelected>0?numSelected:0];
1066 if (numSelected > 0)
1068 System.arraycopy(_dataPoints, inStart, result, 0, numSelected);
1075 * Re-insert the specified point at the given index
1076 * @param inPoint point to insert
1077 * @param inIndex index at which to insert the point
1078 * @return true if it worked, false otherwise
1080 public boolean insertPoint(DataPoint inPoint, int inIndex)
1082 if (inIndex > _numPoints || inPoint == null)
1086 // Make new array to copy points over to
1087 DataPoint[] newPointArray = new DataPoint[_numPoints + 1];
1090 System.arraycopy(_dataPoints, 0, newPointArray, 0, inIndex);
1092 newPointArray[inIndex] = inPoint;
1093 if (inIndex < _numPoints)
1095 System.arraycopy(_dataPoints, inIndex, newPointArray, inIndex+1, _numPoints - inIndex);
1097 // Change over to new array
1098 _dataPoints = newPointArray;
1100 // needs to be scaled again
1102 UpdateMessageBroker.informSubscribers();
1108 * Re-insert the specified point range at the given index
1109 * @param inPoints point array to insert
1110 * @param inIndex index at which to insert the points
1111 * @return true if it worked, false otherwise
1113 public boolean insertRange(DataPoint[] inPoints, int inIndex)
1115 if (inIndex > _numPoints || inPoints == null)
1119 // Make new array to copy points over to
1120 DataPoint[] newPointArray = new DataPoint[_numPoints + inPoints.length];
1123 System.arraycopy(_dataPoints, 0, newPointArray, 0, inIndex);
1125 System.arraycopy(inPoints, 0, newPointArray, inIndex, inPoints.length);
1126 if (inIndex < _numPoints)
1128 System.arraycopy(_dataPoints, inIndex, newPointArray, inIndex+inPoints.length, _numPoints - inIndex);
1130 // Change over to new array
1131 _dataPoints = newPointArray;
1132 _numPoints += inPoints.length;
1133 // needs to be scaled again
1135 UpdateMessageBroker.informSubscribers();
1141 * Replace the track contents with the given point array
1142 * @param inContents array of DataPoint objects
1143 * @return true on success
1145 public boolean replaceContents(DataPoint[] inContents)
1147 // master field array stays the same
1148 // (would need to store field array too if we wanted to redo a load)
1149 // replace data array
1150 _dataPoints = inContents;
1151 _numPoints = _dataPoints.length;
1153 UpdateMessageBroker.informSubscribers();
1159 * Edit the specified point
1160 * @param inPoint point to edit
1161 * @param inEditList list of edits to make
1162 * @param inUndo true if undo operation, false otherwise
1163 * @return true if successful
1165 public boolean editPoint(DataPoint inPoint, FieldEditList inEditList, boolean inUndo)
1167 if (inPoint != null && inEditList != null && inEditList.getNumEdits() > 0)
1169 // remember if coordinates have changed
1170 boolean coordsChanged = false;
1171 // go through edits one by one
1172 int numEdits = inEditList.getNumEdits();
1173 for (int i=0; i<numEdits; i++)
1175 FieldEdit edit = inEditList.getEdit(i);
1176 Field editField = edit.getField();
1177 inPoint.setFieldValue(editField, edit.getValue(), inUndo);
1178 // Check that master field list has this field already (maybe point name has been added)
1179 if (!_masterFieldList.contains(editField)) {
1180 _masterFieldList.extendList(editField);
1182 // check coordinates
1183 coordsChanged |= (editField.equals(Field.LATITUDE)
1184 || editField.equals(Field.LONGITUDE) || editField.equals(Field.ALTITUDE));
1186 // set photo status if coordinates have changed
1187 if (inPoint.getPhoto() != null && coordsChanged)
1189 inPoint.getPhoto().setCurrentStatus(Photo.Status.CONNECTED);
1191 // point possibly needs to be scaled again
1193 // trigger listeners
1194 UpdateMessageBroker.informSubscribers();
1201 * @param inPoint point to check
1202 * @return true if this track contains the given point
1204 public boolean containsPoint(DataPoint inPoint)
1206 if (inPoint == null) return false;
1207 for (int i=0; i < getNumPoints(); i++)
1209 if (getPoint(i) == inPoint) return true;
1211 return false; // not found