| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.util.geometry; |
| 7 |
|
| 8 |
/** |
| 9 |
* Utilities for GEOMETRY data type. |
| 10 |
*/ |
| 11 |
public final class GeometryUtils { |
| 12 |
|
| 13 |
/** |
| 14 |
* Converter output target. |
| 15 |
*/ |
| 16 |
public abstract static class Target { |
| 17 |
|
| 18 |
public Target() { |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Initializes top-level target. |
| 23 |
* |
| 24 |
* @param srid |
| 25 |
* SRID |
| 26 |
*/ |
| 27 |
protected void init(int srid) { |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Invoked to add dimension system requirement. |
| 32 |
* |
| 33 |
* @param dimensionSystem |
| 34 |
* dimension system |
| 35 |
*/ |
| 36 |
protected void dimensionSystem(int dimensionSystem) { |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Invoked before writing a POINT. |
| 41 |
*/ |
| 42 |
protected void startPoint() { |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Invoked before writing a LINESTRING. |
| 47 |
* |
| 48 |
* @param numPoints |
| 49 |
* number of points in line string |
| 50 |
*/ |
| 51 |
protected void startLineString(int numPoints) { |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Invoked before writing a POLYGON. If polygon is empty, both |
| 56 |
* parameters are 0. |
| 57 |
* |
| 58 |
* @param numInner |
| 59 |
* number of inner polygons |
| 60 |
* @param numPoints |
| 61 |
* number of points in outer polygon |
| 62 |
*/ |
| 63 |
protected void startPolygon(int numInner, int numPoints) { |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Invoked before writing an inner polygon in POLYGON. |
| 68 |
* |
| 69 |
* @param numInner |
| 70 |
* number of points in inner polygon |
| 71 |
*/ |
| 72 |
protected void startPolygonInner(int numInner) { |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Invoked after writing of non-empty POLYGON. |
| 77 |
*/ |
| 78 |
protected void endNonEmptyPolygon() { |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Invoked before writing of a collection. |
| 83 |
* |
| 84 |
* @param type |
| 85 |
* type of collection, one of |
| 86 |
* {@link GeometryUtils#MULTI_POINT}, |
| 87 |
* {@link GeometryUtils#MULTI_LINE_STRING}, |
| 88 |
* {@link GeometryUtils#MULTI_POLYGON}, |
| 89 |
* {@link GeometryUtils#GEOMETRY_COLLECTION} |
| 90 |
* @param numItems |
| 91 |
* number of items in this collection |
| 92 |
*/ |
| 93 |
protected void startCollection(int type, int numItems) { |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Invoked before writing of a collection item. |
| 98 |
* |
| 99 |
* @param index |
| 100 |
* 0-based index of this item in the collection |
| 101 |
* @param total |
| 102 |
* total number of items in the collection |
| 103 |
* @return output target that should be used for processing of this |
| 104 |
* collection item. May return this target or an custom |
| 105 |
* sub-target. |
| 106 |
*/ |
| 107 |
protected Target startCollectionItem(int index, int total) { |
| 108 |
return this; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Invoked after writing of a collection item. This method is invoked on |
| 113 |
* the same target that was used for |
| 114 |
* {@link #startCollectionItem(int, int)}. |
| 115 |
* |
| 116 |
* @param target |
| 117 |
* the result of {@link #startCollectionItem(int, int)} |
| 118 |
* @param type |
| 119 |
* type of collection |
| 120 |
* @param index |
| 121 |
* 0-based index of this item in the collection |
| 122 |
* @param total |
| 123 |
* total number of items in the collection |
| 124 |
*/ |
| 125 |
protected void endCollectionItem(Target target, int type, int index, int total) { |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Invoked after writing of the object. |
| 130 |
* |
| 131 |
* @param type |
| 132 |
* type of the object |
| 133 |
*/ |
| 134 |
protected void endObject(int type) { |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Invoked to add a coordinate to a geometry. |
| 139 |
* |
| 140 |
* @param x |
| 141 |
* X coordinate |
| 142 |
* @param y |
| 143 |
* Y coordinate |
| 144 |
* @param z |
| 145 |
* Z coordinate (NaN if not used) |
| 146 |
* @param m |
| 147 |
* M coordinate (NaN if not used) |
| 148 |
* @param index |
| 149 |
* 0-based index of coordinate in the current sequence |
| 150 |
* @param total |
| 151 |
* total number of coordinates in the current sequence |
| 152 |
*/ |
| 153 |
protected abstract void addCoordinate(double x, double y, double z, double m, int index, int total); |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Converter output target that calculates an envelope. |
| 159 |
*/ |
| 160 |
public static final class EnvelopeTarget extends Target { |
| 161 |
|
| 162 |
/** |
| 163 |
* Enables or disables the envelope calculation. Inner rings of polygons |
| 164 |
* are not counted. |
| 165 |
*/ |
| 166 |
private boolean enabled; |
| 167 |
|
| 168 |
/** |
| 169 |
* Whether envelope was set. |
| 170 |
*/ |
| 171 |
private boolean set; |
| 172 |
|
| 173 |
private double minX, maxX, minY, maxY; |
| 174 |
|
| 175 |
/** |
| 176 |
* Creates a new envelope calculation target. |
| 177 |
*/ |
| 178 |
public EnvelopeTarget() { |
| 179 |
} |
| 180 |
|
| 181 |
@Override |
| 182 |
protected void startPoint() { |
| 183 |
enabled = true; |
| 184 |
} |
| 185 |
|
| 186 |
@Override |
| 187 |
protected void startLineString(int numPoints) { |
| 188 |
enabled = true; |
| 189 |
} |
| 190 |
|
| 191 |
@Override |
| 192 |
protected void startPolygon(int numInner, int numPoints) { |
| 193 |
enabled = true; |
| 194 |
} |
| 195 |
|
| 196 |
@Override |
| 197 |
protected void startPolygonInner(int numInner) { |
| 198 |
enabled = false; |
| 199 |
} |
| 200 |
|
| 201 |
@Override |
| 202 |
protected void addCoordinate(double x, double y, double z, double m, int index, int total) { |
| 203 |
// POINT EMPTY has NaNs |
| 204 |
if (enabled && !Double.isNaN(x) && !Double.isNaN(y)) { |
| 205 |
if (!set) { |
| 206 |
minX = maxX = x; |
| 207 |
minY = maxY = y; |
| 208 |
set = true; |
| 209 |
} else { |
| 210 |
if (minX > x) { |
| 211 |
minX = x; |
| 212 |
} |
| 213 |
if (maxX < x) { |
| 214 |
maxX = x; |
| 215 |
} |
| 216 |
if (minY > y) { |
| 217 |
minY = y; |
| 218 |
} |
| 219 |
if (maxY < y) { |
| 220 |
maxY = y; |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Returns the envelope. |
| 228 |
* |
| 229 |
* @return the envelope, or null |
| 230 |
*/ |
| 231 |
public double[] getEnvelope() { |
| 232 |
return set ? new double[] { minX, maxX, minY, maxY } : null; |
| 233 |
} |
| 234 |
|
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Converter output target that determines minimal dimension system for a |
| 239 |
* geometry. |
| 240 |
*/ |
| 241 |
public static final class DimensionSystemTarget extends Target { |
| 242 |
|
| 243 |
private boolean hasZ; |
| 244 |
|
| 245 |
private boolean hasM; |
| 246 |
|
| 247 |
/** |
| 248 |
* Creates a new dimension system determination target. |
| 249 |
*/ |
| 250 |
public DimensionSystemTarget() { |
| 251 |
} |
| 252 |
|
| 253 |
@Override |
| 254 |
protected void dimensionSystem(int dimensionSystem) { |
| 255 |
if ((dimensionSystem & DIMENSION_SYSTEM_XYZ) != 0) { |
| 256 |
hasZ = true; |
| 257 |
} |
| 258 |
if ((dimensionSystem & DIMENSION_SYSTEM_XYM) != 0) { |
| 259 |
hasM = true; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
@Override |
| 264 |
protected void addCoordinate(double x, double y, double z, double m, int index, int total) { |
| 265 |
if (!hasZ && !Double.isNaN(z)) { |
| 266 |
hasZ = true; |
| 267 |
} |
| 268 |
if (!hasM && !Double.isNaN(m)) { |
| 269 |
hasM = true; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* Returns the minimal dimension system. |
| 275 |
* |
| 276 |
* @return the minimal dimension system |
| 277 |
*/ |
| 278 |
public int getDimensionSystem() { |
| 279 |
return (hasZ ? DIMENSION_SYSTEM_XYZ : 0) | (hasM ? DIMENSION_SYSTEM_XYM : 0); |
| 280 |
} |
| 281 |
|
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* POINT geometry type. |
| 286 |
*/ |
| 287 |
public static final int POINT = 1; |
| 288 |
|
| 289 |
/** |
| 290 |
* LINESTRING geometry type. |
| 291 |
*/ |
| 292 |
public static final int LINE_STRING = 2; |
| 293 |
|
| 294 |
/** |
| 295 |
* POLYGON geometry type. |
| 296 |
*/ |
| 297 |
public static final int POLYGON = 3; |
| 298 |
|
| 299 |
/** |
| 300 |
* MULTIPOINT geometry type. |
| 301 |
*/ |
| 302 |
public static final int MULTI_POINT = 4; |
| 303 |
|
| 304 |
/** |
| 305 |
* MULTILINESTRING geometry type. |
| 306 |
*/ |
| 307 |
public static final int MULTI_LINE_STRING = 5; |
| 308 |
|
| 309 |
/** |
| 310 |
* MULTIPOLYGON geometry type. |
| 311 |
*/ |
| 312 |
public static final int MULTI_POLYGON = 6; |
| 313 |
|
| 314 |
/** |
| 315 |
* GEOMETRYCOLLECTION geometry type. |
| 316 |
*/ |
| 317 |
public static final int GEOMETRY_COLLECTION = 7; |
| 318 |
|
| 319 |
/** |
| 320 |
* Number of X coordinate. |
| 321 |
*/ |
| 322 |
public static final int X = 0; |
| 323 |
|
| 324 |
/** |
| 325 |
* Number of Y coordinate. |
| 326 |
*/ |
| 327 |
public static final int Y = 1; |
| 328 |
|
| 329 |
/** |
| 330 |
* Number of Z coordinate. |
| 331 |
*/ |
| 332 |
public static final int Z = 2; |
| 333 |
|
| 334 |
/** |
| 335 |
* Number of M coordinate. |
| 336 |
*/ |
| 337 |
public static final int M = 3; |
| 338 |
|
| 339 |
/** |
| 340 |
* Code of 2D (XY) dimension system. |
| 341 |
*/ |
| 342 |
public static final int DIMENSION_SYSTEM_XY = 0; |
| 343 |
|
| 344 |
/** |
| 345 |
* Code of Z (XYZ) dimension system. Can also be used in bit masks to |
| 346 |
* determine presence of dimension Z. |
| 347 |
*/ |
| 348 |
public static final int DIMENSION_SYSTEM_XYZ = 1; |
| 349 |
|
| 350 |
/** |
| 351 |
* Code of M (XYM) dimension system. Can also be used in bit masks to |
| 352 |
* determine presence of dimension M. |
| 353 |
*/ |
| 354 |
public static final int DIMENSION_SYSTEM_XYM = 2; |
| 355 |
|
| 356 |
/** |
| 357 |
* Code of ZM (XYZM) dimension system. Can be also combined from |
| 358 |
* {@link #DIMENSION_SYSTEM_XYZ} and {@link #DIMENSION_SYSTEM_XYM} using |
| 359 |
* bitwise OR. |
| 360 |
*/ |
| 361 |
public static final int DIMENSION_SYSTEM_XYZM = 3; |
| 362 |
|
| 363 |
/** |
| 364 |
* Minimum X coordinate index. |
| 365 |
*/ |
| 366 |
public static final int MIN_X = 0; |
| 367 |
|
| 368 |
/** |
| 369 |
* Maximum X coordinate index. |
| 370 |
*/ |
| 371 |
public static final int MAX_X = 1; |
| 372 |
|
| 373 |
/** |
| 374 |
* Minimum Y coordinate index. |
| 375 |
*/ |
| 376 |
public static final int MIN_Y = 2; |
| 377 |
|
| 378 |
/** |
| 379 |
* Maximum Y coordinate index. |
| 380 |
*/ |
| 381 |
public static final int MAX_Y = 3; |
| 382 |
|
| 383 |
/** |
| 384 |
* Calculates an envelope of a specified geometry. |
| 385 |
* |
| 386 |
* @param ewkb |
| 387 |
* EWKB of a geometry |
| 388 |
* @return envelope, or null |
| 389 |
*/ |
| 390 |
public static double[] getEnvelope(byte[] ewkb) { |
| 391 |
EnvelopeTarget target = new EnvelopeTarget(); |
| 392 |
EWKBUtils.parseEWKB(ewkb, target); |
| 393 |
return target.getEnvelope(); |
| 394 |
} |
| 395 |
|
| 396 |
/** |
| 397 |
* Checks whether two envelopes intersect with each other. |
| 398 |
* |
| 399 |
* @param envelope1 |
| 400 |
* first envelope, or null |
| 401 |
* @param envelope2 |
| 402 |
* second envelope, or null |
| 403 |
* @return whether the specified envelopes intersects |
| 404 |
*/ |
| 405 |
public static boolean intersects(double[] envelope1, double[] envelope2) { |
| 406 |
return envelope1 != null && envelope2 != null // |
| 407 |
&& envelope1[MAX_X] >= envelope2[MIN_X] // |
| 408 |
&& envelope1[MIN_X] <= envelope2[MAX_X] // |
| 409 |
&& envelope1[MAX_Y] >= envelope2[MIN_Y] // |
| 410 |
&& envelope1[MIN_Y] <= envelope2[MAX_Y]; |
| 411 |
} |
| 412 |
|
| 413 |
/** |
| 414 |
* Returns union of two envelopes. This method does not modify the specified |
| 415 |
* envelopes, but may return one of them as a result. |
| 416 |
* |
| 417 |
* @param envelope1 |
| 418 |
* first envelope, or null |
| 419 |
* @param envelope2 |
| 420 |
* second envelope, or null |
| 421 |
* @return union of two envelopes |
| 422 |
*/ |
| 423 |
public static double[] union(double[] envelope1, double[] envelope2) { |
| 424 |
if (envelope1 == null) { |
| 425 |
return envelope2; |
| 426 |
} else if (envelope2 == null) { |
| 427 |
return envelope1; |
| 428 |
} |
| 429 |
double minX1 = envelope1[MIN_X], maxX1 = envelope1[MAX_X], minY1 = envelope1[MIN_Y], maxY1 = envelope1[MAX_Y]; |
| 430 |
double minX2 = envelope2[MIN_X], maxX2 = envelope2[MAX_X], minY2 = envelope2[MIN_Y], maxY2 = envelope2[MAX_Y]; |
| 431 |
boolean modified = false; |
| 432 |
if (minX1 > minX2) { |
| 433 |
minX1 = minX2; |
| 434 |
modified = true; |
| 435 |
} |
| 436 |
if (maxX1 < maxX2) { |
| 437 |
maxX1 = maxX2; |
| 438 |
modified = true; |
| 439 |
} |
| 440 |
if (minY1 > minY2) { |
| 441 |
minY1 = minY2; |
| 442 |
modified = true; |
| 443 |
} |
| 444 |
if (maxY1 < maxY2) { |
| 445 |
maxY1 = maxY2; |
| 446 |
modified = true; |
| 447 |
} |
| 448 |
return modified ? new double[] { minX1, maxX1, minY1, maxY1 } : envelope1; |
| 449 |
} |
| 450 |
|
| 451 |
/** |
| 452 |
* Normalizes all NaNs into single type on NaN and negative zero to positive |
| 453 |
* zero. |
| 454 |
* |
| 455 |
* @param d |
| 456 |
* double value |
| 457 |
* @return normalized value |
| 458 |
*/ |
| 459 |
static double toCanonicalDouble(double d) { |
| 460 |
return Double.isNaN(d) ? Double.NaN : d == 0d ? 0d : d; |
| 461 |
} |
| 462 |
|
| 463 |
/** |
| 464 |
* Throw exception if param is not finite value (ie. NaN/inf/etc) |
| 465 |
* |
| 466 |
* @param d |
| 467 |
* a double value |
| 468 |
* @return the same double value |
| 469 |
*/ |
| 470 |
static double checkFinite(double d) { |
| 471 |
if (!Double.isFinite(d)) { |
| 472 |
throw new IllegalArgumentException(); |
| 473 |
} |
| 474 |
return d; |
| 475 |
} |
| 476 |
|
| 477 |
private GeometryUtils() { |
| 478 |
} |
| 479 |
|
| 480 |
} |