source: josm/trunk/src/org/openstreetmap/josm/data/osm/BBox.java@ 12778

Last change on this file since 12778 was 12190, checked in by michael2402, 7 years ago

See #14794: More javadoc for data.osm package

  • Property svn:eol-style set to native
File size: 10.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import java.awt.geom.Rectangle2D;
5import java.util.Arrays;
6import java.util.Objects;
7
8import org.openstreetmap.josm.data.Bounds;
9import org.openstreetmap.josm.data.coor.ILatLon;
10import org.openstreetmap.josm.data.coor.LatLon;
11import org.openstreetmap.josm.data.coor.QuadTiling;
12import org.openstreetmap.josm.tools.Utils;
13
14/**
15 * A BBox represents an area in lat/lon space. It is used for the quad tree.
16 *
17 * In contrast to a {@link Bounds} object, a BBox can represent an invalid (empty) area.
18 */
19public class BBox {
20
21 protected double xmin = Double.POSITIVE_INFINITY;
22 protected double xmax = Double.NEGATIVE_INFINITY;
23 protected double ymin = Double.POSITIVE_INFINITY;
24 protected double ymax = Double.NEGATIVE_INFINITY;
25
26 /**
27 * Constructs a new (invalid) BBox
28 */
29 public BBox() {
30 // Nothing to do
31 }
32
33 /**
34 * Constructs a new {@code BBox} defined by a single point.
35 *
36 * @param x X coordinate
37 * @param y Y coordinate
38 * @since 6203
39 */
40 public BBox(final double x, final double y) {
41 add(x, y);
42 }
43
44 /**
45 * Constructs a new {@code BBox} defined by points <code>a</code> and <code>b</code>.
46 * Result is minimal BBox containing both points if they are both valid, else undefined
47 *
48 * @param a first point
49 * @param b second point
50 */
51 public BBox(LatLon a, LatLon b) {
52 this(a.lon(), a.lat(), b.lon(), b.lat());
53 }
54
55 /**
56 * Constructs a new {@code BBox} from another one.
57 *
58 * @param copy the BBox to copy
59 */
60 public BBox(BBox copy) {
61 this.xmin = copy.xmin;
62 this.xmax = copy.xmax;
63 this.ymin = copy.ymin;
64 this.ymax = copy.ymax;
65 }
66
67 /**
68 * Create minimal BBox so that {@code this.bounds(ax,ay)} and {@code this.bounds(bx,by)} will both return true
69 * @param ax left or right X value (-180 .. 180)
70 * @param ay top or bottom Y value (-90 .. 90)
71 * @param bx left or right X value (-180 .. 180)
72 * @param by top or bottom Y value (-90 .. 90)
73 */
74 public BBox(double ax, double ay, double bx, double by) {
75 if (!(Double.isNaN(ax) || Double.isNaN(ay) || Double.isNaN(bx) || Double.isNaN(by))) {
76 add(ax, ay);
77 add(bx, by);
78 }
79 // otherwise use default which is an invalid BBox
80 }
81
82 /**
83 * Create BBox for all nodes of the way with known coordinates.
84 * If no node has a known coordinate, an invalid BBox is returned.
85 * @param w the way
86 */
87 public BBox(Way w) {
88 w.getNodes().forEach(this::add);
89 }
90
91 /**
92 * Create BBox for a node. An invalid BBox is returned if the coordinates are not known.
93 * @param n the node
94 */
95 public BBox(Node n) {
96 this((ILatLon) n);
97 }
98
99 /**
100 * Create BBox for a given latlon. An invalid BBox is returned if the coordinates are not known.
101 * @param ll The lat lon position
102 */
103 public BBox(ILatLon ll) {
104 add(ll);
105 }
106
107 /**
108 * Add a point to an existing BBox. Extends this bbox if necessary so that this.bounds(c) will return true
109 * if c is a valid LatLon instance.
110 * Kept for binary compatibility
111 * @param c a LatLon point
112 */
113 public final void add(LatLon c) {
114 add((ILatLon) c);
115 }
116
117 /**
118 * Add a point to an existing BBox. Extends this bbox if necessary so that this.bounds(c) will return true
119 * if c is a valid LatLon instance.
120 * If it is invalid or <code>null</code>, this call is ignored.
121 * @param c a LatLon point.
122 */
123 public final void add(ILatLon c) {
124 if (c != null) {
125 add(c.lon(), c.lat());
126 }
127 }
128
129 /**
130 * Extends this bbox to include the point (x, y)
131 * @param x X coordinate
132 * @param y Y coordinate
133 */
134 public final void add(double x, double y) {
135 if (!Double.isNaN(x) && !Double.isNaN(y)) {
136 xmin = Math.min(xmin, x);
137 xmax = Math.max(xmax, x);
138 ymin = Math.min(ymin, y);
139 ymax = Math.max(ymax, y);
140 }
141 }
142
143 /**
144 * Extends this bbox to include the bbox other. Does nothing if other is not valid.
145 * @param other a bbox
146 */
147 public final void add(BBox other) {
148 if (other.isValid()) {
149 xmin = Math.min(xmin, other.xmin);
150 xmax = Math.max(xmax, other.xmax);
151 ymin = Math.min(ymin, other.ymin);
152 ymax = Math.max(ymax, other.ymax);
153 }
154 }
155
156 /**
157 * Extends this bbox to include the bbox of the primitive extended by extraSpace.
158 * @param primitive an OSM primitive
159 * @param extraSpace the value to extend the primitives bbox. Unit is in LatLon degrees.
160 */
161 public void addPrimitive(OsmPrimitive primitive, double extraSpace) {
162 BBox primBbox = primitive.getBBox();
163 add(primBbox.xmin - extraSpace, primBbox.ymin - extraSpace);
164 add(primBbox.xmax + extraSpace, primBbox.ymax + extraSpace);
165 }
166
167 /**
168 * Gets the height of the bbox.
169 * @return The difference between ymax and ymin. 0 for invalid bboxes.
170 */
171 public double height() {
172 if (isValid()) {
173 return ymax - ymin;
174 } else {
175 return 0;
176 }
177 }
178
179 /**
180 * Gets the width of the bbox.
181 * @return The difference between xmax and xmin. 0 for invalid bboxes.
182 */
183 public double width() {
184 if (isValid()) {
185 return xmax - xmin;
186 } else {
187 return 0;
188 }
189 }
190
191 /**
192 * Tests, whether the bbox {@code b} lies completely inside this bbox.
193 * @param b bounding box
194 * @return {@code true} if {@code b} lies completely inside this bbox
195 */
196 public boolean bounds(BBox b) {
197 return xmin <= b.xmin && xmax >= b.xmax
198 && ymin <= b.ymin && ymax >= b.ymax;
199 }
200
201 /**
202 * Tests, whether the Point {@code c} lies within the bbox.
203 * @param c point
204 * @return {@code true} if {@code c} lies within the bbox
205 */
206 public boolean bounds(LatLon c) {
207 return xmin <= c.lon() && xmax >= c.lon()
208 && ymin <= c.lat() && ymax >= c.lat();
209 }
210
211 /**
212 * Tests, whether two BBoxes intersect as an area.
213 * I.e. whether there exists a point that lies in both of them.
214 * @param b other bounding box
215 * @return {@code true} if this bbox intersects with the other
216 */
217 public boolean intersects(BBox b) {
218 return xmin <= b.xmax && xmax >= b.xmin
219 && ymin <= b.ymax && ymax >= b.ymin;
220 }
221
222 /**
223 * Returns the top-left point.
224 * @return The top-left point
225 */
226 public LatLon getTopLeft() {
227 return new LatLon(ymax, xmin);
228 }
229
230 /**
231 * Returns the latitude of top-left point.
232 * @return The latitude of top-left point
233 * @since 6203
234 */
235 public double getTopLeftLat() {
236 return ymax;
237 }
238
239 /**
240 * Returns the longitude of top-left point.
241 * @return The longitude of top-left point
242 * @since 6203
243 */
244 public double getTopLeftLon() {
245 return xmin;
246 }
247
248 /**
249 * Returns the bottom-right point.
250 * @return The bottom-right point
251 */
252 public LatLon getBottomRight() {
253 return new LatLon(ymin, xmax);
254 }
255
256 /**
257 * Returns the latitude of bottom-right point.
258 * @return The latitude of bottom-right point
259 * @since 6203
260 */
261 public double getBottomRightLat() {
262 return ymin;
263 }
264
265 /**
266 * Returns the longitude of bottom-right point.
267 * @return The longitude of bottom-right point
268 * @since 6203
269 */
270 public double getBottomRightLon() {
271 return xmax;
272 }
273
274 /**
275 * Gets the center of this BBox.
276 * @return The center.
277 */
278 public LatLon getCenter() {
279 return new LatLon(ymin + (ymax-ymin)/2.0, xmin + (xmax-xmin)/2.0);
280 }
281
282 byte getIndex(final int level) {
283
284 byte idx1 = QuadTiling.index(ymin, xmin, level);
285
286 final byte idx2 = QuadTiling.index(ymin, xmax, level);
287 if (idx1 == -1) idx1 = idx2;
288 else if (idx1 != idx2) return -1;
289
290 final byte idx3 = QuadTiling.index(ymax, xmin, level);
291 if (idx1 == -1) idx1 = idx3;
292 else if (idx1 != idx3) return -1;
293
294 final byte idx4 = QuadTiling.index(ymax, xmax, level);
295 if (idx1 == -1) idx1 = idx4;
296 else if (idx1 != idx4) return -1;
297
298 return idx1;
299 }
300
301 /**
302 * Converts the bounds to a rectangle
303 * @return The rectangle in east/north space.
304 */
305 public Rectangle2D toRectangle() {
306 return new Rectangle2D.Double(xmin, ymin, xmax - xmin, ymax - ymin);
307 }
308
309 @Override
310 public int hashCode() {
311 return Objects.hash(xmin, xmax, ymin, ymax);
312 }
313
314 @Override
315 public boolean equals(Object o) {
316 if (this == o) return true;
317 if (o == null || getClass() != o.getClass()) return false;
318 BBox b = (BBox) o;
319 return Double.compare(b.xmax, xmax) == 0 && Double.compare(b.ymax, ymax) == 0
320 && Double.compare(b.xmin, xmin) == 0 && Double.compare(b.ymin, ymin) == 0;
321 }
322
323 /**
324 * @return true if the bbox covers a part of the planets surface
325 * Height and width must be non-negative, but may (both) be 0.
326 * @since 11269
327 */
328 public boolean isValid() {
329 return xmin <= xmax && ymin <= ymax;
330 }
331
332 /**
333 * @return true if the bbox is avalid and covers a part of the planets surface
334 * @since 11269
335 */
336 public boolean isInWorld() {
337 return xmin >= -180.0 && xmax <= 180.0 && ymin >= -90.0 && ymax <= 90.0 && isValid();
338 }
339
340 @Override
341 public String toString() {
342 return "[ x: " + xmin + " -> " + xmax + ", y: " + ymin + " -> " + ymax + " ]";
343 }
344
345 /**
346 * Creates a CSV string for this bbox
347 * @param separator The separator to use
348 * @return A string
349 */
350 public String toStringCSV(String separator) {
351 return Utils.join(separator, Arrays.asList(
352 LatLon.cDdFormatter.format(xmin),
353 LatLon.cDdFormatter.format(ymin),
354 LatLon.cDdFormatter.format(xmax),
355 LatLon.cDdFormatter.format(ymax)));
356 }
357}
Note: See TracBrowser for help on using the repository browser.