1 package tim.prune.data;
3 import tim.prune.UpdateMessageBroker;
7 * Class to hold all track information,
8 * including track points and waypoints
13 UpdateMessageBroker _broker = null;
15 private DataPoint[] _dataPoints = null;
17 private double[] _xValues = null;
18 private double[] _yValues = null;
19 private boolean _scaled = false;
20 private int _numPoints = 0;
21 private boolean _mixedData = false;
23 private FieldList _masterFieldList = null;
25 private AltitudeRange _altitudeRange = null;
26 private DoubleRange _latRange = null, _longRange = null;
27 private DoubleRange _xRange = null, _yRange = null;
31 * Constructor giving arrays of Fields and Objects
32 * @param inFieldArray field array
33 * @param inPointArray 2d array of field values
35 public Track(UpdateMessageBroker inBroker)
39 _masterFieldList = new FieldList(null);
40 // make empty DataPoint array
41 _dataPoints = new DataPoint[0];
49 * Load method, for initialising and reinitialising data
50 * @param inFieldArray array of Field objects describing fields
51 * @param inPointArray 2d object array containing data
52 * @param inAltFormat altitude format
54 public void load(Field[] inFieldArray, Object[][] inPointArray, int inAltFormat)
57 _masterFieldList = new FieldList(inFieldArray);
58 // make DataPoint object from each point in inPointList
59 _dataPoints = new DataPoint[inPointArray.length];
60 String[] dataArray = null;
62 for (int p=0; p < inPointArray.length; p++)
64 dataArray = (String[]) inPointArray[p];
65 // Convert to DataPoint objects
66 DataPoint point = new DataPoint(dataArray, _masterFieldList, inAltFormat);
69 _dataPoints[pointIndex] = point;
73 _numPoints = pointIndex;
79 ////////////////// Modification methods //////////////////////
83 * Combine this Track with new data
86 public void combine(Track inOtherTrack)
89 _masterFieldList = _masterFieldList.merge(inOtherTrack._masterFieldList);
90 // expand data array and add other track's data points
91 int totalPoints = getNumPoints() + inOtherTrack.getNumPoints();
92 DataPoint[] mergedPoints = new DataPoint[totalPoints];
93 System.arraycopy(_dataPoints, 0, mergedPoints, 0, getNumPoints());
94 System.arraycopy(inOtherTrack._dataPoints, 0, mergedPoints, getNumPoints(), inOtherTrack.getNumPoints());
95 _dataPoints = mergedPoints;
96 // combine point count
97 _numPoints = totalPoints;
98 // needs to be scaled again
101 _broker.informSubscribers();
106 * Crop the track to the given size - subsequent points are not (yet) deleted
107 * @param inNewSize new number of points in track
109 public void cropTo(int inNewSize)
111 if (inNewSize >= 0 && inNewSize < getNumPoints())
113 _numPoints = inNewSize;
114 // needs to be scaled again
116 _broker.informSubscribers();
122 * Compress the track to the given resolution
123 * @param inResolution resolution
124 * @return number of points deleted
126 public int compress(int inResolution)
128 // (maybe should be separate thread?)
129 // (maybe should be in separate class?)
130 // (maybe should be based on subtended angles instead of distances?)
132 if (inResolution <= 0) return 0;
134 // Establish range of track and minimum range between points
136 double wholeScale = _xRange.getMaximum() - _xRange.getMinimum();
137 double yscale = _yRange.getMaximum() - _yRange.getMinimum();
138 if (yscale > wholeScale) wholeScale = yscale;
139 double minDist = wholeScale / inResolution;
141 // Copy selected points
142 DataPoint[] newPointArray = new DataPoint[_numPoints];
143 int[] pointIndices = new int[_numPoints];
144 for (int i=0; i<_numPoints; i++)
146 boolean keepPoint = true;
147 if (!_dataPoints[i].isWaypoint())
149 // go through newPointArray to check for range
150 for (int j=0; j<numCopied && keepPoint; j++)
152 // calculate distance between point j and current point
153 double pointDist = Math.abs(_xValues[i] - _xValues[pointIndices[j]])
154 + Math.abs(_yValues[i] - _yValues[pointIndices[j]]);
155 if (pointDist < minDist)
161 newPointArray[numCopied] = _dataPoints[i];
162 pointIndices[numCopied] = i;
167 // Copy array references
168 int numDeleted = _numPoints - numCopied;
171 _dataPoints = new DataPoint[numCopied];
172 System.arraycopy(newPointArray, 0, _dataPoints, 0, numCopied);
173 _numPoints = _dataPoints.length;
175 _broker.informSubscribers();
182 * Halve the track by deleting alternate points
183 * @return number of points deleted
187 if (_numPoints < 100) return 0;
188 int newSize = _numPoints / 2;
189 int numDeleted = _numPoints - newSize;
190 DataPoint[] newPointArray = new DataPoint[newSize];
191 // Delete alternate points
192 for (int i=0; i<newSize; i++)
193 newPointArray[i] = _dataPoints[i*2];
194 // Copy array references
195 _dataPoints = newPointArray;
196 _numPoints = _dataPoints.length;
198 _broker.informSubscribers();
204 * Delete the specified point
205 * @return true if successful
207 public boolean deletePoint(int inIndex)
209 boolean answer = deleteRange(inIndex, inIndex);
215 * Delete the specified range of points from the Track
216 * @param inStart start of range (inclusive)
217 * @param inEnd end of range (inclusive)
218 * @return true if successful
220 public boolean deleteRange(int inStart, int inEnd)
222 if (inStart < 0 || inEnd < 0 || inEnd < inStart)
224 // no valid range selected so can't delete
227 // valid range, let's delete it
228 int numToDelete = inEnd - inStart + 1;
229 DataPoint[] newPointArray = new DataPoint[_numPoints - numToDelete];
230 // Copy points before the selected range
233 System.arraycopy(_dataPoints, 0, newPointArray, 0, inStart);
235 // Copy points after the deleted one(s)
236 if (inEnd < (_numPoints - 1))
238 System.arraycopy(_dataPoints, inEnd + 1, newPointArray, inStart,
239 _numPoints - inEnd - 1);
241 // Copy points over original array (careful!)
242 _dataPoints = newPointArray;
243 _numPoints -= numToDelete;
244 // needs to be scaled again
251 * Delete all the duplicate points in the track
252 * @return number of points deleted
254 public int deleteDuplicates()
256 // loop through Track counting duplicates first
257 boolean[] dupes = new boolean[_numPoints];
260 for (i=1; i<_numPoints; i++)
262 DataPoint p1 = _dataPoints[i];
263 // Loop through all points before this one
264 for (j=0; j<i && !dupes[i]; j++)
266 DataPoint p2 = _dataPoints[j];
267 if (p1.isDuplicate(p2))
276 // Make new resized array and copy DataPoints over
277 DataPoint[] newPointArray = new DataPoint[_numPoints - numDupes];
279 for (i=0; i<_numPoints; i++)
283 newPointArray[j] = _dataPoints[i];
287 // Copy array references
288 _dataPoints = newPointArray;
289 _numPoints = _dataPoints.length;
291 _broker.informSubscribers();
298 * Reverse the specified range of points
299 * @return true if successful, false otherwise
301 public boolean reverseRange(int inStart, int inEnd)
303 if (inStart < 0 || inEnd < 0 || inStart >= inEnd || inEnd >= _numPoints)
307 // calculate how many point swaps are required
308 int numPointsToReverse = (inEnd - inStart + 1) / 2;
310 for (int i=0; i<numPointsToReverse; i++)
312 // swap pairs of points
313 p = _dataPoints[inStart + i];
314 _dataPoints[inStart + i] = _dataPoints[inEnd - i];
315 _dataPoints[inEnd - i] = p;
317 // needs to be scaled again
319 _broker.informSubscribers();
325 * Collect all waypoints to the start or end of the track
326 * @param inAtStart true to collect at start, false for end
327 * @return true if successful, false if no change
329 public boolean collectWaypoints(boolean inAtStart)
331 // Check for mixed data, numbers of waypoints & nons
332 int numWaypoints = 0, numNonWaypoints = 0;
333 boolean wayAfterNon = false, nonAfterWay = false;
334 DataPoint[] waypoints = new DataPoint[_numPoints];
335 DataPoint[] nonWaypoints = new DataPoint[_numPoints];
336 DataPoint point = null;
337 for (int i=0; i<_numPoints; i++)
339 point = _dataPoints[i];
340 if (point.isWaypoint())
342 waypoints[numWaypoints] = point;
344 wayAfterNon |= (numNonWaypoints > 0);
348 nonWaypoints[numNonWaypoints] = point;
350 nonAfterWay |= (numWaypoints > 0);
353 // Exit if the data is already in the specified order
354 if (numWaypoints == 0 || numNonWaypoints == 0
355 || (inAtStart && !wayAfterNon && nonAfterWay)
356 || (!inAtStart && wayAfterNon && !nonAfterWay))
361 // Copy the arrays back into _dataPoints in the specified order
364 System.arraycopy(waypoints, 0, _dataPoints, 0, numWaypoints);
365 System.arraycopy(nonWaypoints, 0, _dataPoints, numWaypoints, numNonWaypoints);
369 System.arraycopy(nonWaypoints, 0, _dataPoints, 0, numNonWaypoints);
370 System.arraycopy(waypoints, 0, _dataPoints, numNonWaypoints, numWaypoints);
372 // needs to be scaled again
374 _broker.informSubscribers();
380 * Interleave all waypoints by each nearest track point
381 * @return true if successful, false if no change
383 public boolean interleaveWaypoints()
385 // Separate waypoints and find nearest track point
386 int numWaypoints = 0;
387 DataPoint[] waypoints = new DataPoint[_numPoints];
388 int[] pointIndices = new int[_numPoints];
389 DataPoint point = null;
391 for (i=0; i<_numPoints; i++)
393 point = _dataPoints[i];
394 if (point.isWaypoint())
396 waypoints[numWaypoints] = point;
397 pointIndices[numWaypoints] = getNearestPointIndex(
398 _xValues[i], _yValues[i], -1.0, true);
402 // Exit if data not mixed
403 if (numWaypoints == 0 || numWaypoints == _numPoints)
406 // Loop round points copying to correct order
407 DataPoint[] dataCopy = new DataPoint[_numPoints];
409 for (i=0; i<_numPoints; i++)
411 point = _dataPoints[i];
412 // if it's a track point, copy it
413 if (!point.isWaypoint())
415 dataCopy[copyIndex] = point;
418 // check for waypoints with this index
419 for (int j=0; j<numWaypoints; j++)
421 if (pointIndices[j] == i)
423 dataCopy[copyIndex] = waypoints[j];
428 // Copy data back to track
429 _dataPoints = dataCopy;
430 // needs to be scaled again to recalc x, y
432 _broker.informSubscribers();
438 * Interpolate extra points between two selected ones
439 * @param inStartIndex start index of interpolation
440 * @param inNumPoints num points to insert
441 * @return true if successful
443 public boolean interpolate(int inStartIndex, int inNumPoints)
446 if (inStartIndex < 0 || inStartIndex >= _numPoints || inNumPoints <= 0)
449 // get start and end points
450 DataPoint startPoint = getPoint(inStartIndex);
451 DataPoint endPoint = getPoint(inStartIndex + 1);
453 // Make array of points to insert
454 DataPoint[] insertedPoints = startPoint.interpolate(endPoint, inNumPoints);
456 // Insert points into track
457 insertRange(insertedPoints, inStartIndex + 1);
462 //////// information methods /////////////
466 * Get the point at the given index
467 * @param inPointNum index number, starting at 0
468 * @return DataPoint object, or null if out of range
470 public DataPoint getPoint(int inPointNum)
472 if (inPointNum > -1 && inPointNum < getNumPoints())
474 return _dataPoints[inPointNum];
481 * @return altitude range of points as AltitudeRange object
483 public AltitudeRange getAltitudeRange()
485 if (!_scaled) scalePoints();
486 return _altitudeRange;
489 * @return the number of (valid) points in the track
491 public int getNumPoints()
497 * @return The range of x values as a DoubleRange object
499 public DoubleRange getXRange()
501 if (!_scaled) scalePoints();
506 * @return The range of y values as a DoubleRange object
508 public DoubleRange getYRange()
510 if (!_scaled) scalePoints();
515 * @param inPointNum point index, starting at 0
516 * @return scaled x value of specified point
518 public double getX(int inPointNum)
520 if (!_scaled) scalePoints();
521 return _xValues[inPointNum];
525 * @param inPointNum point index, starting at 0
526 * @return scaled y value of specified point
528 public double getY(int inPointNum)
530 if (!_scaled) scalePoints();
531 return _yValues[inPointNum];
535 * @return the master field list
537 public FieldList getFieldList()
539 return _masterFieldList;
544 * Checks if any data exists for the specified field
545 * @param inField Field to examine
546 * @return true if data exists for this field
548 public boolean hasData(Field inField)
550 return hasData(inField, 0, _numPoints-1);
555 * Checks if any data exists for the specified field in the specified range
556 * @param inField Field to examine
557 * @param inStart start of range to check
558 * @param inEnd end of range to check (inclusive)
559 * @return true if data exists for this field
561 public boolean hasData(Field inField, int inStart, int inEnd)
563 for (int i=inStart; i<=inEnd; i++)
565 if (_dataPoints[i].getFieldValue(inField) != null)
575 * @return true if track contains waypoints and trackpoints
577 public boolean hasMixedData()
579 if (!_scaled) scalePoints();
584 ///////// Internal processing methods ////////////////
588 * Scale all the points in the track to gain x and y values
591 private void scalePoints()
593 // Loop through all points in track, to see limits of lat, long and altitude
594 _longRange = new DoubleRange();
595 _latRange = new DoubleRange();
596 _altitudeRange = new AltitudeRange();
598 boolean hasWaypoint = false, hasTrackpoint = false;
599 for (p=0; p < getNumPoints(); p++)
601 DataPoint point = getPoint(p);
602 if (point != null && point.isValid())
604 _longRange.addValue(point.getLongitude().getDouble());
605 _latRange.addValue(point.getLatitude().getDouble());
606 if (point.getAltitude().isValid())
607 _altitudeRange.addValue(point.getAltitude());
608 if (point.isWaypoint())
611 hasTrackpoint = true;
614 _mixedData = hasWaypoint && hasTrackpoint;
616 // Use medians to centre at 0
617 double longMedian = (_longRange.getMaximum() + _longRange.getMinimum()) / 2.0;
618 double latMedian = (_latRange.getMaximum() + _latRange.getMinimum()) / 2.0;
619 double longFactor = Math.cos(latMedian / 180.0 * Math.PI); // Function of median latitude
621 // Loop over points and calculate scales
622 _xValues = new double[getNumPoints()];
623 _yValues = new double[getNumPoints()];
624 _xRange = new DoubleRange();
625 _yRange = new DoubleRange();
626 for (p=0; p < getNumPoints(); p++)
628 DataPoint point = getPoint(p);
631 _xValues[p] = (point.getLongitude().getDouble() - longMedian) * longFactor;
632 _xRange.addValue(_xValues[p]);
633 _yValues[p] = (point.getLatitude().getDouble() - latMedian);
634 _yRange.addValue(_yValues[p]);
642 * Find the nearest point to the specified x and y coordinates
643 * or -1 if no point is within the specified max distance
644 * @param inX x coordinate
645 * @param inY y coordinate
646 * @param inMaxDist maximum distance from selected coordinates
647 * @param inJustTrackPoints true if waypoints should be ignored
648 * @return index of nearest point or -1 if not found
650 public int getNearestPointIndex(double inX, double inY, double inMaxDist, boolean inJustTrackPoints)
652 int nearestPoint = 0;
653 double nearestDist = -1.0;
655 for (int i=0; i < getNumPoints(); i++)
657 if (!inJustTrackPoints || !_dataPoints[i].isWaypoint())
659 currDist = Math.abs(_xValues[i] - inX) + Math.abs(_yValues[i] - inY);
660 if (currDist < nearestDist || nearestDist < 0.0)
663 nearestDist = currDist;
667 // Check whether it's within required distance
668 if (nearestDist > inMaxDist && inMaxDist > 0.0)
676 ////////////////// Cloning and replacing ///////////////////
679 * Clone the array of DataPoints
680 * @return shallow copy of DataPoint objects
682 public DataPoint[] cloneContents()
684 DataPoint[] clone = new DataPoint[getNumPoints()];
685 System.arraycopy(_dataPoints, 0, clone, 0, getNumPoints());
691 * Clone the specified range of data points
692 * @param inStart start index (inclusive)
693 * @param inEnd end index (inclusive)
694 * @return shallow copy of DataPoint objects
696 public DataPoint[] cloneRange(int inStart, int inEnd)
699 if (inEnd >= 0 && inEnd >= inStart)
701 numSelected = inEnd - inStart + 1;
703 DataPoint[] result = new DataPoint[numSelected>0?numSelected:0];
706 System.arraycopy(_dataPoints, inStart, result, 0, numSelected);
713 * Re-insert the specified point at the given index
714 * @param inPoint point to insert
715 * @param inIndex index at which to insert the point
716 * @return true if it worked, false otherwise
718 public boolean insertPoint(DataPoint inPoint, int inIndex)
720 if (inIndex > _numPoints || inPoint == null)
724 // Make new array to copy points over to
725 DataPoint[] newPointArray = new DataPoint[_numPoints + 1];
728 System.arraycopy(_dataPoints, 0, newPointArray, 0, inIndex);
730 newPointArray[inIndex] = inPoint;
731 if (inIndex < _numPoints)
733 System.arraycopy(_dataPoints, inIndex, newPointArray, inIndex+1, _numPoints - inIndex);
735 // Change over to new array
736 _dataPoints = newPointArray;
738 // needs to be scaled again
740 _broker.informSubscribers();
746 * Re-insert the specified point range at the given index
747 * @param inPoints point array to insert
748 * @param inIndex index at which to insert the points
749 * @return true if it worked, false otherwise
751 public boolean insertRange(DataPoint[] inPoints, int inIndex)
753 if (inIndex > _numPoints || inPoints == null)
757 // Make new array to copy points over to
758 DataPoint[] newPointArray = new DataPoint[_numPoints + inPoints.length];
761 System.arraycopy(_dataPoints, 0, newPointArray, 0, inIndex);
763 System.arraycopy(inPoints, 0, newPointArray, inIndex, inPoints.length);
764 if (inIndex < _numPoints)
766 System.arraycopy(_dataPoints, inIndex, newPointArray, inIndex+inPoints.length, _numPoints - inIndex);
768 // Change over to new array
769 _dataPoints = newPointArray;
770 _numPoints += inPoints.length;
771 // needs to be scaled again
773 _broker.informSubscribers();
779 * Replace the track contents with the given point array
780 * @param inContents array of DataPoint objects
782 public boolean replaceContents(DataPoint[] inContents)
784 // master field array stays the same
785 // (would need to store field array too if we wanted to redo a load)
786 // replace data array
787 _dataPoints = inContents;
788 _numPoints = _dataPoints.length;
790 _broker.informSubscribers();