Search in sources :

Example 26 with Position

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

the class TestGeoQueries method testNear.

@Test
public void testNear() {
    final Place place1 = new Place("place1", new double[] { 1, 1 });
    getDs().save(place1);
    final Place found = getDs().find(Place.class).filter(near("loc", new Point(new Position(0, 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 27 with Position

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

the class TestGeoQueries method testNearMaxDistance.

@Test
public void testNearMaxDistance() {
    final Place place1 = new Place("place1", new double[] { 1, 1 });
    getDs().save(place1);
    FindOptions options = new FindOptions().logQuery().limit(1);
    Query<Place> query = getDs().find(Place.class).filter(near("loc", new Point(new Position(1, 1))).maxDistance(2.0));
    Assert.assertNotNull(query.iterator(options).tryNext(), query.getLoggedQuery());
    query = getDs().find(Place.class).filter(near("loc", new Point(new Position(0, 0))).maxDistance(1.0));
    Assert.assertNull(query.first(options), query.getLoggedQuery());
}
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 28 with Position

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

the class TestGeoQueries method testGeoWithinBox.

@Test
public void testGeoWithinBox() {
    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)

Example 29 with Position

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

the class TestGeoQueries method testGeoWithinRadius.

@Test
public void testGeoWithinRadius() {
    final Place place1 = new Place("place1", new double[] { 1, 1 });
    getDs().save(place1);
    final Place found = getDs().find(Place.class).filter(center("loc", new Point(new Position(0, 1)), 1.1)).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 30 with Position

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

the class TestGeoQueries method maxDistance.

@Test
public void maxDistance() {
    // given
    double latitude = 51.5286416;
    double longitude = -0.1015987;
    Datastore datastore = getDs();
    City london = datastore.save(new City("London", new Point(new Position(latitude, longitude))));
    datastore.save(List.of(new City("Manchester", new Point(new Position(53.4722454, -2.2235922))), new City("Sevilla", new Point(new Position(37.3753708, -5.9550582)))));
    getDs().ensureIndexes();
    // when
    List<City> cities = datastore.find(City.class).filter(near("location", new Point(new Position(latitude, longitude))).maxDistance(200000.0)).iterator().toList();
    // then
    assertThat(cities.size(), is(1));
    assertThat(cities.get(0), is(london));
}
Also used : Datastore(dev.morphia.Datastore) Position(com.mongodb.client.model.geojson.Position) Point(com.mongodb.client.model.geojson.Point) Test(org.testng.annotations.Test)

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