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

Last change on this file since 12167 was 12161, checked in by michael2402, 7 years ago

See #13415: Add the ILatLon interface, unify handling of Nodes and CachedLatLon

  • Property svn:eol-style set to native
File size: 9.7 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 * @param c a LatLon point
121 */
122 public final void add(ILatLon c) {
123 add(c.lon(), c.lat());
124 }
125
126 /**
127 * Extends this bbox to include the point (x, y)
128 * @param x X coordinate
129 * @param y Y coordinate
130 */
131 public final void add(double x, double y) {
132 if (!Double.isNaN(x) && !Double.isNaN(y)) {
133 xmin = Math.min(xmin, x);
134 xmax = Math.max(xmax, x);
135 ymin = Math.min(ymin, y);
136 ymax = Math.max(ymax, y);
137 }
138 }
139
140 /**
141 * Extends this bbox to include the bbox other. Does nothing if other is not valid.
142 * @param other a bbox
143 */
144 public final void add(BBox other) {
145 if (other.isValid()) {
146 xmin = Math.min(xmin, other.xmin);
147 xmax = Math.max(xmax, other.xmax);
148 ymin = Math.min(ymin, other.ymin);
149 ymax = Math.max(ymax, other.ymax);
150 }
151 }
152
153 /**
154 * Extends this bbox to include the bbox of the primitive extended by extraSpace.
155 * @param primitive an OSM primitive
156 * @param extraSpace the value to extend the primitives bbox. Unit is in LatLon degrees.
157 */
158 public void addPrimitive(OsmPrimitive primitive, double extraSpace) {
159 BBox primBbox = primitive.getBBox();
160 add(primBbox.xmin - extraSpace, primBbox.ymin - extraSpace);
161 add(primBbox.xmax + extraSpace, primBbox.ymax + extraSpace);
162 }
163
164 /**
165 * Gets the height of the bbox.
166 * @return The difference between ymax and ymin. 0 for invalid bboxes.
167 */
168 public double height() {
169 if (isValid()) {
170 return ymax - ymin;
171 } else {
172 return 0;
173 }
174 }
175
176 /**
177 * Gets the width of the bbox.
178 * @return The difference between xmax and xmin. 0 for invalid bboxes.
179 */
180 public double width() {
181 if (isValid()) {
182 return xmax - xmin;
183 } else {
184 return 0;
185 }
186 }
187
188 /**
189 * Tests, whether the bbox {@code b} lies completely inside this bbox.
190 * @param b bounding box
191 * @return {@code true} if {@code b} lies completely inside this bbox
192 */
193 public boolean bounds(BBox b) {
194 return xmin <= b.xmin && xmax >= b.xmax
195 && ymin <= b.ymin && ymax >= b.ymax;
196 }
197
198 /**
199 * Tests, whether the Point {@code c} lies within the bbox.
200 * @param c point
201 * @return {@code true} if {@code c} lies within the bbox
202 */
203 public boolean bounds(LatLon c) {
204 return xmin <= c.lon() && xmax >= c.lon()
205 && ymin <= c.lat() && ymax >= c.lat();
206 }
207
208 /**
209 * Tests, whether two BBoxes intersect as an area.
210 * I.e. whether there exists a point that lies in both of them.
211 * @param b other bounding box
212 * @return {@code true} if this bbox intersects with the other
213 */
214 public boolean intersects(BBox b) {
215 return xmin <= b.xmax && xmax >= b.xmin
216 && ymin <= b.ymax && ymax >= b.ymin;
217 }
218
219 /**
220 * Returns the top-left point.
221 * @return The top-left point
222 */
223 public LatLon getTopLeft() {
224 return new LatLon(ymax, xmin);
225 }
226
227 /**
228 * Returns the latitude of top-left point.
229 * @return The latitude of top-left point
230 * @since 6203
231 */
232 public double getTopLeftLat() {
233 return ymax;
234 }
235
236 /**
237 * Returns the longitude of top-left point.
238 * @return The longitude of top-left point
239 * @since 6203
240 */
241 public double getTopLeftLon() {
242 return xmin;
243 }
244
245 /**
246 * Returns the bottom-right point.
247 * @return The bottom-right point
248 */
249 public LatLon getBottomRight() {
250 return new LatLon(ymin, xmax);
251 }
252
253 /**
254 * Returns the latitude of bottom-right point.
255 * @return The latitude of bottom-right point
256 * @since 6203
257 */
258 public double getBottomRightLat() {
259 return ymin;
260 }
261
262 /**
263 * Returns the longitude of bottom-right point.
264 * @return The longitude of bottom-right point
265 * @since 6203
266 */
267 public double getBottomRightLon() {
268 return xmax;
269 }
270
271 /**
272 * Gets the center of this BBox.
273 * @return The center.
274 */
275 public LatLon getCenter() {
276 return new LatLon(ymin + (ymax-ymin)/2.0, xmin + (xmax-xmin)/2.0);
277 }
278
279 byte getIndex(final int level) {
280
281 byte idx1 = QuadTiling.index(ymin, xmin, level);
282
283 final byte idx2 = QuadTiling.index(ymin, xmax, level);
284 if (idx1 == -1) idx1 = idx2;
285 else if (idx1 != idx2) return -1;
286
287 final byte idx3 = QuadTiling.index(ymax, xmin, level);
288 if (idx1 == -1) idx1 = idx3;
289 else if (idx1 != idx3) return -1;
290
291 final byte idx4 = QuadTiling.index(ymax, xmax, level);
292 if (idx1 == -1) idx1 = idx4;
293 else if (idx1 != idx4) return -1;
294
295 return idx1;
296 }
297
298 public Rectangle2D toRectangle() {
299 return new Rectangle2D.Double(xmin, ymin, xmax - xmin, ymax - ymin);
300 }
301
302 @Override
303 public int hashCode() {
304 return Objects.hash(xmin, xmax, ymin, ymax);
305 }
306
307 @Override
308 public boolean equals(Object o) {
309 if (this == o) return true;
310 if (o == null || getClass() != o.getClass()) return false;
311 BBox b = (BBox) o;
312 return Double.compare(b.xmax, xmax) == 0 && Double.compare(b.ymax, ymax) == 0
313 && Double.compare(b.xmin, xmin) == 0 && Double.compare(b.ymin, ymin) == 0;
314 }
315
316 /**
317 * @return true if the bbox covers a part of the planets surface
318 * Height and width must be non-negative, but may (both) be 0.
319 * @since 11269
320 */
321 public boolean isValid() {
322 return xmin <= xmax && ymin <= ymax;
323 }
324
325 /**
326 * @return true if the bbox is avalid and covers a part of the planets surface
327 * @since 11269
328 */
329 public boolean isInWorld() {
330 return xmin >= -180.0 && xmax <= 180.0 && ymin >= -90.0 && ymax <= 90.0 && isValid();
331 }
332
333 @Override
334 public String toString() {
335 return "[ x: " + xmin + " -> " + xmax + ", y: " + ymin + " -> " + ymax + " ]";
336 }
337
338 public String toStringCSV(String separator) {
339 return Utils.join(separator, Arrays.asList(
340 LatLon.cDdFormatter.format(xmin),
341 LatLon.cDdFormatter.format(ymin),
342 LatLon.cDdFormatter.format(xmax),
343 LatLon.cDdFormatter.format(ymax)));
344 }
345}
Note: See TracBrowser for help on using the repository browser.