source: josm/trunk/src/org/openstreetmap/josm/data/coor/LatLon.java

Last change on this file was 19394, checked in by stoecker, 12 months ago

fix #24104 - drop remaining deprecations

  • Property svn:eol-style set to native
File size: 9.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.coor;
3
4import java.awt.geom.Area;
5import java.text.DecimalFormat;
6import java.text.NumberFormat;
7import java.util.Locale;
8import java.util.Objects;
9
10import org.openstreetmap.josm.data.Bounds;
11import org.openstreetmap.josm.tools.Utils;
12
13/**
14 * LatLon are unprojected latitude / longitude coordinates.
15 * <br>
16 * <b>Latitude</b> specifies the north-south position in degrees
17 * where valid values are in the [-90,90] and positive values specify positions north of the equator.
18 * <br>
19 * <b>Longitude</b> specifies the east-west position in degrees
20 * where valid values are in the [-180,180] and positive values specify positions east of the prime meridian.
21 * <br>
22 * <img alt="lat/lon" src="https://upload.wikimedia.org/wikipedia/commons/6/62/Latitude_and_Longitude_of_the_Earth.svg">
23 * <br>
24 * This class is immutable.
25 *
26 * @author Imi
27 */
28public class LatLon extends Coordinate implements ILatLon {
29
30 private static final long serialVersionUID = 1L;
31
32 /**
33 * Minimum difference in location to not be represented as the same position.
34 * The API returns 7 decimals.
35 */
36 public static final double MAX_SERVER_PRECISION = ILatLon.MAX_SERVER_PRECISION;
37 /**
38 * The inverse of the server precision
39 * @see #MAX_SERVER_PRECISION
40 */
41 public static final double MAX_SERVER_INV_PRECISION = 1e7;
42
43 /**
44 * The (0,0) coordinates.
45 * @since 6178
46 */
47 public static final LatLon ZERO = new LatLon(0, 0);
48
49 /** North pole. */
50 public static final LatLon NORTH_POLE = new LatLon(90, 0);
51 /** South pole. */
52 public static final LatLon SOUTH_POLE = new LatLon(-90, 0);
53
54 /**
55 * The normal number format for server precision coordinates
56 */
57 public static final DecimalFormat cDdFormatter;
58 /**
59 * The number format used for high precision coordinates
60 */
61 public static final DecimalFormat cDdHighPrecisionFormatter;
62 static {
63 // Don't use the localized decimal separator. This way we can present
64 // a comma separated list of coordinates.
65 cDdFormatter = (DecimalFormat) NumberFormat.getInstance(Locale.UK);
66 cDdFormatter.applyPattern("###0.0######");
67 cDdHighPrecisionFormatter = (DecimalFormat) NumberFormat.getInstance(Locale.UK);
68 cDdHighPrecisionFormatter.applyPattern("###0.0##########");
69 }
70
71 /**
72 * Replies true if lat is in the range [-90,90]
73 *
74 * @param lat the latitude
75 * @return true if lat is in the range [-90,90]
76 */
77 public static boolean isValidLat(double lat) {
78 return lat >= -90d && lat <= 90d;
79 }
80
81 /**
82 * Replies true if lon is in the range [-180,180]
83 *
84 * @param lon the longitude
85 * @return true if lon is in the range [-180,180]
86 */
87 public static boolean isValidLon(double lon) {
88 return lon >= -180d && lon <= 180d;
89 }
90
91 /**
92 * Make sure longitude value is within <code>[-180, 180]</code> range.
93 * @param lon the longitude in degrees
94 * @return lon plus/minus multiples of <code>360</code>, as needed to get
95 * in <code>[-180, 180]</code> range
96 */
97 public static double normalizeLon(double lon) {
98 if (lon >= -180 && lon <= 180)
99 return lon;
100 else {
101 lon = lon % 360.0;
102 if (lon > 180) {
103 return lon - 360;
104 } else if (lon < -180) {
105 return lon + 360;
106 }
107 return lon;
108 }
109 }
110
111 /**
112 * Replies true if lat is in the range [-90,90] and lon is in the range [-180,180]
113 *
114 * @return true if lat is in the range [-90,90] and lon is in the range [-180,180]
115 */
116 public boolean isValid() {
117 return isValidLat(lat()) && isValidLon(lon());
118 }
119
120 /**
121 * Clamp the lat value to be inside the world.
122 * @param value The value
123 * @return The value clamped to the world.
124 */
125 public static double toIntervalLat(double value) {
126 return Utils.clamp(value, -90, 90);
127 }
128
129 /**
130 * Returns a valid OSM longitude [-180,+180] for the given extended longitude value.
131 * For example, a value of -181 will return +179, a value of +181 will return -179.
132 * @param value A longitude value not restricted to the [-180,+180] range.
133 * @return a valid OSM longitude [-180,+180]
134 */
135 public static double toIntervalLon(double value) {
136 if (isValidLon(value))
137 return value;
138 else {
139 int n = (int) (value + Math.signum(value)*180.0) / 360;
140 return value - n*360.0;
141 }
142 }
143
144 /**
145 * Constructs a new object representing the given latitude/longitude.
146 * @param lat the latitude, i.e., the north-south position in degrees
147 * @param lon the longitude, i.e., the east-west position in degrees
148 */
149 public LatLon(double lat, double lon) {
150 super(lon, lat);
151 }
152
153 /**
154 * Creates a new LatLon object for the given coordinate
155 * @param coor The coordinates to copy from.
156 */
157 public LatLon(ILatLon coor) {
158 super(coor.lon(), coor.lat());
159 }
160
161 @Override
162 public double lat() {
163 return y;
164 }
165
166 @Override
167 public double lon() {
168 return x;
169 }
170
171 /**
172 * Determines if this lat/lon is within the given bounding box.
173 * @param b bounding box
174 * @return <code>true</code> if this is within the given bounding box.
175 */
176 public boolean isWithin(Bounds b) {
177 return b.contains(this);
178 }
179
180 /**
181 * Check if this is contained in given area or area is null.
182 *
183 * @param a Area
184 * @return <code>true</code> if this is contained in given area or area is null.
185 */
186 public boolean isIn(Area a) {
187 return a == null || a.contains(x, y);
188 }
189
190 /**
191 * Returns this lat/lon pair in human-readable format.
192 *
193 * @return String in the format "lat=1.23456 deg, lon=2.34567 deg"
194 */
195 public String toDisplayString() {
196 NumberFormat nf = NumberFormat.getInstance();
197 nf.setMaximumFractionDigits(5);
198 return "lat=" + nf.format(lat()) + "\u00B0, lon=" + nf.format(lon()) + '\u00B0';
199 }
200
201 /**
202 * Interpolate between this and a other latlon. If you don't care about the return type, use {@link ILatLon#interpolate(ILatLon, double)}
203 * instead.
204 * @param ll2 The other lat/lon object
205 * @param proportion The proportion to interpolate
206 * @return a new latlon at this position if proportion is 0, at the other position it proportion is 1 and linearly interpolated otherwise.
207 */
208 public LatLon interpolate(LatLon ll2, double proportion) {
209 ILatLon interpolated = ILatLon.super.interpolate(ll2, proportion);
210 if (interpolated instanceof LatLon) {
211 return (LatLon) interpolated;
212 }
213 return new LatLon(interpolated);
214 }
215
216 /**
217 * Get the center between two lat/lon points
218 * @param ll2 The other {@link LatLon}
219 * @return The center at the average coordinates of the two points. Does not take the 180° meridian into account.
220 */
221 public LatLon getCenter(LatLon ll2) {
222 // The JIT will inline this for us, it is as fast as the normal /2 approach
223 return interpolate(ll2, .5);
224 }
225
226 /**
227 * Returns the euclidean distance from this {@code LatLon} to a specified {@code LatLon}.
228 *
229 * @param ll the specified coordinate to be measured against this {@code LatLon}
230 * @return the euclidean distance from this {@code LatLon} to a specified {@code LatLon}
231 * @since 6166
232 */
233 public double distance(final LatLon ll) {
234 return super.distance(ll);
235 }
236
237 /**
238 * Returns the square of the euclidean distance from this {@code LatLon} to a specified {@code LatLon}.
239 *
240 * @param ll the specified coordinate to be measured against this {@code LatLon}
241 * @return the square of the euclidean distance from this {@code LatLon} to a specified {@code LatLon}
242 * @since 6166
243 */
244 public double distanceSq(final LatLon ll) {
245 return super.distanceSq(ll);
246 }
247
248 @Override
249 public String toString() {
250 return "LatLon[lat="+lat()+",lon="+lon()+']';
251 }
252
253 /**
254 * Returns the value rounded to OSM precisions, i.e. to {@link #MAX_SERVER_PRECISION}.
255 * @param value lat/lon value
256 *
257 * @return rounded value
258 */
259 public static double roundToOsmPrecision(double value) {
260 return Math.round(value * MAX_SERVER_INV_PRECISION) / MAX_SERVER_INV_PRECISION;
261 }
262
263 /**
264 * Replies a clone of this lat LatLon, rounded to OSM precisions, i.e. to {@link #MAX_SERVER_PRECISION}
265 *
266 * @return a clone of this lat LatLon
267 */
268 public LatLon getRoundedToOsmPrecision() {
269 return new LatLon(
270 roundToOsmPrecision(lat()),
271 roundToOsmPrecision(lon())
272 );
273 }
274
275 @Override
276 public int hashCode() {
277 return Objects.hash(x, y);
278 }
279
280 @Override
281 public boolean equals(Object obj) {
282 if (this == obj) return true;
283 if (obj == null || getClass() != obj.getClass()) return false;
284 LatLon that = (LatLon) obj;
285 return Double.compare(that.x, x) == 0 &&
286 Double.compare(that.y, y) == 0;
287 }
288}
Note: See TracBrowser for help on using the repository browser.