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

Last change on this file since 2174 was 2174, checked in by stoecker, 15 years ago

added context handling for translated strings

  • Property svn:eol-style set to native
File size: 5.1 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 java.text.DecimalFormat;
8import java.text.NumberFormat;
9
10import org.openstreetmap.josm.Main;
11import org.openstreetmap.josm.data.Bounds;
12import 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 */
21public 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 public static String dms(double pCoordinate) {
29
30 double tAbsCoord = Math.abs(pCoordinate);
31 int tDegree = (int) tAbsCoord;
32 double tTmpMinutes = (tAbsCoord - tDegree) * 60;
33 int tMinutes = (int) tTmpMinutes;
34 double tSeconds = (tTmpMinutes - tMinutes) * 60;
35
36 return tDegree + "\u00B0" + cDmsMinuteFormatter.format(tMinutes) + "\'"
37 + cDmsSecondFormatter.format(tSeconds) + "\"";
38 }
39
40 public LatLon(double lat, double lon) {
41 super(lon, lat);
42 }
43
44 public LatLon(LatLon coor) {
45 super(coor.lon(), coor.lat());
46 }
47
48 public double lat() {
49 return y;
50 }
51
52 public String latToString(CoordinateFormat d) {
53 switch(d) {
54 case DECIMAL_DEGREES: return cDdFormatter.format(y);
55 case DEGREES_MINUTES_SECONDS: return dms(y) + ((y < 0) ?
56 /* short symbol for South */ tr("S") :
57 /* short symbol for North */ 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) ?
70 /* short symbol for West */ tr("W") :
71 /* short symbol for East */ tr("E"));
72 default: return "ERR";
73 }
74 }
75
76 /**
77 * @return <code>true</code> if the other point has almost the same lat/lon
78 * values, only differing by no more than
79 * 1 / {@link org.openstreetmap.josm.data.projection.Projection#MAX_SERVER_PRECISION MAX_SERVER_PRECISION}.
80 */
81 public boolean equalsEpsilon(LatLon other) {
82 final double p = 1/Projection.MAX_SERVER_PRECISION;
83 return Math.abs(lat()-other.lat()) <= p && Math.abs(lon()-other.lon()) <= p;
84 }
85
86 /**
87 * @return <code>true</code>, if the coordinate is outside the world, compared
88 * by using lat/lon.
89 */
90 public boolean isOutSideWorld() {
91 Bounds b = Main.proj.getWorldBoundsLatLon();
92 return lat() < b.min.lat() || lat() > b.max.lat() ||
93 lon() < b.min.lon() || lon() > b.max.lon();
94 }
95
96 /**
97 * @return <code>true</code> if this is within the given bounding box.
98 */
99 public boolean isWithin(Bounds b) {
100 return lat() >= b.min.lat() && lat() <= b.max.lat() && lon() > b.min.lon() && lon() < b.max.lon();
101 }
102
103 /**
104 * Computes the distance between this lat/lon and another point on the earth.
105 * Uses spherical law of cosines formula, not Haversine.
106 * @param other the other point.
107 * @return distance in metres.
108 */
109 public double greatCircleDistance(LatLon other) {
110 return (Math.acos(
111 Math.sin(Math.toRadians(lat())) * Math.sin(Math.toRadians(other.lat())) +
112 Math.cos(Math.toRadians(lat()))*Math.cos(Math.toRadians(other.lat())) *
113 Math.cos(Math.toRadians(other.lon()-lon()))) * 6378135);
114 }
115
116 /**
117 * Returns the heading, in radians, that you have to use to get from
118 * this lat/lon to another.
119 *
120 * @param other the "destination" position
121 * @return heading
122 */
123 public double heading(LatLon other) {
124 double rv;
125 if (other.lat() == lat()) {
126 rv = (other.lon()>lon() ? Math.PI / 2 : Math.PI * 3 / 2);
127 } else {
128 rv = Math.atan((other.lon()-lon())/(other.lat()-lat()));
129 if (rv < 0) {
130 rv += Math.PI;
131 }
132 if (other.lon() < lon()) {
133 rv += Math.PI;
134 }
135 }
136 return rv;
137 }
138
139 /**
140 * Returns this lat/lon pair in human-readable format.
141 *
142 * @return String in the format "lat=1.23456°, lon=2.34567°"
143 */
144 public String toDisplayString() {
145 NumberFormat nf = NumberFormat.getInstance();
146 nf.setMaximumFractionDigits(5);
147 return "lat=" + nf.format(lat()) + "\u00B0, lon=" + nf.format(lon()) + "\u00B0";
148 }
149
150 public LatLon interpolate(LatLon ll2, double proportion) {
151 return new LatLon(this.lat() + proportion * (ll2.lat() - this.lat()),
152 this.lon() + proportion * (ll2.lon() - this.lon()));
153 }
154
155 public LatLon getCenter(LatLon ll2) {
156 return new LatLon((this.lat() + ll2.lat())/2.0, (this.lon() + ll2.lon())/2.0);
157 }
158
159 @Override public String toString() {
160 return "LatLon[lat="+lat()+",lon="+lon()+"]";
161 }
162}
Note: See TracBrowser for help on using the repository browser.