001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.data.coor; 003 004import java.awt.geom.Area; 005import java.text.DecimalFormat; 006import java.text.NumberFormat; 007import java.util.Locale; 008import java.util.Objects; 009 010import org.openstreetmap.josm.data.Bounds; 011import org.openstreetmap.josm.tools.Utils; 012 013/** 014 * LatLon are unprojected latitude / longitude coordinates. 015 * <br> 016 * <b>Latitude</b> specifies the north-south position in degrees 017 * where valid values are in the [-90,90] and positive values specify positions north of the equator. 018 * <br> 019 * <b>Longitude</b> specifies the east-west position in degrees 020 * where valid values are in the [-180,180] and positive values specify positions east of the prime meridian. 021 * <br> 022 * <img alt="lat/lon" src="https://upload.wikimedia.org/wikipedia/commons/6/62/Latitude_and_Longitude_of_the_Earth.svg"> 023 * <br> 024 * This class is immutable. 025 * 026 * @author Imi 027 */ 028public class LatLon extends Coordinate implements ILatLon { 029 030 private static final long serialVersionUID = 1L; 031 032 /** 033 * Minimum difference in location to not be represented as the same position. 034 * The API returns 7 decimals. 035 */ 036 public static final double MAX_SERVER_PRECISION = ILatLon.MAX_SERVER_PRECISION; 037 /** 038 * The inverse of the server precision 039 * @see #MAX_SERVER_PRECISION 040 */ 041 public static final double MAX_SERVER_INV_PRECISION = 1e7; 042 043 /** 044 * The (0,0) coordinates. 045 * @since 6178 046 */ 047 public static final LatLon ZERO = new LatLon(0, 0); 048 049 /** North pole. */ 050 public static final LatLon NORTH_POLE = new LatLon(90, 0); 051 /** South pole. */ 052 public static final LatLon SOUTH_POLE = new LatLon(-90, 0); 053 054 /** 055 * The normal number format for server precision coordinates 056 */ 057 public static final DecimalFormat cDdFormatter; 058 /** 059 * The number format used for high precision coordinates 060 */ 061 public static final DecimalFormat cDdHighPrecisionFormatter; 062 static { 063 // Don't use the localized decimal separator. This way we can present 064 // a comma separated list of coordinates. 065 cDdFormatter = (DecimalFormat) NumberFormat.getInstance(Locale.UK); 066 cDdFormatter.applyPattern("###0.0######"); 067 cDdHighPrecisionFormatter = (DecimalFormat) NumberFormat.getInstance(Locale.UK); 068 cDdHighPrecisionFormatter.applyPattern("###0.0##########"); 069 } 070 071 /** 072 * Replies true if lat is in the range [-90,90] 073 * 074 * @param lat the latitude 075 * @return true if lat is in the range [-90,90] 076 */ 077 public static boolean isValidLat(double lat) { 078 return lat >= -90d && lat <= 90d; 079 } 080 081 /** 082 * Replies true if lon is in the range [-180,180] 083 * 084 * @param lon the longitude 085 * @return true if lon is in the range [-180,180] 086 */ 087 public static boolean isValidLon(double lon) { 088 return lon >= -180d && lon <= 180d; 089 } 090 091 /** 092 * Make sure longitude value is within <code>[-180, 180]</code> range. 093 * @param lon the longitude in degrees 094 * @return lon plus/minus multiples of <code>360</code>, as needed to get 095 * in <code>[-180, 180]</code> range 096 */ 097 public static double normalizeLon(double lon) { 098 if (lon >= -180 && lon <= 180) 099 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}