Search in sources :

Example 1 with CoordinateReferenceSystem

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

the class GeometryDecoderHelper method decodePoint.

private static Point decodePoint(final BsonReader reader) {
    String type = null;
    Position position = 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")) {
            position = decodePosition(reader);
        } else if (key.equals("crs")) {
            crs = decodeCoordinateReferenceSystem(reader);
        } else {
            throw new CodecConfigurationException(format("Unexpected key '%s' found when decoding a GeoJSON point", key));
        }
    }
    reader.readEndDocument();
    if (type == null) {
        throw new CodecConfigurationException("Invalid Point, document contained no type information.");
    } else if (!type.equals("Point")) {
        throw new CodecConfigurationException(format("Invalid Point, found type '%s'.", type));
    } else if (position == null) {
        throw new CodecConfigurationException("Invalid Point, missing position coordinates.");
    }
    return crs != null ? new Point(crs, position) : new Point(position);
}
Also used : Position(com.mongodb.client.model.geojson.Position) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) 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) Point(com.mongodb.client.model.geojson.Point) MultiPoint(com.mongodb.client.model.geojson.MultiPoint)

Example 2 with CoordinateReferenceSystem

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

the class GeometryDecoderHelper method decodeMultiPoint.

private static MultiPoint decodeMultiPoint(final BsonReader reader) {
    String type = null;
    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 = decodeCoordinates(reader);
        } else if (key.equals("crs")) {
            crs = decodeCoordinateReferenceSystem(reader);
        } else {
            throw new CodecConfigurationException(format("Unexpected key '%s' found when decoding a GeoJSON point", key));
        }
    }
    reader.readEndDocument();
    if (type == null) {
        throw new CodecConfigurationException("Invalid MultiPoint, document contained no type information.");
    } else if (!type.equals("MultiPoint")) {
        throw new CodecConfigurationException(format("Invalid MultiPoint, found type '%s'.", type));
    } else if (coordinates == null) {
        throw new CodecConfigurationException("Invalid MultiPoint, missing position coordinates.");
    }
    return crs != null ? new MultiPoint(crs, coordinates) : new MultiPoint(coordinates);
}
Also used : MultiPoint(com.mongodb.client.model.geojson.MultiPoint) Position(com.mongodb.client.model.geojson.Position) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) 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 CoordinateReferenceSystem

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

the class GeometryDecoderHelper method decodeMultiPolygon.

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

Example 4 with CoordinateReferenceSystem

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

the class GeometryEncoderHelper method encodeCoordinateReferenceSystem.

@SuppressWarnings({ "unchecked", "rawtypes" })
static void encodeCoordinateReferenceSystem(final BsonWriter writer, final Geometry geometry, final EncoderContext encoderContext, final CodecRegistry registry) {
    CoordinateReferenceSystem coordinateReferenceSystem = geometry.getCoordinateReferenceSystem();
    if (coordinateReferenceSystem != null) {
        writer.writeName("crs");
        Codec codec = registry.get(coordinateReferenceSystem.getClass());
        encoderContext.encodeWithChildContext(codec, writer, coordinateReferenceSystem);
    }
}
Also used : Codec(org.bson.codecs.Codec) CoordinateReferenceSystem(com.mongodb.client.model.geojson.CoordinateReferenceSystem)

Example 5 with CoordinateReferenceSystem

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

the class GeometryDecoderHelper method decodeLineString.

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

Aggregations

CoordinateReferenceSystem (com.mongodb.client.model.geojson.CoordinateReferenceSystem)8 LineString (com.mongodb.client.model.geojson.LineString)7 MultiLineString (com.mongodb.client.model.geojson.MultiLineString)7 NamedCoordinateReferenceSystem (com.mongodb.client.model.geojson.NamedCoordinateReferenceSystem)7 CodecConfigurationException (org.bson.codecs.configuration.CodecConfigurationException)7 Position (com.mongodb.client.model.geojson.Position)3 MultiPoint (com.mongodb.client.model.geojson.MultiPoint)2 MultiPolygon (com.mongodb.client.model.geojson.MultiPolygon)2 PolygonCoordinates (com.mongodb.client.model.geojson.PolygonCoordinates)2 GeometryCollection (com.mongodb.client.model.geojson.GeometryCollection)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 Codec (org.bson.codecs.Codec)1