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 giving arrays of Fields and Objects
36 * @param inFieldArray field array
37 * @param inPointArray 2d array of field values
39 public Track(UpdateMessageBroker inBroker)
43 _masterFieldList = new FieldList(null);
44 // make empty DataPoint array
45 _dataPoints = new DataPoint[0];
53 * Load method, for initialising and reinitialising data
54 * @param inFieldArray array of Field objects describing fields
55 * @param inPointArray 2d object array containing data
56 * @param inAltFormat altitude format
58 public void load(Field[] inFieldArray, Object[][] inPointArray, int inAltFormat)
60 if (inFieldArray == null || inPointArray == null)
66 _masterFieldList = new FieldList(inFieldArray);
67 // make DataPoint object from each point in inPointList
68 _dataPoints = new DataPoint[inPointArray.length];
69 String[] dataArray = null;
71 for (int p=0; p < inPointArray.length; p++)
73 dataArray = (String[]) inPointArray[p];
74 // Convert to DataPoint objects
75 DataPoint point = new DataPoint(dataArray, _masterFieldList, inAltFormat);
78 _dataPoints[pointIndex] = point;
82 _numPoints = pointIndex;
88 ////////////////// Modification methods //////////////////////
92 * Combine this Track with new data
95 public void combine(Track inOtherTrack)
98 _masterFieldList = _masterFieldList.merge(inOtherTrack._masterFieldList);
99 // expand data array and add other track's data points
100 int totalPoints = getNumPoints() + inOtherTrack.getNumPoints();
101 DataPoint[] mergedPoints = new DataPoint[totalPoints];
102 System.arraycopy(_dataPoints, 0, mergedPoints, 0, getNumPoints());
103 System.arraycopy(inOtherTrack._dataPoints, 0, mergedPoints, getNumPoints(), inOtherTrack.getNumPoints());
104 _dataPoints = mergedPoints;
105 // combine point count
106 _numPoints = totalPoints;
107 // needs to be scaled again
110 _broker.informSubscribers();
115 * Crop the track to the given size - subsequent points are not (yet) deleted
116 * @param inNewSize new number of points in track
118 public void cropTo(int inNewSize)
120 if (inNewSize >= 0 && inNewSize < getNumPoints())
122 _numPoints = inNewSize;
123 // needs to be scaled again
125 _broker.informSubscribers();
131 * Compress the track to the given resolution
132 * @param inResolution resolution
133 * @return number of points deleted
135 public int compress(int inResolution)
137 // (maybe should be separate thread?)
138 // (maybe should be in separate class?)
139 // (maybe should be based on subtended angles instead of distances?)
141 if (inResolution <= 0) return 0;
143 // Establish range of track and minimum range between points
145 double wholeScale = _xRange.getMaximum() - _xRange.getMinimum();
146 double yscale = _yRange.getMaximum() - _yRange.getMinimum();
147 if (yscale > wholeScale) wholeScale = yscale;
148 double minDist = wholeScale / inResolution;
150 // Copy selected points
151 DataPoint[] newPointArray = new DataPoint[_numPoints];
152 int[] pointIndices = new int[_numPoints];
153 for (int i=0; i<_numPoints; i++)
155 boolean keepPoint = true;
156 // Don't delete waypoints or photo points
157 if (!_dataPoints[i].isWaypoint() && _dataPoints[i].getPhoto() == null)
159 // go through newPointArray to check for range
160 for (int j=0; j<numCopied && keepPoint; j++)
162 // calculate distance between point j and current point
163 double pointDist = Math.abs(_xValues[i] - _xValues[pointIndices[j]])
164 + Math.abs(_yValues[i] - _yValues[pointIndices[j]]);
165 if (pointDist < minDist)
171 newPointArray[numCopied] = _dataPoints[i];
172 pointIndices[numCopied] = i;
177 // Copy array references
178 int numDeleted = _numPoints - numCopied;
181 _dataPoints = new DataPoint[numCopied];
182 System.arraycopy(newPointArray, 0, _dataPoints, 0, numCopied);
183 _numPoints = _dataPoints.length;
185 _broker.informSubscribers();
192 * Halve the track by deleting alternate points
193 * @return number of points deleted
197 if (_numPoints < 100) return 0;
198 int newSize = _numPoints / 2;
199 int numDeleted = _numPoints - newSize;
200 DataPoint[] newPointArray = new DataPoint[newSize];
201 // Delete alternate points
202 for (int i=0; i<newSize; i++)
203 newPointArray[i] = _dataPoints[i*2];
204 // Copy array references
205 _dataPoints = newPointArray;
206 _numPoints = _dataPoints.length;
208 _broker.informSubscribers();
214 * Delete the specified point
215 * @return true if successful
217 public boolean deletePoint(int inIndex)
219 boolean answer = deleteRange(inIndex, inIndex);
225 * Delete the specified range of points from the Track
226 * @param inStart start of range (inclusive)
227 * @param inEnd end of range (inclusive)
228 * @return true if successful
230 public boolean deleteRange(int inStart, int inEnd)
232 if (inStart < 0 || inEnd < 0 || inEnd < inStart)
234 // no valid range selected so can't delete
237 // valid range, let's delete it
238 int numToDelete = inEnd - inStart + 1;
239 DataPoint[] newPointArray = new DataPoint[_numPoints - numToDelete];
240 // Copy points before the selected range
243 System.arraycopy(_dataPoints, 0, newPointArray, 0, inStart);
245 // Copy points after the deleted one(s)
246 if (inEnd < (_numPoints - 1))
248 System.arraycopy(_dataPoints, inEnd + 1, newPointArray, inStart,
249 _numPoints - inEnd - 1);
251 // Copy points over original array
252 _dataPoints = newPointArray;
253 _numPoints -= numToDelete;
254 // needs to be scaled again
261 * Delete all the duplicate points in the track
262 * @return number of points deleted
264 public int deleteDuplicates()
266 // loop through Track counting duplicates first
267 boolean[] dupes = new boolean[_numPoints];
270 for (i=1; i<_numPoints; i++)
272 DataPoint p1 = _dataPoints[i];
273 // Loop through all points before this one
274 for (j=0; j<i && !dupes[i]; j++)
276 DataPoint p2 = _dataPoints[j];
277 if (p1.isDuplicate(p2))
286 // Make new resized array and copy DataPoints over
287 DataPoint[] newPointArray = new DataPoint[_numPoints - numDupes];
289 for (i=0; i<_numPoints; i++)
293 newPointArray[j] = _dataPoints[i];
297 // Copy array references
298 _dataPoints = newPointArray;
299 _numPoints = _dataPoints.length;
301 _broker.informSubscribers();
308 * Reverse the specified range of points
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
852 public boolean replaceContents(DataPoint[] inContents)
854 // master field array stays the same
855 // (would need to store field array too if we wanted to redo a load)
856 // replace data array
857 _dataPoints = inContents;
858 _numPoints = _dataPoints.length;
860 _broker.informSubscribers();
866 * Edit the specified point
867 * @param inPoint point to edit
868 * @param inEditList list of edits to make
869 * @return true if successful
871 public boolean editPoint(DataPoint inPoint, FieldEditList inEditList)
873 if (inPoint != null && inEditList != null && inEditList.getNumEdits() > 0)
875 // remember if coordinates have changed
876 boolean coordsChanged = false;
877 // go through edits one by one
878 int numEdits = inEditList.getNumEdits();
879 for (int i=0; i<numEdits; i++)
881 FieldEdit edit = inEditList.getEdit(i);
882 inPoint.setFieldValue(edit.getField(), edit.getValue());
884 coordsChanged |= (edit.getField().equals(Field.LATITUDE)
885 || edit.getField().equals(Field.LONGITUDE) || edit.getField().equals(Field.ALTITUDE));
887 // set photo status if coordinates have changed
888 if (inPoint.getPhoto() != null && coordsChanged)
890 inPoint.getPhoto().setCurrentStatus(PhotoStatus.CONNECTED);
892 // point possibly needs to be scaled again
895 _broker.informSubscribers();