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

Last change on this file since 3253 was 3067, checked in by bastiK, 14 years ago

applied #4645 - option to show projected coordinates instead of lat/lon (patch by tibob)

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