Search in sources :

Example 1 with Polygon

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

the class TestGeoQueries method geoIntersects.

@Test
public void geoIntersects() {
    // given
    City manchester = new City("Manchester", new Point(new Position(53.4722454, -2.2235922)));
    getDs().save(manchester);
    getDs().save(new City("London", new Point(new Position(51.5286416, -0.1015987))));
    City sevilla = getDs().save(new City("Sevilla", new Point(new Position(37.4057731, -5.966287))));
    getDs().ensureIndexes();
    // when
    List<City> matchingCity = getDs().find(City.class).filter(Filters.geoIntersects("location", new Polygon(asList(new Position(37.40759155713022, -5.964911067858338), new Position(37.40341208875179, -5.9643941558897495), new Position(37.40297396667302, -5.970452763140202), new Position(37.40759155713022, -5.964911067858338))))).iterator().toList();
    // then
    assertThat(matchingCity.size(), is(1));
    assertThat(matchingCity.get(0), is(sevilla));
}
Also used : Position(com.mongodb.client.model.geojson.Position) Point(com.mongodb.client.model.geojson.Point) Polygon(com.mongodb.client.model.geojson.Polygon) Test(org.testng.annotations.Test)

Example 2 with Polygon

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

Example 3 with Polygon

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

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

the class TestLifecycles method testWithGeoJson.

@Test
public void testWithGeoJson() {
    final Polygon polygon = new Polygon(asList(new Position(0d, 0d), new Position(1d, 1d), new Position(2d, 2d), new Position(3d, 3d), new Position(0d, 0d)));
    getDs().save(new HoldsPolygon(ObjectId.get(), polygon));
    Assert.assertFalse(HoldsPolygon.lifecycle);
    Assert.assertNotNull(getDs().find(HoldsPolygon.class).first());
    Assert.assertTrue(HoldsPolygon.lifecycle);
}
Also used : Position(com.mongodb.client.model.geojson.Position) Polygon(com.mongodb.client.model.geojson.Polygon) Test(org.testng.annotations.Test)

Aggregations

Polygon (com.mongodb.client.model.geojson.Polygon)4 LineString (com.mongodb.client.model.geojson.LineString)2 MultiLineString (com.mongodb.client.model.geojson.MultiLineString)2 MultiPolygon (com.mongodb.client.model.geojson.MultiPolygon)2 Point (com.mongodb.client.model.geojson.Point)2 Position (com.mongodb.client.model.geojson.Position)2 CodecConfigurationException (org.bson.codecs.configuration.CodecConfigurationException)2 Test (org.testng.annotations.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 PolygonCoordinates (com.mongodb.client.model.geojson.PolygonCoordinates)1