]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/data/Track.java
Version 5, May 2008
[GpsPrune.git] / tim / prune / data / Track.java
index 683cd21fcf6b3f07da472b64fc151970372ef51d..9c65437b60abc86df5bd803c3f045d946e89a2f5 100644 (file)
@@ -1,6 +1,10 @@
 package tim.prune.data;
 
+import java.util.List;
+
 import tim.prune.UpdateMessageBroker;
+import tim.prune.edit.FieldEdit;
+import tim.prune.edit.FieldEditList;
 
 
 /**
@@ -9,8 +13,6 @@ import tim.prune.UpdateMessageBroker;
  */
 public class Track
 {
-       // Broker object
-       UpdateMessageBroker _broker = null;
        // Data points
        private DataPoint[] _dataPoints = null;
        // Scaled x, y values
@@ -28,13 +30,10 @@ public class Track
 
 
        /**
-        * Constructor giving arrays of Fields and Objects
-        * @param inFieldArray field array
-        * @param inPointArray 2d array of field values
+        * Constructor for empty track
         */
-       public Track(UpdateMessageBroker inBroker)
+       public Track()
        {
-               _broker = inBroker;
                // create field list
                _masterFieldList = new FieldList(null);
                // make empty DataPoint array
@@ -53,6 +52,11 @@ public class Track
         */
        public void load(Field[] inFieldArray, Object[][] inPointArray, int inAltFormat)
        {
+               if (inFieldArray == null || inPointArray == null)
+               {
+                       _numPoints = 0;
+                       return;
+               }
                // copy field list
                _masterFieldList = new FieldList(inFieldArray);
                // make DataPoint object from each point in inPointList
@@ -71,6 +75,11 @@ public class Track
                        }
                }
                _numPoints = pointIndex;
+               // Set first track point to be start of segment
+               DataPoint firstTrackPoint = getNextTrackPoint(0);
+               if (firstTrackPoint != null) {
+                       firstTrackPoint.setSegmentStart(true);
+               }
                // needs to be scaled
                _scaled = false;
        }
@@ -81,7 +90,7 @@ public class Track
 
        /**
         * Combine this Track with new data
-        * @param inOtherTrack
+        * @param inOtherTrack other track to combine
         */
        public void combine(Track inOtherTrack)
        {
@@ -98,7 +107,7 @@ public class Track
                // needs to be scaled again
                _scaled = false;
                // inform listeners
-               _broker.informSubscribers();
+               UpdateMessageBroker.informSubscribers();
        }
 
 
@@ -113,7 +122,7 @@ public class Track
                        _numPoints = inNewSize;
                        // needs to be scaled again
                        _scaled = false;
-                       _broker.informSubscribers();
+                       UpdateMessageBroker.informSubscribers();
                }
        }
 
@@ -138,13 +147,17 @@ public class Track
                if (yscale > wholeScale) wholeScale = yscale;
                double minDist = wholeScale / inResolution;
 
+               // Keep track of segment start flags of the deleted points
+               boolean setSegment = false;
                // Copy selected points
                DataPoint[] newPointArray = new DataPoint[_numPoints];
                int[] pointIndices = new int[_numPoints];
                for (int i=0; i<_numPoints; i++)
                {
+                       DataPoint point = _dataPoints[i];
                        boolean keepPoint = true;
-                       if (!_dataPoints[i].isWaypoint())
+                       // Don't delete waypoints or photo points
+                       if (!point.isWaypoint() && point.getPhoto() == null)
                        {
                                // go through newPointArray to check for range
                                for (int j=0; j<numCopied && keepPoint; j++)
@@ -158,9 +171,20 @@ public class Track
                        }
                        if (keepPoint)
                        {
-                               newPointArray[numCopied] = _dataPoints[i];
+                               newPointArray[numCopied] = point;
                                pointIndices[numCopied] = i;
                                numCopied++;
+                               // set segment flag if it's the first track point
+                               if (setSegment && !point.isWaypoint())
+                               {
+                                       point.setSegmentStart(true);
+                                       setSegment = false;
+                               }
+                       }
+                       else
+                       {
+                               // point will be removed, so check segment flag
+                               if (point.getSegmentStart()) {setSegment = true;}
                        }
                }
 
