1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.data.coor;
|
---|
3 |
|
---|
4 | import org.openstreetmap.josm.data.Bounds;
|
---|
5 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
6 | import java.text.NumberFormat;
|
---|
7 |
|
---|
8 | /**
|
---|
9 | * LatLon are unprojected latitude / longitude coordinates.
|
---|
10 | *
|
---|
11 | * This class is immutable.
|
---|
12 | *
|
---|
13 | * @author Imi
|
---|
14 | */
|
---|
15 | public 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 differing by no more than
|
---|
32 | * 1 / {@link org.openstreetmap.josm.data.projection.Projection#MAX_SERVER_PRECISION MAX_SERVER_PRECISION}.
|
---|
33 | */
|
---|
34 | public boolean equalsEpsilon(LatLon other) {
|
---|
35 | final double p = 1/Projection.MAX_SERVER_PRECISION;
|
---|
36 | return Math.abs(lat()-other.lat()) <= p && Math.abs(lon()-other.lon()) <= p;
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * @return <code>true</code>, if the coordinate is outside the world, compared
|
---|
41 | * by using lat/lon.
|
---|
42 | */
|
---|
43 | public boolean isOutSideWorld() {
|
---|
44 | return lat() < -Projection.MAX_LAT || lat() > Projection.MAX_LAT ||
|
---|
45 | lon() < -Projection.MAX_LON || lon() > Projection.MAX_LON;
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * @return <code>true</code> if this is within the given bounding box.
|
---|
50 | */
|
---|
51 | public boolean isWithin(Bounds b) {
|
---|
52 | return lat() >= b.min.lat() && lat() <= b.max.lat() && lon() > b.min.lon() && lon() < b.max.lon();
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Computes the distance between this lat/lon and another point on the earth.
|
---|
57 | * Uses spherical law of cosines formula, not Haversine.
|
---|
58 | * @param other the other point.
|
---|
59 | * @return distance in metres.
|
---|
60 | */
|
---|
61 | public double greatCircleDistance(LatLon other) {
|
---|
62 | return (Math.acos(
|
---|
63 | Math.sin(Math.toRadians(lat())) * Math.sin(Math.toRadians(other.lat())) +
|
---|
64 | Math.cos(Math.toRadians(lat()))*Math.cos(Math.toRadians(other.lat())) *
|
---|
65 | Math.cos(Math.toRadians(other.lon()-lon()))) * 6378135);
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Returns the heading, in radians, that you have to use to get from
|
---|
70 | * this lat/lon to another.
|
---|
71 | *
|
---|
72 | * @param other the "destination" position
|
---|
73 | * @return heading
|
---|
74 | */
|
---|
75 | public double heading(LatLon other) {
|
---|
76 | double rv;
|
---|
77 | if (other.lat() == lat()) {
|
---|
78 | rv = (other.lon()>lon() ? Math.PI / 2 : Math.PI * 3 / 2);
|
---|
79 | } else {
|
---|
80 | rv = Math.atan((other.lon()-lon())/(other.lat()-lat()));
|
---|
81 | if (rv < 0) rv += Math.PI;
|
---|
82 | if (other.lon() < lon()) rv += Math.PI;
|
---|
83 | }
|
---|
84 | return rv;
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Returns this lat/lon pair in human-readable format.
|
---|
89 | *
|
---|
90 | * @return String in the format "lat=1.23456°, lon=2.34567°"
|
---|
91 | */
|
---|
92 | public String toDisplayString() {
|
---|
93 | NumberFormat nf = NumberFormat.getInstance();
|
---|
94 | nf.setMaximumFractionDigits(5);
|
---|
95 | return "lat=" + nf.format(lat()) + "°, lon=" + nf.format(lon()) + "°";
|
---|
96 | }
|
---|
97 |
|
---|
98 | @Override public String toString() {
|
---|
99 | return "LatLon[lat="+lat()+",lon="+lon()+"]";
|
---|
100 | }
|
---|
101 | }
|
---|