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