1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.data.coor;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.trc;
|
---|
5 |
|
---|
6 | import static java.lang.Math.PI;
|
---|
7 | import static java.lang.Math.asin;
|
---|
8 | import static java.lang.Math.atan2;
|
---|
9 | import static java.lang.Math.cos;
|
---|
10 | import static java.lang.Math.sin;
|
---|
11 | import static java.lang.Math.sqrt;
|
---|
12 | import static java.lang.Math.toRadians;
|
---|
13 |
|
---|
14 | import java.text.DecimalFormat;
|
---|
15 | import java.text.NumberFormat;
|
---|
16 | import java.util.Locale;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.Main;
|
---|
19 | import org.openstreetmap.josm.data.Bounds;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * LatLon are unprojected latitude / longitude coordinates.
|
---|
23 | *
|
---|
24 | * This class is immutable.
|
---|
25 | *
|
---|
26 | * @author Imi
|
---|
27 | */
|
---|
28 | public class LatLon extends Coordinate {
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Minimum difference in location to not be represented as the same position.
|
---|
32 | * The API returns 7 decimals.
|
---|
33 | */
|
---|
34 | public static final double MAX_SERVER_PRECISION = 1e-7;
|
---|
35 |
|
---|
36 | private static DecimalFormat cDmsMinuteFormatter = new DecimalFormat("00");
|
---|
37 | private static DecimalFormat cDmsSecondFormatter = new DecimalFormat("00.0");
|
---|
38 | private static DecimalFormat cDmMinuteFormatter = new DecimalFormat("00.000");
|
---|
39 | public static DecimalFormat cDdFormatter;
|
---|
40 | static {
|
---|
41 | // Don't use the localized decimal separator. This way we can present
|
---|
42 | // a comma separated list of coordinates.
|
---|
43 | cDdFormatter = (DecimalFormat) NumberFormat.getInstance(Locale.UK);
|
---|
44 | cDdFormatter.applyPattern("###0.0000000");
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Replies true if lat is in the range [-90,90]
|
---|
49 | *
|
---|
50 | * @param lat the latitude
|
---|
51 | * @return true if lat is in the range [-90,90]
|
---|
52 | */
|
---|
53 | public static boolean isValidLat(double lat) {
|
---|
54 | return lat >= -90d && lat <= 90d;
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Replies true if lon is in the range [-180,180]
|
---|
59 | *
|
---|
60 | * @param lon the longitude
|
---|
61 | * @return true if lon is in the range [-180,180]
|
---|
62 | */
|
---|
63 | public static boolean isValidLon(double lon) {
|
---|
64 | return lon >= -180d && lon <= 180d;
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Replies the coordinate in degrees/minutes/seconds format
|
---|
69 | */
|
---|
70 | public static String dms(double pCoordinate) {
|
---|
71 |
|
---|
72 | double tAbsCoord = Math.abs(pCoordinate);
|
---|
73 | int tDegree = (int) tAbsCoord;
|
---|
74 | double tTmpMinutes = (tAbsCoord - tDegree) * 60;
|
---|
75 | int tMinutes = (int) tTmpMinutes;
|
---|
76 | double tSeconds = (tTmpMinutes - tMinutes) * 60;
|
---|
77 |
|
---|
78 | return tDegree + "\u00B0" + cDmsMinuteFormatter.format(tMinutes) + "\'"
|
---|
79 | + cDmsSecondFormatter.format(tSeconds) + "\"";
|
---|
80 | }
|
---|
81 |
|
---|
82 | public static String dm(double pCoordinate) {
|
---|
83 |
|
---|
84 | double tAbsCoord = Math.abs(pCoordinate);
|
---|
85 | int tDegree = (int) tAbsCoord;
|
---|
86 | double tMinutes = (tAbsCoord - tDegree) * 60;
|
---|
87 | return tDegree + "\u00B0" + cDmMinuteFormatter.format(tMinutes) + "\'";
|
---|
88 | }
|
---|
89 |
|
---|
90 | public LatLon(double lat, double lon) {
|
---|
91 | super(lon, lat);
|
---|
92 | }
|
---|
93 |
|
---|
94 | public LatLon(LatLon coor) {
|
---|
95 | super(coor.lon(), coor.lat());
|
---|
96 | }
|
---|
97 |
|
---|
98 | public double lat() {
|
---|
99 | return y;
|
---|
100 | }
|
---|
101 |
|
---|
102 | public final static String SOUTH = trc("compass", "S");
|
---|
103 | public final static String NORTH = trc("compass", "N");
|
---|
104 | public String latToString(CoordinateFormat d) {
|
---|
105 | switch(d) {
|
---|
106 | case DECIMAL_DEGREES: return cDdFormatter.format(y);
|
---|
107 | case DEGREES_MINUTES_SECONDS: return dms(y) + ((y < 0) ? SOUTH : NORTH);
|
---|
108 | case NAUTICAL: return dm(y) + ((y < 0) ? SOUTH : NORTH);
|
---|
109 | case EAST_NORTH: return cDdFormatter.format(Main.getProjection().latlon2eastNorth(this).north());
|
---|
110 | default: return "ERR";
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | public double lon() {
|
---|
115 | return x;
|
---|
116 | }
|
---|
117 |
|
---|
118 | public final static String WEST = trc("compass", "W");
|
---|
119 | public final static String EAST = trc("compass", "E");
|
---|
120 | public String lonToString(CoordinateFormat d) {
|
---|
121 | switch(d) {
|
---|
122 | case DECIMAL_DEGREES: return cDdFormatter.format(x);
|
---|
123 | case DEGREES_MINUTES_SECONDS: return dms(x) + ((x < 0) ? WEST : EAST);
|
---|
124 | case NAUTICAL: return dm(x) + ((x < 0) ? WEST : EAST);
|
---|
125 | case EAST_NORTH: return cDdFormatter.format(Main.getProjection().latlon2eastNorth(this).east());
|
---|
126 | default: return "ERR";
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * @return <code>true</code> if the other point has almost the same lat/lon
|
---|
132 | * values, only differing by no more than
|
---|
133 | * 1 / {@link #MAX_SERVER_PRECISION MAX_SERVER_PRECISION}.
|
---|
134 | */
|
---|
135 | public boolean equalsEpsilon(LatLon other) {
|
---|
136 | double p = MAX_SERVER_PRECISION / 2;
|
---|
137 | return Math.abs(lat()-other.lat()) <= p && Math.abs(lon()-other.lon()) <= p;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * @return <code>true</code>, if the coordinate is outside the world, compared
|
---|
142 | * by using lat/lon.
|
---|
143 | */
|
---|
144 | public boolean isOutSideWorld() {
|
---|
145 | Bounds b = Main.getProjection().getWorldBoundsLatLon();
|
---|
146 | return lat() < b.getMin().lat() || lat() > b.getMax().lat() ||
|
---|
147 | lon() < b.getMin().lon() || lon() > b.getMax().lon();
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * @return <code>true</code> if this is within the given bounding box.
|
---|
152 | */
|
---|
153 | public boolean isWithin(Bounds b) {
|
---|
154 | return lat() >= b.getMin().lat() && lat() <= b.getMax().lat() && lon() > b.getMin().lon() && lon() < b.getMax().lon();
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Computes the distance between this lat/lon and another point on the earth.
|
---|
159 | * Uses Haversine formular.
|
---|
160 | * @param other the other point.
|
---|
161 | * @return distance in metres.
|
---|
162 | */
|
---|
163 | public double greatCircleDistance(LatLon other) {
|
---|
164 | double R = 6378135;
|
---|
165 | double sinHalfLat = sin(toRadians(other.lat() - this.lat()) / 2);
|
---|
166 | double sinHalfLon = sin(toRadians(other.lon() - this.lon()) / 2);
|
---|
167 | double d = 2 * R * asin(
|
---|
168 | sqrt(sinHalfLat*sinHalfLat +
|
---|
169 | cos(toRadians(this.lat()))*cos(toRadians(other.lat()))*sinHalfLon*sinHalfLon));
|
---|
170 | // For points opposite to each other on the sphere,
|
---|
171 | // rounding errors could make the argument of asin greater than 1
|
---|
172 | // (This should almost never happen.)
|
---|
173 | if (java.lang.Double.isNaN(d)) {
|
---|
174 | System.err.println("Error: NaN in greatCircleDistance");
|
---|
175 | d = PI * R;
|
---|
176 | }
|
---|
177 | return d;
|
---|
178 | }
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Returns the heading, in radians, that you have to use to get from
|
---|
182 | * this lat/lon to another.
|
---|
183 | *
|
---|
184 | * (I don't know the original source of this formula, but see
|
---|
185 | * http://math.stackexchange.com/questions/720/how-to-calculate-a-heading-on-the-earths-surface
|
---|
186 | * for some hints how it is derived.)
|
---|
187 | *
|
---|
188 | * @param other the "destination" position
|
---|
189 | * @return heading in the range 0 <= hd < 2*PI
|
---|
190 | */
|
---|
191 | public double heading(LatLon other) {
|
---|
192 | double hd = atan2(sin(toRadians(this.lon() - other.lon())) * cos(toRadians(other.lat())),
|
---|
193 | cos(toRadians(this.lat())) * sin(toRadians(other.lat())) -
|
---|
194 | sin(toRadians(this.lat())) * cos(toRadians(other.lat())) * cos(toRadians(this.lon() - other.lon())));
|
---|
195 | hd %= 2 * PI;
|
---|
196 | if (hd < 0) {
|
---|
197 | hd += 2 * PI;
|
---|
198 | }
|
---|
199 | return hd;
|
---|
200 | }
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Returns this lat/lon pair in human-readable format.
|
---|
204 | *
|
---|
205 | * @return String in the format "lat=1.23456 deg, lon=2.34567 deg"
|
---|
206 | */
|
---|
207 | public String toDisplayString() {
|
---|
208 | NumberFormat nf = NumberFormat.getInstance();
|
---|
209 | nf.setMaximumFractionDigits(5);
|
---|
210 | return "lat=" + nf.format(lat()) + "\u00B0, lon=" + nf.format(lon()) + "\u00B0";
|
---|
211 | }
|
---|
212 |
|
---|
213 | public LatLon interpolate(LatLon ll2, double proportion) {
|
---|
214 | return new LatLon(this.lat() + proportion * (ll2.lat() - this.lat()),
|
---|
215 | this.lon() + proportion * (ll2.lon() - this.lon()));
|
---|
216 | }
|
---|
217 |
|
---|
218 | public LatLon getCenter(LatLon ll2) {
|
---|
219 | return new LatLon((this.lat() + ll2.lat())/2.0, (this.lon() + ll2.lon())/2.0);
|
---|
220 | }
|
---|
221 |
|
---|
222 | @Override public String toString() {
|
---|
223 | return "LatLon[lat="+lat()+",lon="+lon()+"]";
|
---|
224 | }
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * Replies a clone of this lat LatLon, rounded to OSM precisions, i.e. to
|
---|
228 | * MAX_SERVER_PRECISION
|
---|
229 | *
|
---|
230 | * @return a clone of this lat LatLon
|
---|
231 | */
|
---|
232 | public LatLon getRoundedToOsmPrecision() {
|
---|
233 | return new LatLon(
|
---|
234 | Math.round(lat() / MAX_SERVER_PRECISION) * MAX_SERVER_PRECISION,
|
---|
235 | Math.round(lon() / MAX_SERVER_PRECISION) * MAX_SERVER_PRECISION
|
---|
236 | );
|
---|
237 | }
|
---|
238 |
|
---|
239 | @Override
|
---|
240 | public int hashCode() {
|
---|
241 | final int prime = 31;
|
---|
242 | int result = super.hashCode();
|
---|
243 | long temp;
|
---|
244 | temp = java.lang.Double.doubleToLongBits(x);
|
---|
245 | result = prime * result + (int) (temp ^ (temp >>> 32));
|
---|
246 | temp = java.lang.Double.doubleToLongBits(y);
|
---|
247 | result = prime * result + (int) (temp ^ (temp >>> 32));
|
---|
248 | return result;
|
---|
249 | }
|
---|
250 |
|
---|
251 | @Override
|
---|
252 | public boolean equals(Object obj) {
|
---|
253 | if (this == obj)
|
---|
254 | return true;
|
---|
255 | if (!super.equals(obj))
|
---|
256 | return false;
|
---|
257 | if (getClass() != obj.getClass())
|
---|
258 | return false;
|
---|
259 | Coordinate other = (Coordinate) obj;
|
---|
260 | if (java.lang.Double.doubleToLongBits(x) != java.lang.Double.doubleToLongBits(other.x))
|
---|
261 | return false;
|
---|
262 | if (java.lang.Double.doubleToLongBits(y) != java.lang.Double.doubleToLongBits(other.y))
|
---|
263 | return false;
|
---|
264 | return true;
|
---|
265 | }
|
---|
266 | }
|
---|