@@ -172,7 +196,6 @@ public class Track
                        System.arraycopy(newPointArray, 0, _dataPoints, 0, numCopied);
                        _numPoints = _dataPoints.length;
                        _scaled = false;
-                       _broker.informSubscribers();
                }
                return numDeleted;
        }
@@ -195,13 +218,14 @@ public class Track
                _dataPoints = newPointArray;
                _numPoints = _dataPoints.length;
                _scaled = false;
-               _broker.informSubscribers();
+               UpdateMessageBroker.informSubscribers();
                return numDeleted;
        }
 
 
        /**
         * Delete the specified point
+        * @param inIndex point index
         * @return true if successful
         */
        public boolean deletePoint(int inIndex)
@@ -224,6 +248,16 @@ public class Track
                        // no valid range selected so can't delete
                        return false;
                }
+               // check through range to be deleted, and see if any new segment flags present
+               boolean hasSegmentStart = false;
+               DataPoint nextTrackPoint = getNextTrackPoint(inEnd+1);
+               if (nextTrackPoint != null) {
+                       for (int i=inStart; i<=inEnd && !hasSegmentStart; i++) {
+                               hasSegmentStart |= _dataPoints[i].getSegmentStart();
+                       }
+                       // If segment break found, make sure next trackpoint also has break
+                       if (hasSegmentStart) {nextTrackPoint.setSegmentStart(true);}
+               }
                // valid range, let's delete it
                int numToDelete = inEnd - inStart + 1;
                DataPoint[] newPointArray = new DataPoint[_numPoints - numToDelete];
@@ -238,7 +272,7 @@ public class Track
                        System.arraycopy(_dataPoints, inEnd + 1, newPointArray, inStart,
                                _numPoints - inEnd - 1);
                }
-               // Copy points over original array (careful!)
+               // Copy points over original array
                _dataPoints = newPointArray;
                _numPoints -= numToDelete;
                // needs to be scaled again
@@ -288,7 +322,6 @@ public class Track
                        _dataPoints = newPointArray;
                        _numPoints = _dataPoints.length;
                        _scaled = false;
-                       _broker.informSubscribers();
                }
                return numDupes;
        }
@@ -296,6 +329,8 @@ public class Track
 
        /**
         * Reverse the specified range of points
+        * @param inStart start index
+        * @param inEnd end index
         * @return true if successful, false otherwise
         */
        public boolean reverseRange(int inStart, int inEnd)
@@ -314,13 +349,45 @@ public class Track
                        _dataPoints[inStart + i] = _dataPoints[inEnd - i];
                        _dataPoints[inEnd - i] = p;
                }
+               // adjust segment starts
+               shiftSegmentStarts(inStart, inEnd);
+               // Find first track point and following track point, and set segment starts to true
+               DataPoint firstTrackPoint = getNextTrackPoint(inStart);
+               if (firstTrackPoint != null) {firstTrackPoint.setSegmentStart(true);}
+               DataPoint nextTrackPoint = getNextTrackPoint(inEnd+1);
+               if (nextTrackPoint != null) {nextTrackPoint.setSegmentStart(true);}
                // needs to be scaled again
                _scaled = false;
-               _broker.informSubscribers();
+               UpdateMessageBroker.informSubscribers();
                return true;
        }
 
 
