Search in sources :

Example 1 with MultiPolygon

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

use of com.mongodb.client.model.geojson.MultiPolygon in project spring-data-mongodb by spring-projects.

the class MongoTemplateMappingTests method writesAndReadsEntityWithNativeMongoGeoJsonTypesCorrectly.

// DATAMONGO-2357
@Test
public void writesAndReadsEntityWithNativeMongoGeoJsonTypesCorrectly() {
    WithMongoGeoJson source = new WithMongoGeoJson();
    source.id = "id-2";
    source.multiPolygon = new MultiPolygon(Arrays.asList(new PolygonCoordinates(Arrays.asList(new Position(0, 0), new Position(0, 1), new Position(1, 1), new Position(1, 0), new Position(0, 0)))));
    template1.save(source);
    assertThat(template1.findOne(query(where("id").is(source.id)), WithMongoGeoJson.class)).isEqualTo(source);
}
Also used : MultiPolygon(com.mongodb.client.model.geojson.MultiPolygon) Position(com.mongodb.client.model.geojson.Position) PolygonCoordinates(com.mongodb.client.model.geojson.PolygonCoordinates) Test(org.junit.Test)

Example 3 with MultiPolygon

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

Example 4 with MultiPolygon

use of com.mongodb.client.model.geojson.MultiPolygon in project spring-data-mongodb by spring-projects.

the class MongoTemplateMappingTests method writesAndReadsEntityWithOpenNativeMongoGeoJsonTypesCorrectly.

// DATAMONGO-2357
@Test
public void writesAndReadsEntityWithOpenNativeMongoGeoJsonTypesCorrectly() {
    WithOpenMongoGeoJson source = new WithOpenMongoGeoJson();
    source.id = "id-2";
    source.geometry = new MultiPolygon(Arrays.asList(new PolygonCoordinates(Arrays.asList(new Position(0, 0), new Position(0, 1), new Position(1, 1), new Position(1, 0), new Position(0, 0)))));
    template1.save(source);
    assertThat(template1.findOne(query(where("id").is(source.id)), WithOpenMongoGeoJson.class)).isEqualTo(source);
}
Also used : MultiPolygon(com.mongodb.client.model.geojson.MultiPolygon) Position(com.mongodb.client.model.geojson.Position) PolygonCoordinates(com.mongodb.client.model.geojson.PolygonCoordinates) Test(org.junit.Test)

Aggregations

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