1 package tim.prune.data;
4 * Class to hold the extents of a track, in 2d and in 3d,
5 * and to calculate a square area with a default border
7 public class TrackExtents
10 private Track _track = null;
12 private DoubleRange _xRange = null, _yRange = null;
14 /** Border multiplier */
15 private static final double BORDER_MULTIPLIER = 1.1; // 10% border
19 * @param inTrack track object to take extents from
21 public TrackExtents(Track inTrack)
24 _xRange = inTrack.getXRange().copy();
25 _yRange = inTrack.getYRange().copy();
30 * Make the x and y ranges square with a default border around
32 public void applySquareBorder()
34 // Find the middle of the x and y
35 final double midXvalue = _xRange.getMidValue();
36 final double midYvalue = _yRange.getMidValue();
37 // Find x and y range, take maximum
38 double xyRange = Math.max(_xRange.getRange(), _yRange.getRange()) * BORDER_MULTIPLIER;
39 if (getHorizontalDistanceMetres() < 10.0)
41 // all the points are near enough on the same spot, expand scale to avoid dividing by zero
45 // Apply these new min and max to the ranges
46 _xRange.addValue(midXvalue - xyRange / 2.0);
47 _xRange.addValue(midXvalue + xyRange / 2.0);
48 _yRange.addValue(midYvalue - xyRange / 2.0);
49 _yRange.addValue(midYvalue + xyRange / 2.0);
52 /** @return x range */
53 public DoubleRange getXRange() {
57 /** @return y range */
58 public DoubleRange getYRange() {
62 /** @return altitude range */
63 public DoubleRange getAltitudeRange()
65 final int numPoints = _track.getNumPoints();
66 DoubleRange altRange = new DoubleRange();
67 for (int i=0; i<numPoints; i++)
69 DataPoint p = _track.getPoint(i);
70 if (p != null && p.hasAltitude()) {
71 altRange.addValue(p.getAltitude().getMetricValue());
78 * @return the greater of the N/S and E/W extent of the track, in metres (including border)
80 public double getHorizontalDistanceMetres()
82 DoubleRange lonRange = _track.getLonRange();
83 DoubleRange latRange = _track.getLatRange();
85 // Find horizontal and vertical extents of enclosing rectangle
86 DataPoint southPoint = new DataPoint(new Latitude(latRange.getMinimum(), Coordinate.FORMAT_DEG),
87 new Longitude(lonRange.getMidValue(), Coordinate.FORMAT_DEG), null);
88 DataPoint northPoint = new DataPoint(new Latitude(latRange.getMaximum(), Coordinate.FORMAT_DEG),
89 new Longitude(lonRange.getMidValue(), Coordinate.FORMAT_DEG), null);
90 double nsDist = Distance.convertRadiansToDistance(
91 DataPoint.calculateRadiansBetween(northPoint, southPoint), UnitSetLibrary.UNITS_METRES); // both in m
92 // Same again for bottom and top, take maximum
93 DataPoint westPoint = new DataPoint(new Latitude(latRange.getMidValue(), Coordinate.FORMAT_DEG),
94 new Longitude(lonRange.getMinimum(), Coordinate.FORMAT_DEG), null);
95 DataPoint eastPoint = new DataPoint(new Latitude(latRange.getMidValue(), Coordinate.FORMAT_DEG),
96 new Longitude(lonRange.getMinimum(), Coordinate.FORMAT_DEG), null);
97 double ewDist = Distance.convertRadiansToDistance(
98 DataPoint.calculateRadiansBetween(westPoint, eastPoint), UnitSetLibrary.UNITS_METRES); // both in m
99 final double horizDistance = Math.max(nsDist, ewDist) * BORDER_MULTIPLIER;
101 return horizDistance;