1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.data;
|
---|
3 |
|
---|
4 | import java.awt.geom.Rectangle2D;
|
---|
5 |
|
---|
6 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
7 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * This is a simple data class for "rectangular" areas of the world, given in
|
---|
11 | * lat/lon min/max values.
|
---|
12 | *
|
---|
13 | * @author imi
|
---|
14 | */
|
---|
15 | public class Bounds {
|
---|
16 | /**
|
---|
17 | * The minimum and maximum coordinates.
|
---|
18 | */
|
---|
19 | public LatLon min, max;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Construct bounds out of two points
|
---|
23 | */
|
---|
24 | public Bounds(LatLon min, LatLon max) {
|
---|
25 | this.min = min;
|
---|
26 | this.max = max;
|
---|
27 | }
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Construct bounds that span the whole world.
|
---|
31 | */
|
---|
32 | public Bounds() {
|
---|
33 | min = new LatLon(-Projection.MAX_LAT, -Projection.MAX_LON);
|
---|
34 | max = new LatLon(Projection.MAX_LAT, Projection.MAX_LON);
|
---|
35 | }
|
---|
36 |
|
---|
37 | @Override public String toString() {
|
---|
38 | return "Bounds["+min.lat()+","+min.lon()+","+max.lat()+","+max.lon()+"]";
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * @return Center of the bounding box.
|
---|
43 | */
|
---|
44 | public LatLon center() {
|
---|
45 | // FIXME: not sure whether this calculation is right; maybe there is some
|
---|
46 | // more complex calculation needed to get a center of a spherical
|
---|
47 | // dimension?
|
---|
48 | return new LatLon((min.lat()+max.lat())/2, (min.lon()+max.lon())/2);
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Extend the bounds if necessary to include the given point.
|
---|
53 | */
|
---|
54 | public void extend(LatLon ll) {
|
---|
55 | if (ll.lat() < min.lat() || ll.lon() < min.lon())
|
---|
56 | min = new LatLon(Math.min(ll.lat(), min.lat()), Math.min(ll.lon(), min.lon()));
|
---|
57 | if (ll.lat() > max.lat() || ll.lon() > max.lon())
|
---|
58 | max = new LatLon(Math.max(ll.lat(), max.lat()), Math.max(ll.lon(), max.lon()));
|
---|
59 | }
|
---|
60 | /**
|
---|
61 | * Is the given point within this bounds?
|
---|
62 | */
|
---|
63 | public boolean contains(LatLon ll) {
|
---|
64 | if (ll.lat() < min.lat() || ll.lon() < min.lon())
|
---|
65 | return false;
|
---|
66 | if (ll.lat() > max.lat() || ll.lon() > max.lon())
|
---|
67 | return false;
|
---|
68 | return true;
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Converts the lat/lon bounding box to an object of type Rectangle2D.Double
|
---|
73 | * @return the bounding box to Rectangle2D.Double
|
---|
74 | */
|
---|
75 | public Rectangle2D.Double asRect() {
|
---|
76 | return new Rectangle2D.Double(min.lon(), min.lat(), max.lon()-min.lon(), max.lat()-min.lat());
|
---|
77 | }
|
---|
78 |
|
---|
79 | }
|
---|