use of com.mongodb.client.model.geojson.GeometryCollection in project mongo-java-driver by mongodb.
the class GeometryDecoderHelper method decodeGeometryCollection.
private static GeometryCollection decodeGeometryCollection(final BsonReader reader) {
String type = null;
List<? extends Geometry> geometries = null;
CoordinateReferenceSystem crs = null;
reader.readStartDocument();
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
String key = reader.readName();
if (key.equals("type")) {
type = reader.readString();
} else if (key.equals("geometries")) {
geometries = decodeGeometries(reader);
} else if (key.equals("crs")) {
crs = decodeCoordinateReferenceSystem(reader);
} else {
throw new CodecConfigurationException(format("Unexpected key '%s' found when decoding a GeoJSON Polygon", key));
}
}
reader.readEndDocument();
if (type == null) {
throw new CodecConfigurationException("Invalid GeometryCollection, document contained no type information.");
} else if (!type.equals("GeometryCollection")) {
throw new CodecConfigurationException(format("Invalid GeometryCollection, found type '%s'.", type));
} else if (geometries == null) {
throw new CodecConfigurationException("Invalid GeometryCollection, missing geometries.");
}
return crs != null ? new GeometryCollection(crs, geometries) : new GeometryCollection(geometries);
}
use of com.mongodb.client.model.geojson.GeometryCollection in project mongo-java-driver by mongodb.
the class GeometryEncoderHelper method encodeGeometry.
@SuppressWarnings("unchecked")
static void encodeGeometry(final BsonWriter writer, final Geometry value, final EncoderContext encoderContext, final CodecRegistry registry) {
writer.writeStartDocument();
writer.writeString("type", value.getType().getTypeName());
if (value instanceof GeometryCollection) {
writer.writeName("geometries");
encodeGeometryCollection(writer, (GeometryCollection) value, encoderContext, registry);
} else {
writer.writeName("coordinates");
if (value instanceof Point) {
encodePoint(writer, (Point) value);
} else if (value instanceof MultiPoint) {
encodeMultiPoint(writer, (MultiPoint) value);
} else if (value instanceof Polygon) {
encodePolygon(writer, (Polygon) value);
} else if (value instanceof MultiPolygon) {
encodeMultiPolygon(writer, (MultiPolygon) value);
} else if (value instanceof LineString) {
encodeLineString(writer, (LineString) value);
} else if (value instanceof MultiLineString) {
encodeMultiLineString(writer, (MultiLineString) value);
} else {
throw new CodecConfigurationException(format("Unsupported Geometry: %s", value));
}
}
encodeCoordinateReferenceSystem(writer, value, encoderContext, registry);
writer.writeEndDocument();
}
Aggregations