1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.data;
|
---|
3 |
|
---|
4 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
5 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
6 |
|
---|
7 | /**
|
---|
8 | * This is a simple data class for "rectangular" areas of the world, given in
|
---|
9 | * lat/lon min/max values.
|
---|
10 | *
|
---|
11 | * Do not confuse this with "Area", which is an OSM-primitive for a vector of nodes,
|
---|
12 | * describing some area (like a sea).
|
---|
13 | *
|
---|
14 | * @author imi
|
---|
15 | */
|
---|
16 | public class Bounds {
|
---|
17 | /**
|
---|
18 | * The minimum and maximum coordinates.
|
---|
19 | */
|
---|
20 | public LatLon min, max;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Construct bounds out of two points
|
---|
24 | */
|
---|
25 | public Bounds(LatLon min, LatLon max) {
|
---|
26 | this.min = min;
|
---|
27 | this.max = max;
|
---|
28 | }
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Construct bounds that span the whole world.
|
---|
32 | */
|
---|
33 | public Bounds() {
|
---|
34 | min = new LatLon(-Projection.MAX_LAT, -Projection.MAX_LON);
|
---|
35 | max = new LatLon(Projection.MAX_LAT, Projection.MAX_LON);
|
---|
36 | }
|
---|
37 |
|
---|
38 | @Override public String toString() {
|
---|
39 | return "Bounds["+min.lat()+","+min.lon()+","+max.lat()+","+max.lon()+"]";
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * @return Center of the bounding box.
|
---|
44 | */
|
---|
45 | public LatLon center() {
|
---|
46 | // not sure, whether this calculation is right.. maybe there is some
|
---|
47 | // more complex calculation needed to get a center of a spherical
|
---|
48 | // dimension?
|
---|
49 | return new LatLon((min.lat()+max.lat())/2, (min.lon()+max.lon())/2);
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Extend the bounds if necessary to include the given point.
|
---|
54 | */
|
---|
55 | public void extend(LatLon ll) {
|
---|
56 | if (ll.lat() < min.lat() || ll.lon() < min.lon())
|
---|
57 | min = new LatLon(Math.min(ll.lat(), min.lat()), Math.min(ll.lon(), min.lon()));
|
---|
58 | if (ll.lat() > max.lat() || ll.lon() > max.lon())
|
---|
59 | max = new LatLon(Math.max(ll.lat(), max.lat()), Math.max(ll.lon(), max.lon()));
|
---|
60 | }
|
---|
61 | }
|
---|