Search in sources :

Example 31 with Position

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

Example 32 with Position

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

the class TestGeoQueries method testNearNoIndex.

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

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

the class TestGeoQueries method testGeoWithinRadiusSphere.

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

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

the class AggregationTest method testGeoNearWithGeoJson.

@Test
public void testGeoNearWithGeoJson() {
    // given
    Point londonPoint = new Point(new Position(51.5286416, -0.1015987));
    GeoCity london = new GeoCity("London", londonPoint);
    getDs().save(london);
    GeoCity manchester = new GeoCity("Manchester", new Point(new Position(53.4722454, -2.2235922)));
    getDs().save(manchester);
    GeoCity sevilla = new GeoCity("Sevilla", new Point(new Position(37.3753708, -5.9550582)));
    getDs().save(sevilla);
    getDs().ensureIndexes();
    // when
    Iterator<GeoCity> cities = getDs().aggregate(GeoCity.class).geoNear(geoNear(londonPoint).distanceField("distance").spherical(true)).execute(GeoCity.class);
    // then
    Assert.assertTrue(cities.hasNext());
    Assert.assertEquals(london, cities.next());
    Assert.assertEquals(manchester, cities.next());
    Assert.assertEquals(sevilla, cities.next());
    Assert.assertFalse(cities.hasNext());
}
Also used : Position(com.mongodb.client.model.geojson.Position) Point(com.mongodb.client.model.geojson.Point) GeoCity(dev.morphia.test.models.geo.GeoCity) Test(org.testng.annotations.Test)

Example 35 with Position

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

the class AggregationTest method testGeoNearWithSphericalGeometry.

@Test
public void testGeoNearWithSphericalGeometry() {
    // given
    double latitude = 51.5286416;
    double longitude = -0.1015987;
    GeoCity london = new GeoCity("London", new Point(new Position(latitude, longitude)));
    getDs().save(london);
    GeoCity manchester = new GeoCity("Manchester", new Point(new Position(53.4722454, -2.2235922)));
    getDs().save(manchester);
    GeoCity sevilla = new GeoCity("Sevilla", new Point(new Position(37.3753708, -5.9550582)));
    getDs().save(sevilla);
    getDs().ensureIndexes();
    // when
    Iterator<GeoCity> cities = getDs().aggregate(GeoCity.class).geoNear(geoNear(new double[] { latitude, longitude }).distanceField("distance").spherical(true)).execute(GeoCity.class);
    // then
    Assert.assertTrue(cities.hasNext());
    Assert.assertEquals(london, cities.next());
    Assert.assertEquals(manchester, cities.next());
    Assert.assertEquals(sevilla, cities.next());
    Assert.assertFalse(cities.hasNext());
}
Also used : Position(com.mongodb.client.model.geojson.Position) Point(com.mongodb.client.model.geojson.Point) GeoCity(dev.morphia.test.models.geo.GeoCity) 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