Search in sources :

Example 1 with PolygonCoordinates

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

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

use of com.mongodb.client.model.geojson.PolygonCoordinates in project morphia by mongodb.

the class Polygon method convert.

@Override
@SuppressWarnings({ "unchecked" })
public com.mongodb.client.model.geojson.Polygon convert(@Nullable CoordinateReferenceSystem crs) {
    final List<List<Position>> lists = GeoJson.convertLineStrings(interiorBoundaries);
    final List[] holeArray = lists.toArray(new List[0]);
    return new com.mongodb.client.model.geojson.Polygon(crs != null ? crs.convert() : null, new PolygonCoordinates(exteriorBoundary.convert().getCoordinates(), holeArray));
}
Also used : List(java.util.List) ArrayList(java.util.ArrayList) PolygonCoordinates(com.mongodb.client.model.geojson.PolygonCoordinates)

Example 4 with PolygonCoordinates

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

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

the class GeometryDecoderHelper method decodePolygon.

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

Aggregations

PolygonCoordinates (com.mongodb.client.model.geojson.PolygonCoordinates)7 MultiPolygon (com.mongodb.client.model.geojson.MultiPolygon)4 Position (com.mongodb.client.model.geojson.Position)3 CodecConfigurationException (org.bson.codecs.configuration.CodecConfigurationException)3 CoordinateReferenceSystem (com.mongodb.client.model.geojson.CoordinateReferenceSystem)2 LineString (com.mongodb.client.model.geojson.LineString)2 MultiLineString (com.mongodb.client.model.geojson.MultiLineString)2 NamedCoordinateReferenceSystem (com.mongodb.client.model.geojson.NamedCoordinateReferenceSystem)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Test (org.junit.Test)2 Polygon (com.mongodb.client.model.geojson.Polygon)1