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

Last change on this file since 1230 was 1209, checked in by framm, 15 years ago
  • cosmetics
  • Property svn:eol-style set to native
File size: 4.7 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.data.coor;
3
4
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import org.openstreetmap.josm.data.Bounds;
8import org.openstreetmap.josm.data.projection.Projection;
9
10import java.text.DecimalFormat;
11import java.text.NumberFormat;
12
13/**
14 * LatLon are unprojected latitude / longitude coordinates.
15 *
16 * This class is immutable.
17 *
18 * @author Imi
19 */
20public class LatLon extends Coordinate {
21
22 private static DecimalFormat cDmsMinuteFormatter = new DecimalFormat("00");
23 private static DecimalFormat cDmsSecondFormatter = new DecimalFormat("00.0");
24 private static DecimalFormat cDdFormatter = new DecimalFormat("###0.0000");
25
26 /**
27 * Possible ways to display coordinates
28 */
29 public enum CoordinateFormat {
30 DECIMAL_DEGREES {public String toString() {return tr("Decimal Degrees");}},
31 DEGREES_MINUTES_SECONDS {public String toString() {return tr("Degrees Minutes Seconds");}};
32 }
33
34 public static String dms(double pCoordinate) {
35
36 double tAbsCoord = Math.abs(pCoordinate);
37 int tDegree = (int) tAbsCoord;
38 double tTmpMinutes = (tAbsCoord - tDegree) * 60;
39 int tMinutes = (int) tTmpMinutes;
40 double tSeconds = (tTmpMinutes - tMinutes) * 60;
41
42 return tDegree + "\u00B0" + cDmsMinuteFormatter.format(tMinutes) + "\'"
43 + cDmsSecondFormatter.format(tSeconds) + "\"";
44 }
45
46 public LatLon(double lat, double lon) {
47 super(lon, lat);
48 }
49
50 public double lat() {
51 return y;
52 }
53
54 public String latToString(CoordinateFormat d) {
55 switch(d) {
56 case DECIMAL_DEGREES: return cDdFormatter.format(y);
57 case DEGREES_MINUTES_SECONDS: return dms(y) + ((y < 0) ? tr("S") : tr("N"));
58 default: return "ERR";
59 }
60 }
61
62 public double lon() {
63 return x;
64 }
65
66 public String lonToString(CoordinateFormat d) {
67 switch(d) {
68 case DECIMAL_DEGREES: return cDdFormatter.format(x);
69 case DEGREES_MINUTES_SECONDS: return dms(x) + ((x < 0) ? tr("W") : tr("E"));
70 default: return "ERR";
71 }
72 }
73
74 /**
75 * @return <code>true</code> if the other point has almost the same lat/lon
76 * values, only differing by no more than
77 * 1 / {@link org.openstreetmap.josm.data.projection.Projection#MAX_SERVER_PRECISION MAX_SERVER_PRECISION}.
78 */
79 public boolean equalsEpsilon(LatLon other) {
80 final double p = 1/Projection.MAX_SERVER_PRECISION;
81 return Math.abs(lat()-other.lat()) <= p && Math.abs(lon()-other.lon()) <= p;
82 }
83
84 /**
85 * @return <code>true</code>, if the coordinate is outside the world, compared
86 * by using lat/lon.
87 */
88 public boolean isOutSideWorld() {
89 return lat() < -Projection.MAX_LAT || lat() > Projection.MAX_LAT ||
90 lon() < -Projection.MAX_LON || lon() > Projection.MAX_LON;
91 }
92
93 /**
94 * @return <code>true</code> if this is within the given bounding box.
95 */
96 public boolean isWithin(Bounds b) {
97 return lat() >= b.min.lat() && lat() <= b.max.lat() && lon() > b.min.lon() && lon() < b.max.lon();
98 }
99
100 /**
101 * Computes the distance between this lat/lon and another point on the earth.
102 * Uses spherical law of cosines formula, not Haversine.
103 * @param other the other point.
104 * @return distance in metres.
105 */
106 public double greatCircleDistance(LatLon other) {
107 return (Math.acos(
108 Math.sin(Math.toRadians(lat())) * Math.sin(Math.toRadians(other.lat())) +
109 Math.cos(Math.toRadians(lat()))*Math.cos(Math.toRadians(other.lat())) *
110 Math.cos(Math.toRadians(other.lon()-lon()))) * 6378135);
111 }
112
113 /**
114 * Returns the heading, in radians, that you have to use to get from
115 * this lat/lon to another.
116 *
117 * @param other the "destination" position
118 * @return heading
119 */
120 public double heading(LatLon other) {
121 double rv;
122 if (other.lat() == lat()) {
123 rv = (other.lon()>lon() ? Math.PI / 2 : Math.PI * 3 / 2);
124 } else {
125 rv = Math.atan((other.lon()-lon())/(other.lat()-lat()));
126 if (rv < 0) rv += Math.PI;
127 if (other.lon() < lon()) rv += Math.PI;
128 }
129 return rv;
130 }
131
132 /**
133 * Returns this lat/lon pair in human-readable format.
134 *
135 * @return String in the format "lat=1.23456°, lon=2.34567°"
136 */
137 public String toDisplayString() {
138 NumberFormat nf = NumberFormat.getInstance();
139 nf.setMaximumFractionDigits(5);
140 return "lat=" + nf.format(lat()) + "°, lon=" + nf.format(lon()) + "°";
141 }
142
143 @Override public String toString() {
144 return "LatLon[lat="+lat()+",lon="+lon()+"]";
145 }
146}
Note: See TracBrowser for help on using the repository browser.