| 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 |
import static org.h2.util.geometry.GeometryUtils.DIMENSION_SYSTEM_XY; |
| 9 |
import static org.h2.util.geometry.GeometryUtils.DIMENSION_SYSTEM_XYM; |
| 10 |
import static org.h2.util.geometry.GeometryUtils.DIMENSION_SYSTEM_XYZ; |
| 11 |
import static org.h2.util.geometry.GeometryUtils.DIMENSION_SYSTEM_XYZM; |
| 12 |
import static org.h2.util.geometry.GeometryUtils.GEOMETRY_COLLECTION; |
| 13 |
import static org.h2.util.geometry.GeometryUtils.LINE_STRING; |
| 14 |
import static org.h2.util.geometry.GeometryUtils.M; |
| 15 |
import static org.h2.util.geometry.GeometryUtils.MULTI_LINE_STRING; |
| 16 |
import static org.h2.util.geometry.GeometryUtils.MULTI_POINT; |
| 17 |
import static org.h2.util.geometry.GeometryUtils.MULTI_POLYGON; |
| 18 |
import static org.h2.util.geometry.GeometryUtils.POINT; |
| 19 |
import static org.h2.util.geometry.GeometryUtils.POLYGON; |
| 20 |
import static org.h2.util.geometry.GeometryUtils.X; |
| 21 |
import static org.h2.util.geometry.GeometryUtils.Y; |
| 22 |
import static org.h2.util.geometry.GeometryUtils.Z; |
| 23 |
import static org.h2.util.geometry.GeometryUtils.checkFinite; |
| 24 |
import static org.h2.util.geometry.GeometryUtils.toCanonicalDouble; |
| 25 |
|
| 26 |
import java.io.ByteArrayOutputStream; |
| 27 |
|
| 28 |
import org.h2.message.DbException; |
| 29 |
import org.h2.util.geometry.EWKBUtils.EWKBTarget; |
| 30 |
import org.h2.util.geometry.GeometryUtils.Target; |
| 31 |
import org.locationtech.jts.geom.CoordinateSequence; |
| 32 |
import org.locationtech.jts.geom.Geometry; |
| 33 |
import org.locationtech.jts.geom.GeometryCollection; |
| 34 |
import org.locationtech.jts.geom.GeometryFactory; |
| 35 |
import org.locationtech.jts.geom.LineString; |
| 36 |
import org.locationtech.jts.geom.LinearRing; |
| 37 |
import org.locationtech.jts.geom.MultiLineString; |
| 38 |
import org.locationtech.jts.geom.MultiPoint; |
| 39 |
import org.locationtech.jts.geom.MultiPolygon; |
| 40 |
import org.locationtech.jts.geom.Point; |
| 41 |
import org.locationtech.jts.geom.Polygon; |
| 42 |
import org.locationtech.jts.geom.PrecisionModel; |
| 43 |
import org.locationtech.jts.geom.impl.CoordinateArraySequenceFactory; |
| 44 |
import org.locationtech.jts.geom.impl.PackedCoordinateSequenceFactory; |
| 45 |
|
| 46 |
/** |
| 47 |
* Utilities for Geometry data type from JTS library. |
| 48 |
*/ |
| 49 |
public final class JTSUtils { |
| 50 |
|
| 51 |
/** |
| 52 |
* Converter output target that creates a JTS Geometry. |
| 53 |
*/ |
| 54 |
public static final class GeometryTarget extends Target { |
| 55 |
|
| 56 |
private final int dimensionSystem; |
| 57 |
|
| 58 |
private GeometryFactory factory; |
| 59 |
|
| 60 |
private int type; |
| 61 |
|
| 62 |
private CoordinateSequence coordinates; |
| 63 |
|
| 64 |
private CoordinateSequence[] innerCoordinates; |
| 65 |
|
| 66 |
private int innerOffset; |
| 67 |
|
| 68 |
private Geometry[] subgeometries; |
| 69 |
|
| 70 |
/** |
| 71 |
* Creates a new instance of JTS Geometry target. |
| 72 |
* |
| 73 |
* @param dimensionSystem |
| 74 |
* dimension system to use |
| 75 |
*/ |
| 76 |
public GeometryTarget(int dimensionSystem) { |
| 77 |
this.dimensionSystem = dimensionSystem; |
| 78 |
} |
| 79 |
|
| 80 |
private GeometryTarget(int dimensionSystem, GeometryFactory factory) { |
| 81 |
this.dimensionSystem = dimensionSystem; |
| 82 |
this.factory = factory; |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
protected void init(int srid) { |
| 87 |
factory = new GeometryFactory(new PrecisionModel(), srid, |
| 88 |
(dimensionSystem & DIMENSION_SYSTEM_XYM) != 0 ? PackedCoordinateSequenceFactory.DOUBLE_FACTORY |
| 89 |
: CoordinateArraySequenceFactory.instance()); |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
protected void startPoint() { |
| 94 |
type = POINT; |
| 95 |
initCoordinates(1); |
| 96 |
innerOffset = -1; |
| 97 |
} |
| 98 |
|
| 99 |
@Override |
| 100 |
protected void startLineString(int numPoints) { |
| 101 |
type = LINE_STRING; |
| 102 |
initCoordinates(numPoints); |
| 103 |
innerOffset = -1; |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
protected void startPolygon(int numInner, int numPoints) { |
| 108 |
type = POLYGON; |
| 109 |
initCoordinates(numPoints); |
| 110 |
innerCoordinates = new CoordinateSequence[numInner]; |
| 111 |
innerOffset = -1; |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
protected void startPolygonInner(int numInner) { |
| 116 |
innerCoordinates[++innerOffset] = createCoordinates(numInner); |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
protected void startCollection(int type, int numItems) { |
| 121 |
this.type = type; |
| 122 |
switch (type) { |
| 123 |
case MULTI_POINT: |
| 124 |
subgeometries = new Point[numItems]; |
| 125 |
break; |
| 126 |
case MULTI_LINE_STRING: |
| 127 |
subgeometries = new LineString[numItems]; |
| 128 |
break; |
| 129 |
case MULTI_POLYGON: |
| 130 |
subgeometries = new Polygon[numItems]; |
| 131 |
break; |
| 132 |
case GEOMETRY_COLLECTION: |
| 133 |
subgeometries = new Geometry[numItems]; |
| 134 |
break; |
| 135 |
default: |
| 136 |
throw new IllegalArgumentException(); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
protected Target startCollectionItem(int index, int total) { |
| 142 |
return new GeometryTarget(dimensionSystem, factory); |
| 143 |
} |
| 144 |
|
| 145 |
@Override |
| 146 |
protected void endCollectionItem(Target target, int type, int index, int total) { |
| 147 |
subgeometries[index] = ((GeometryTarget) target).getGeometry(); |
| 148 |
} |
| 149 |
|
| 150 |
private void initCoordinates(int numPoints) { |
| 151 |
coordinates = createCoordinates(numPoints); |
| 152 |
} |
| 153 |
|
| 154 |
private CoordinateSequence createCoordinates(int numPoints) { |
| 155 |
int d, m; |
| 156 |
switch (dimensionSystem) { |
| 157 |
case DIMENSION_SYSTEM_XY: |
| 158 |
d = 2; |
| 159 |
m = 0; |
| 160 |
break; |
| 161 |
case DIMENSION_SYSTEM_XYZ: |
| 162 |
d = 3; |
| 163 |
m = 0; |
| 164 |
break; |
| 165 |
case DIMENSION_SYSTEM_XYM: |
| 166 |
d = 3; |
| 167 |
m = 1; |
| 168 |
break; |
| 169 |
case DIMENSION_SYSTEM_XYZM: |
| 170 |
d = 4; |
| 171 |
m = 1; |
| 172 |
break; |
| 173 |
default: |
| 174 |
throw DbException.getInternalError(); |
| 175 |
} |
| 176 |
return factory.getCoordinateSequenceFactory().create(numPoints, d, m); |
| 177 |
} |
| 178 |
|
| 179 |
@Override |
| 180 |
protected void addCoordinate(double x, double y, double z, double m, int index, int total) { |
| 181 |
if (type == POINT && Double.isNaN(x) && Double.isNaN(y) && Double.isNaN(z) && Double.isNaN(m)) { |
| 182 |
this.coordinates = createCoordinates(0); |
| 183 |
return; |
| 184 |
} |
| 185 |
CoordinateSequence coordinates = innerOffset < 0 ? this.coordinates : innerCoordinates[innerOffset]; |
| 186 |
coordinates.setOrdinate(index, X, checkFinite(x)); |
| 187 |
coordinates.setOrdinate(index, Y, checkFinite(y)); |
| 188 |
switch (dimensionSystem) { |
| 189 |
case DIMENSION_SYSTEM_XYZM: |
| 190 |
coordinates.setOrdinate(index, M, checkFinite(m)); |
| 191 |
//$FALL-THROUGH$ |
| 192 |
case DIMENSION_SYSTEM_XYZ: |
| 193 |
coordinates.setOrdinate(index, Z, checkFinite(z)); |
| 194 |
break; |
| 195 |
case DIMENSION_SYSTEM_XYM: |
| 196 |
coordinates.setOrdinate(index, 2, checkFinite(m)); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
Geometry getGeometry() { |
| 201 |
switch (type) { |
| 202 |
case POINT: |
| 203 |
return new Point(coordinates, factory); |
| 204 |
case LINE_STRING: |
| 205 |
return new LineString(coordinates, factory); |
| 206 |
case POLYGON: { |
| 207 |
LinearRing shell = new LinearRing(coordinates, factory); |
| 208 |
int innerCount = innerCoordinates.length; |
| 209 |
LinearRing[] holes = new LinearRing[innerCount]; |
| 210 |
for (int i = 0; i < innerCount; i++) { |
| 211 |
holes[i] = new LinearRing(innerCoordinates[i], factory); |
| 212 |
} |
| 213 |
return new Polygon(shell, holes, factory); |
| 214 |
} |
| 215 |
case MULTI_POINT: |
| 216 |
return new MultiPoint((Point[]) subgeometries, factory); |
| 217 |
case MULTI_LINE_STRING: |
| 218 |
return new MultiLineString((LineString[]) subgeometries, factory); |
| 219 |
case MULTI_POLYGON: |
| 220 |
return new MultiPolygon((Polygon[]) subgeometries, factory); |
| 221 |
case GEOMETRY_COLLECTION: |
| 222 |
return new GeometryCollection(subgeometries, factory); |
| 223 |
default: |
| 224 |
throw new IllegalStateException(); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Converts EWKB to a JTS geometry object. |
| 232 |
* |
| 233 |
* @param ewkb |
| 234 |
* source EWKB |
| 235 |
* @return JTS geometry object |
| 236 |
*/ |
| 237 |
public static Geometry ewkb2geometry(byte[] ewkb) { |
| 238 |
return ewkb2geometry(ewkb, EWKBUtils.getDimensionSystem(ewkb)); |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Converts EWKB to a JTS geometry object. |
| 243 |
* |
| 244 |
* @param ewkb |
| 245 |
* source EWKB |
| 246 |
* @param dimensionSystem |
| 247 |
* dimension system |
| 248 |
* @return JTS geometry object |
| 249 |
*/ |
| 250 |
public static Geometry ewkb2geometry(byte[] ewkb, int dimensionSystem) { |
| 251 |
GeometryTarget target = new GeometryTarget(dimensionSystem); |
| 252 |
EWKBUtils.parseEWKB(ewkb, target); |
| 253 |
return target.getGeometry(); |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Converts Geometry to EWKB. |
| 258 |
* |
| 259 |
* @param geometry |
| 260 |
* source geometry |
| 261 |
* @return EWKB representation |
| 262 |
*/ |
| 263 |
public static byte[] geometry2ewkb(Geometry geometry) { |
| 264 |
return geometry2ewkb(geometry, getDimensionSystem(geometry)); |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Converts Geometry to EWKB. |
| 269 |
* |
| 270 |
* @param geometry |
| 271 |
* source geometry |
| 272 |
* @param dimensionSystem |
| 273 |
* dimension system |
| 274 |
* @return EWKB representation |
| 275 |
*/ |
| 276 |
public static byte[] geometry2ewkb(Geometry geometry, int dimensionSystem) { |
| 277 |
// Write an EWKB |
| 278 |
ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 279 |
EWKBTarget target = new EWKBTarget(output, dimensionSystem); |
| 280 |
parseGeometry(geometry, target); |
| 281 |
return output.toByteArray(); |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* Parses a JTS Geometry object. |
| 286 |
* |
| 287 |
* @param geometry |
| 288 |
* geometry to parse |
| 289 |
* @param target |
| 290 |
* output target |
| 291 |
*/ |
| 292 |
public static void parseGeometry(Geometry geometry, Target target) { |
| 293 |
parseGeometry(geometry, target, 0); |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Parses a JTS Geometry object. |
| 298 |
* |
| 299 |
* @param geometry |
| 300 |
* geometry to parse |
| 301 |
* @param target |
| 302 |
* output target |
| 303 |
* @param parentType |
| 304 |
* type of parent geometry collection, or 0 for the root geometry |
| 305 |
*/ |
| 306 |
private static void parseGeometry(Geometry geometry, Target target, int parentType) { |
| 307 |
if (parentType == 0) { |
| 308 |
target.init(geometry.getSRID()); |
| 309 |
} |
| 310 |
if (geometry instanceof Point) { |
| 311 |
if (parentType != 0 && parentType != MULTI_POINT && parentType != GEOMETRY_COLLECTION) { |
| 312 |
throw new IllegalArgumentException(); |
| 313 |
} |
| 314 |
target.startPoint(); |
| 315 |
Point p = (Point) geometry; |
| 316 |
if (p.isEmpty()) { |
| 317 |
target.addCoordinate(Double.NaN, Double.NaN, Double.NaN, Double.NaN, 0, 1); |
| 318 |
} else { |
| 319 |
addCoordinate(p.getCoordinateSequence(), target, 0, 1); |
| 320 |
} |
| 321 |
target.endObject(POINT); |
| 322 |
} else if (geometry instanceof LineString) { |
| 323 |
if (parentType != 0 && parentType != MULTI_LINE_STRING && parentType != GEOMETRY_COLLECTION) { |
| 324 |
throw new IllegalArgumentException(); |
| 325 |
} |
| 326 |
LineString ls = (LineString) geometry; |
| 327 |
CoordinateSequence cs = ls.getCoordinateSequence(); |
| 328 |
int numPoints = cs.size(); |
| 329 |
if (numPoints == 1) { |
| 330 |
throw new IllegalArgumentException(); |
| 331 |
} |
| 332 |
target.startLineString(numPoints); |
| 333 |
for (int i = 0; i < numPoints; i++) { |
| 334 |
addCoordinate(cs, target, i, numPoints); |
| 335 |
} |
| 336 |
target.endObject(LINE_STRING); |
| 337 |
} else if (geometry instanceof Polygon) { |
| 338 |
if (parentType != 0 && parentType != MULTI_POLYGON && parentType != GEOMETRY_COLLECTION) { |
| 339 |
throw new IllegalArgumentException(); |
| 340 |
} |
| 341 |
Polygon p = (Polygon) geometry; |
| 342 |
int numInner = p.getNumInteriorRing(); |
| 343 |
CoordinateSequence cs = p.getExteriorRing().getCoordinateSequence(); |
| 344 |
int size = cs.size(); |
| 345 |
// Size may be 0 (EMPTY) or 4+ |
| 346 |
if (size >= 1 && size <= 3) { |
| 347 |
throw new IllegalArgumentException(); |
| 348 |
} |
| 349 |
if (size == 0 && numInner > 0) { |
| 350 |
throw new IllegalArgumentException(); |
| 351 |
} |
| 352 |
target.startPolygon(numInner, size); |
| 353 |
if (size > 0) { |
| 354 |
addRing(cs, target, size); |
| 355 |
for (int i = 0; i < numInner; i++) { |
| 356 |
cs = p.getInteriorRingN(i).getCoordinateSequence(); |
| 357 |
size = cs.size(); |
| 358 |
// Size may be 0 (EMPTY) or 4+ |
| 359 |
if (size >= 1 && size <= 3) { |
| 360 |
throw new IllegalArgumentException(); |
| 361 |
} |
| 362 |
target.startPolygonInner(size); |
| 363 |
addRing(cs, target, size); |
| 364 |
} |
| 365 |
target.endNonEmptyPolygon(); |
| 366 |
} |
| 367 |
target.endObject(POLYGON); |
| 368 |
} else if (geometry instanceof GeometryCollection) { |
| 369 |
if (parentType != 0 && parentType != GEOMETRY_COLLECTION) { |
| 370 |
throw new IllegalArgumentException(); |
| 371 |
} |
| 372 |
GeometryCollection gc = (GeometryCollection) geometry; |
| 373 |
int type; |
| 374 |
if (gc instanceof MultiPoint) { |
| 375 |
type = MULTI_POINT; |
| 376 |
} else if (gc instanceof MultiLineString) { |
| 377 |
type = MULTI_LINE_STRING; |
| 378 |
} else if (gc instanceof MultiPolygon) { |
| 379 |
type = MULTI_POLYGON; |
| 380 |
} else { |
| 381 |
type = GEOMETRY_COLLECTION; |
| 382 |
} |
| 383 |
int numItems = gc.getNumGeometries(); |
| 384 |
target.startCollection(type, numItems); |
| 385 |
for (int i = 0; i < numItems; i++) { |
| 386 |
Target innerTarget = target.startCollectionItem(i, numItems); |
| 387 |
parseGeometry(gc.getGeometryN(i), innerTarget, type); |
| 388 |
target.endCollectionItem(innerTarget, type, i, numItems); |
| 389 |
} |
| 390 |
target.endObject(type); |
| 391 |
} else { |
| 392 |
throw new IllegalArgumentException(); |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
private static void addRing(CoordinateSequence sequence, Target target, int size) { |
| 397 |
// 0 or 4+ are valid |
| 398 |
if (size >= 4) { |
| 399 |
double startX = toCanonicalDouble(sequence.getX(0)), startY = toCanonicalDouble(sequence.getY(0)); |
| 400 |
addCoordinate(sequence, target, 0, size, startX, startY); |
| 401 |
for (int i = 1; i < size - 1; i++) { |
| 402 |
addCoordinate(sequence, target, i, size); |
| 403 |
} |
| 404 |
double endX = toCanonicalDouble(sequence.getX(size - 1)), // |
| 405 |
endY = toCanonicalDouble(sequence.getY(size - 1)); |
| 406 |
/* |
| 407 |
* TODO OGC 06-103r4 determines points as equal if they have the |
| 408 |
* same X and Y coordinates. Should we check Z and M here too? |
| 409 |
*/ |
| 410 |
if (startX != endX || startY != endY) { |
| 411 |
throw new IllegalArgumentException(); |
| 412 |
} |
| 413 |
addCoordinate(sequence, target, size - 1, size, endX, endY); |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
private static void addCoordinate(CoordinateSequence sequence, Target target, int index, int total) { |
| 418 |
addCoordinate(sequence, target, index, total, toCanonicalDouble(sequence.getX(index)), |
| 419 |
toCanonicalDouble(sequence.getY(index))); |
| 420 |
} |
| 421 |
|
| 422 |
private static void addCoordinate(CoordinateSequence sequence, Target target, int index, int total, double x, |
| 423 |
double y) { |
| 424 |
double z = toCanonicalDouble(sequence.getZ(index)); |
| 425 |
double m = toCanonicalDouble(sequence.getM(index)); |
| 426 |
target.addCoordinate(x, y, z, m, index, total); |
| 427 |
} |
| 428 |
|
| 429 |
/** |
| 430 |
* Determines a dimension system of a JTS Geometry object. |
| 431 |
* |
| 432 |
* @param geometry |
| 433 |
* geometry to parse |
| 434 |
* @return the dimension system |
| 435 |
*/ |
| 436 |
public static int getDimensionSystem(Geometry geometry) { |
| 437 |
int d = getDimensionSystem1(geometry); |
| 438 |
return d >= 0 ? d : 0; |
| 439 |
} |
| 440 |
|
| 441 |
private static int getDimensionSystem1(Geometry geometry) { |
| 442 |
int d; |
| 443 |
if (geometry instanceof Point) { |
| 444 |
d = getDimensionSystemFromSequence(((Point) geometry).getCoordinateSequence()); |
| 445 |
} else if (geometry instanceof LineString) { |
| 446 |
d = getDimensionSystemFromSequence(((LineString) geometry).getCoordinateSequence()); |
| 447 |
} else if (geometry instanceof Polygon) { |
| 448 |
d = getDimensionSystemFromSequence(((Polygon) geometry).getExteriorRing().getCoordinateSequence()); |
| 449 |
} else if (geometry instanceof GeometryCollection) { |
| 450 |
d = -1; |
| 451 |
GeometryCollection gc = (GeometryCollection) geometry; |
| 452 |
for (int i = 0, l = gc.getNumGeometries(); i < l; i++) { |
| 453 |
d = getDimensionSystem1(gc.getGeometryN(i)); |
| 454 |
if (d >= 0) { |
| 455 |
break; |
| 456 |
} |
| 457 |
} |
| 458 |
} else { |
| 459 |
throw new IllegalArgumentException(); |
| 460 |
} |
| 461 |
return d; |
| 462 |
} |
| 463 |
|
| 464 |
private static int getDimensionSystemFromSequence(CoordinateSequence sequence) { |
| 465 |
int size = sequence.size(); |
| 466 |
if (size > 0) { |
| 467 |
for (int i = 0; i < size; i++) { |
| 468 |
int d = getDimensionSystemFromCoordinate(sequence, i); |
| 469 |
if (d >= 0) { |
| 470 |
return d; |
| 471 |
} |
| 472 |
} |
| 473 |
} |
| 474 |
return (sequence.hasZ() ? DIMENSION_SYSTEM_XYZ : 0) | (sequence.hasM() ? DIMENSION_SYSTEM_XYM : 0); |
| 475 |
} |
| 476 |
|
| 477 |
private static int getDimensionSystemFromCoordinate(CoordinateSequence sequence, int index) { |
| 478 |
if (Double.isNaN(sequence.getX(index))) { |
| 479 |
return -1; |
| 480 |
} |
| 481 |
return (!Double.isNaN(sequence.getZ(index)) ? DIMENSION_SYSTEM_XYZ : 0) |
| 482 |
| (!Double.isNaN(sequence.getM(index)) ? DIMENSION_SYSTEM_XYM : 0); |
| 483 |
} |
| 484 |
|
| 485 |
private JTSUtils() { |
| 486 |
} |
| 487 |
|
| 488 |
} |