Search in sources :

Example 1 with MultiLineString

use of com.mongodb.client.model.geojson.MultiLineString 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 MultiLineString

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

the class GeometryDecoderHelper method decodeMultiLineString.

private static MultiLineString decodeMultiLineString(final BsonReader reader) {
    String type = null;
    List<List<Position>> coordinates = 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("coordinates")) {
            coordinates = decodeMultiCoordinates(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 MultiLineString, document contained no type information.");
    } else if (!type.equals("MultiLineString")) {
        throw new CodecConfigurationException(format("Invalid MultiLineString, found type '%s'.", type));
    } else if (coordinates == null) {
        throw new CodecConfigurationException("Invalid MultiLineString, missing coordinates.");
    }
    return crs != null ? new MultiLineString(crs, coordinates) : new MultiLineString(coordinates);
}
Also used : MultiLineString(com.mongodb.client.model.geojson.MultiLineString) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) ArrayList(java.util.ArrayList) List(java.util.List) LineString(com.mongodb.client.model.geojson.LineString) MultiLineString(com.mongodb.client.model.geojson.MultiLineString) CoordinateReferenceSystem(com.mongodb.client.model.geojson.CoordinateReferenceSystem) NamedCoordinateReferenceSystem(com.mongodb.client.model.geojson.NamedCoordinateReferenceSystem)

Example 3 with MultiLineString

use of com.mongodb.client.model.geojson.MultiLineString 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();
}
Also used : GeometryCollection(com.mongodb.client.model.geojson.GeometryCollection) MultiPoint(com.mongodb.client.model.geojson.MultiPoint) MultiLineString(com.mongodb.client.model.geojson.MultiLineString) MultiPolygon(com.mongodb.client.model.geojson.MultiPolygon) LineString(com.mongodb.client.model.geojson.LineString) MultiLineString(com.mongodb.client.model.geojson.MultiLineString) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) Point(com.mongodb.client.model.geojson.Point) MultiPoint(com.mongodb.client.model.geojson.MultiPoint) Polygon(com.mongodb.client.model.geojson.Polygon) MultiPolygon(com.mongodb.client.model.geojson.MultiPolygon)

Aggregations

LineString (com.mongodb.client.model.geojson.LineString)3 MultiLineString (com.mongodb.client.model.geojson.MultiLineString)3 CodecConfigurationException (org.bson.codecs.configuration.CodecConfigurationException)3 CoordinateReferenceSystem (com.mongodb.client.model.geojson.CoordinateReferenceSystem)1 Geometry (com.mongodb.client.model.geojson.Geometry)1 GeometryCollection (com.mongodb.client.model.geojson.GeometryCollection)1 MultiPoint (com.mongodb.client.model.geojson.MultiPoint)1 MultiPolygon (com.mongodb.client.model.geojson.MultiPolygon)1 NamedCoordinateReferenceSystem (com.mongodb.client.model.geojson.NamedCoordinateReferenceSystem)1 Point (com.mongodb.client.model.geojson.Point)1 Polygon (com.mongodb.client.model.geojson.Polygon)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 BsonReaderMark (org.bson.BsonReaderMark)1