| 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_XYM; |
| 9 |
import static org.h2.util.geometry.GeometryUtils.DIMENSION_SYSTEM_XYZ; |
| 10 |
import static org.h2.util.geometry.GeometryUtils.GEOMETRY_COLLECTION; |
| 11 |
import static org.h2.util.geometry.GeometryUtils.LINE_STRING; |
| 12 |
import static org.h2.util.geometry.GeometryUtils.M; |
| 13 |
import static org.h2.util.geometry.GeometryUtils.MULTI_LINE_STRING; |
| 14 |
import static org.h2.util.geometry.GeometryUtils.MULTI_POINT; |
| 15 |
import static org.h2.util.geometry.GeometryUtils.MULTI_POLYGON; |
| 16 |
import static org.h2.util.geometry.GeometryUtils.POINT; |
| 17 |
import static org.h2.util.geometry.GeometryUtils.POLYGON; |
| 18 |
import static org.h2.util.geometry.GeometryUtils.X; |
| 19 |
import static org.h2.util.geometry.GeometryUtils.Y; |
| 20 |
import static org.h2.util.geometry.GeometryUtils.Z; |
| 21 |
|
| 22 |
import java.io.ByteArrayOutputStream; |
| 23 |
import java.math.BigDecimal; |
| 24 |
|
| 25 |
import org.h2.api.ErrorCode; |
| 26 |
import org.h2.message.DbException; |
| 27 |
import org.h2.util.geometry.EWKBUtils.EWKBTarget; |
| 28 |
import org.h2.util.geometry.GeometryUtils.DimensionSystemTarget; |
| 29 |
import org.h2.util.geometry.GeometryUtils.Target; |
| 30 |
import org.h2.util.json.JSONArray; |
| 31 |
import org.h2.util.json.JSONByteArrayTarget; |
| 32 |
import org.h2.util.json.JSONBytesSource; |
| 33 |
import org.h2.util.json.JSONNull; |
| 34 |
import org.h2.util.json.JSONNumber; |
| 35 |
import org.h2.util.json.JSONObject; |
| 36 |
import org.h2.util.json.JSONString; |
| 37 |
import org.h2.util.json.JSONValue; |
| 38 |
import org.h2.util.json.JSONValueTarget; |
| 39 |
|
| 40 |
/** |
| 41 |
* GeoJson format support for GEOMETRY data type. |
| 42 |
*/ |
| 43 |
public final class GeoJsonUtils { |
| 44 |
|
| 45 |
/** |
| 46 |
* 0-based type names of geometries, subtract 1 from type code to get index |
| 47 |
* in this array. |
| 48 |
*/ |
| 49 |
static final String[] TYPES = { // |
| 50 |
"Point", // |
| 51 |
"LineString", // |
| 52 |
"Polygon", // |
| 53 |
"MultiPoint", // |
| 54 |
"MultiLineString", // |
| 55 |
"MultiPolygon", // |
| 56 |
"GeometryCollection", // |
| 57 |
}; |
| 58 |
|
| 59 |
/** |
| 60 |
* Converter output target that writes a GeoJson. |
| 61 |
*/ |
| 62 |
public static final class GeoJsonTarget extends Target { |
| 63 |
|
| 64 |
private final JSONByteArrayTarget output; |
| 65 |
|
| 66 |
private final int dimensionSystem; |
| 67 |
|
| 68 |
private int type; |
| 69 |
|
| 70 |
private boolean inMulti, inMultiLine, wasEmpty; |
| 71 |
|
| 72 |
/** |
| 73 |
* Creates new GeoJson output target. |
| 74 |
* |
| 75 |
* @param output |
| 76 |
* output JSON target |
| 77 |
* @param dimensionSystem |
| 78 |
* dimension system to use |
| 79 |
*/ |
| 80 |
public GeoJsonTarget(JSONByteArrayTarget output, int dimensionSystem) { |
| 81 |
if (dimensionSystem == DIMENSION_SYSTEM_XYM) { |
| 82 |
throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, |
| 83 |
"M (XYM) dimension system is not supported in GeoJson"); |
| 84 |
} |
| 85 |
this.output = output; |
| 86 |
this.dimensionSystem = dimensionSystem; |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
protected void startPoint() { |
| 91 |
type = POINT; |
| 92 |
wasEmpty = false; |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
protected void startLineString(int numPoints) { |
| 97 |
writeHeader(LINE_STRING); |
| 98 |
if (numPoints == 0) { |
| 99 |
output.endArray(); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
@Override |
| 104 |
protected void startPolygon(int numInner, int numPoints) { |
| 105 |
writeHeader(POLYGON); |
| 106 |
if (numPoints == 0) { |
| 107 |
output.endArray(); |
| 108 |
} else { |
| 109 |
output.startArray(); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
protected void startPolygonInner(int numInner) { |
| 115 |
output.startArray(); |
| 116 |
if (numInner == 0) { |
| 117 |
output.endArray(); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
@Override |
| 122 |
protected void endNonEmptyPolygon() { |
| 123 |
output.endArray(); |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
protected void startCollection(int type, int numItems) { |
| 128 |
writeHeader(type); |
| 129 |
if (type != GEOMETRY_COLLECTION) { |
| 130 |
inMulti = true; |
| 131 |
if (type == MULTI_LINE_STRING || type == MULTI_POLYGON) { |
| 132 |
inMultiLine = true; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
@Override |
| 138 |
protected Target startCollectionItem(int index, int total) { |
| 139 |
if (inMultiLine) { |
| 140 |
output.startArray(); |
| 141 |
} |
| 142 |
return this; |
| 143 |
} |
| 144 |
|
| 145 |
@Override |
| 146 |
protected void endObject(int type) { |
| 147 |
switch (type) { |
| 148 |
case MULTI_POINT: |
| 149 |
case MULTI_LINE_STRING: |
| 150 |
case MULTI_POLYGON: |
| 151 |
inMultiLine = inMulti = false; |
| 152 |
//$FALL-THROUGH$ |
| 153 |
case GEOMETRY_COLLECTION: |
| 154 |
output.endArray(); |
| 155 |
} |
| 156 |
if (!inMulti && !wasEmpty) { |
| 157 |
output.endObject(); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
private void writeHeader(int type) { |
| 162 |
this.type = type; |
| 163 |
wasEmpty = false; |
| 164 |
if (!inMulti) { |
| 165 |
writeStartObject(type); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
@Override |
| 170 |
protected void addCoordinate(double x, double y, double z, double m, int index, int total) { |
| 171 |
if (type == POINT) { |
| 172 |
if (Double.isNaN(x) && Double.isNaN(y) && Double.isNaN(z) && Double.isNaN(m)) { |
| 173 |
wasEmpty = true; |
| 174 |
output.valueNull(); |
| 175 |
return; |
| 176 |
} |
| 177 |
if (!inMulti) { |
| 178 |
writeStartObject(POINT); |
| 179 |
} |
| 180 |
} |
| 181 |
output.startArray(); |
| 182 |
writeDouble(x); |
| 183 |
writeDouble(y); |
| 184 |
if ((dimensionSystem & DIMENSION_SYSTEM_XYZ) != 0) { |
| 185 |
writeDouble(z); |
| 186 |
} |
| 187 |
if ((dimensionSystem & DIMENSION_SYSTEM_XYM) != 0) { |
| 188 |
writeDouble(m); |
| 189 |
} |
| 190 |
output.endArray(); |
| 191 |
if (type != POINT && index + 1 == total) { |
| 192 |
output.endArray(); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
private void writeStartObject(int type) { |
| 197 |
output.startObject(); |
| 198 |
output.member("type"); |
| 199 |
output.valueString(TYPES[type - 1]); |
| 200 |
output.member(type != GEOMETRY_COLLECTION ? "coordinates" : "geometries"); |
| 201 |
if (type != POINT) { |
| 202 |
output.startArray(); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
private void writeDouble(double v) { |
| 207 |
output.valueNumber(BigDecimal.valueOf(GeometryUtils.checkFinite(v)).stripTrailingZeros()); |
| 208 |
} |
| 209 |
|
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Converts EWKB with known dimension system to GeoJson. |
| 214 |
* |
| 215 |
* @param ewkb |
| 216 |
* geometry object in EWKB format |
| 217 |
* @param dimensionSystem |
| 218 |
* dimension system of the specified object, may be the same or |
| 219 |
* smaller than its real dimension system. M dimension system is |
| 220 |
* not supported. |
| 221 |
* @return GeoJson representation of the specified geometry |
| 222 |
* @throws DbException |
| 223 |
* on unsupported dimension system |
| 224 |
*/ |
| 225 |
public static byte[] ewkbToGeoJson(byte[] ewkb, int dimensionSystem) { |
| 226 |
JSONByteArrayTarget output = new JSONByteArrayTarget(); |
| 227 |
GeoJsonTarget target = new GeoJsonTarget(output, dimensionSystem); |
| 228 |
EWKBUtils.parseEWKB(ewkb, target); |
| 229 |
return output.getResult(); |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Converts EWKB with known dimension system to GeoJson. |
| 234 |
* |
| 235 |
* @param json |
| 236 |
* geometry object in GeoJson format |
| 237 |
* @param srid |
| 238 |
* the SRID of geometry |
| 239 |
* @return GeoJson representation of the specified geometry |
| 240 |
* @throws DbException |
| 241 |
* on unsupported dimension system |
| 242 |
*/ |
| 243 |
public static byte[] geoJsonToEwkb(byte[] json, int srid) { |
| 244 |
JSONValue v = JSONBytesSource.parse(json, new JSONValueTarget()); |
| 245 |
DimensionSystemTarget dst = new DimensionSystemTarget(); |
| 246 |
parse(v, dst); |
| 247 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 248 |
EWKBTarget target = new EWKBTarget(baos, dst.getDimensionSystem()); |
| 249 |
target.init(srid); |
| 250 |
parse(v, target); |
| 251 |
return baos.toByteArray(); |
| 252 |
} |
| 253 |
|
| 254 |
private static void parse(JSONValue v, GeometryUtils.Target target) { |
| 255 |
if (v instanceof JSONNull) { |
| 256 |
target.startPoint(); |
| 257 |
target.addCoordinate(Double.NaN, Double.NaN, Double.NaN, Double.NaN, 0, 1); |
| 258 |
target.endObject(POINT); |
| 259 |
} else if (v instanceof JSONObject) { |
| 260 |
JSONObject o = (JSONObject) v; |
| 261 |
JSONValue t = o.getFirst("type"); |
| 262 |
if (!(t instanceof JSONString)) { |
| 263 |
throw new IllegalArgumentException(); |
| 264 |
} |
| 265 |
switch (((JSONString) t).getString()) { |
| 266 |
case "Point": |
| 267 |
parse(o, target, POINT); |
| 268 |
break; |
| 269 |
case "LineString": |
| 270 |
parse(o, target, LINE_STRING); |
| 271 |
break; |
| 272 |
case "Polygon": |
| 273 |
parse(o, target, POLYGON); |
| 274 |
break; |
| 275 |
case "MultiPoint": |
| 276 |
parse(o, target, MULTI_POINT); |
| 277 |
break; |
| 278 |
case "MultiLineString": |
| 279 |
parse(o, target, MULTI_LINE_STRING); |
| 280 |
break; |
| 281 |
case "MultiPolygon": |
| 282 |
parse(o, target, MULTI_POLYGON); |
| 283 |
break; |
| 284 |
case "GeometryCollection": |
| 285 |
parseGeometryCollection(o, target); |
| 286 |
break; |
| 287 |
default: |
| 288 |
throw new IllegalArgumentException(); |
| 289 |
} |
| 290 |
} else { |
| 291 |
throw new IllegalArgumentException(); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
private static void parse(JSONObject o, Target target, int type) { |
| 296 |
JSONValue t = o.getFirst("coordinates"); |
| 297 |
if (!(t instanceof JSONArray)) { |
| 298 |
throw new IllegalArgumentException(); |
| 299 |
} |
| 300 |
JSONArray a = (JSONArray) t; |
| 301 |
switch (type) { |
| 302 |
case POINT: |
| 303 |
target.startPoint(); |
| 304 |
parseCoordinate(a, target, 0, 1); |
| 305 |
target.endObject(POINT); |
| 306 |
break; |
| 307 |
case LINE_STRING: { |
| 308 |
parseLineString(a, target); |
| 309 |
break; |
| 310 |
} |
| 311 |
case POLYGON: { |
| 312 |
parsePolygon(a, target); |
| 313 |
break; |
| 314 |
} |
| 315 |
case MULTI_POINT: { |
| 316 |
JSONValue[] points = a.getArray(); |
| 317 |
int numPoints = points.length; |
| 318 |
target.startCollection(MULTI_POINT, numPoints); |
| 319 |
for (int i = 0; i < numPoints; i++) { |
| 320 |
target.startPoint(); |
| 321 |
parseCoordinate(points[i], target, 0, 1); |
| 322 |
target.endObject(POINT); |
| 323 |
target.endCollectionItem(target, MULTI_POINT, i, numPoints); |
| 324 |
} |
| 325 |
target.endObject(MULTI_POINT); |
| 326 |
break; |
| 327 |
} |
| 328 |
case MULTI_LINE_STRING: { |
| 329 |
JSONValue[] strings = a.getArray(); |
| 330 |
int numStrings = strings.length; |
| 331 |
target.startCollection(MULTI_LINE_STRING, numStrings); |
| 332 |
for (int i = 0; i < numStrings; i++) { |
| 333 |
JSONValue string = strings[i]; |
| 334 |
if (!(string instanceof JSONArray)) { |
| 335 |
throw new IllegalArgumentException(); |
| 336 |
} |
| 337 |
parseLineString((JSONArray) string, target); |
| 338 |
target.endCollectionItem(target, MULTI_LINE_STRING, i, numStrings); |
| 339 |
} |
| 340 |
target.endObject(MULTI_LINE_STRING); |
| 341 |
break; |
| 342 |
} |
| 343 |
case MULTI_POLYGON: { |
| 344 |
JSONValue[] polygons = a.getArray(); |
| 345 |
int numPolygons = polygons.length; |
| 346 |
target.startCollection(MULTI_POLYGON, numPolygons); |
| 347 |
for (int i = 0; i < numPolygons; i++) { |
| 348 |
JSONValue string = polygons[i]; |
| 349 |
if (!(string instanceof JSONArray)) { |
| 350 |
throw new IllegalArgumentException(); |
| 351 |
} |
| 352 |
parsePolygon((JSONArray) string, target); |
| 353 |
target.endCollectionItem(target, MULTI_POLYGON, i, numPolygons); |
| 354 |
} |
| 355 |
target.endObject(MULTI_POLYGON); |
| 356 |
break; |
| 357 |
} |
| 358 |
default: |
| 359 |
throw new IllegalArgumentException(); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
private static void parseGeometryCollection(JSONObject o, Target target) { |
| 364 |
JSONValue t = o.getFirst("geometries"); |
| 365 |
if (!(t instanceof JSONArray)) { |
| 366 |
throw new IllegalArgumentException(); |
| 367 |
} |
| 368 |
JSONArray a = (JSONArray) t; |
| 369 |
JSONValue[] geometries = a.getArray(); |
| 370 |
int numGeometries = geometries.length; |
| 371 |
target.startCollection(GEOMETRY_COLLECTION, numGeometries); |
| 372 |
for (int i = 0; i < numGeometries; i++) { |
| 373 |
JSONValue geometry = geometries[i]; |
| 374 |
parse(geometry, target); |
| 375 |
target.endCollectionItem(target, GEOMETRY_COLLECTION, i, numGeometries); |
| 376 |
} |
| 377 |
target.endObject(GEOMETRY_COLLECTION); |
| 378 |
} |
| 379 |
|
| 380 |
private static void parseLineString(JSONArray a, Target target) { |
| 381 |
JSONValue[] points = a.getArray(); |
| 382 |
int numPoints = points.length; |
| 383 |
target.startLineString(numPoints); |
| 384 |
for (int i = 0; i < numPoints; i++) { |
| 385 |
parseCoordinate(points[i], target, i, numPoints); |
| 386 |
} |
| 387 |
target.endObject(LINE_STRING); |
| 388 |
} |
| 389 |
|
| 390 |
private static void parsePolygon(JSONArray a, Target target) { |
| 391 |
JSONValue[] rings = a.getArray(); |
| 392 |
int numRings = rings.length; |
| 393 |
if (numRings == 0) { |
| 394 |
target.startPolygon(0, 0); |
| 395 |
} else { |
| 396 |
JSONValue ring = rings[0]; |
| 397 |
if (!(ring instanceof JSONArray)) { |
| 398 |
throw new IllegalArgumentException(); |
| 399 |
} |
| 400 |
JSONValue[] points = ((JSONArray) ring).getArray(); |
| 401 |
target.startPolygon(numRings - 1, points.length); |
| 402 |
parseRing(points, target); |
| 403 |
for (int i = 1; i < numRings; i++) { |
| 404 |
ring = rings[i]; |
| 405 |
if (!(ring instanceof JSONArray)) { |
| 406 |
throw new IllegalArgumentException(); |
| 407 |
} |
| 408 |
points = ((JSONArray) ring).getArray(); |
| 409 |
target.startPolygonInner(points.length); |
| 410 |
parseRing(points, target); |
| 411 |
} |
| 412 |
target.endNonEmptyPolygon(); |
| 413 |
} |
| 414 |
target.endObject(POLYGON); |
| 415 |
} |
| 416 |
|
| 417 |
private static void parseRing(JSONValue[] points, Target target) { |
| 418 |
int numPoints = points.length; |
| 419 |
for (int i = 0; i < numPoints; i++) { |
| 420 |
parseCoordinate(points[i], target, i, numPoints); |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
private static void parseCoordinate(JSONValue v, Target target, int index, int total) { |
| 425 |
if (v instanceof JSONNull) { |
| 426 |
target.addCoordinate(Double.NaN, Double.NaN, Double.NaN, Double.NaN, 0, 1); |
| 427 |
return; |
| 428 |
} |
| 429 |
if (!(v instanceof JSONArray)) { |
| 430 |
throw new IllegalArgumentException(); |
| 431 |
} |
| 432 |
JSONValue[] values = ((JSONArray) v).getArray(); |
| 433 |
int length = values.length; |
| 434 |
if (length < 2) { |
| 435 |
throw new IllegalArgumentException(); |
| 436 |
} |
| 437 |
target.addCoordinate(readCoordinate(values, X), readCoordinate(values, Y), readCoordinate(values, Z), |
| 438 |
readCoordinate(values, M), index, total); |
| 439 |
} |
| 440 |
|
| 441 |
private static double readCoordinate(JSONValue[] values, int index) { |
| 442 |
if (index >= values.length) { |
| 443 |
return Double.NaN; |
| 444 |
} |
| 445 |
JSONValue v = values[index]; |
| 446 |
if (!(v instanceof JSONNumber)) { |
| 447 |
throw new IllegalArgumentException(); |
| 448 |
} |
| 449 |
return ((JSONNumber) v).getBigDecimal().doubleValue(); |
| 450 |
} |
| 451 |
|
| 452 |
private GeoJsonUtils() { |
| 453 |
} |
| 454 |
|
| 455 |
} |