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 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 int distance(LatLon other) {
|
---|
61 | return (int) (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 this lat/lon pair in human-readable format.
|
---|
69 | *
|
---|
70 | * @return String in the format "lat=1.23456°, lon=2.34567°"
|
---|
71 | */
|
---|
72 | public String toDisplayString() {
|
---|
73 | NumberFormat nf = NumberFormat.getInstance();
|
---|
74 | nf.setMaximumFractionDigits(5);
|
---|
75 | return "lat=" + nf.format(lat()) + "°, lon=" + nf.format(lon()) + "°";
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Override public String toString() {
|
---|
79 | return "LatLon[lat="+lat()+",lon="+lon()+"]";
|
---|
80 | }
|
---|
81 | }
|
---|