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

Last change on this file since 318 was 318, checked in by imi, 17 years ago
  • fixed source file encoding (UTF-8, not ISO8859!)
File size: 1.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 * Returns this lat/lon pair in human-readable format.
56 *
57 * @return String in the format "lat=1.23456°, lon=2.34567°"
58 */
59 public String toDisplayString() {
60 NumberFormat nf = NumberFormat.getInstance();
61 nf.setMaximumFractionDigits(5);
62 return "lat=" + nf.format(lat()) + "°, lon=" + nf.format(lon()) + "°";
63 }
64
65 @Override public String toString() {
66 return "LatLon[lat="+lat()+",lon="+lon()+"]";
67 }
68}
Note: See TracBrowser for help on using the repository browser.