Search in sources :

Example 1 with Point

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

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

the class CenterFilter method encode.

@Override
public void encode(Datastore datastore, BsonWriter writer, EncoderContext context) {
    writer.writeStartDocument(path(datastore.getMapper()));
    writer.writeStartDocument("$geoWithin");
    writer.writeStartArray(getName());
    Point center = getValue();
    writer.writeStartArray();
    for (Double value : center.getPosition().getValues()) {
        writer.writeDouble(value);
    }
    writer.writeEndArray();
    writer.writeDouble(radius);
    writer.writeEndArray();
    writer.writeEndDocument();
    writer.writeEndDocument();
}
Also used : Point(com.mongodb.client.model.geojson.Point)

Example 3 with Point

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

the class TestGeoQueries method testWithinOutsideBox.

@Test
public void testWithinOutsideBox() {
    final Place place1 = new Place("place1", new double[] { 1, 1 });
    getDs().save(place1);
    final Place found = getDs().find(Place.class).filter(box("loc", new Point(new Position(0, 0)), new Point(new Position(.4, .5)))).iterator(new FindOptions().limit(1)).tryNext();
    Assert.assertNull(found);
}
Also used : FindOptions(dev.morphia.query.FindOptions) Position(com.mongodb.client.model.geojson.Position) Point(com.mongodb.client.model.geojson.Point) Test(org.testng.annotations.Test)

Example 4 with Point

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

the class TestGeoQueries method testGeoWithinPolygon.

@Test
public void testGeoWithinPolygon() {
    final Place place1 = new Place("place1", new double[] { 0, 1 });
    getDs().save(place1);
    final Place found = getDs().find(Place.class).filter(polygon("loc", new Point(new Position(0, 0)), new Point(new Position(0, 5)), new Point(new Position(2, 3)), new Point(new Position(2, 0)))).iterator(new FindOptions().limit(1)).next();
    Assert.assertNotNull(found);
}
Also used : FindOptions(dev.morphia.query.FindOptions) Position(com.mongodb.client.model.geojson.Position) Point(com.mongodb.client.model.geojson.Point) Test(org.testng.annotations.Test)

Example 5 with Point

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

the class TestGeoQueries method testWithinBox.

@Test
public void testWithinBox() {
    final Place place1 = new Place("place1", new double[] { 1, 1 });
    getDs().save(place1);
    final Place found = getDs().find(Place.class).filter(box("loc", new Point(new Position(0, 0)), new Point(new Position(2, 2)))).iterator(new FindOptions().limit(1)).next();
    Assert.assertNotNull(found);
}
Also used : FindOptions(dev.morphia.query.FindOptions) Position(com.mongodb.client.model.geojson.Position) Point(com.mongodb.client.model.geojson.Point) Test(org.testng.annotations.Test)

Aggregations

Point (com.mongodb.client.model.geojson.Point)26 Position (com.mongodb.client.model.geojson.Position)23 Test (org.testng.annotations.Test)22 FindOptions (dev.morphia.query.FindOptions)16 LineString (com.mongodb.client.model.geojson.LineString)2 MultiLineString (com.mongodb.client.model.geojson.MultiLineString)2 MultiPoint (com.mongodb.client.model.geojson.MultiPoint)2 Polygon (com.mongodb.client.model.geojson.Polygon)2 Datastore (dev.morphia.Datastore)2 GeoCity (dev.morphia.test.models.geo.GeoCity)2 CodecConfigurationException (org.bson.codecs.configuration.CodecConfigurationException)2 CoordinateReferenceSystem (com.mongodb.client.model.geojson.CoordinateReferenceSystem)1 GeometryCollection (com.mongodb.client.model.geojson.GeometryCollection)1 MultiPolygon (com.mongodb.client.model.geojson.MultiPolygon)1 NamedCoordinateReferenceSystem (com.mongodb.client.model.geojson.NamedCoordinateReferenceSystem)1