package tim.prune.data;
+import java.util.List;
+
import tim.prune.UpdateMessageBroker;
+import tim.prune.edit.FieldEdit;
+import tim.prune.edit.FieldEditList;
/**
*/
public class Track
{
- // Broker object
- UpdateMessageBroker _broker = null;
// Data points
private DataPoint[] _dataPoints = null;
// Scaled x, y values
/**
- * 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
*/
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
}
}
_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;
}
/**
* Combine this Track with new data
- * @param inOtherTrack
+ * @param inOtherTrack other track to combine
*/
public void combine(Track inOtherTrack)
{
// needs to be scaled again
_scaled = false;
// inform listeners
- _broker.informSubscribers();
+ UpdateMessageBroker.informSubscribers();
}
_numPoints = inNewSize;
// needs to be scaled again
_scaled = false;
- _broker.informSubscribers();
+ UpdateMessageBroker.informSubscribers();
}
}
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++)
}
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;}
}
}
System.arraycopy(newPointArray, 0, _dataPoints, 0, numCopied);
_numPoints = _dataPoints.length;
_scaled = false;
- _broker.informSubscribers();
}
return numDeleted;
}
_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)
// 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];
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
_dataPoints = newPointArray;
_numPoints = _dataPoints.length;
_scaled = false;
- _broker.informSubscribers();
}
return numDupes;
}
/**
* 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)
_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
}
// needs to be scaled again
_scaled = false;
- _broker.informSubscribers();
+ UpdateMessageBroker.informSubscribers();
return true;
}
_dataPoints = dataCopy;
// needs to be scaled again to recalc x, y
_scaled = false;
- _broker.informSubscribers();
+ UpdateMessageBroker.informSubscribers();
return true;
}
// 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();
}
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
}
+ /**
+ * 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 ////////////////
_longRange.addValue(point.getLongitude().getDouble());
_latRange.addValue(point.getLatitude().getDouble());
if (point.getAltitude().isValid())
+ {
_altitudeRange.addValue(point.getAltitude());
+ }
if (point.isWaypoint())
hasWaypoint = true;
else
}
+ /**
+ * 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 ///////////////////
/**
_numPoints++;
// needs to be scaled again
_scaled = false;
- _broker.informSubscribers();
+ UpdateMessageBroker.informSubscribers();
return true;
}
_numPoints += inPoints.length;
// needs to be scaled again
_scaled = false;
- _broker.informSubscribers();
+ UpdateMessageBroker.informSubscribers();
return true;
}
/**
* Replace the track contents with the given point array
* @param inContents array of DataPoint objects
+ * @return true on success
*/
public boolean replaceContents(DataPoint[] inContents)
{
_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;
+ }
}