+       /**
+        * Merge the track segments within the given range
+        * @param inStart start index
+        * @param inEnd end index
+        * @return true if successful
+        */
+       public boolean mergeTrackSegments(int inStart, int inEnd)
+       {
+               boolean firstTrackPoint = true;
+               // Loop between start and end
+               for (int i=inStart; i<=inEnd; i++) {
+                       DataPoint point = getPoint(i);
+                       // Set all segments to false apart from first track point
+                       if (point != null && !point.isWaypoint()) {
+                               point.setSegmentStart(firstTrackPoint);
+                               firstTrackPoint = false;
+                       }
+               }
+               // Find following track point, if any
+               DataPoint nextPoint = getNextTrackPoint(inEnd+1);
+               if (nextPoint != null) {nextPoint.setSegmentStart(true);}
+               UpdateMessageBroker.informSubscribers();
+               return true;
+       }
+
        /**
         * Collect all waypoints to the start or end of the track
         * @param inAtStart true to collect at start, false for end
@@ -371,7 +438,7 @@ public class Track
                }
                // needs to be scaled again
                _scaled = false;
-               _broker.informSubscribers();
+               UpdateMessageBroker.informSubscribers();
                return true;
        }
 
@@ -429,7 +496,7 @@ public class Track
                _dataPoints = dataCopy;
                // needs to be scaled again to recalc x, y
                _scaled = false;
-               _broker.informSubscribers();
+               UpdateMessageBroker.informSubscribers();
                return true;
        }
 
@@ -452,10 +519,26 @@ public class Track
 
                // Make array of points to insert
                DataPoint[] insertedPoints = startPoint.interpolate(endPoint, inNumPoints);
-               
+
                // Insert points into track
-               insertRange(insertedPoints, inStartIndex + 1);
-               return true;
+               return insertRange(insertedPoints, inStartIndex + 1);
+       }
+
+
+       /**
+        * Append the specified points to the end of the track
+        * @param inPoints DataPoint objects to add
+        */
+       public void appendPoints(DataPoint[] inPoints)
+       {
+               // Insert points into track
+               if (inPoints != null && inPoints.length > 0)
+               {
+                       insertRange(inPoints, _numPoints);
+               }
+               // needs to be scaled again to recalc x, y
+               _scaled = false;
+               UpdateMessageBroker.informSubscribers();
        }
 
 
@@ -511,6 +594,23 @@ public class Track
                return _yRange;
        }
 
+       /**
+        * @return The range of lat values as a DoubleRange object
+        */
+       public DoubleRange getLatRange()
+       {
+               if (!_scaled) scalePoints();
+               return _latRange;
+       }
+       /**
+        * @return The range of lon values as a DoubleRange object
+        */
+       public DoubleRange getLonRange()
+       {
+               if (!_scaled) scalePoints();
+               return _longRange;
+       }
+
        /**
         * @param inPointNum point index, starting at 0
         * @return scaled x value of specified point
@@ -581,6 +681,48 @@ public class Track
        }
 
 
+       /**
+        * Collect all the waypoints into the given List
+        * @param inList List to fill with waypoints
+        */
+       public void getWaypoints(List inList)
+       {
+               // clear list
+               inList.clear();
+               // loop over points and copy all waypoints into list
+               for (int i=0; i<=_numPoints-1; i++)
+               {
+                       if (_dataPoints[i] != null && _dataPoints[i].isWaypoint())
+                       {
+                               inList.add(_dataPoints[i]);
+                       }
+               }
+       }
+
+
+       /**
+        * Search for the given Point in the track and return the index
+        * @param inPoint Point to look for
+        * @return index of Point, if any or -1 if not found
+        */
+       public int getPointIndex(DataPoint inPoint)
+       {
+               if (inPoint != null)
+               {
+                       // Loop over points in track
+                       for (int i=0; i<=_numPoints-1; i++)
+                       {
+                               if (_dataPoints[i] == inPoint)
+                               {
+                                       return i;
+                               }
+                       }
+               }
+               // not found
+               return -1;
+       }
+
+
        ///////// Internal processing methods ////////////////
 
 
@@ -604,7 +746,9 @@ public class Track
                                _longRange.addValue(point.getLongitude().getDouble());
                                _latRange.addValue(point.getLatitude().getDouble());
                                if (point.getAltitude().isValid())
+                               {
                                        _altitudeRange.addValue(point.getAltitude());
+                               }
                                if (point.isWaypoint())
                                        hasWaypoint = true;
                                else
@@ -673,6 +817,71 @@ public class Track
        }
 
 
