File: TrackExtents.java

package info (click to toggle)
gpsprune 17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,984 kB
  • ctags: 5,218
  • sloc: java: 39,403; sh: 25; makefile: 17; python: 15
file content (103 lines) | stat: -rw-r--r-- 3,393 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package tim.prune.data;

/**
 * Class to hold the extents of a track, in 2d and in 3d,
 * and to calculate a square area with a default border
 */
public class TrackExtents
{
	/** Track object */
	private Track _track = null;
	/** X and Y ranges */
	private DoubleRange _xRange   = null, _yRange   = null;

	/** Border multiplier */
	private static final double BORDER_MULTIPLIER = 1.1; // 10% border

	/**
	 * Constructor
	 * @param inTrack track object to take extents from
	 */
	public TrackExtents(Track inTrack)
	{
		_track  = inTrack;
		_xRange = inTrack.getXRange().copy();
		_yRange = inTrack.getYRange().copy();
	}


	/**
	 * Make the x and y ranges square with a default border around
	 */
	public void applySquareBorder()
	{
		// Find the middle of the x and y
		final double midXvalue = _xRange.getMidValue();
		final double midYvalue = _yRange.getMidValue();
		// Find x and y range, take maximum
		double xyRange = Math.max(_xRange.getRange(), _yRange.getRange()) * BORDER_MULTIPLIER;
		if (getHorizontalDistanceMetres() < 10.0)
		{
			// all the points are near enough on the same spot, expand scale to avoid dividing by zero
			xyRange = 0.1;
		}

		// Apply these new min and max to the ranges
		_xRange.addValue(midXvalue - xyRange / 2.0);
		_xRange.addValue(midXvalue + xyRange / 2.0);
		_yRange.addValue(midYvalue - xyRange / 2.0);
		_yRange.addValue(midYvalue + xyRange / 2.0);
	}

	/** @return x range */
	public DoubleRange getXRange() {
		return _xRange;
	}

	/** @return y range */
	public DoubleRange getYRange() {
		return _yRange;
	}

	/** @return altitude range */
	public DoubleRange getAltitudeRange()
	{
		final int numPoints = _track.getNumPoints();
		DoubleRange altRange = new DoubleRange();
		for (int i=0; i<numPoints; i++)
		{
			DataPoint p = _track.getPoint(i);
			if (p != null && p.hasAltitude()) {
				altRange.addValue(p.getAltitude().getMetricValue());
			}
		}
		return altRange;
	}

	/**
	 * @return the greater of the N/S and E/W extent of the track, in metres (including border)
	 */
	public double getHorizontalDistanceMetres()
	{
		DoubleRange lonRange = _track.getLonRange();
		DoubleRange latRange = _track.getLatRange();

		// Find horizontal and vertical extents of enclosing rectangle
		DataPoint southPoint = new DataPoint(new Latitude(latRange.getMinimum(), Coordinate.FORMAT_DEG),
			new Longitude(lonRange.getMidValue(), Coordinate.FORMAT_DEG), null);
		DataPoint northPoint = new DataPoint(new Latitude(latRange.getMaximum(), Coordinate.FORMAT_DEG),
			new Longitude(lonRange.getMidValue(), Coordinate.FORMAT_DEG), null);
		double nsDist = Distance.convertRadiansToDistance(
			DataPoint.calculateRadiansBetween(northPoint, southPoint), UnitSetLibrary.UNITS_METRES); // both in m
		// Same again for bottom and top, take maximum
		DataPoint westPoint = new DataPoint(new Latitude(latRange.getMidValue(), Coordinate.FORMAT_DEG),
			new Longitude(lonRange.getMinimum(), Coordinate.FORMAT_DEG), null);
		DataPoint eastPoint = new DataPoint(new Latitude(latRange.getMidValue(), Coordinate.FORMAT_DEG),
			new Longitude(lonRange.getMinimum(), Coordinate.FORMAT_DEG), null);
		double ewDist = Distance.convertRadiansToDistance(
			DataPoint.calculateRadiansBetween(westPoint, eastPoint), UnitSetLibrary.UNITS_METRES); // both in m
		final double horizDistance = Math.max(nsDist, ewDist) * BORDER_MULTIPLIER;

		return horizDistance;
	}
}