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.cos;
|
---|
9 | import static java.lang.Math.sin;
|
---|
10 | import static java.lang.Math.sqrt;
|
---|
11 | import static java.lang.Math.toRadians;
|
---|
12 |
|
---|
13 | import java.text.DecimalFormat;
|
---|
14 | import java.text.NumberFormat;
|
---|
15 | import java.util.Locale;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.data.Bounds;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * LatLon are unprojected latitude / longitude coordinates.
|
---|
22 | *
|
---|
23 | * This class is immutable.
|
---|
24 | *
|
---|
25 | * @author Imi
|
---|
26 | */
|
---|
27 | public class LatLon extends Coordinate {
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Minimum difference in location to not be represented as the same position.
|
---|
31 | * The API returns 7 decimals.
|
---|
32 | */
|
---|
33 | public static final double MAX_SERVER_PRECISION = 1e-7;
|
---|
34 |
|
---|
35 | private static DecimalFormat cDmsMinuteFormatter = new DecimalFormat("00");
|
---|
36 | private static DecimalFormat cDmsSecondFormatter = new DecimalFormat("00.0");
|
---|
37 | private static DecimalFormat cDmMinuteFormatter = new DecimalFormat("00.000");
|
---|
38 | public static DecimalFormat cDdFormatter;
|
---|
39 | static {
|
---|
40 | // Don't use the localized decimal separator. This way we can present
|
---|
41 | // a comma separated list of coordinates.
|
---|
42 | cDdFormatter = (DecimalFormat) NumberFormat.getInstance(Locale.UK);
|
---|
43 | cDdFormatter.applyPattern("###0.0000000");
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Replies true if lat is in the range [-90,90]
|
---|
48 | *
|
---|
49 | * @param lat the latitude
|
---|
50 | * @return true if lat is in the range [-90,90]
|
---|
51 | */
|
---|
52 | public static boolean isValidLat(double lat) {
|
---|
53 | return lat >= -90d && lat <= 90d;
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Replies true if lon is in the range [-180,180]
|
---|
58 | *
|
---|
59 | * @param lon the longitude
|
---|
60 | * @return true if lon is in the range [-180,180]
|
---|
61 | */
|
---|
62 | public static boolean isValidLon(double lon) {
|
---|
63 | return lon >= -180d && lon <= 180d;
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Replies the coordinate in degrees/minutes/seconds format
|
---|
68 | */
|
---|
69 | public static String dms(double pCoordinate) {
|
---|
70 |
|
---|
71 | double tAbsCoord = Math.abs(pCoordinate);
|
---|
72 | int tDegree = (int) tAbsCoord;
|
---|
73 | double tTmpMinutes = (tAbsCoord - tDegree) * 60;
|
---|
74 | int tMinutes = (int) tTmpMinutes;
|
---|
75 | double tSeconds = (tTmpMinutes - tMinutes) * 60;
|
---|
76 |
|
---|
77 | return tDegree + "\u00B0" + cDmsMinuteFormatter.format(tMinutes) + "\'"
|
---|
78 | + cDmsSecondFormatter.format(tSeconds) + "\"";
|
---|
79 | }
|
---|
80 |
|
---|
81 | public static String dm(double pCoordinate) {
|
---|
82 |
|
---|
83 | double tAbsCoord = Math.abs(pCoordinate);
|
---|
84 | int tDegree = (int) tAbsCoord;
|
---|
85 | double tMinutes = (tAbsCoord - tDegree) * 60;
|
---|
86 | return tDegree + "\u00B0" + cDmMinuteFormatter.format(tMinutes) + "\'";
|
---|
87 | }
|
---|
88 |
|
---|
89 | public LatLon(double lat, double lon) {
|
---|
90 | super(lon, lat);
|
---|
91 | }
|
---|
92 |
|
---|
93 | public LatLon(LatLon coor) {
|
---|
94 | super(coor.lon(), coor.lat());
|
---|
95 | }
|
---|
96 |
|
---|
97 | public double lat() {
|
---|
98 | return y;
|
---|
99 | }
|
---|
100 |
|
---|
101 | public final static String SOUTH = trc("compass", "S");
|
---|
102 | public final static String NORTH = trc("compass", "N");
|
---|
103 | public String latToString(CoordinateFormat d) {
|
---|
104 | switch(d) {
|
---|
105 | case DECIMAL_DEGREES: return cDdFormatter.format(y);
|
---|
106 | case DEGREES_MINUTES_SECONDS: return dms(y) + ((y < 0) ? SOUTH : NORTH);
|
---|
107 | case NAUTICAL: return dm(y) + ((y < 0) ? SOUTH : NORTH);
|
---|
108 | case EAST_NORTH: return cDdFormatter.format(Main.proj.latlon2eastNorth(this).north());
|
---|
109 | default: return "ERR";
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | public double lon() {
|
---|
114 | return x;
|
---|
115 | }
|
---|
116 |
|
---|
117 | public final static String WEST = trc("compass", "W");
|
---|
118 | public final static String EAST = trc("compass", "E");
|
---|
119 | public String lonToString(CoordinateFormat d) {
|
---|
120 | switch(d) {
|
---|
121 | case DECIMAL_DEGREES: return cDdFormatter.format(x);
|
---|
122 | case DEGREES_MINUTES_SECONDS: return dms(x) + ((x < 0) ? WEST : EAST);
|
---|
123 | case NAUTICAL: return dm(x) + ((x < 0) ? WEST : EAST);
|
---|
124 | case EAST_NORTH: return cDdFormatter.format(Main.proj.latlon2eastNorth(this).east());
|
---|
125 | default: return "ERR";
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * @return <code>true</code> if the other point has almost the same lat/lon
|
---|
131 | * values, only differing by no more than
|
---|
132 | * 1 / {@link #MAX_SERVER_PRECISION MAX_SERVER_PRECISION}.
|
---|
133 | */
|
---|
134 | public boolean equalsEpsilon(LatLon other) {
|
---|
135 | double p = MAX_SERVER_PRECISION / 2;
|
---|
136 | return Math.abs(lat()-other.lat()) <= p && Math.abs(lon()-other.lon()) <= p;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * @return <code>true</code>, if the coordinate is outside the world, compared
|
---|
141 | * by using lat/lon.
|
---|
142 | */
|
---|
143 | public boolean isOutSideWorld() {
|
---|
144 | Bounds b = Main.proj.getWorldBoundsLatLon();
|
---|
145 | return lat() < b.getMin().lat() || lat() > b.getMax().lat() ||
|
---|
146 | lon() < b.getMin().lon() || lon() > b.getMax().lon();
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * @return <code>true</code> if this is within the given bounding box.
|
---|
151 | */
|
---|
152 | public boolean isWithin(Bounds b) {
|
---|
153 | return lat() >= b.getMin().lat() && lat() <= b.getMax().lat() && lon() > b.getMin().lon() && lon() < b.getMax().lon();
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Computes the distance between this lat/lon and another point on the earth.
|
---|
158 | * Uses Haversine formular.
|
---|
159 | * @param other the other point.
|
---|
160 | * @return distance in metres.
|
---|
161 | */
|
---|
162 | public double greatCircleDistance(LatLon other) {
|
---|
163 | double R = 6378135;
|
---|
164 | double sinHalfLat = sin(toRadians(other.lat() - this.lat()) / 2);
|
---|
165 | double sinHalfLon = sin(toRadians(other.lon() - this.lon()) / 2);
|
---|
166 | double d = 2 * R * asin(
|
---|
167 | sqrt(sinHalfLat*sinHalfLat +
|
---|
168 | cos(toRadians(this.lat()))*cos(toRadians(other.lat()))*sinHalfLon*sinHalfLon));
|
---|
169 | // For points opposite to each other on the sphere,
|
---|
170 | // rounding errors could make the argument of asin greater than 1
|
---|
171 | // (This should almost never happen.)
|
---|
172 | if (java.lang.Double.isNaN(d)) {
|
---|
173 | System.err.println("Error: NaN in greatCircleDistance");
|
---|
174 | d = PI * R;
|
---|
175 | }
|
---|
176 | return d;
|
---|
177 | }
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Returns the heading, in radians, that you have to use to get from
|
---|
181 | * this lat/lon to another.
|
---|
182 | *
|
---|
183 | * @param other the "destination" position
|
---|
184 | * @return heading
|
---|
185 | */
|
---|
186 | public double heading(LatLon other) {
|
---|
187 | double rv;
|
---|
188 | if (other.lat() == lat()) {
|
---|
189 | rv = (other.lon()>lon() ? Math.PI / 2 : Math.PI * 3 / 2);
|
---|
190 | } else {
|
---|
191 | rv = Math.atan((other.lon()-lon())/(other.lat()-lat()));
|
---|
192 | if (rv < 0) {
|
---|
193 | rv += Math.PI;
|
---|
194 | }
|
---|
195 | if (other.lon() < lon()) {
|
---|
196 | rv += Math.PI;
|
---|
197 | }
|
---|
198 | }
|
---|
199 | return rv;
|
---|
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 | }
|
---|