+       /**
+        * Get the next track point starting from the given index
+        * @param inStartIndex index to start looking from
+        * @return next track point, or null if end of data reached
+        */
+       public DataPoint getNextTrackPoint(int inStartIndex)
+       {
+               return getNextTrackPoint(inStartIndex, 1);
+       }
+
+       /**
+        * Get the previous track point starting from the given index
+        * @param inStartIndex index to start looking from
+        * @return next track point, or null if end of data reached
+        */
+       public DataPoint getPreviousTrackPoint(int inStartIndex)
+       {
+               return getNextTrackPoint(inStartIndex, -1);
+       }
+
+       /**
+        * Get the next track point starting from the given index
+        * @param inStartIndex index to start looking from
+        * @param inIncrement increment to add to point index, +1 for next, -1 for previous
+        * @return next track point, or null if end of data reached
+        */
+       private DataPoint getNextTrackPoint(int inStartIndex, int inIncrement)
+       {
+               // Loop forever over points
+               for (int i=inStartIndex; ; i+=inIncrement)
+               {
+                       DataPoint point = getPoint(i);
+                       // Exit if end of data reached - there wasn't a track point
+                       if (point == null) {return null;}
+                       if (point.isValid() && !point.isWaypoint()) {
+                               // next track point found
+                               return point;
+                       }
+               }
+       }
+
+       /**
+        * Shift all the segment start flags in the given range by 1
+        * Method used by reverse range and its undo
+        * @param inStartIndex start of range, inclusive
+        * @param inEndIndex end of range, inclusive
+        */
+       public void shiftSegmentStarts(int inStartIndex, int inEndIndex)
+       {
+               boolean prevFlag = true;
+               boolean currFlag = true;
+               for (int i=inStartIndex; i<= inEndIndex; i++)
+               {
+                       DataPoint point = getPoint(i);
+                       if (point != null && !point.isWaypoint())
+                       {
+                               // remember flag
+                               currFlag = point.getSegmentStart();
+                               // shift flag by 1
+                               point.setSegmentStart(prevFlag);
+                               prevFlag = currFlag;
+                       }
+               }
+       }
+
        ////////////////// Cloning and replacing ///////////////////
 
        /**
@@ -737,7 +946,7 @@ public class Track
                _numPoints++;
                // needs to be scaled again
                _scaled = false;
-               _broker.informSubscribers();
+               UpdateMessageBroker.informSubscribers();
                return true;
        }
 
@@ -770,7 +979,7 @@ public class Track
                _numPoints += inPoints.length;
                // needs to be scaled again
                _scaled = false;
-               _broker.informSubscribers();
+               UpdateMessageBroker.informSubscribers();
                return true;
        }
 
@@ -778,6 +987,7 @@ public class Track
        /**
         * Replace the track contents with the given point array
         * @param inContents array of DataPoint objects
+        * @return true on success
         */
        public boolean replaceContents(DataPoint[] inContents)
        {
@@ -787,7 +997,44 @@ public class Track
                _dataPoints = inContents;
                _numPoints = _dataPoints.length;
                _scaled = false;
-               _broker.informSubscribers();
+               UpdateMessageBroker.informSubscribers();
                return true;
        }
+
+
+       /**
+        * Edit the specified point
+        * @param inPoint point to edit
+        * @param inEditList list of edits to make
+        * @return true if successful
+        */
+       public boolean editPoint(DataPoint inPoint, FieldEditList inEditList)
+       {
+               if (inPoint != null && inEditList != null && inEditList.getNumEdits() > 0)
+               {
+                       // remember if coordinates have changed
+                       boolean coordsChanged = false;
+                       // go through edits one by one
+                       int numEdits = inEditList.getNumEdits();
+                       for (int i=0; i<numEdits; i++)
+                       {
+                               FieldEdit edit = inEditList.getEdit(i);
+                               inPoint.setFieldValue(edit.getField(), edit.getValue());
+                               // check coordinates
+                               coordsChanged |= (edit.getField().equals(Field.LATITUDE)
+                                       || edit.getField().equals(Field.LONGITUDE) || edit.getField().equals(Field.ALTITUDE));
+                       }
+                       // set photo status if coordinates have changed
+                       if (inPoint.getPhoto() != null && coordsChanged)
+                       {
+                               inPoint.getPhoto().setCurrentStatus(PhotoStatus.CONNECTED);
+                       }
+                       // point possibly needs to be scaled again
+                       _scaled = false;
+                       // trigger listeners
+                       UpdateMessageBroker.informSubscribers();
+                       return true;
+               }
+               return false;
+       }
 }