Search in sources :

Example 1 with GeoCity

use of dev.morphia.test.models.geo.GeoCity 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 2 with GeoCity

use of dev.morphia.test.models.geo.GeoCity 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

Point (com.mongodb.client.model.geojson.Point)2 Position (com.mongodb.client.model.geojson.Position)2 GeoCity (dev.morphia.test.models.geo.GeoCity)2 Test (org.testng.annotations.Test)2