[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 | /**
|
---|
| 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 | */
|
---|
[10378] | 74 | public BBox(double ax, double ay, double bx, double by) {
|
---|
[11272] | 75 | if (!(Double.isNaN(ax) || Double.isNaN(ay) || Double.isNaN(bx) || Double.isNaN(by))) {
|
---|
| 76 | add(ax, ay);
|
---|
| 77 | add(bx, by);
|
---|
[11269] | 78 | }
|
---|
[11272] | 79 | // otherwise use default which is an invalid BBox
|
---|
[2450] | 80 | }
|
---|
| 81 |
|
---|
[11269] | 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 | */
|
---|
[2450] | 87 | public BBox(Way w) {
|
---|
[12161] | 88 | w.getNodes().forEach(this::add);
|
---|
[2450] | 89 | }
|
---|
| 90 |
|
---|
[11269] | 91 | /**
|
---|
| 92 | * Create BBox for a node. An invalid BBox is returned if the coordinates are not known.
|
---|
| 93 | * @param n the node
|
---|
| 94 | */
|
---|
[3163] | 95 | public BBox(Node n) {
|
---|
[12161] | 96 | this((ILatLon) n);
|
---|
[3163] | 97 | }
|
---|
| 98 |
|
---|
[11269] | 99 | /**
|
---|
[12161] | 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 | /**
|
---|
[11269] | 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.
|
---|
[12161] | 110 | * Kept for binary compatibility
|
---|
[11269] | 111 | * @param c a LatLon point
|
---|
| 112 | */
|
---|
[6890] | 113 | public final void add(LatLon c) {
|
---|
[12161] | 114 | add((ILatLon) c);
|
---|
[2450] | 115 | }
|
---|
| 116 |
|
---|
[3209] | 117 | /**
|
---|
[12161] | 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.
|
---|
[12183] | 120 | * If it is invalid or <code>null</code>, this call is ignored.
|
---|
| 121 | * @param c a LatLon point.
|
---|
[12161] | 122 | */
|
---|
| 123 | public final void add(ILatLon c) {
|
---|
[12183] | 124 | if (c != null) {
|
---|
| 125 | add(c.lon(), c.lat());
|
---|
| 126 | }
|
---|
[12161] | 127 | }
|
---|
| 128 |
|
---|
| 129 | /**
|
---|
[3209] | 130 | * Extends this bbox to include the point (x, y)
|
---|
[9243] | 131 | * @param x X coordinate
|
---|
| 132 | * @param y Y coordinate
|
---|
[3209] | 133 | */
|
---|
[6890] | 134 | public final void add(double x, double y) {
|
---|
[11272] | 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 | }
|
---|
[2450] | 141 | }
|
---|
| 142 |
|
---|
[11269] | 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 | }
|
---|
[2982] | 154 | }
|
---|
| 155 |
|
---|
[11269] | 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 | */
|
---|
[2450] | 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 |
|
---|
[11272] | 167 | /**
|
---|
| 168 | * Gets the height of the bbox.
|
---|
| 169 | * @return The difference between ymax and ymin. 0 for invalid bboxes.
|
---|
| 170 | */
|
---|
[2450] | 171 | public double height() {
|
---|
[11272] | 172 | if (isValid()) {
|
---|
| 173 | return ymax - ymin;
|
---|
| 174 | } else {
|
---|
| 175 | return 0;
|
---|
| 176 | }
|
---|
[2450] | 177 | }
|
---|
| 178 |
|
---|
[11272] | 179 | /**
|
---|
| 180 | * Gets the width of the bbox.
|
---|
| 181 | * @return The difference between xmax and xmin. 0 for invalid bboxes.
|
---|
| 182 | */
|
---|
[2450] | 183 | public double width() {
|
---|
[11272] | 184 | if (isValid()) {
|
---|
| 185 | return xmax - xmin;
|
---|
| 186 | } else {
|
---|
| 187 | return 0;
|
---|
| 188 | }
|
---|
[2450] | 189 | }
|
---|
| 190 |
|
---|
[3209] | 191 | /**
|
---|
[9243] | 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
|
---|
[3209] | 195 | */
|
---|
[2450] | 196 | public boolean bounds(BBox b) {
|
---|
[9976] | 197 | return xmin <= b.xmin && xmax >= b.xmax
|
---|
| 198 | && ymin <= b.ymin && ymax >= b.ymax;
|
---|
[2450] | 199 | }
|
---|
| 200 |
|
---|
[3209] | 201 | /**
|
---|
[9243] | 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
|
---|
[3209] | 205 | */
|
---|
[2450] | 206 | public boolean bounds(LatLon c) {
|
---|
[9976] | 207 | return xmin <= c.lon() && xmax >= c.lon()
|
---|
| 208 | && ymin <= c.lat() && ymax >= c.lat();
|
---|
[2450] | 209 | }
|
---|
| 210 |
|
---|
[3209] | 211 | /**
|
---|
[9243] | 212 | * Tests, whether two BBoxes intersect as an area.
|
---|
[4319] | 213 | * I.e. whether there exists a point that lies in both of them.
|
---|
[9243] | 214 | * @param b other bounding box
|
---|
| 215 | * @return {@code true} if this bbox intersects with the other
|
---|
[3209] | 216 | */
|
---|
| 217 | public boolean intersects(BBox b) {
|
---|
[11272] | 218 | return xmin <= b.xmax && xmax >= b.xmin
|
---|
| 219 | && ymin <= b.ymax && ymax >= b.ymin;
|
---|
[2450] | 220 | }
|
---|
| 221 |
|
---|
[6203] | 222 | /**
|
---|
| 223 | * Returns the top-left point.
|
---|
| 224 | * @return The top-left point
|
---|
| 225 | */
|
---|
[2450] | 226 | public LatLon getTopLeft() {
|
---|
| 227 | return new LatLon(ymax, xmin);
|
---|
| 228 | }
|
---|
| 229 |
|
---|
[6203] | 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 | */
|
---|
[2450] | 252 | public LatLon getBottomRight() {
|
---|
| 253 | return new LatLon(ymin, xmax);
|
---|
| 254 | }
|
---|
[6069] | 255 |
|
---|
[6203] | 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 |
|
---|
[11272] | 274 | /**
|
---|
| 275 | * Gets the center of this BBox.
|
---|
| 276 | * @return The center.
|
---|
| 277 | */
|
---|
[4622] | 278 | public LatLon getCenter() {
|
---|
| 279 | return new LatLon(ymin + (ymax-ymin)/2.0, xmin + (xmax-xmin)/2.0);
|
---|
| 280 | }
|
---|
[2450] | 281 |
|
---|
[11237] | 282 | byte getIndex(final int level) {
|
---|
[6171] | 283 |
|
---|
[11237] | 284 | byte idx1 = QuadTiling.index(ymin, xmin, level);
|
---|
[6171] | 285 |
|
---|
[11237] | 286 | final byte idx2 = QuadTiling.index(ymin, xmax, level);
|
---|
[6171] | 287 | if (idx1 == -1) idx1 = idx2;
|
---|
| 288 | else if (idx1 != idx2) return -1;
|
---|
| 289 |
|
---|
[11237] | 290 | final byte idx3 = QuadTiling.index(ymax, xmin, level);
|
---|
[6171] | 291 | if (idx1 == -1) idx1 = idx3;
|
---|
| 292 | else if (idx1 != idx3) return -1;
|
---|
| 293 |
|
---|
[11237] | 294 | final byte idx4 = QuadTiling.index(ymax, xmax, level);
|
---|
[6171] | 295 | if (idx1 == -1) idx1 = idx4;
|
---|
| 296 | else if (idx1 != idx4) return -1;
|
---|
| 297 |
|
---|
| 298 | return idx1;
|
---|
| 299 | }
|
---|
[7509] | 300 |
|
---|
[7193] | 301 | public Rectangle2D toRectangle() {
|
---|
| 302 | return new Rectangle2D.Double(xmin, ymin, xmax - xmin, ymax - ymin);
|
---|
| 303 | }
|
---|
[6171] | 304 |
|
---|
[2450] | 305 | @Override
|
---|
[2982] | 306 | public int hashCode() {
|
---|
[10336] | 307 | return Objects.hash(xmin, xmax, ymin, ymax);
|
---|
[2982] | 308 | }
|
---|
| 309 |
|
---|
| 310 | @Override
|
---|
| 311 | public boolean equals(Object o) {
|
---|
[10336] | 312 | if (this == o) return true;
|
---|
| 313 | if (o == null || getClass() != o.getClass()) return false;
|
---|
| 314 | BBox b = (BBox) o;
|
---|
| 315 | return Double.compare(b.xmax, xmax) == 0 && Double.compare(b.ymax, ymax) == 0
|
---|
| 316 | && Double.compare(b.xmin, xmin) == 0 && Double.compare(b.ymin, ymin) == 0;
|
---|
[2982] | 317 | }
|
---|
| 318 |
|
---|
[11269] | 319 | /**
|
---|
| 320 | * @return true if the bbox covers a part of the planets surface
|
---|
| 321 | * Height and width must be non-negative, but may (both) be 0.
|
---|
[11272] | 322 | * @since 11269
|
---|
[11269] | 323 | */
|
---|
| 324 | public boolean isValid() {
|
---|
[11272] | 325 | return xmin <= xmax && ymin <= ymax;
|
---|
[11269] | 326 | }
|
---|
| 327 |
|
---|
| 328 | /**
|
---|
[11272] | 329 | * @return true if the bbox is avalid and covers a part of the planets surface
|
---|
| 330 | * @since 11269
|
---|
[11269] | 331 | */
|
---|
| 332 | public boolean isInWorld() {
|
---|
[11452] | 333 | return xmin >= -180.0 && xmax <= 180.0 && ymin >= -90.0 && ymax <= 90.0 && isValid();
|
---|
[11269] | 334 | }
|
---|
| 335 |
|
---|
[2982] | 336 | @Override
|
---|
[2450] | 337 | public String toString() {
|
---|
[8384] | 338 | return "[ x: " + xmin + " -> " + xmax + ", y: " + ymin + " -> " + ymax + " ]";
|
---|
[2450] | 339 | }
|
---|
[4420] | 340 |
|
---|
| 341 | public String toStringCSV(String separator) {
|
---|
| 342 | return Utils.join(separator, Arrays.asList(
|
---|
| 343 | LatLon.cDdFormatter.format(xmin),
|
---|
| 344 | LatLon.cDdFormatter.format(ymin),
|
---|
| 345 | LatLon.cDdFormatter.format(xmax),
|
---|
| 346 | LatLon.cDdFormatter.format(ymax)));
|
---|
| 347 | }
|
---|
[2450] | 348 | }
|
---|