[3719] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[2450] | 2 | package org.openstreetmap.josm.data.osm;
|
---|
| 3 |
|
---|
[7193] | 4 | import java.awt.geom.Rectangle2D;
|
---|
[4420] | 5 | import java.util.Arrays;
|
---|
[10336] | 6 | import java.util.Objects;
|
---|
[2450] | 7 |
|
---|
[11272] | 8 | import org.openstreetmap.josm.data.Bounds;
|
---|
[12161] | 9 | import org.openstreetmap.josm.data.coor.ILatLon;
|
---|
[2450] | 10 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
[6171] | 11 | import org.openstreetmap.josm.data.coor.QuadTiling;
|
---|
[4420] | 12 | import org.openstreetmap.josm.tools.Utils;
|
---|
[2450] | 13 |
|
---|
[11272] | 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 | */
|
---|
[2450] | 19 | public class BBox {
|
---|
| 20 |
|
---|
[11237] | 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;
|
---|
[2450] | 25 |
|
---|
[6203] | 26 | /**
|
---|
[11237] | 27 | * Constructs a new (invalid) BBox
|
---|
| 28 | */
|
---|
[11277] | 29 | public BBox() {
|
---|
| 30 | // Nothing to do
|
---|
| 31 | }
|
---|
[11237] | 32 |
|
---|
| 33 | /**
|
---|
[6203] | 34 | * Constructs a new {@code BBox} defined by a single point.
|
---|
[7509] | 35 | *
|
---|
[6203] | 36 | * @param x X coordinate
|
---|
| 37 | * @param y Y coordinate
|
---|
| 38 | * @since 6203
|
---|
| 39 | */
|
---|
| 40 | public BBox(final double x, final double y) {
|
---|
[11272] | 41 | add(x, y);
|
---|
[2450] | 42 | }
|
---|
| 43 |
|
---|
[6203] | 44 | /**
|
---|
| 45 | * Constructs a new {@code BBox} defined by points <code>a</code> and <code>b</code>.
|
---|
[11269] | 46 | * Result is minimal BBox containing both points if they are both valid, else undefined
|
---|
[7509] | 47 | *
|
---|
[8470] | 48 | * @param a first point
|
---|
| 49 | * @param b second point
|
---|
[6203] | 50 | */
|
---|
[2450] | 51 | public BBox(LatLon a, LatLon b) {
|
---|
[6203] | 52 | this(a.lon(), a.lat(), b.lon(), b.lat());
|
---|
[2450] | 53 | }
|
---|
| 54 |
|
---|
[6203] | 55 | /**
|
---|
| 56 | * Constructs a new {@code BBox} from another one.
|
---|
[7509] | 57 | *
|
---|
[6203] | 58 | * @param copy the BBox to copy
|
---|
| 59 | */
|
---|
[3153] | 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 |
|
---|
[11269] | 67 | /**
|
---|
[13140] | 68 | * Creates bbox around the coordinate (x, y).
|
---|
| 69 | * Coordinate defines center of bbox, its edge will be 2*r.
|
---|
| 70 | *
|
---|
| 71 | * @param x X coordinate
|
---|
| 72 | * @param y Y coordinate
|
---|
| 73 | * @param r size
|
---|
| 74 | * @since 13140
|
---|
| 75 | */
|
---|
| 76 | public BBox(double x, double y, double r) {
|
---|
| 77 | this(x - r, y - r, x + r, y + r);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | /**
|
---|
| 81 | * Create minimal BBox so that {@code this.bounds(ax,ay)} and {@code this.bounds(bx,by)} will both return true
|
---|
[11269] | 82 | * @param ax left or right X value (-180 .. 180)
|
---|
| 83 | * @param ay top or bottom Y value (-90 .. 90)
|
---|
| 84 | * @param bx left or right X value (-180 .. 180)
|
---|
| 85 | * @param by top or bottom Y value (-90 .. 90)
|
---|
| 86 | */
|
---|
[10378] | 87 | public BBox(double ax, double ay, double bx, double by) {
|
---|
[11272] | 88 | if (!(Double.isNaN(ax) || Double.isNaN(ay) || Double.isNaN(bx) || Double.isNaN(by))) {
|
---|
| 89 | add(ax, ay);
|
---|
| 90 | add(bx, by);
|
---|
[11269] | 91 | }
|
---|
[11272] | 92 | // otherwise use default which is an invalid BBox
|
---|
[2450] | 93 | }
|
---|
| 94 |
|
---|
[11269] | 95 | /**
|
---|
| 96 | * Create BBox for all nodes of the way with known coordinates.
|
---|
| 97 | * If no node has a known coordinate, an invalid BBox is returned.
|
---|
| 98 | * @param w the way
|
---|
| 99 | */
|
---|
[13910] | 100 | public BBox(IWay<?> w) {
|
---|
[14026] | 101 | for (INode ll : w.getNodes()) {
|
---|
| 102 | add(ll);
|
---|
| 103 | }
|
---|
[2450] | 104 | }
|
---|
| 105 |
|
---|
[11269] | 106 | /**
|
---|
| 107 | * Create BBox for a node. An invalid BBox is returned if the coordinates are not known.
|
---|
| 108 | * @param n the node
|
---|
| 109 | */
|
---|
[13910] | 110 | public BBox(INode n) {
|
---|
[12161] | 111 | this((ILatLon) n);
|
---|
[3163] | 112 | }
|
---|
| 113 |
|
---|
[11269] | 114 | /**
|
---|
[12161] | 115 | * Create BBox for a given latlon. An invalid BBox is returned if the coordinates are not known.
|
---|
| 116 | * @param ll The lat lon position
|
---|
| 117 | */
|
---|
| 118 | public BBox(ILatLon ll) {
|
---|
| 119 | add(ll);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | /**
|
---|
[11269] | 123 | * Add a point to an existing BBox. Extends this bbox if necessary so that this.bounds(c) will return true
|
---|
| 124 | * if c is a valid LatLon instance.
|
---|
[12161] | 125 | * Kept for binary compatibility
|
---|
[11269] | 126 | * @param c a LatLon point
|
---|
| 127 | */
|
---|
[6890] | 128 | public final void add(LatLon c) {
|
---|
[12161] | 129 | add((ILatLon) c);
|
---|
[2450] | 130 | }
|
---|
| 131 |
|
---|
[3209] | 132 | /**
|
---|
[12161] | 133 | * Add a point to an existing BBox. Extends this bbox if necessary so that this.bounds(c) will return true
|
---|
| 134 | * if c is a valid LatLon instance.
|
---|
[12183] | 135 | * If it is invalid or <code>null</code>, this call is ignored.
|
---|
| 136 | * @param c a LatLon point.
|
---|
[12161] | 137 | */
|
---|
| 138 | public final void add(ILatLon c) {
|
---|
[12183] | 139 | if (c != null) {
|
---|
| 140 | add(c.lon(), c.lat());
|
---|
| 141 | }
|
---|
[12161] | 142 | }
|
---|
| 143 |
|
---|
| 144 | /**
|
---|
[3209] | 145 | * Extends this bbox to include the point (x, y)
|
---|
[9243] | 146 | * @param x X coordinate
|
---|
| 147 | * @param y Y coordinate
|
---|
[3209] | 148 | */
|
---|
[6890] | 149 | public final void add(double x, double y) {
|
---|
[11272] | 150 | if (!Double.isNaN(x) && !Double.isNaN(y)) {
|
---|
| 151 | xmin = Math.min(xmin, x);
|
---|
| 152 | xmax = Math.max(xmax, x);
|
---|
| 153 | ymin = Math.min(ymin, y);
|
---|
| 154 | ymax = Math.max(ymax, y);
|
---|
| 155 | }
|
---|
[2450] | 156 | }
|
---|
| 157 |
|
---|
[11269] | 158 | /**
|
---|
| 159 | * Extends this bbox to include the bbox other. Does nothing if other is not valid.
|
---|
| 160 | * @param other a bbox
|
---|
| 161 | */
|
---|
| 162 | public final void add(BBox other) {
|
---|
| 163 | if (other.isValid()) {
|
---|
| 164 | xmin = Math.min(xmin, other.xmin);
|
---|
| 165 | xmax = Math.max(xmax, other.xmax);
|
---|
| 166 | ymin = Math.min(ymin, other.ymin);
|
---|
| 167 | ymax = Math.max(ymax, other.ymax);
|
---|
| 168 | }
|
---|
[2982] | 169 | }
|
---|
| 170 |
|
---|
[11269] | 171 | /**
|
---|
| 172 | * Extends this bbox to include the bbox of the primitive extended by extraSpace.
|
---|
| 173 | * @param primitive an OSM primitive
|
---|
| 174 | * @param extraSpace the value to extend the primitives bbox. Unit is in LatLon degrees.
|
---|
| 175 | */
|
---|
[2450] | 176 | public void addPrimitive(OsmPrimitive primitive, double extraSpace) {
|
---|
| 177 | BBox primBbox = primitive.getBBox();
|
---|
| 178 | add(primBbox.xmin - extraSpace, primBbox.ymin - extraSpace);
|
---|
| 179 | add(primBbox.xmax + extraSpace, primBbox.ymax + extraSpace);
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[11272] | 182 | /**
|
---|
| 183 | * Gets the height of the bbox.
|
---|
| 184 | * @return The difference between ymax and ymin. 0 for invalid bboxes.
|
---|
| 185 | */
|
---|
[2450] | 186 | public double height() {
|
---|
[11272] | 187 | if (isValid()) {
|
---|
| 188 | return ymax - ymin;
|
---|
| 189 | } else {
|
---|
| 190 | return 0;
|
---|
| 191 | }
|
---|
[2450] | 192 | }
|
---|
| 193 |
|
---|
[11272] | 194 | /**
|
---|
| 195 | * Gets the width of the bbox.
|
---|
| 196 | * @return The difference between xmax and xmin. 0 for invalid bboxes.
|
---|
| 197 | */
|
---|
[2450] | 198 | public double width() {
|
---|
[11272] | 199 | if (isValid()) {
|
---|
| 200 | return xmax - xmin;
|
---|
| 201 | } else {
|
---|
| 202 | return 0;
|
---|
| 203 | }
|
---|
[2450] | 204 | }
|
---|
| 205 |
|
---|
[3209] | 206 | /**
|
---|
[9243] | 207 | * Tests, whether the bbox {@code b} lies completely inside this bbox.
|
---|
| 208 | * @param b bounding box
|
---|
| 209 | * @return {@code true} if {@code b} lies completely inside this bbox
|
---|
[3209] | 210 | */
|
---|
[2450] | 211 | public boolean bounds(BBox b) {
|
---|
[9976] | 212 | return xmin <= b.xmin && xmax >= b.xmax
|
---|
| 213 | && ymin <= b.ymin && ymax >= b.ymax;
|
---|
[2450] | 214 | }
|
---|
| 215 |
|
---|
[3209] | 216 | /**
|
---|
[9243] | 217 | * Tests, whether the Point {@code c} lies within the bbox.
|
---|
| 218 | * @param c point
|
---|
| 219 | * @return {@code true} if {@code c} lies within the bbox
|
---|
[3209] | 220 | */
|
---|
[2450] | 221 | public boolean bounds(LatLon c) {
|
---|
[9976] | 222 | return xmin <= c.lon() && xmax >= c.lon()
|
---|
| 223 | && ymin <= c.lat() && ymax >= c.lat();
|
---|
[2450] | 224 | }
|
---|
| 225 |
|
---|
[3209] | 226 | /**
|
---|
[9243] | 227 | * Tests, whether two BBoxes intersect as an area.
|
---|
[4319] | 228 | * I.e. whether there exists a point that lies in both of them.
|
---|
[9243] | 229 | * @param b other bounding box
|
---|
| 230 | * @return {@code true} if this bbox intersects with the other
|
---|
[3209] | 231 | */
|
---|
| 232 | public boolean intersects(BBox b) {
|
---|
[11272] | 233 | return xmin <= b.xmax && xmax >= b.xmin
|
---|
| 234 | && ymin <= b.ymax && ymax >= b.ymin;
|
---|
[2450] | 235 | }
|
---|
| 236 |
|
---|
[6203] | 237 | /**
|
---|
| 238 | * Returns the top-left point.
|
---|
| 239 | * @return The top-left point
|
---|
| 240 | */
|
---|
[2450] | 241 | public LatLon getTopLeft() {
|
---|
| 242 | return new LatLon(ymax, xmin);
|
---|
| 243 | }
|
---|
| 244 |
|
---|
[6203] | 245 | /**
|
---|
| 246 | * Returns the latitude of top-left point.
|
---|
| 247 | * @return The latitude of top-left point
|
---|
| 248 | * @since 6203
|
---|
| 249 | */
|
---|
| 250 | public double getTopLeftLat() {
|
---|
| 251 | return ymax;
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | /**
|
---|
| 255 | * Returns the longitude of top-left point.
|
---|
| 256 | * @return The longitude of top-left point
|
---|
| 257 | * @since 6203
|
---|
| 258 | */
|
---|
| 259 | public double getTopLeftLon() {
|
---|
| 260 | return xmin;
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | /**
|
---|
| 264 | * Returns the bottom-right point.
|
---|
| 265 | * @return The bottom-right point
|
---|
| 266 | */
|
---|
[2450] | 267 | public LatLon getBottomRight() {
|
---|
| 268 | return new LatLon(ymin, xmax);
|
---|
| 269 | }
|
---|
[6069] | 270 |
|
---|
[6203] | 271 | /**
|
---|
| 272 | * Returns the latitude of bottom-right point.
|
---|
| 273 | * @return The latitude of bottom-right point
|
---|
| 274 | * @since 6203
|
---|
| 275 | */
|
---|
| 276 | public double getBottomRightLat() {
|
---|
| 277 | return ymin;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | /**
|
---|
| 281 | * Returns the longitude of bottom-right point.
|
---|
| 282 | * @return The longitude of bottom-right point
|
---|
| 283 | * @since 6203
|
---|
| 284 | */
|
---|
| 285 | public double getBottomRightLon() {
|
---|
| 286 | return xmax;
|
---|
| 287 | }
|
---|
| 288 |
|
---|
[11272] | 289 | /**
|
---|
| 290 | * Gets the center of this BBox.
|
---|
| 291 | * @return The center.
|
---|
| 292 | */
|
---|
[4622] | 293 | public LatLon getCenter() {
|
---|
| 294 | return new LatLon(ymin + (ymax-ymin)/2.0, xmin + (xmax-xmin)/2.0);
|
---|
| 295 | }
|
---|
[2450] | 296 |
|
---|
[11237] | 297 | byte getIndex(final int level) {
|
---|
[6171] | 298 |
|
---|
[11237] | 299 | byte idx1 = QuadTiling.index(ymin, xmin, level);
|
---|
[6171] | 300 |
|
---|
[11237] | 301 | final byte idx2 = QuadTiling.index(ymin, xmax, level);
|
---|
[6171] | 302 | if (idx1 == -1) idx1 = idx2;
|
---|
| 303 | else if (idx1 != idx2) return -1;
|
---|
| 304 |
|
---|
[11237] | 305 | final byte idx3 = QuadTiling.index(ymax, xmin, level);
|
---|
[6171] | 306 | if (idx1 == -1) idx1 = idx3;
|
---|
| 307 | else if (idx1 != idx3) return -1;
|
---|
| 308 |
|
---|
[11237] | 309 | final byte idx4 = QuadTiling.index(ymax, xmax, level);
|
---|
[6171] | 310 | if (idx1 == -1) idx1 = idx4;
|
---|
| 311 | else if (idx1 != idx4) return -1;
|
---|
| 312 |
|
---|
| 313 | return idx1;
|
---|
| 314 | }
|
---|
[7509] | 315 |
|
---|
[12190] | 316 | /**
|
---|
| 317 | * Converts the bounds to a rectangle
|
---|
| 318 | * @return The rectangle in east/north space.
|
---|
| 319 | */
|
---|
[7193] | 320 | public Rectangle2D toRectangle() {
|
---|
| 321 | return new Rectangle2D.Double(xmin, ymin, xmax - xmin, ymax - ymin);
|
---|
| 322 | }
|
---|
[6171] | 323 |
|
---|
[2450] | 324 | @Override
|
---|
[2982] | 325 | public int hashCode() {
|
---|
[10336] | 326 | return Objects.hash(xmin, xmax, ymin, ymax);
|
---|
[2982] | 327 | }
|
---|
| 328 |
|
---|
| 329 | @Override
|
---|
| 330 | public boolean equals(Object o) {
|
---|
[10336] | 331 | if (this == o) return true;
|
---|
| 332 | if (o == null || getClass() != o.getClass()) return false;
|
---|
| 333 | BBox b = (BBox) o;
|
---|
| 334 | return Double.compare(b.xmax, xmax) == 0 && Double.compare(b.ymax, ymax) == 0
|
---|
| 335 | && Double.compare(b.xmin, xmin) == 0 && Double.compare(b.ymin, ymin) == 0;
|
---|
[2982] | 336 | }
|
---|
| 337 |
|
---|
[11269] | 338 | /**
|
---|
[15483] | 339 | * Check if bboxes are functionally equal
|
---|
| 340 | * @param other The other bbox to compare with
|
---|
| 341 | * @param maxDifference The maximum difference (in degrees) between the bboxes. May be null.
|
---|
| 342 | * @return true if they are functionally equivalent
|
---|
[15486] | 343 | * @since 15486
|
---|
[15483] | 344 | */
|
---|
[15486] | 345 | public boolean bboxIsFunctionallyEqual(BBox other, Double maxDifference) {
|
---|
[15483] | 346 | return bboxesAreFunctionallyEqual(this, other, maxDifference);
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | /**
|
---|
| 350 | * Check if bboxes are functionally equal
|
---|
| 351 | * @param bbox1 A bbox to compare with another bbox
|
---|
| 352 | * @param bbox2 The other bbox to compare with
|
---|
| 353 | * @param maxDifference The maximum difference (in degrees) between the bboxes. May be null.
|
---|
| 354 | * @return true if they are functionally equivalent
|
---|
| 355 | * @since 15483
|
---|
| 356 | */
|
---|
| 357 | public static boolean bboxesAreFunctionallyEqual(BBox bbox1, BBox bbox2, Double maxDifference) {
|
---|
| 358 | if (maxDifference == null) {
|
---|
| 359 | maxDifference = LatLon.MAX_SERVER_PRECISION;
|
---|
| 360 | }
|
---|
| 361 | return (bbox1 != null && bbox2 != null)
|
---|
| 362 | && (Math.abs(bbox1.getBottomRightLat() - bbox2.getBottomRightLat()) <= maxDifference
|
---|
| 363 | && Math.abs(bbox1.getBottomRightLon() - bbox2.getBottomRightLon()) <= maxDifference
|
---|
| 364 | && Math.abs(bbox1.getTopLeftLat() - bbox2.getTopLeftLat()) <= maxDifference
|
---|
| 365 | && Math.abs(bbox1.getTopLeftLon() - bbox2.getTopLeftLon()) <= maxDifference);
|
---|
| 366 | }
|
---|
| 367 |
|
---|
| 368 | /**
|
---|
[11269] | 369 | * @return true if the bbox covers a part of the planets surface
|
---|
| 370 | * Height and width must be non-negative, but may (both) be 0.
|
---|
[11272] | 371 | * @since 11269
|
---|
[11269] | 372 | */
|
---|
| 373 | public boolean isValid() {
|
---|
[11272] | 374 | return xmin <= xmax && ymin <= ymax;
|
---|
[11269] | 375 | }
|
---|
| 376 |
|
---|
| 377 | /**
|
---|
[11272] | 378 | * @return true if the bbox is avalid and covers a part of the planets surface
|
---|
| 379 | * @since 11269
|
---|
[11269] | 380 | */
|
---|
| 381 | public boolean isInWorld() {
|
---|
[11452] | 382 | return xmin >= -180.0 && xmax <= 180.0 && ymin >= -90.0 && ymax <= 90.0 && isValid();
|
---|
[11269] | 383 | }
|
---|
| 384 |
|
---|
[2982] | 385 | @Override
|
---|
[2450] | 386 | public String toString() {
|
---|
[8384] | 387 | return "[ x: " + xmin + " -> " + xmax + ", y: " + ymin + " -> " + ymax + " ]";
|
---|
[2450] | 388 | }
|
---|
[4420] | 389 |
|
---|
[12190] | 390 | /**
|
---|
| 391 | * Creates a CSV string for this bbox
|
---|
| 392 | * @param separator The separator to use
|
---|
| 393 | * @return A string
|
---|
| 394 | */
|
---|
[4420] | 395 | public String toStringCSV(String separator) {
|
---|
| 396 | return Utils.join(separator, Arrays.asList(
|
---|
| 397 | LatLon.cDdFormatter.format(xmin),
|
---|
| 398 | LatLon.cDdFormatter.format(ymin),
|
---|
| 399 | LatLon.cDdFormatter.format(xmax),
|
---|
| 400 | LatLon.cDdFormatter.format(ymax)));
|
---|
| 401 | }
|
---|
[2450] | 402 | }
|
---|