1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.plugins.columbusCSV;
|
---|
3 |
|
---|
4 | import static java.lang.Math.asin;
|
---|
5 | import static java.lang.Math.cos;
|
---|
6 | import static java.lang.Math.sin;
|
---|
7 | import static java.lang.Math.sqrt;
|
---|
8 | import static java.lang.Math.toDegrees;
|
---|
9 | import static java.lang.Math.toRadians;
|
---|
10 |
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
15 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
16 | import org.openstreetmap.josm.tools.Logging;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * @author Oliver Wieland <oliver.wieland@online.de> Provides several static methods to access way point
|
---|
20 | * attributes.
|
---|
21 | */
|
---|
22 | public class WayPointHelper {
|
---|
23 | /**
|
---|
24 | * The name of the elevation height of a way point.
|
---|
25 | */
|
---|
26 | public static final String HEIGHT_ATTRIBUTE = "ele";
|
---|
27 |
|
---|
28 | private static final double R = 6378135;
|
---|
29 |
|
---|
30 | private WayPointHelper() {
|
---|
31 | // Private constructor for the utility class.
|
---|
32 | }
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Gets the elevation (Z coordinate) of a JOSM way point.
|
---|
36 | *
|
---|
37 | * @param wpt
|
---|
38 | * The way point instance.
|
---|
39 | * @return The x coordinate or 0, if the given way point is null or contains
|
---|
40 | * not height attribute.
|
---|
41 | */
|
---|
42 | public static double getElevation(WayPoint wpt) {
|
---|
43 | if (wpt != null) {
|
---|
44 | if (!wpt.attr.containsKey(HEIGHT_ATTRIBUTE)) {
|
---|
45 | return 0;
|
---|
46 | }
|
---|
47 |
|
---|
48 | String height = wpt.getString(WayPointHelper.HEIGHT_ATTRIBUTE);
|
---|
49 | try {
|
---|
50 | return Double.parseDouble(height);
|
---|
51 | } catch (NumberFormatException e) {
|
---|
52 | Logging.error(String.format(
|
---|
53 | "Cannot parse double from '%s': %s", height, e
|
---|
54 | .getMessage()));
|
---|
55 | }
|
---|
56 | }
|
---|
57 | return 0;
|
---|
58 | }
|
---|
59 |
|
---|
60 | public static double getLonDist(WayPoint w1, WayPoint w2) {
|
---|
61 | LatLon ll = new LatLon(w1.getCoor().lat(), w2.getCoor().lon());
|
---|
62 | return w1.getCoor().greatCircleDistance(ll);
|
---|
63 | }
|
---|
64 |
|
---|
65 | public static double getLatDist(WayPoint w1, WayPoint w2) {
|
---|
66 | LatLon ll = new LatLon(w2.getCoor().lat(), w1.getCoor().lon());
|
---|
67 | return w1.getCoor().greatCircleDistance(ll);
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Moves a given lat/lon coordinate by a given amount of meters in
|
---|
72 | * x and y direction.
|
---|
73 | * @param src The original lat/lon coordinate.
|
---|
74 | * @param dlat The distance in latitude direction in meters
|
---|
75 | * @param dlon The distance in longitude direction in meters
|
---|
76 | * @return
|
---|
77 | */
|
---|
78 | public static LatLon moveLatLon(LatLon src, double dlat, double dlon) {
|
---|
79 | double lat1 = toRadians(src.lat());
|
---|
80 | double lon1 = toRadians(src.lon());
|
---|
81 |
|
---|
82 | double dlonsin2 = sin(dlon/2 / R);
|
---|
83 | double dlatsin2 = sin(dlat/2 / R);
|
---|
84 | double dlatcos = cos(lon1);
|
---|
85 |
|
---|
86 | double lon2rad = 2 * asin(sqrt(dlonsin2 * dlonsin2 / dlatcos/dlatcos)) + lon1;
|
---|
87 | double lat2rad = 2 * asin(dlatsin2) + lat1;
|
---|
88 |
|
---|
89 | double lon2 = toDegrees(lon2rad);
|
---|
90 | double lat2 = toDegrees(lat2rad);
|
---|
91 |
|
---|
92 | return new LatLon(lat2, lon2);
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Reduces a given list of way points to the specified target size.
|
---|
97 | *
|
---|
98 | * @param origList
|
---|
99 | * The original list containing the way points.
|
---|
100 | * @param targetSize
|
---|
101 | * The desired target size of the list. The resulting list may
|
---|
102 | * contain fewer items, so targetSize should be considered as
|
---|
103 | * maximum.
|
---|
104 | * @return A list containing the reduced list.
|
---|
105 | */
|
---|
106 | public static List<WayPoint> downsampleWayPoints(List<WayPoint> origList,
|
---|
107 | int targetSize) {
|
---|
108 | if (origList == null)
|
---|
109 | return null;
|
---|
110 | if (targetSize <= 0)
|
---|
111 | throw new IllegalArgumentException(
|
---|
112 | "targetSize must be greater than zero");
|
---|
113 |
|
---|
114 | int origSize = origList.size();
|
---|
115 | if (origSize <= targetSize) {
|
---|
116 | return origList;
|
---|
117 | }
|
---|
118 |
|
---|
119 | int delta = (int) Math.max(Math.ceil(origSize / targetSize), 2);
|
---|
120 |
|
---|
121 | List<WayPoint> res = new ArrayList<>(targetSize);
|
---|
122 | for (int i = 0; i < origSize; i += delta) {
|
---|
123 | res.add(origList.get(i));
|
---|
124 | }
|
---|
125 |
|
---|
126 | return res;
|
---|
127 | }
|
---|
128 | }
|
---|