1 package tim.prune.data;
5 import tim.prune.UpdateMessageBroker;
6 import tim.prune.edit.FieldEdit;
7 import tim.prune.edit.FieldEditList;
11 * Class to hold all track information,
12 * including track points and waypoints
17 UpdateMessageBroker _broker = null;
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 _mixedData = false;
27 private FieldList _masterFieldList = null;
29 private AltitudeRange _altitudeRange = null;
30 private DoubleRange _latRange = null, _longRange = null;
31 private DoubleRange _xRange = null, _yRange = null;
35 * Constructor for empty track
36 * @param inBroker message broker object
38 public Track(UpdateMessageBroker inBroker)
42 _masterFieldList = new FieldList(null);
43 // make empty DataPoint array
44 _dataPoints = new DataPoint[0];
52 * Load method, for initialising and reinitialising data
53 * @param inFieldArray array of Field objects describing fields
54 * @param inPointArray 2d object array containing data
55 * @param inAltFormat altitude format
57 public void load(Field[] inFieldArray, Object[][] inPointArray, int inAltFormat)
59 if (inFieldArray == null || inPointArray == null)
65 _masterFieldList = new FieldList(inFieldArray);
66 // make DataPoint object from each point in inPointList
67 _dataPoints = new DataPoint[inPointArray.length];
68 String[] dataArray = null;
70 for (int p=0; p < inPointArray.length; p++)
72 dataArray = (String[]) inPointArray[p];
73 // Convert to DataPoint objects
74 DataPoint point = new DataPoint(dataArray, _masterFieldList, inAltFormat);
77 _dataPoints[pointIndex] = point;
81 _numPoints = pointIndex;
87 ////////////////// Modification methods //////////////////////
91 * Combine this Track with new data
94 public void combine(Track inOtherTrack)
97 _masterFieldList = _masterFieldList.merge(inOtherTrack._masterFieldList);
98 // expand data array and add other track's data points
99 int totalPoints = getNumPoints() + inOtherTrack.getNumPoints();
100 DataPoint[] mergedPoints = new DataPoint[totalPoints];
101 System.arraycopy(_dataPoints, 0, mergedPoints, 0, getNumPoints());
102 System.arraycopy(inOtherTrack._dataPoints, 0, mergedPoints, getNumPoints(), inOtherTrack.getNumPoints());
103 _dataPoints = mergedPoints;
104 // combine point count
105 _numPoints = totalPoints;
106 // needs to be scaled again
109 _broker.informSubscribers();
114 * Crop the track to the given size - subsequent points are not (yet) deleted
115 * @param inNewSize new number of points in track
117 public void cropTo(int inNewSize)
119 if (inNewSize >= 0 && inNewSize < getNumPoints())
121 _numPoints = inNewSize;
122 // needs to be scaled again
124 _broker.informSubscribers();
130 * Compress the track to the given resolution
131 * @param inResolution resolution
132 * @return number of points deleted
134 public int compress(int inResolution)
136 // (maybe should be separate thread?)
137 // (maybe should be in separate class?)
138 // (maybe should be based on subtended angles instead of distances?)
140 if (inResolution <= 0) return 0;
142 // Establish range of track and minimum range between points
144 double wholeScale = _xRange.getMaximum() - _xRange.getMinimum();
145 double yscale = _yRange.getMaximum() - _yRange.getMinimum();
146 if (yscale > wholeScale) wholeScale = yscale;
147 double minDist = wholeScale / inResolution;
149 // Copy selected points
150 DataPoint[] newPointArray = new DataPoint[_numPoints];
151 int[] pointIndices = new int[_numPoints];
152 for (int i=0; i<_numPoints; i++)
154 boolean keepPoint = true;
155 // Don't delete waypoints or photo points
156 if (!_dataPoints[i].isWaypoint() && _dataPoints[i].getPhoto() == null)
158 // go through newPointArray to check for range
159 for (int j=0; j<numCopied && keepPoint; j++)
161 // calculate distance between point j and current point
162 double pointDist = Math.abs(_xValues[i] - _xValues[pointIndices[j]])
163 + Math.abs(_yValues[i] - _yValues[pointIndices[j]]);
164 if (pointDist < minDist)
170 newPointArray[numCopied] = _dataPoints[i];
171 pointIndices[numCopied] = i;
176 // Copy array references
177 int numDeleted = _numPoints - numCopied;
180 _dataPoints = new DataPoint[numCopied];
181 System.arraycopy(newPointArray, 0, _dataPoints, 0, numCopied);
182 _numPoints = _dataPoints.length;
190 * Halve the track by deleting alternate points
191 * @return number of points deleted
195 if (_numPoints < 100) return 0;
196 int newSize = _numPoints / 2;
197 int numDeleted = _numPoints - newSize;
198 DataPoint[] newPointArray = new DataPoint[newSize];
199 // Delete alternate points
200 for (int i=0; i<newSize; i++)
201 newPointArray[i] = _dataPoints[i*2];
202 // Copy array references
203 _dataPoints = newPointArray;
204 _numPoints = _dataPoints.length;
206 _broker.informSubscribers();
212 * Delete the specified point
213 * @param inIndex point index
214 * @return true if successful
216 public boolean deletePoint(int inIndex)
218 boolean answer = deleteRange(inIndex, inIndex);
224 * Delete the specified range of points from the Track
225 * @param inStart start of range (inclusive)
226 * @param inEnd end of range (inclusive)
227 * @return true if successful
229 public boolean deleteRange(int inStart, int inEnd)
231 if (inStart < 0 || inEnd < 0 || inEnd < inStart)
233 // no valid range selected so can't delete
236 // valid range, let's delete it
237 int numToDelete = inEnd - inStart + 1;
238 DataPoint[] newPointArray = new DataPoint[_numPoints - numToDelete];
239 // Copy points before the selected range
242 System.arraycopy(_dataPoints, 0, newPointArray, 0, inStart);
244 // Copy points after the deleted one(s)
245 if (inEnd < (_numPoints - 1))
247 System.arraycopy(_dataPoints, inEnd + 1, newPointArray, inStart,
248 _numPoints - inEnd - 1);
250 // Copy points over original array
251 _dataPoints = newPointArray;
252 _numPoints -= numToDelete;
253 // needs to be scaled again
260 * Delete all the duplicate points in the track
261 * @return number of points deleted
263 public int deleteDuplicates()
265 // loop through Track counting duplicates first
266 boolean[] dupes = new boolean[_numPoints];
269 for (i=1; i<_numPoints; i++)
271 DataPoint p1 = _dataPoints[i];
272 // Loop through all points before this one
273 for (j=0; j<i && !dupes[i]; j++)
275 DataPoint p2 = _dataPoints[j];
276 if (p1.isDuplicate(p2))
285 // Make new resized array and copy DataPoints over
286 DataPoint[] newPointArray = new DataPoint[_numPoints - numDupes];
288 for (i=0; i<_numPoints; i++)
292 newPointArray[j] = _dataPoints[i];
296 // Copy array references
297 _dataPoints = newPointArray;
298 _numPoints = _dataPoints.length;
306 * Reverse the specified range of points
307 * @param inStart start index
308 * @param inEnd end index
309 * @return true if successful, false otherwise
311 public boolean reverseRange(int inStart, int inEnd)
313 if (inStart < 0 || inEnd < 0 || inStart >= inEnd || inEnd >= _numPoints)
317 // calculate how many point swaps are required
318 int numPointsToReverse = (inEnd - inStart + 1) / 2;
320 for (int i=0; i<numPointsToReverse; i++)
322 // swap pairs of points
323 p = _dataPoints[inStart + i];
324 _dataPoints[inStart + i] = _dataPoints[inEnd - i];
325 _dataPoints[inEnd - i] = p;
327 // needs to be scaled again
329 _broker.informSubscribers();
335 * Collect all waypoints to the start or end of the track
336 * @param inAtStart true to collect at start, false for end
337 * @return true if successful, false if no change
339 public boolean collectWaypoints(boolean inAtStart)
341 // Check for mixed data, numbers of waypoints & nons
342 int numWaypoints = 0, numNonWaypoints = 0;
343 boolean wayAfterNon = false, nonAfterWay = false;
344 DataPoint[] waypoints = new DataPoint[_numPoints];
345 DataPoint[] nonWaypoints = new DataPoint[_numPoints];
346 DataPoint point = null;
347 for (int i=0; i<_numPoints; i++)
349 point = _dataPoints[i];
350 if (point.isWaypoint())
352 waypoints[numWaypoints] = point;
354 wayAfterNon |= (numNonWaypoints > 0);
358 nonWaypoints[numNonWaypoints] = point;
360 nonAfterWay |= (numWaypoints > 0);
363 // Exit if the data is already in the specified order
364 if (numWaypoints == 0 || numNonWaypoints == 0
365 || (inAtStart && !wayAfterNon && nonAfterWay)
366 || (!inAtStart && wayAfterNon && !nonAfterWay))
371 // Copy the arrays back into _dataPoints in the specified order
374 System.arraycopy(waypoints, 0, _dataPoints, 0, numWaypoints);
375 System.arraycopy(nonWaypoints, 0, _dataPoints, numWaypoints, numNonWaypoints);
379 System.arraycopy(nonWaypoints, 0, _dataPoints, 0, numNonWaypoints);
380 System.arraycopy(waypoints, 0, _dataPoints, numNonWaypoints, numWaypoints);
382 // needs to be scaled again
384 _broker.informSubscribers();
390 * Interleave all waypoints by each nearest track point
391 * @return true if successful, false if no change
393 public boolean interleaveWaypoints()
395 // Separate waypoints and find nearest track point
396 int numWaypoints = 0;
397 DataPoint[] waypoints = new DataPoint[_numPoints];
398 int[] pointIndices = new int[_numPoints];
399 DataPoint point = null;
401 for (i=0; i<_numPoints; i++)
403 point = _dataPoints[i];
404 if (point.isWaypoint())
406 waypoints[numWaypoints] = point;
407 pointIndices[numWaypoints] = getNearestPointIndex(
408 _xValues[i], _yValues[i], -1.0, true);
412 // Exit if data not mixed
413 if (numWaypoints == 0 || numWaypoints == _numPoints)
416 // Loop round points copying to correct order
417 DataPoint[] dataCopy = new DataPoint[_numPoints];
419 for (i=0; i<_numPoints; i++)
421 point = _dataPoints[i];
422 // if it's a track point, copy it
423 if (!point.isWaypoint())
425 dataCopy[copyIndex] = point;
428 // check for waypoints with this index
429 for (int j=0; j<numWaypoints; j++)
431 if (pointIndices[j] == i)
433 dataCopy[copyIndex] = waypoints[j];
438 // Copy data back to track
439 _dataPoints = dataCopy;
440 // needs to be scaled again to recalc x, y
442 _broker.informSubscribers();
448 * Interpolate extra points between two selected ones
449 * @param inStartIndex start index of interpolation
450 * @param inNumPoints num points to insert
451 * @return true if successful
453 public boolean interpolate(int inStartIndex, int inNumPoints)
456 if (inStartIndex < 0 || inStartIndex >= _numPoints || inNumPoints <= 0)
459 // get start and end points
460 DataPoint startPoint = getPoint(inStartIndex);
461 DataPoint endPoint = getPoint(inStartIndex + 1);
463 // Make array of points to insert
464 DataPoint[] insertedPoints = startPoint.interpolate(endPoint, inNumPoints);
466 // Insert points into track
467 return insertRange(insertedPoints, inStartIndex + 1);
472 * Append the specified points to the end of the track
473 * @param inPoints DataPoint objects to add
475 public void appendPoints(DataPoint[] inPoints)
477 // Insert points into track
478 if (inPoints != null && inPoints.length > 0)
480 insertRange(inPoints, _numPoints);
482 // needs to be scaled again to recalc x, y
484 _broker.informSubscribers();
488 //////// information methods /////////////
492 * Get the point at the given index
493 * @param inPointNum index number, starting at 0
494 * @return DataPoint object, or null if out of range
496 public DataPoint getPoint(int inPointNum)
498 if (inPointNum > -1 && inPointNum < getNumPoints())
500 return _dataPoints[inPointNum];
507 * @return altitude range of points as AltitudeRange object
509 public AltitudeRange getAltitudeRange()
511 if (!_scaled) scalePoints();
512 return _altitudeRange;
515 * @return the number of (valid) points in the track
517 public int getNumPoints()
523 * @return The range of x values as a DoubleRange object
525 public DoubleRange getXRange()
527 if (!_scaled) scalePoints();
532 * @return The range of y values as a DoubleRange object
534 public DoubleRange getYRange()
536 if (!_scaled) scalePoints();
541 * @param inPointNum point index, starting at 0
542 * @return scaled x value of specified point
544 public double getX(int inPointNum)
546 if (!_scaled) scalePoints();
547 return _xValues[inPointNum];
551 * @param inPointNum point index, starting at 0
552 * @return scaled y value of specified point
554 public double getY(int inPointNum)
556 if (!_scaled) scalePoints();
557 return _yValues[inPointNum];
561 * @return the master field list
563 public FieldList getFieldList()
565 return _masterFieldList;
570 * Checks if any data exists for the specified field
571 * @param inField Field to examine
572 * @return true if data exists for this field
574 public boolean hasData(Field inField)
576 return hasData(inField, 0, _numPoints-1);
581 * Checks if any data exists for the specified field in the specified range
582 * @param inField Field to examine
583 * @param inStart start of range to check
584 * @param inEnd end of range to check (inclusive)
585 * @return true if data exists for this field
587 public boolean hasData(Field inField, int inStart, int inEnd)
589 for (int i=inStart; i<=inEnd; i++)
591 if (_dataPoints[i].getFieldValue(inField) != null)
601 * @return true if track contains waypoints and trackpoints
603 public boolean hasMixedData()
605 if (!_scaled) scalePoints();
611 * Collect all the waypoints into the given List
612 * @param inList List to fill with waypoints
614 public void getWaypoints(List inList)
618 // loop over points and copy all waypoints into list
619 for (int i=0; i<=_numPoints-1; i++)
621 if (_dataPoints[i] != null && _dataPoints[i].isWaypoint())
623 inList.add(_dataPoints[i]);
630 * Search for the given Point in the track and return the index
631 * @param inPoint Point to look for
632 * @return index of Point, if any or -1 if not found
634 public int getPointIndex(DataPoint inPoint)
638 // Loop over points in track
639 for (int i=0; i<=_numPoints-1; i++)
641 if (_dataPoints[i] == inPoint)
652 ///////// Internal processing methods ////////////////
656 * Scale all the points in the track to gain x and y values
659 private void scalePoints()
661 // Loop through all points in track, to see limits of lat, long and altitude
662 _longRange = new DoubleRange();
663 _latRange = new DoubleRange();
664 _altitudeRange = new AltitudeRange();
666 boolean hasWaypoint = false, hasTrackpoint = false;
667 for (p=0; p < getNumPoints(); p++)
669 DataPoint point = getPoint(p);
670 if (point != null && point.isValid())
672 _longRange.addValue(point.getLongitude().getDouble());
673 _latRange.addValue(point.getLatitude().getDouble());
674 if (point.getAltitude().isValid())
676 _altitudeRange.addValue(point.getAltitude());
678 if (point.isWaypoint())
681 hasTrackpoint = true;
684 _mixedData = hasWaypoint && hasTrackpoint;
686 // Use medians to centre at 0
687 double longMedian = (_longRange.getMaximum() + _longRange.getMinimum()) / 2.0;
688 double latMedian = (_latRange.getMaximum() + _latRange.getMinimum()) / 2.0;
689 double longFactor = Math.cos(latMedian / 180.0 * Math.PI); // Function of median latitude
691 // Loop over points and calculate scales
692 _xValues = new double[getNumPoints()];
693 _yValues = new double[getNumPoints()];
694 _xRange = new DoubleRange();
695 _yRange = new DoubleRange();
696 for (p=0; p < getNumPoints(); p++)
698 DataPoint point = getPoint(p);
701 _xValues[p] = (point.getLongitude().getDouble() - longMedian) * longFactor;
702 _xRange.addValue(_xValues[p]);
703 _yValues[p] = (point.getLatitude().getDouble() - latMedian);
704 _yRange.addValue(_yValues[p]);
712 * Find the nearest point to the specified x and y coordinates
713 * or -1 if no point is within the specified max distance
714 * @param inX x coordinate
715 * @param inY y coordinate
716 * @param inMaxDist maximum distance from selected coordinates
717 * @param inJustTrackPoints true if waypoints should be ignored
718 * @return index of nearest point or -1 if not found
720 public int getNearestPointIndex(double inX, double inY, double inMaxDist, boolean inJustTrackPoints)
722 int nearestPoint = 0;
723 double nearestDist = -1.0;
725 for (int i=0; i < getNumPoints(); i++)
727 if (!inJustTrackPoints || !_dataPoints[i].isWaypoint())
729 currDist = Math.abs(_xValues[i] - inX) + Math.abs(_yValues[i] - inY);
730 if (currDist < nearestDist || nearestDist < 0.0)
733 nearestDist = currDist;
737 // Check whether it's within required distance
738 if (nearestDist > inMaxDist && inMaxDist > 0.0)
746 ////////////////// Cloning and replacing ///////////////////
749 * Clone the array of DataPoints
750 * @return shallow copy of DataPoint objects
752 public DataPoint[] cloneContents()
754 DataPoint[] clone = new DataPoint[getNumPoints()];
755 System.arraycopy(_dataPoints, 0, clone, 0, getNumPoints());
761 * Clone the specified range of data points
762 * @param inStart start index (inclusive)
763 * @param inEnd end index (inclusive)
764 * @return shallow copy of DataPoint objects
766 public DataPoint[] cloneRange(int inStart, int inEnd)
769 if (inEnd >= 0 && inEnd >= inStart)
771 numSelected = inEnd - inStart + 1;
773 DataPoint[] result = new DataPoint[numSelected>0?numSelected:0];
776 System.arraycopy(_dataPoints, inStart, result, 0, numSelected);
783 * Re-insert the specified point at the given index
784 * @param inPoint point to insert
785 * @param inIndex index at which to insert the point
786 * @return true if it worked, false otherwise
788 public boolean insertPoint(DataPoint inPoint, int inIndex)
790 if (inIndex > _numPoints || inPoint == null)
794 // Make new array to copy points over to
795 DataPoint[] newPointArray = new DataPoint[_numPoints + 1];
798 System.arraycopy(_dataPoints, 0, newPointArray, 0, inIndex);
800 newPointArray[inIndex] = inPoint;
801 if (inIndex < _numPoints)
803 System.arraycopy(_dataPoints, inIndex, newPointArray, inIndex+1, _numPoints - inIndex);
805 // Change over to new array
806 _dataPoints = newPointArray;
808 // needs to be scaled again
810 _broker.informSubscribers();
816 * Re-insert the specified point range at the given index
817 * @param inPoints point array to insert
818 * @param inIndex index at which to insert the points
819 * @return true if it worked, false otherwise
821 public boolean insertRange(DataPoint[] inPoints, int inIndex)
823 if (inIndex > _numPoints || inPoints == null)
827 // Make new array to copy points over to
828 DataPoint[] newPointArray = new DataPoint[_numPoints + inPoints.length];
831 System.arraycopy(_dataPoints, 0, newPointArray, 0, inIndex);
833 System.arraycopy(inPoints, 0, newPointArray, inIndex, inPoints.length);
834 if (inIndex < _numPoints)
836 System.arraycopy(_dataPoints, inIndex, newPointArray, inIndex+inPoints.length, _numPoints - inIndex);
838 // Change over to new array
839 _dataPoints = newPointArray;
840 _numPoints += inPoints.length;
841 // needs to be scaled again
843 _broker.informSubscribers();
849 * Replace the track contents with the given point array
850 * @param inContents array of DataPoint objects
851 * @return true on success
853 public boolean replaceContents(DataPoint[] inContents)
855 // master field array stays the same
856 // (would need to store field array too if we wanted to redo a load)
857 // replace data array
858 _dataPoints = inContents;
859 _numPoints = _dataPoints.length;
861 _broker.informSubscribers();
867 * Edit the specified point
868 * @param inPoint point to edit
869 * @param inEditList list of edits to make
870 * @return true if successful
872 public boolean editPoint(DataPoint inPoint, FieldEditList inEditList)
874 if (inPoint != null && inEditList != null && inEditList.getNumEdits() > 0)
876 // remember if coordinates have changed
877 boolean coordsChanged = false;
878 // go through edits one by one
879 int numEdits = inEditList.getNumEdits();
880 for (int i=0; i<numEdits; i++)
882 FieldEdit edit = inEditList.getEdit(i);
883 inPoint.setFieldValue(edit.getField(), edit.getValue());
885 coordsChanged |= (edit.getField().equals(Field.LATITUDE)
886 || edit.getField().equals(Field.LONGITUDE) || edit.getField().equals(Field.ALTITUDE));
888 // set photo status if coordinates have changed
889 if (inPoint.getPhoto() != null && coordsChanged)
891 inPoint.getPhoto().setCurrentStatus(PhotoStatus.CONNECTED);
893 // point possibly needs to be scaled again
896 _broker.informSubscribers();