Search in sources :

Example 1 with Position

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

the class GeometryDecoderHelper method decodePolygonCoordinates.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static PolygonCoordinates decodePolygonCoordinates(final BsonReader reader) {
    validateIsArray(reader);
    reader.readStartArray();
    List<List<Position>> values = new ArrayList<List<Position>>();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        values.add(decodeCoordinates(reader));
    }
    reader.readEndArray();
    if (values.isEmpty()) {
        throw new CodecConfigurationException("Invalid Polygon no coordinates.");
    }
    List<Position> exterior = values.remove(0);
    ArrayList[] holes = values.toArray(new ArrayList[values.size()]);
    try {
        return new PolygonCoordinates(exterior, holes);
    } catch (IllegalArgumentException e) {
        throw new CodecConfigurationException(format("Invalid Polygon: %s", e.getMessage()));
    }
}
Also used : Position(com.mongodb.client.model.geojson.Position) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) PolygonCoordinates(com.mongodb.client.model.geojson.PolygonCoordinates)

Example 2 with Position

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

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

the class GeometryDecoderHelper method decodePosition.

private static Position decodePosition(final BsonReader reader) {
    validateIsArray(reader);
    reader.readStartArray();
    List<Double> values = new ArrayList<Double>();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        values.add(readAsDouble(reader));
    }
    reader.readEndArray();
    try {
        return new Position(values);
    } catch (IllegalArgumentException e) {
        throw new CodecConfigurationException(format("Invalid Position: %s", e.getMessage()));
    }
}
Also used : Position(com.mongodb.client.model.geojson.Position) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) ArrayList(java.util.ArrayList)

Example 4 with Position

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

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

the class GeometryEncoderHelper method encodeMultiLineString.

private static void encodeMultiLineString(final BsonWriter writer, final MultiLineString value) {
    writer.writeStartArray();
    for (List<Position> ring : value.getCoordinates()) {
        writer.writeStartArray();
        for (Position position : ring) {
            encodePosition(writer, position);
        }
        writer.writeEndArray();
    }
    writer.writeEndArray();
}
Also used : Position(com.mongodb.client.model.geojson.Position)

Aggregations

Position (com.mongodb.client.model.geojson.Position)35 Point (com.mongodb.client.model.geojson.Point)23 Test (org.testng.annotations.Test)23 FindOptions (dev.morphia.query.FindOptions)16 CodecConfigurationException (org.bson.codecs.configuration.CodecConfigurationException)5 CoordinateReferenceSystem (com.mongodb.client.model.geojson.CoordinateReferenceSystem)3 LineString (com.mongodb.client.model.geojson.LineString)3 MultiLineString (com.mongodb.client.model.geojson.MultiLineString)3 NamedCoordinateReferenceSystem (com.mongodb.client.model.geojson.NamedCoordinateReferenceSystem)3 PolygonCoordinates (com.mongodb.client.model.geojson.PolygonCoordinates)3 MultiPoint (com.mongodb.client.model.geojson.MultiPoint)2 MultiPolygon (com.mongodb.client.model.geojson.MultiPolygon)2 Polygon (com.mongodb.client.model.geojson.Polygon)2 Datastore (dev.morphia.Datastore)2 GeoCity (dev.morphia.test.models.geo.GeoCity)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 List (java.util.List)1