X-Git-Url: https://gitweb.fperrin.net/?a=blobdiff_plain;f=tim%2Fprune%2Fdata%2FTrack.java;h=8623c587ba5c9131d9051ad083cdcafffd24e54c;hb=7f5ed2be62905bd37717376dc22d09e5ea7edb4d;hp=63cd01faaf488028085a6712c5648ad9ffa8ea1d;hpb=f1b92378a792131ac8fb33a869405851d5b2d1f7;p=GpsPrune.git diff --git a/tim/prune/data/Track.java b/tim/prune/data/Track.java index 63cd01f..8623c58 100644 --- a/tim/prune/data/Track.java +++ b/tim/prune/data/Track.java @@ -3,7 +3,6 @@ package tim.prune.data; import java.util.List; import tim.prune.UpdateMessageBroker; -import tim.prune.config.Config; import tim.prune.function.edit.FieldEdit; import tim.prune.function.edit.FieldEditList; import tim.prune.gui.map.MapUtils; @@ -27,7 +26,6 @@ public class Track // Master field list private FieldList _masterFieldList = null; // variable ranges - private AltitudeRange _altitudeRange = null; private DoubleRange _latRange = null, _longRange = null; private DoubleRange _xRange = null, _yRange = null; @@ -64,9 +62,9 @@ public class Track * Load method, for initialising and reinitialising data * @param inFieldArray array of Field objects describing fields * @param inPointArray 2d object array containing data - * @param inAltFormat altitude format + * @param inOptions load options such as units */ - public void load(Field[] inFieldArray, Object[][] inPointArray, Altitude.Format inAltFormat) + public void load(Field[] inFieldArray, Object[][] inPointArray, PointCreateOptions inOptions) { if (inFieldArray == null || inPointArray == null) { @@ -83,7 +81,7 @@ public class Track { dataArray = (String[]) inPointArray[p]; // Convert to DataPoint objects - DataPoint point = new DataPoint(dataArray, _masterFieldList, inAltFormat); + DataPoint point = new DataPoint(dataArray, _masterFieldList, inOptions); if (point.isValid()) { _dataPoints[pointIndex] = point; @@ -335,12 +333,12 @@ public class Track * @param inStart start of range * @param inEnd end of range * @param inOffset offset to add (-ve to subtract) - * @param inFormat altitude format of offset + * @param inUnit altitude unit of offset * @param inDecimals number of decimal places in offset * @return true on success */ public boolean addAltitudeOffset(int inStart, int inEnd, double inOffset, - Altitude.Format inFormat, int inDecimals) + Unit inUnit, int inDecimals) { // sanity check if (inStart < 0 || inEnd < 0 || inStart >= inEnd || inEnd >= _numPoints) { @@ -355,7 +353,7 @@ public class Track { // This point has an altitude so add the offset to it foundAlt = true; - alt.addOffset(inOffset, inFormat, inDecimals); + alt.addOffset(inOffset, inUnit, inDecimals); _dataPoints[i].setModified(false); } } @@ -580,15 +578,19 @@ public class Track double latitudeDiff = 0.0, longitudeDiff = 0.0; double totalAltitude = 0; int numAltitudes = 0; - Altitude.Format altFormat = Config.getConfigBoolean(Config.KEY_METRIC_UNITS)?Altitude.Format.METRES:Altitude.Format.FEET; + Unit altUnit = null; // loop between start and end points for (int i=inStartIndex; i<= inEndIndex; i++) { DataPoint currPoint = getPoint(i); latitudeDiff += (currPoint.getLatitude().getDouble() - firstLatitude); longitudeDiff += (currPoint.getLongitude().getDouble() - firstLongitude); - if (currPoint.hasAltitude()) { - totalAltitude += currPoint.getAltitude().getValue(altFormat); + if (currPoint.hasAltitude()) + { + totalAltitude += currPoint.getAltitude().getValue(altUnit); + // Use altitude format of first valid altitude + if (altUnit == null) + altUnit = currPoint.getAltitude().getUnit(); numAltitudes++; } } @@ -596,7 +598,9 @@ public class Track double meanLatitude = firstLatitude + (latitudeDiff / numPoints); double meanLongitude = firstLongitude + (longitudeDiff / numPoints); Altitude meanAltitude = null; - if (numAltitudes > 0) {meanAltitude = new Altitude((int) (totalAltitude / numAltitudes), altFormat);} + if (numAltitudes > 0) { + meanAltitude = new Altitude((int) (totalAltitude / numAltitudes), altUnit); + } DataPoint insertedPoint = new DataPoint(new Latitude(meanLatitude, Coordinate.FORMAT_NONE), new Longitude(meanLongitude, Coordinate.FORMAT_NONE), meanAltitude); @@ -643,16 +647,6 @@ public class Track return null; } - - /** - * @return altitude range of points as AltitudeRange object - */ - public AltitudeRange getAltitudeRange() - { - if (!_scaled) scalePoints(); - return _altitudeRange; - } - /** * @return the number of (valid) points in the track */ @@ -666,7 +660,7 @@ public class Track */ public DoubleRange getXRange() { - if (!_scaled) scalePoints(); + if (!_scaled) {scalePoints();} return _xRange; } @@ -675,7 +669,7 @@ public class Track */ public DoubleRange getYRange() { - if (!_scaled) scalePoints(); + if (!_scaled) {scalePoints();} return _yRange; } @@ -684,7 +678,7 @@ public class Track */ public DoubleRange getLatRange() { - if (!_scaled) scalePoints(); + if (!_scaled) {scalePoints();} return _latRange; } /** @@ -692,7 +686,7 @@ public class Track */ public DoubleRange getLonRange() { - if (!_scaled) scalePoints(); + if (!_scaled) {scalePoints();} return _longRange; } @@ -702,7 +696,7 @@ public class Track */ public double getX(int inPointNum) { - if (!_scaled) scalePoints(); + if (!_scaled) {scalePoints();} return _xValues[inPointNum]; } @@ -712,7 +706,7 @@ public class Track */ public double getY(int inPointNum) { - if (!_scaled) scalePoints(); + if (!_scaled) {scalePoints();} return _yValues[inPointNum]; } @@ -779,7 +773,7 @@ public class Track */ public boolean hasTrackPoints() { - if (!_scaled) scalePoints(); + if (!_scaled) {scalePoints();} return _hasTrackpoint; } @@ -788,7 +782,7 @@ public class Track */ public boolean hasWaypoints() { - if (!_scaled) scalePoints(); + if (!_scaled) {scalePoints();} return _hasWaypoint; } @@ -871,12 +865,11 @@ public class Track * Scale all the points in the track to gain x and y values * ready for plotting */ - private void scalePoints() + private synchronized void scalePoints() { - // Loop through all points in track, to see limits of lat, long and altitude + // Loop through all points in track, to see limits of lat, long _longRange = new DoubleRange(); _latRange = new DoubleRange(); - _altitudeRange = new AltitudeRange(); int p; _hasWaypoint = false; _hasTrackpoint = false; for (p=0; p < getNumPoints(); p++) @@ -886,10 +879,6 @@ 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 @@ -930,16 +919,21 @@ public class Track { int nearestPoint = 0; double nearestDist = -1.0; - double currDist; + double mDist, yDist; for (int i=0; i < getNumPoints(); i++) { if (!inJustTrackPoints || !_dataPoints[i].isWaypoint()) { - currDist = Math.abs(_xValues[i] - inX) + Math.abs(_yValues[i] - inY); - if (currDist < nearestDist || nearestDist < 0.0) + yDist = Math.abs(_yValues[i] - inY); + if (yDist < nearestDist || nearestDist < 0.0) { - nearestPoint = i; - nearestDist = currDist; + // y dist is within range, so check x too + mDist = yDist + getMinXDist(_xValues[i] - inX); + if (mDist < nearestDist || nearestDist < 0.0) + { + nearestPoint = i; + nearestDist = mDist; + } } } } @@ -951,6 +945,16 @@ public class Track return nearestPoint; } + /** + * @param inX x value of point + * @return minimum wrapped value + */ + private static final double getMinXDist(double inX) + { + // TODO: Can use some kind of floor here? + return Math.min(Math.min(Math.abs(inX), Math.abs(inX-1.0)), Math.abs(inX+1.0)); + } + /** * Get the next track point starting from the given index * @param inStartIndex index to start looking from