1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.data;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.geom.Rectangle2D;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * This is a simple data class for "rectangular" areas of the world, given in
|
---|
12 | * lat/lon min/max values.
|
---|
13 | *
|
---|
14 | * @author imi
|
---|
15 | */
|
---|
16 | public class Bounds {
|
---|
17 | /**
|
---|
18 | * The minimum and maximum coordinates.
|
---|
19 | */
|
---|
20 | private LatLon min, max;
|
---|
21 |
|
---|
22 | public LatLon getMin() {
|
---|
23 | return min;
|
---|
24 | }
|
---|
25 |
|
---|
26 | public LatLon getMax() {
|
---|
27 | return max;
|
---|
28 | }
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Construct bounds out of two points
|
---|
32 | */
|
---|
33 | public Bounds(LatLon min, LatLon max) {
|
---|
34 | this.min = min;
|
---|
35 | this.max = max;
|
---|
36 | }
|
---|
37 |
|
---|
38 | public Bounds(LatLon b) {
|
---|
39 | this.min = b;
|
---|
40 | this.max = b;
|
---|
41 | }
|
---|
42 |
|
---|
43 | public Bounds(double minlat, double minlon, double maxlat, double maxlon) {
|
---|
44 | this.min = new LatLon(minlat, minlon);
|
---|
45 | this.max = new LatLon(maxlat, maxlon);
|
---|
46 | }
|
---|
47 |
|
---|
48 | public Bounds(double [] coords) {
|
---|
49 | if (coords == null)
|
---|
50 | throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null", "coords"));
|
---|
51 | if (coords.length != 4)
|
---|
52 | throw new IllegalArgumentException(tr("Expected array of length 4, got {0}", coords.length));
|
---|
53 | this.min = new LatLon(coords[0], coords[1]);
|
---|
54 | this.max = new LatLon(coords[2], coords[3]);
|
---|
55 | }
|
---|
56 |
|
---|
57 | public Bounds(String asString, String separator) throws IllegalArgumentException {
|
---|
58 | if (asString == null)
|
---|
59 | throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null", "asString"));
|
---|
60 | String[] components = asString.split(separator);
|
---|
61 | if (components.length != 4)
|
---|
62 | throw new IllegalArgumentException(tr("Exactly four doubles excpected in string, got {0}", components.length));
|
---|
63 | double[] values = new double[4];
|
---|
64 | for (int i=0; i<4; i++) {
|
---|
65 | try {
|
---|
66 | values[i] = Double.parseDouble(components[i]);
|
---|
67 | } catch(NumberFormatException e) {
|
---|
68 | throw new IllegalArgumentException(tr("Illegal double value ''{0}''", components[i]));
|
---|
69 | }
|
---|
70 | }
|
---|
71 | if (!LatLon.isValidLat(values[0]))
|
---|
72 | throw new IllegalArgumentException(tr("Illegal latitude value ''{0}''", values[0]));
|
---|
73 | if (!LatLon.isValidLon(values[1]))
|
---|
74 | throw new IllegalArgumentException(tr("Illegal longitude value ''{0}''", values[1]));
|
---|
75 | if (!LatLon.isValidLat(values[2]))
|
---|
76 | throw new IllegalArgumentException(tr("Illegal latitude value ''{0}''", values[2]));
|
---|
77 | if (!LatLon.isValidLon(values[3]))
|
---|
78 | throw new IllegalArgumentException(tr("Illegal latitude value ''{0}''", values[3]));
|
---|
79 |
|
---|
80 | this.min = new LatLon(values[0], values[1]);
|
---|
81 | this.max = new LatLon(values[2], values[3]);
|
---|
82 | }
|
---|
83 |
|
---|
84 | public Bounds(Bounds other) {
|
---|
85 | this.min = new LatLon(other.min);
|
---|
86 | this.max = new LatLon(other.max);
|
---|
87 | }
|
---|
88 |
|
---|
89 | public Bounds(Rectangle2D rect) {
|
---|
90 | this.min = new LatLon(rect.getMinY(), rect.getMinX());
|
---|
91 | this.max = new LatLon(rect.getMaxY(), rect.getMaxX());
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Creates new bounds around a coordinate pair <code>center</code>. The
|
---|
96 | * new bounds shall have an extension in latitude direction of <code>latExtent</code>,
|
---|
97 | * and in longitude direction of <code>lonExtent</code>.
|
---|
98 | *
|
---|
99 | * @param center the center coordinate pair. Must not be null.
|
---|
100 | * @param latExtent the latitude extent. > 0 required.
|
---|
101 | * @param lonExtent the longitude extent. > 0 required.
|
---|
102 | * @throws IllegalArgumentException thrown if center is null
|
---|
103 | * @throws IllegalArgumentException thrown if latExtent <= 0
|
---|
104 | * @throws IllegalArgumentException thrown if lonExtent <= 0
|
---|
105 | */
|
---|
106 | public Bounds(LatLon center, double latExtent, double lonExtent) {
|
---|
107 | if (center == null)
|
---|
108 | throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null", "center"));
|
---|
109 | if (latExtent <= 0.0)
|
---|
110 | throw new IllegalArgumentException(tr("Parameter ''{0}'' > 0.0 exptected, got {1}", "latExtent", latExtent));
|
---|
111 | if (lonExtent <= 0.0)
|
---|
112 | throw new IllegalArgumentException(tr("Parameter ''{0}'' > 0.0 exptected, got {1}", "lonExtent", lonExtent));
|
---|
113 |
|
---|
114 | this.min = new LatLon(
|
---|
115 | center.lat() - latExtent / 2,
|
---|
116 | center.lon() - lonExtent / 2
|
---|
117 | );
|
---|
118 | this.max = new LatLon(
|
---|
119 | center.lat() + latExtent / 2,
|
---|
120 | center.lon() + lonExtent / 2
|
---|
121 | );
|
---|
122 | }
|
---|
123 |
|
---|
124 | @Override public String toString() {
|
---|
125 | return "Bounds["+min.lat()+","+min.lon()+","+max.lat()+","+max.lon()+"]";
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * @return Center of the bounding box.
|
---|
130 | */
|
---|
131 | public LatLon getCenter()
|
---|
132 | {
|
---|
133 | return min.getCenter(max);
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Extend the bounds if necessary to include the given point.
|
---|
138 | */
|
---|
139 | public void extend(LatLon ll) {
|
---|
140 | if (ll.lat() < min.lat() || ll.lon() < min.lon()) {
|
---|
141 | min = new LatLon(Math.min(ll.lat(), min.lat()), Math.min(ll.lon(), min.lon()));
|
---|
142 | }
|
---|
143 | if (ll.lat() > max.lat() || ll.lon() > max.lon()) {
|
---|
144 | max = new LatLon(Math.max(ll.lat(), max.lat()), Math.max(ll.lon(), max.lon()));
|
---|
145 | }
|
---|
146 | }
|
---|
147 | /**
|
---|
148 | * Is the given point within this bounds?
|
---|
149 | */
|
---|
150 | public boolean contains(LatLon ll) {
|
---|
151 | if (ll.lat() < min.lat() || ll.lon() < min.lon())
|
---|
152 | return false;
|
---|
153 | if (ll.lat() > max.lat() || ll.lon() > max.lon())
|
---|
154 | return false;
|
---|
155 | return true;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Converts the lat/lon bounding box to an object of type Rectangle2D.Double
|
---|
160 | * @return the bounding box to Rectangle2D.Double
|
---|
161 | */
|
---|
162 | public Rectangle2D.Double asRect() {
|
---|
163 | return new Rectangle2D.Double(min.lon(), min.lat(), max.lon()-min.lon(), max.lat()-min.lat());
|
---|
164 | }
|
---|
165 |
|
---|
166 | public double getArea() {
|
---|
167 | return (max.lon() - min.lon()) * (max.lat() - min.lat());
|
---|
168 | }
|
---|
169 |
|
---|
170 | public String encodeAsString(String separator) {
|
---|
171 | StringBuffer sb = new StringBuffer();
|
---|
172 | sb.append(min.lat()).append(separator).append(min.lon())
|
---|
173 | .append(separator).append(max.lat()).append(separator)
|
---|
174 | .append(max.lon());
|
---|
175 | return sb.toString();
|
---|
176 | }
|
---|
177 |
|
---|
178 | @Override
|
---|
179 | public int hashCode() {
|
---|
180 | final int prime = 31;
|
---|
181 | int result = 1;
|
---|
182 | result = prime * result + ((max == null) ? 0 : max.hashCode());
|
---|
183 | result = prime * result + ((min == null) ? 0 : min.hashCode());
|
---|
184 | return result;
|
---|
185 | }
|
---|
186 |
|
---|
187 | @Override
|
---|
188 | public boolean equals(Object obj) {
|
---|
189 | if (this == obj)
|
---|
190 | return true;
|
---|
191 | if (obj == null)
|
---|
192 | return false;
|
---|
193 | if (getClass() != obj.getClass())
|
---|
194 | return false;
|
---|
195 | Bounds other = (Bounds) obj;
|
---|
196 | if (max == null) {
|
---|
197 | if (other.max != null)
|
---|
198 | return false;
|
---|
199 | } else if (!max.equals(other.max))
|
---|
200 | return false;
|
---|
201 | if (min == null) {
|
---|
202 | if (other.min != null)
|
---|
203 | return false;
|
---|
204 | } else if (!min.equals(other.min))
|
---|
205 | return false;
|
---|
206 | return true;
|
---|
207 | }
|
---|
208 |
|
---|
209 | }
|
---|