]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/TrackExtents.java
0bf308172ceee02763bfe79f1a8f837db130bdd5
[GpsPrune.git] / tim / prune / data / TrackExtents.java
1 package tim.prune.data;
2
3 /**
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
6  */
7 public class TrackExtents
8 {
9         /** Track object */
10         private Track _track = null;
11         /** X and Y ranges */
12         private DoubleRange _xRange   = null, _yRange   = null;
13
14         /** Border multiplier */
15         private static final double BORDER_MULTIPLIER = 1.1; // 10% border
16
17         /**
18          * Constructor
19          * @param inTrack track object to take extents from
20          */
21         public TrackExtents(Track inTrack)
22         {
23                 _track  = inTrack;
24                 _xRange = inTrack.getXRange().copy();
25                 _yRange = inTrack.getYRange().copy();
26         }
27
28
29         /**
30          * Make the x and y ranges square with a default border around
31          */
32         public void applySquareBorder()
33         {
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)
40                 {
41                         // all the points are near enough on the same spot, expand scale to avoid dividing by zero
42                         xyRange = 0.1;
43                 }
44
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);
50         }
51
52         /** @return x range */
53         public DoubleRange getXRange() {
54                 return _xRange;
55         }
56
57         /** @return y range */
58         public DoubleRange getYRange() {
59                 return _yRange;
60         }
61
62         /** @return altitude range */
63         public DoubleRange getAltitudeRange()
64         {
65                 final int numPoints = _track.getNumPoints();
66                 DoubleRange altRange = new DoubleRange();
67                 for (int i=0; i<numPoints; i++)
68                 {
69                         DataPoint p = _track.getPoint(i);
70                         if (p != null && p.hasAltitude()) {
71                                 altRange.addValue(p.getAltitude().getMetricValue());
72                         }
73                 }
74                 return altRange;
75         }
76
77         /**
78          * @return the greater of the N/S and E/W extent of the track, in metres (including border)
79          */
80         public double getHorizontalDistanceMetres()
81         {
82                 DoubleRange lonRange = _track.getLonRange();
83                 DoubleRange latRange = _track.getLatRange();
84
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;
100
101                 return horizDistance;
102         }
103 }