Search in sources :

Example 1 with Geometry

use of com.mongodb.client.model.geojson.Geometry in project mongo-java-driver by mongodb.

the class GeometryDecoderHelper method decodeGeometry.

private static Geometry decodeGeometry(final BsonReader reader) {
    String type = null;
    BsonReaderMark mark = reader.getMark();
    validateIsDocument(reader);
    reader.readStartDocument();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        String key = reader.readName();
        if (key.equals("type")) {
            type = reader.readString();
            break;
        } else {
            reader.skipValue();
        }
    }
    mark.reset();
    if (type == null) {
        throw new CodecConfigurationException("Invalid Geometry item, document contained no type information.");
    }
    Geometry geometry = null;
    if (type.equals("Point")) {
        geometry = decodePoint(reader);
    } else if (type.equals("MultiPoint")) {
        geometry = decodeMultiPoint(reader);
    } else if (type.equals("Polygon")) {
        geometry = decodePolygon(reader);
    } else if (type.equals("MultiPolygon")) {
        geometry = decodeMultiPolygon(reader);
    } else if (type.equals("LineString")) {
        geometry = decodeLineString(reader);
    } else if (type.equals("MultiLineString")) {
        geometry = decodeMultiLineString(reader);
    } else if (type.equals("GeometryCollection")) {
        geometry = decodeGeometryCollection(reader);
    } else {
        throw new CodecConfigurationException(format("Invalid Geometry item, found type '%s'.", type));
    }
    return geometry;
}
Also used : Geometry(com.mongodb.client.model.geojson.Geometry) BsonReaderMark(org.bson.BsonReaderMark) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) LineString(com.mongodb.client.model.geojson.LineString) MultiLineString(com.mongodb.client.model.geojson.MultiLineString)

Example 2 with Geometry

use of com.mongodb.client.model.geojson.Geometry in project mongo-java-driver by mongodb.

the class GeometryCollectionCodec method encode.

@Override
public void encode(final BsonWriter writer, final GeometryCollection value, final EncoderContext encoderContext) {
    writer.writeStartDocument();
    encodeType(writer, value);
    writer.writeName("geometries");
    writer.writeStartArray();
    for (Geometry geometry : value.getGeometries()) {
        encodeGeometry(writer, geometry, encoderContext);
    }
    writer.writeEndArray();
    encodeCoordinateReferenceSystem(writer, value, encoderContext, registry);
    writer.writeEndDocument();
}
Also used : Geometry(com.mongodb.client.model.geojson.Geometry)

Example 3 with Geometry

use of com.mongodb.client.model.geojson.Geometry in project mongo-java-driver by mongodb.

the class GeometryDecoderHelper method decodeGeometries.

private static List<? extends Geometry> decodeGeometries(final BsonReader reader) {
    validateIsArray(reader);
    reader.readStartArray();
    List<Geometry> values = new ArrayList<Geometry>();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        Geometry geometry = decodeGeometry(reader);
        values.add(geometry);
    }
    reader.readEndArray();
    return values;
}
Also used : Geometry(com.mongodb.client.model.geojson.Geometry) ArrayList(java.util.ArrayList)

Example 4 with Geometry

use of com.mongodb.client.model.geojson.Geometry in project mongo-java-driver by mongodb.

the class GeometryEncoderHelper method encodeGeometryCollection.

private static void encodeGeometryCollection(final BsonWriter writer, final GeometryCollection value, final EncoderContext encoderContext, final CodecRegistry registry) {
    writer.writeStartArray();
    for (Geometry geometry : value.getGeometries()) {
        encodeGeometry(writer, geometry, encoderContext, registry);
    }
    writer.writeEndArray();
}
Also used : Geometry(com.mongodb.client.model.geojson.Geometry)

Aggregations

Geometry (com.mongodb.client.model.geojson.Geometry)4 LineString (com.mongodb.client.model.geojson.LineString)1 MultiLineString (com.mongodb.client.model.geojson.MultiLineString)1 ArrayList (java.util.ArrayList)1 BsonReaderMark (org.bson.BsonReaderMark)1 CodecConfigurationException (org.bson.codecs.configuration.CodecConfigurationException)1