+ /**
+ * Writes the {@link Envelope} of a {@link Geometry} as a bbox GeoJSON object.
+ */
+ public static void writeBBox(JsonGenerator generator, Geometry geometry) {
+ generator.writeStartArray(BBOX);
+ Envelope envelope = geometry.getEnvelopeInternal();
+ generator.write(envelope.getMinX());
+ generator.write(envelope.getMinY());
+ generator.write(envelope.getMaxX());
+ generator.write(envelope.getMaxY());
+ generator.writeEnd();
+ }
+
+ /*
+ * READ
+ */
+ /** Reads a geometry from the geometry object of a GEoJSON feature. */
+ @SuppressWarnings("unchecked")
+ public static <T extends Geometry> T readGeometry(JsonObject geom, Class<T> clss) {
+ String type = geom.getString(TYPE);
+ JsonArray coordinates = geom.getJsonArray(COORDINATES);
+ Geometry res = switch (type) {
+ case POINT_TYPE: {
+ Coordinate coord = readCoordinate(coordinates);
+ yield JTS.GEOMETRY_FACTORY_WGS84.createPoint(coord);
+ }
+ case LINE_STRING_TYPE: {
+ Coordinate[] coords = readCoordinates(coordinates);
+ yield JTS.GEOMETRY_FACTORY_WGS84.createLineString(coords);
+ }
+ case POLYGON_TYPE: {
+ assert coordinates.size() > 0;
+ LinearRing exterior = JTS.GEOMETRY_FACTORY_WGS84
+ .createLinearRing(readCoordinates(coordinates.getJsonArray(0)));
+ LinearRing[] holes = new LinearRing[coordinates.size() - 1];
+ for (int i = 0; i < coordinates.size() - 1; i++) {
+ holes[i] = JTS.GEOMETRY_FACTORY_WGS84
+ .createLinearRing(readCoordinates(coordinates.getJsonArray(i + 1)));
+ }
+ yield JTS.GEOMETRY_FACTORY_WGS84.createPolygon(exterior, holes);
+ }
+ default:
+ throw new IllegalArgumentException("Unexpected value: " + type);
+ };
+// res.normalize();
+ return (T)res;
+ }
+
+ /** Reads a coordinate sequence [[lat,lon],[lat,lon]]. */
+ public static Coordinate readCoordinate(JsonArray arr) {
+ assert arr.size() >= 2;
+ return new Coordinate(arr.getJsonNumber(0).doubleValue(), arr.getJsonNumber(1).doubleValue());
+ }
+
+ /** Reads a coordinate pair [lat,lon]. */
+ public static Coordinate[] readCoordinates(JsonArray arr) {
+ Coordinate[] coords = new Coordinate[arr.size()];
+ for (int i = 0; i < arr.size(); i++)
+ coords[i] = readCoordinate(arr.getJsonArray(i));
+ return coords;
+ }
+