1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.data.coor;
|
---|
3 |
|
---|
4 |
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.text.DecimalFormat;
|
---|
8 | import java.text.NumberFormat;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 | import org.openstreetmap.josm.data.Bounds;
|
---|
12 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * LatLon are unprojected latitude / longitude coordinates.
|
---|
16 | *
|
---|
17 | * This class is immutable.
|
---|
18 | *
|
---|
19 | * @author Imi
|
---|
20 | */
|
---|
21 | public class LatLon extends Coordinate {
|
---|
22 |
|
---|
23 | private static DecimalFormat cDmsMinuteFormatter = new DecimalFormat("00");
|
---|
24 | private static DecimalFormat cDmsSecondFormatter = new DecimalFormat("00.0");
|
---|
25 | private static DecimalFormat cDdFormatter = new DecimalFormat("###0.0000");
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Replies true if lat is in the range [-90,90]
|
---|
29 | *
|
---|
30 | * @param lat the latitude
|
---|
31 | * @return true if lat is in the range [-90,90]
|
---|
32 | */
|
---|
33 | public static boolean isValidLat(double lat) {
|
---|
34 | return lat >= -90d && lat <= 90d;
|
---|
35 | }
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Replies true if lon is in the range [-180,180]
|
---|
39 | *
|
---|
40 | * @param lon the longitude
|
---|
41 | * @return true if lon is in the range [-180,180]
|
---|
42 | */
|
---|
43 | public static boolean isValidLon(double lon) {
|
---|
44 | return lon >= -180d && lon <= 180d;
|
---|
45 | }
|
---|
46 |
|
---|
47 | public static String dms(double pCoordinate) {
|
---|
48 |
|
---|
49 | double tAbsCoord = Math.abs(pCoordinate);
|
---|
50 | int tDegree = (int) tAbsCoord;
|
---|
51 | double tTmpMinutes = (tAbsCoord - tDegree) * 60;
|
---|
52 | int tMinutes = (int) tTmpMinutes;
|
---|
53 | double tSeconds = (tTmpMinutes - tMinutes) * 60;
|
---|
54 |
|
---|
55 | return tDegree + "\u00B0" + cDmsMinuteFormatter.format(tMinutes) + "\'"
|
---|
56 | + cDmsSecondFormatter.format(tSeconds) + "\"";
|
---|
57 | }
|
---|
58 |
|
---|
59 | public LatLon(double lat, double lon) {
|
---|
60 | super(lon, lat);
|
---|
61 | }
|
---|
62 |
|
---|
63 | public LatLon(LatLon coor) {
|
---|
64 | super(coor.lon(), coor.lat());
|
---|
65 | }
|
---|
66 |
|
---|
67 | public double lat() {
|
---|
68 | return y;
|
---|
69 | }
|
---|
70 |
|
---|
71 | public String latToString(CoordinateFormat d) {
|
---|
72 | switch(d) {
|
---|
73 | case DECIMAL_DEGREES: return cDdFormatter.format(y);
|
---|
74 | case DEGREES_MINUTES_SECONDS: return dms(y) + ((y < 0) ?
|
---|
75 | /* short symbol for South */ tr("S") :
|
---|
76 | /* short symbol for North */ tr("N"));
|
---|
77 | default: return "ERR";
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | public double lon() {
|
---|
82 | return x;
|
---|
83 | }
|
---|
84 |
|
---|
85 | public String lonToString(CoordinateFormat d) {
|
---|
86 | switch(d) {
|
---|
87 | case DECIMAL_DEGREES: return cDdFormatter.format(x);
|
---|
88 | case DEGREES_MINUTES_SECONDS: return dms(x) + ((x < 0) ?
|
---|
89 | /* short symbol for West */ tr("W") :
|
---|
90 | /* short symbol for East */ tr("E"));
|
---|
91 | default: return "ERR";
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * @return <code>true</code> if the other point has almost the same lat/lon
|
---|
97 | * values, only differing by no more than
|
---|
98 | * 1 / {@link org.openstreetmap.josm.data.projection.Projection#MAX_SERVER_PRECISION MAX_SERVER_PRECISION}.
|
---|
99 | */
|
---|
100 | public boolean equalsEpsilon(LatLon other) {
|
---|
101 | final double p = 1/Projection.MAX_SERVER_PRECISION;
|
---|
102 | return Math.abs(lat()-other.lat()) <= p && Math.abs(lon()-other.lon()) <= p;
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * @return <code>true</code>, if the coordinate is outside the world, compared
|
---|
107 | * by using lat/lon.
|
---|
108 | */
|
---|
109 | public boolean isOutSideWorld() {
|
---|
110 | Bounds b = Main.proj.getWorldBoundsLatLon();
|
---|
111 | return lat() < b.getMin().lat() || lat() > b.getMax().lat() ||
|
---|
112 | lon() < b.getMin().lon() || lon() > b.getMax().lon();
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * @return <code>true</code> if this is within the given bounding box.
|
---|
117 | */
|
---|
118 | public boolean isWithin(Bounds b) {
|
---|
119 | return lat() >= b.getMin().lat() && lat() <= b.getMax().lat() && lon() > b.getMin().lon() && lon() < b.getMax().lon();
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Computes the distance between this lat/lon and another point on the earth.
|
---|
124 | * Uses spherical law of cosines formula, not Haversine.
|
---|
125 | * @param other the other point.
|
---|
126 | * @return distance in metres.
|
---|
127 | */
|
---|
128 | public double greatCircleDistance(LatLon other) {
|
---|
129 | return (Math.acos(
|
---|
130 | Math.sin(Math.toRadians(lat())) * Math.sin(Math.toRadians(other.lat())) +
|
---|
131 | Math.cos(Math.toRadians(lat()))*Math.cos(Math.toRadians(other.lat())) *
|
---|
132 | Math.cos(Math.toRadians(other.lon()-lon()))) * 6378135);
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Returns the heading, in radians, that you have to use to get from
|
---|
137 | * this lat/lon to another.
|
---|
138 | *
|
---|
139 | * @param other the "destination" position
|
---|
140 | * @return heading
|
---|
141 | */
|
---|
142 | public double heading(LatLon other) {
|
---|
143 | double rv;
|
---|
144 | if (other.lat() == lat()) {
|
---|
145 | rv = (other.lon()>lon() ? Math.PI / 2 : Math.PI * 3 / 2);
|
---|
146 | } else {
|
---|
147 | rv = Math.atan((other.lon()-lon())/(other.lat()-lat()));
|
---|
148 | if (rv < 0) {
|
---|
149 | rv += Math.PI;
|
---|
150 | }
|
---|
151 | if (other.lon() < lon()) {
|
---|
152 | rv += Math.PI;
|
---|
153 | }
|
---|
154 | }
|
---|
155 | return rv;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Returns this lat/lon pair in human-readable format.
|
---|
160 | *
|
---|
161 | * @return String in the format "lat=1.23456°, lon=2.34567°"
|
---|
162 | */
|
---|
163 | public String toDisplayString() {
|
---|
164 | NumberFormat nf = NumberFormat.getInstance();
|
---|
165 | nf.setMaximumFractionDigits(5);
|
---|
166 | return "lat=" + nf.format(lat()) + "\u00B0, lon=" + nf.format(lon()) + "\u00B0";
|
---|
167 | }
|
---|
168 |
|
---|
169 | public LatLon interpolate(LatLon ll2, double proportion) {
|
---|
170 | return new LatLon(this.lat() + proportion * (ll2.lat() - this.lat()),
|
---|
171 | this.lon() + proportion * (ll2.lon() - this.lon()));
|
---|
172 | }
|
---|
173 |
|
---|
174 | public LatLon getCenter(LatLon ll2) {
|
---|
175 | return new LatLon((this.lat() + ll2.lat())/2.0, (this.lon() + ll2.lon())/2.0);
|
---|
176 | }
|
---|
177 |
|
---|
178 | @Override public String toString() {
|
---|
179 | return "LatLon[lat="+lat()+",lon="+lon()+"]";
|
---|
180 | }
|
---|
181 | }
|
---|