[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) {
|
---|
[12161] | 101 | w.getNodes().forEach(this::add);
|
---|
[2450] | 102 | }
|
---|
| 103 |
|
---|
[11269] | 104 | /**
|
---|
| 105 | * Create BBox for a node. An invalid BBox is returned if the coordinates are not known.
|
---|
| 106 | * @param n the node
|
---|
| 107 | */
|
---|
[13910] | 108 | public BBox(INode n) {
|
---|
[12161] | 109 | this((ILatLon) n);
|
---|
[3163] | 110 | }
|
---|
| 111 |
|
---|
[11269] | 112 | /**
|
---|
[12161] | 113 | * Create BBox for a given latlon. An invalid BBox is returned if the coordinates are not known.
|
---|
| 114 | * @param ll The lat lon position
|
---|
| 115 | */
|
---|
| 116 | public BBox(ILatLon ll) {
|
---|
| 117 | add(ll);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | /**
|
---|
[11269] | 121 | * Add a point to an existing BBox. Extends this bbox if necessary so that this.bounds(c) will return true
|
---|
| 122 | * if c is a valid LatLon instance.
|
---|
[12161] | 123 | * Kept for binary compatibility
|
---|
[11269] | 124 | * @param c a LatLon point
|
---|
| 125 | */
|
---|
[6890] | 126 | public final void add(LatLon c) {
|
---|
[12161] | 127 | add((ILatLon) c);
|
---|
[2450] | 128 | }
|
---|
| 129 |
|
---|
[3209] | 130 | /**
|
---|
[12161] | 131 | * Add a point to an existing BBox. Extends this bbox if necessary so that this.bounds(c) will return true
|
---|
| 132 | * if c is a valid LatLon instance.
|
---|
[12183] | 133 | * If it is invalid or <code>null</code>, this call is ignored.
|
---|
| 134 | * @param c a LatLon point.
|
---|
[12161] | 135 | */
|
---|
| 136 | public final void add(ILatLon c) {
|
---|
[12183] | 137 | if (c != null) {
|
---|
| 138 | add(c.lon(), c.lat());
|
---|
| 139 | }
|
---|
[12161] | 140 | }
|
---|
| 141 |
|
---|
| 142 | /**
|
---|
[3209] | 143 | * Extends this bbox to include the point (x, y)
|
---|
[9243] | 144 | * @param x X coordinate
|
---|
| 145 | * @param y Y coordinate
|
---|
[3209] | 146 | */
|
---|
[6890] | 147 | public final void add(double x, double y) {
|
---|
[11272] | 148 | if (!Double.isNaN(x) && !Double.isNaN(y)) {
|
---|
| 149 | xmin = Math.min(xmin, x);
|
---|
| 150 | xmax = Math.max(xmax, x);
|
---|
| 151 | ymin = Math.min(ymin, y);
|
---|
| 152 | ymax = Math.max(ymax, y);
|
---|
| 153 | }
|
---|
[2450] | 154 | }
|
---|
| 155 |
|
---|
[11269] | 156 | /**
|
---|
| 157 | * Extends this bbox to include the bbox other. Does nothing if other is not valid.
|
---|
| 158 | * @param other a bbox
|
---|
| 159 | */
|
---|
| 160 | public final void add(BBox other) {
|
---|
| 161 | if (other.isValid()) {
|
---|
| 162 | xmin = Math.min(xmin, other.xmin);
|
---|
| 163 | xmax = Math.max(xmax, other.xmax);
|
---|
| 164 | ymin = Math.min(ymin, other.ymin);
|
---|
| 165 | ymax = Math.max(ymax, other.ymax);
|
---|
| 166 | }
|
---|
[2982] | 167 | }
|
---|
| 168 |
|
---|
[11269] | 169 | /**
|
---|
| 170 | * Extends this bbox to include the bbox of the primitive extended by extraSpace.
|
---|
| 171 | * @param primitive an OSM primitive
|
---|
| 172 | * @param extraSpace the value to extend the primitives bbox. Unit is in LatLon degrees.
|
---|
| 173 | */
|
---|
[2450] | 174 | public void addPrimitive(OsmPrimitive primitive, double extraSpace) {
|
---|
| 175 | BBox primBbox = primitive.getBBox();
|
---|
| 176 | add(primBbox.xmin - extraSpace, primBbox.ymin - extraSpace);
|
---|
| 177 | add(primBbox.xmax + extraSpace, primBbox.ymax + extraSpace);
|
---|
| 178 | }
|
---|
| 179 |
|
---|
[11272] | 180 | /**
|
---|
| 181 | * Gets the height of the bbox.
|
---|
| 182 | * @return The difference between ymax and ymin. 0 for invalid bboxes.
|
---|
| 183 | */
|
---|
[2450] | 184 | public double height() {
|
---|
[11272] | 185 | if (isValid()) {
|
---|
| 186 | return ymax - ymin;
|
---|
| 187 | } else {
|
---|
| 188 | return 0;
|
---|
| 189 | }
|
---|
[2450] | 190 | }
|
---|
| 191 |
|
---|
[11272] | 192 | /**
|
---|
| 193 | * Gets the width of the bbox.
|
---|
| 194 | * @return The difference between xmax and xmin. 0 for invalid bboxes.
|
---|
| 195 | */
|
---|
[2450] | 196 | public double width() {
|
---|
[11272] | 197 | if (isValid()) {
|
---|
| 198 | return xmax - xmin;
|
---|
| 199 | } else {
|
---|
| 200 | return 0;
|
---|
| 201 | }
|
---|
[2450] | 202 | }
|
---|
| 203 |
|
---|
[3209] | 204 | /**
|
---|
[9243] | 205 | * Tests, whether the bbox {@code b} lies completely inside this bbox.
|
---|
| 206 | * @param b bounding box
|
---|
| 207 | * @return {@code true} if {@code b} lies completely inside this bbox
|
---|
[3209] | 208 | */
|
---|
[2450] | 209 | public boolean bounds(BBox b) {
|
---|
[9976] | 210 | return xmin <= b.xmin && xmax >= b.xmax
|
---|
| 211 | && ymin <= b.ymin && ymax >= b.ymax;
|
---|
[2450] | 212 | }
|
---|
| 213 |
|
---|
[3209] | 214 | /**
|
---|
[9243] | 215 | * Tests, whether the Point {@code c} lies within the bbox.
|
---|
| 216 | * @param c point
|
---|
| 217 | * @return {@code true} if {@code c} lies within the bbox
|
---|
[3209] | 218 | */
|
---|
[2450] | 219 | public boolean bounds(LatLon c) {
|
---|
[9976] | 220 | return xmin <= c.lon() && xmax >= c.lon()
|
---|
| 221 | && ymin <= c.lat() && ymax >= c.lat();
|
---|
[2450] | 222 | }
|
---|
| 223 |
|
---|
[3209] | 224 | /**
|
---|
[9243] | 225 | * Tests, whether two BBoxes intersect as an area.
|
---|
[4319] | 226 | * I.e. whether there exists a point that lies in both of them.
|
---|
[9243] | 227 | * @param b other bounding box
|
---|
| 228 | * @return {@code true} if this bbox intersects with the other
|
---|
[3209] | 229 | */
|
---|
| 230 | public boolean intersects(BBox b) {
|
---|
[11272] | 231 | return xmin <= b.xmax && xmax >= b.xmin
|
---|
| 232 | && ymin <= b.ymax && ymax >= b.ymin;
|
---|
[2450] | 233 | }
|
---|
| 234 |
|
---|
[6203] | 235 | /**
|
---|
| 236 | * Returns the top-left point.
|
---|
| 237 | * @return The top-left point
|
---|
| 238 | */
|
---|
[2450] | 239 | public LatLon getTopLeft() {
|
---|
| 240 | return new LatLon(ymax, xmin);
|
---|
| 241 | }
|
---|
| 242 |
|
---|
[6203] | 243 | /**
|
---|
| 244 | * Returns the latitude of top-left point.
|
---|
| 245 | * @return The latitude of top-left point
|
---|
| 246 | * @since 6203
|
---|
| 247 | */
|
---|
| 248 | public double getTopLeftLat() {
|
---|
| 249 | return ymax;
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | /**
|
---|
| 253 | * Returns the longitude of top-left point.
|
---|
| 254 | * @return The longitude of top-left point
|
---|
| 255 | * @since 6203
|
---|
| 256 | */
|
---|
| 257 | public double getTopLeftLon() {
|
---|
| 258 | return xmin;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | /**
|
---|
| 262 | * Returns the bottom-right point.
|
---|
| 263 | * @return The bottom-right point
|
---|
| 264 | */
|
---|
[2450] | 265 | public LatLon getBottomRight() {
|
---|
| 266 | return new LatLon(ymin, xmax);
|
---|
| 267 | }
|
---|
[6069] | 268 |
|
---|
[6203] | 269 | /**
|
---|
| 270 | * Returns the latitude of bottom-right point.
|
---|
| 271 | * @return The latitude of bottom-right point
|
---|
| 272 | * @since 6203
|
---|
| 273 | */
|
---|
| 274 | public double getBottomRightLat() {
|
---|
| 275 | return ymin;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | /**
|
---|
| 279 | * Returns the longitude of bottom-right point.
|
---|
| 280 | * @return The longitude of bottom-right point
|
---|
| 281 | * @since 6203
|
---|
| 282 | */
|
---|
| 283 | public double getBottomRightLon() {
|
---|
| 284 | return xmax;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
[11272] | 287 | /**
|
---|
| 288 | * Gets the center of this BBox.
|
---|
| 289 | * @return The center.
|
---|
| 290 | */
|
---|
[4622] | 291 | public LatLon getCenter() {
|
---|
| 292 | return new LatLon(ymin + (ymax-ymin)/2.0, xmin + (xmax-xmin)/2.0);
|
---|
| 293 | }
|
---|
[2450] | 294 |
|
---|
[11237] | 295 | byte getIndex(final int level) {
|
---|
[6171] | 296 |
|
---|
[11237] | 297 | byte idx1 = QuadTiling.index(ymin, xmin, level);
|
---|
[6171] | 298 |
|
---|
[11237] | 299 | final byte idx2 = QuadTiling.index(ymin, xmax, level);
|
---|
[6171] | 300 | if (idx1 == -1) idx1 = idx2;
|
---|
| 301 | else if (idx1 != idx2) return -1;
|
---|
| 302 |
|
---|
[11237] | 303 | final byte idx3 = QuadTiling.index(ymax, xmin, level);
|
---|
[6171] | 304 | if (idx1 == -1) idx1 = idx3;
|
---|
| 305 | else if (idx1 != idx3) return -1;
|
---|
| 306 |
|
---|
[11237] | 307 | final byte idx4 = QuadTiling.index(ymax, xmax, level);
|
---|
[6171] | 308 | if (idx1 == -1) idx1 = idx4;
|
---|
| 309 | else if (idx1 != idx4) return -1;
|
---|
| 310 |
|
---|
| 311 | return idx1;
|
---|
| 312 | }
|
---|
[7509] | 313 |
|
---|
[12190] | 314 | /**
|
---|
| 315 | * Converts the bounds to a rectangle
|
---|
| 316 | * @return The rectangle in east/north space.
|
---|
| 317 | */
|
---|
[7193] | 318 | public Rectangle2D toRectangle() {
|
---|
| 319 | return new Rectangle2D.Double(xmin, ymin, xmax - xmin, ymax - ymin);
|
---|
| 320 | }
|
---|
[6171] | 321 |
|
---|
[2450] | 322 | @Override
|
---|
[2982] | 323 | public int hashCode() {
|
---|
[10336] | 324 | return Objects.hash(xmin, xmax, ymin, ymax);
|
---|
[2982] | 325 | }
|
---|
| 326 |
|
---|
| 327 | @Override
|
---|
| 328 | public boolean equals(Object o) {
|
---|
[10336] | 329 | if (this == o) return true;
|
---|
| 330 | if (o == null || getClass() != o.getClass()) return false;
|
---|
| 331 | BBox b = (BBox) o;
|
---|
| 332 | return Double.compare(b.xmax, xmax) == 0 && Double.compare(b.ymax, ymax) == 0
|
---|
| 333 | && Double.compare(b.xmin, xmin) == 0 && Double.compare(b.ymin, ymin) == 0;
|
---|
[2982] | 334 | }
|
---|
| 335 |
|
---|
[11269] | 336 | /**
|
---|
| 337 | * @return true if the bbox covers a part of the planets surface
|
---|
| 338 | * Height and width must be non-negative, but may (both) be 0.
|
---|
[11272] | 339 | * @since 11269
|
---|
[11269] | 340 | */
|
---|
| 341 | public boolean isValid() {
|
---|
[11272] | 342 | return xmin <= xmax && ymin <= ymax;
|
---|
[11269] | 343 | }
|
---|
| 344 |
|
---|
| 345 | /**
|
---|
[11272] | 346 | * @return true if the bbox is avalid and covers a part of the planets surface
|
---|
| 347 | * @since 11269
|
---|
[11269] | 348 | */
|
---|
| 349 | public boolean isInWorld() {
|
---|
[11452] | 350 | return xmin >= -180.0 && xmax <= 180.0 && ymin >= -90.0 && ymax <= 90.0 && isValid();
|
---|
[11269] | 351 | }
|
---|
| 352 |
|
---|
[2982] | 353 | @Override
|
---|
[2450] | 354 | public String toString() {
|
---|
[8384] | 355 | return "[ x: " + xmin + " -> " + xmax + ", y: " + ymin + " -> " + ymax + " ]";
|
---|
[2450] | 356 | }
|
---|
[4420] | 357 |
|
---|
[12190] | 358 | /**
|
---|
| 359 | * Creates a CSV string for this bbox
|
---|
| 360 | * @param separator The separator to use
|
---|
| 361 | * @return A string
|
---|
| 362 | */
|
---|
[4420] | 363 | public String toStringCSV(String separator) {
|
---|
| 364 | return Utils.join(separator, Arrays.asList(
|
---|
| 365 | LatLon.cDdFormatter.format(xmin),
|
---|
| 366 | LatLon.cDdFormatter.format(ymin),
|
---|
| 367 | LatLon.cDdFormatter.format(xmax),
|
---|
| 368 | LatLon.cDdFormatter.format(ymax)));
|
---|
| 369 | }
|
---|
[2450] | 370 | }
|
---|