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

Last change on this file since 608 was 608, checked in by framm, 16 years ago
  • new extrude mode allows creation of rectangular shapes
  • new AlignInRectangle function
  • additional information in status bar about length, heading, and angle of segment being drawn
  • helper line from last node to mouse cursor (disable with edit.helper-line=false)
File size: 2.8 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.data.coor;
3
4import org.openstreetmap.josm.data.Bounds;
5import org.openstreetmap.josm.data.projection.Projection;
6import java.text.NumberFormat;
7
8/**
9 * LatLon are unprojected latitude / longitude coordinates.
10 *
11 * This class is immutable.
12 *
13 * @author Imi
14 */
15public class LatLon extends Coordinate {
16
17 public LatLon(double lat, double lon) {
18 super(lon, lat);
19 }
20
21 public double lat() {
22 return y;
23 }
24
25 public double lon() {
26 return x;
27 }
28
29 /**
30 * @return <code>true</code>, if the other point has almost the same lat/lon
31 * values, only differ by no more than 1/Projection.MAX_SERVER_PRECISION.
32 */
33 public boolean equalsEpsilon(LatLon other) {
34 final double p = 1/Projection.MAX_SERVER_PRECISION;
35 return Math.abs(lat()-other.lat()) <= p && Math.abs(lon()-other.lon()) <= p;
36 }
37
38 /**
39 * @return <code>true</code>, if the coordinate is outside the world, compared
40 * by using lat/lon.
41 */
42 public boolean isOutSideWorld() {
43 return lat() < -Projection.MAX_LAT || lat() > Projection.MAX_LAT ||
44 lon() < -Projection.MAX_LON || lon() > Projection.MAX_LON;
45 }
46
47 /**
48 * @return <code>true</code> if this is within the given bounding box.
49 */
50 public boolean isWithin(Bounds b) {
51 return lat() >= b.min.lat() && lat() <= b.max.lat() && lon() > b.min.lon() && lon() < b.max.lon();
52 }
53
54 /**
55 * Computes the distance between this lat/lon and another point on the earth.
56 * Uses spherical law of cosines formula, not Haversine.
57 * @param other the other point.
58 * @return distance in metres.
59 */
60 public double greatCircleDistance(LatLon other) {
61 return (Math.acos(
62 Math.sin(Math.toRadians(lat())) * Math.sin(Math.toRadians(other.lat())) +
63 Math.cos(Math.toRadians(lat()))*Math.cos(Math.toRadians(other.lat())) *
64 Math.cos(Math.toRadians(other.lon()-lon()))) * 6378135);
65 }
66
67 /**
68 * Returns the heading, in radians, that you have to use to get from
69 * this lat/lon to another.
70 *
71 * @param other the "destination" position
72 * @return heading
73 */
74 public double heading(LatLon other) {
75 double rv;
76 if (other.lat() == lat()) {
77 rv = (other.lon()>lon() ? Math.PI / 2 : Math.PI * 3 / 2);
78 } else {
79 rv = Math.atan((other.lon()-lon())/(other.lat()-lat()));
80 if (rv < 0) rv += Math.PI;
81 if (other.lon() < lon()) rv += Math.PI;
82 }
83 return rv;
84 }
85
86 /**
87 * Returns this lat/lon pair in human-readable format.
88 *
89 * @return String in the format "lat=1.23456°, lon=2.34567°"
90 */
91 public String toDisplayString() {
92 NumberFormat nf = NumberFormat.getInstance();
93 nf.setMaximumFractionDigits(5);
94 return "lat=" + nf.format(lat()) + "°, lon=" + nf.format(lon()) + "°";
95 }
96
97 @Override public String toString() {
98 return "LatLon[lat="+lat()+",lon="+lon()+"]";
99 }
100}
Note: See TracBrowser for help on using the repository browser.