下面列出了怎么用com.mongodb.client.model.geojson.Point的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
@SuppressWarnings("removal")
public CriteriaContainer within(final Shape shape) {
Filter converted;
if (shape instanceof dev.morphia.query.Shape.Center) {
final dev.morphia.query.Shape.Center center = (dev.morphia.query.Shape.Center) shape;
converted = Filters.center(getField(), center.getCenter(), center.getRadius());
} else if (shape.getGeometry().equals("$box")) {
Point[] points = shape.getPoints();
converted = Filters.box(getField(), points[0], points[1]);
} else if (shape.getGeometry().equals("$polygon")) {
converted = Filters.polygon(getField(), shape.getPoints());
} else {
throw new UnsupportedOperationException(Sofia.conversionNotSupported(shape.getGeometry()));
}
if (isNot()) {
converted.not();
}
filter(converted);
return MorphiaQuery.this;
}
@Override
public void encode(final Mapper mapper, final BsonWriter writer, final EncoderContext context) {
writer.writeStartDocument(field(mapper));
writer.writeStartDocument("$geoWithin");
writer.writeStartArray(getFilterName());
Point center = getValue();
writer.writeStartArray();
for (final Double value : center.getPosition().getValues()) {
writer.writeDouble(value);
}
writer.writeEndArray();
writer.writeDouble(radius);
writer.writeEndArray();
writer.writeEndDocument();
writer.writeEndDocument();
}
@Override
public void encode(final Mapper mapper, final BsonWriter writer, final EncoderContext context) {
writer.writeStartDocument(field(mapper));
writer.writeStartDocument("$geoWithin");
writer.writeStartArray("$polygon");
for (final Point point : points) {
writer.writeStartArray();
for (final Double value : point.getPosition().getValues()) {
writer.writeDouble(value);
}
writer.writeEndArray();
}
writer.writeEndArray();
writer.writeEndDocument();
writer.writeEndDocument();
}
@Test
public void shouldFindCitiesCloseToAGivenPointWithinARadiusOfMeters() {
// given
double latitude = 51.5286416;
double longitude = -0.1015987;
Datastore datastore = getDs();
City london = new City("London", new Point(new Position(latitude, longitude)));
datastore.save(london);
City manchester = new City("Manchester", new Point(new Position(53.4722454, -2.2235922)));
datastore.save(manchester);
City sevilla = new City("Sevilla", new Point(new Position(37.3753708, -5.9550582)));
datastore.save(sevilla);
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));
}
@Test
public void shouldFindAPointThatLiesOnTheQueryLine() {
// given
LineString spanishLine = new LineString(asList(
new Position(37.40759155713022, -5.964911067858338),
new Position(37.3753708, -5.9550582)));
City manchester = new City("Manchester", new Point(new Position(53.4722454, -2.2235922)));
getDs().save(manchester);
City london = new City("London", new Point(new Position(51.5286416, -0.1015987)));
getDs().save(london);
City sevilla = new City("Sevilla", new Point(new Position(37.3753708, -5.9550582)));
getDs().save(sevilla);
getDs().ensureIndexes();
// when
MongoCursor<City> matchingCity = getDs().find(City.class)
.filter(geoIntersects("location", spanishLine)).iterator();
// then
assertThat(matchingCity.next(), is(sevilla));
assertFalse(matchingCity.hasNext());
}
@Test
public void shouldFindAPointThatExactlyMatchesTheQueryPoint() {
// given
Point coordsOfManchester = new Point(new Position(53.4722454, -2.2235922));
City manchester = new City("Manchester", coordsOfManchester);
getDs().save(manchester);
City london = new City("London", new Point(new Position(51.5286416, -0.1015987)));
getDs().save(london);
City sevilla = new City("Sevilla", new Point(new Position(37.3753708, -5.9550582)));
getDs().save(sevilla);
getDs().ensureIndexes();
// when
List<City> matchingCity = getDs().find(City.class)
.filter(geoIntersects("location", coordsOfManchester)).iterator().toList();
// then
assertThat(matchingCity.size(), is(1));
assertThat(matchingCity.get(0), is(manchester));
}
@Test
public void shouldFindCitiesInTheUK() {
// given
Polygon uk = new Polygon(asList(new Position(49.78, -10.5),
new Position(49.78, 1.78),
new Position(59, 1.78),
new Position(59, -10.5),
new Position(49.78, -10.5)));
City manchester = new City("Manchester", new Point(new Position(53.4722454, -2.2235922)));
getDs().save(manchester);
City london = new City("London", new Point(new Position(51.5286416, -0.1015987)));
getDs().save(london);
City sevilla = new City("Sevilla", new Point(new Position(37.3753708, -5.9550582)));
getDs().save(sevilla);
getDs().ensureIndexes();
// when
List<City> citiesInTheUK = getDs().find(City.class)
.filter(geoWithin("location", uk)).iterator().toList();
// then
assertThat(citiesInTheUK.size(), is(2));
assertThat(citiesInTheUK, contains(london, manchester));
}
@Test
public void testNearMaxDistance() {
getDs().getMapper().map(Place.class);
getDs().ensureIndexes();
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));
Place found = query.iterator(options).tryNext();
Assert.assertNotNull(getDs().getLoggedQuery(options), found);
final Place notFound = getDs().find(Place.class)
.filter(near("loc", new Point(new Position(0, 0)))
.maxDistance(1.0)).iterator(options)
.tryNext();
Assert.assertNull(getDs().getLoggedQuery(options), notFound);
}
@Test
public void shouldFindAPointThatLiesInAQueryPolygon() {
// given
City manchester = new City("Manchester", new Point(new Position(53.4722454, -2.2235922)));
getDs().save(manchester);
City london = new City("London", new Point(new Position(51.5286416, -0.1015987)));
getDs().save(london);
City sevilla = new City("Sevilla", new Point(new Position(37.4057731, -5.966287)));
getDs().save(sevilla);
getDs().ensureIndexes();
// when
List<City> matchingCity = getDs().find(City.class)
.filter(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));
}
@Test
public void shouldFindCitiesInEurope() {
City manchester = new City("Manchester", new Point(new Position(53.4722454, -2.2235922)));
getDs().save(manchester);
City london = new City("London", new Point(new Position(51.5286416, -0.1015987)));
getDs().save(london);
City sevilla = new City("Sevilla", new Point(new Position(37.3753708, -5.9550582)));
getDs().save(sevilla);
City newYork = new City("New York", new Point(new Position(40.75981395319104, -73.98302106186748)));
getDs().save(newYork);
getDs().ensureIndexes();
// when
List<City> citiesInTheUK;
citiesInTheUK = getDs().find(City.class)
.filter(geoWithin("location", europeanCountries)).iterator().toList();
// then
assertThat(citiesInTheUK.size(), is(3));
assertThat(citiesInTheUK, containsInAnyOrder(london, manchester, sevilla));
}
@Test
public void givenNearbyLocation_whenSearchNearby_thenFound() {
Point currentLoc = new Point(new Position(-0.126821, 51.495885));
FindIterable<Document> result = collection.find(Filters.near("location", currentLoc, 1000.0, 10.0));
assertNotNull(result.first());
assertEquals("Big Ben", result.first().get("name"));
}
@Test
public void givenFarLocation_whenSearchNearby_thenNotFound() {
Point currentLoc = new Point(new Position(-0.5243333, 51.4700223));
FindIterable<Document> result = collection.find(Filters.near("location", currentLoc, 5000.0, 10.0));
assertNull(result.first());
}
@Override
public void encode(final BsonWriter writer, final dev.morphia.query.Shape value, final EncoderContext encoderContext) {
writer.writeStartDocument();
writer.writeStartArray(value.getGeometry());
for (final Point point : value.getPoints()) {
encodePosition(writer, point.getCoordinates());
}
writer.writeEndArray();
writer.writeEndDocument();
}
@Test
public void testGeoNearWithGeoJson() {
// given
Point londonPoint = new Point(new Position(51.5286416, -0.1015987));
City london = new City("London", londonPoint);
getDs().save(london);
City manchester = new City("Manchester", new Point(new Position(53.4722454, -2.2235922)));
getDs().save(manchester);
City sevilla = new City("Sevilla", new Point(new Position(37.3753708, -5.9550582)));
getDs().save(sevilla);
getDs().ensureIndexes();
// when
Iterator<City> citiesOrderedByDistanceFromLondon = getDs().aggregate(City.class)
.geoNear(
to(londonPoint)
.distanceField("distance")
.spherical(true))
.execute(City.class);
// then
Assert.assertTrue(citiesOrderedByDistanceFromLondon.hasNext());
Assert.assertEquals(london, citiesOrderedByDistanceFromLondon.next());
Assert.assertEquals(manchester, citiesOrderedByDistanceFromLondon.next());
Assert.assertEquals(sevilla, citiesOrderedByDistanceFromLondon.next());
Assert.assertFalse(citiesOrderedByDistanceFromLondon.hasNext());
}
@Test(expected = Exception.class)
public void shouldErrorWhenCreatingA2dIndexOnGeoJson() {
// given
Place pointB = new Place(new Point(new Position(3.1, 7.5)), "Point B");
getDs().save(pointB);
// when
getDs().ensureIndexes();
//"location object expected, location array not in correct format", code : 13654
}
@Test
public void shouldRetrieveGeoJsonPoint() {
// given
City city = new City("New City", new Point(new Position(3.0, 7.0)));
getDs().save(city);
// when
City found = getDs().find(City.class)
.filter(eq("name", "New City")).iterator(new FindOptions().limit(1))
.tryNext();
// then
assertThat(found, is(notNullValue()));
assertThat(found, is(city));
}
@Test
public void shouldFindAreasWhereTheGivenPointIsOnTheBoundary() {
// given
Area sevilla = new Area("Spain",
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))));
getDs().save(sevilla);
Area newYork = new Area("New York",
new Polygon(asList(new Position(40.75981395319104, -73.98302106186748),
new Position(40.7636824529618, -73.98049869574606),
new Position(40.76962974853814, -73.97964206524193),
new Position(40.75981395319104, -73.98302106186748))));
getDs().save(newYork);
Area london = new Area("London",
new Polygon(asList(new Position(51.507780365645885, -0.21786745637655258),
new Position(51.50802478194237, -0.21474729292094707),
new Position(51.5086863655597, -0.20895397290587425),
new Position(51.507780365645885, -0.21786745637655258))));
getDs().save(london);
getDs().ensureIndexes();
// when
List<Area> areaContainingPoint = getDs().find(Area.class)
.filter(geoIntersects("area", new Point(new Position(51.507780365645885,
-0.21786745637655258)))).iterator()
.toList();
// then
assertThat(areaContainingPoint.size(), is(1));
assertThat(areaContainingPoint.get(0), is(london));
}
@Test
public void shouldFindRoutesThatAGivenPointIsOn() {
// given
Route sevilla = new Route("Spain", new LineString(asList(
new Position(37.40759155713022, -5.964911067858338),
new Position(37.40341208875179, -5.9643941558897495),
new Position(37.40297396667302, -5.970452763140202))));
getDs().save(sevilla);
Route newYork = new Route("New York", new LineString(asList(
new Position(40.75981395319104, -73.98302106186748),
new Position(40.7636824529618, -73.98049869574606),
new Position(40.76962974853814, -73.97964206524193))));
getDs().save(newYork);
Route london = new Route("London", new LineString(asList(
new Position(51.507780365645885, -0.21786745637655258),
new Position(51.50802478194237, -0.21474729292094707),
new Position(51.5086863655597, -0.20895397290587425))));
getDs().save(london);
Route londonToParis = new Route("London To Paris", new LineString(asList(
new Position(51.5286416, -0.1015987),
new Position(48.858859, 2.3470599))));
getDs().save(londonToParis);
getDs().ensureIndexes();
// when
List<Route> routeContainingPoint = getDs().find(Route.class)
.filter(geoIntersects("route",
new Point(new Position(37.40759155713022, -5.964911067858338)))).iterator()
.toList();
// then
assertThat(routeContainingPoint.size(), is(1));
assertThat(routeContainingPoint.get(0), is(sevilla));
}
@Test
public void shouldFindAreasCloseToAGivenPointWithinARadiusOfMeters() {
// given
Area sevilla = new Area("Spain", 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))));
getDs().save(sevilla);
Area newYork = new Area("New York", new Polygon(asList(
new Position(40.75981395319104, -73.98302106186748),
new Position(40.7636824529618, -73.98049869574606),
new Position(40.76962974853814, -73.97964206524193),
new Position(40.75981395319104, -73.98302106186748))));
getDs().save(newYork);
Area london = new Area("London", new Polygon(asList(
new Position(51.507780365645885, -0.21786745637655258),
new Position(51.50802478194237, -0.21474729292094707),
new Position(51.5086863655597, -0.20895397290587425),
new Position(51.507780365645885, -0.21786745637655258))));
getDs().save(london);
getDs().ensureIndexes();
// when
List<Area> routesOrderedByDistanceFromLondon = getDs().find(Area.class)
.filter(near("area", new Point(new Position(51.5286416, -0.1015987)))
.maxDistance(20000.0)).iterator()
.toList();
// then
assertThat(routesOrderedByDistanceFromLondon.size(), is(1));
assertThat(routesOrderedByDistanceFromLondon.get(0), is(london));
}
@Test
public void shouldFindAreasOrderedByDistanceFromAGivenPoint() {
// given
Area sevilla = new Area("Spain", 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))));
getDs().save(sevilla);
Area newYork = new Area("New York", new Polygon(asList(
new Position(40.75981395319104, -73.98302106186748),
new Position(40.7636824529618, -73.98049869574606),
new Position(40.76962974853814, -73.97964206524193),
new Position(40.75981395319104, -73.98302106186748))));
getDs().save(newYork);
Area london = new Area("London", new Polygon(asList(
new Position(51.507780365645885, -0.21786745637655258),
new Position(51.50802478194237, -0.21474729292094707),
new Position(51.5086863655597, -0.20895397290587425),
new Position(51.507780365645885, -0.21786745637655258))));
getDs().save(london);
getDs().ensureIndexes();
// when
List<Area> routesOrderedByDistanceFromLondon = getDs().find(Area.class)
.filter(near("area", new Point(new Position(51.5286416, -0.1015987))))
.iterator()
.toList();
// then
assertThat(routesOrderedByDistanceFromLondon.size(), is(3));
assertThat(routesOrderedByDistanceFromLondon.get(0), is(london));
assertThat(routesOrderedByDistanceFromLondon.get(1), is(sevilla));
assertThat(routesOrderedByDistanceFromLondon.get(2), is(newYork));
}
@Test
public void shouldFindCitiesOrderedByDistance() {
double latitudeLondon = 51.5286416;
double longitudeLondon = -0.1015987;
City manchester = new City("Manchester", new Point(new Position(53.4722454, -2.2235922)));
getDs().save(manchester);
City london = new City("London", new Point(new Position(latitudeLondon, longitudeLondon)));
getDs().save(london);
City sevilla = new City("Sevilla", new Point(new Position(37.3753708, -5.9550582)));
getDs().save(sevilla);
getDs().ensureIndexes();
List<City> cities = getDs().find(City.class)
.filter(near("location", new Point(new Position(latitudeLondon, longitudeLondon)))).iterator()
.toList();
assertThat(cities.size(), is(3));
assertThat(cities.get(0), is(london));
assertThat(cities.get(1), is(manchester));
assertThat(cities.get(2), is(sevilla));
cities = getDs().find(City.class)
.filter(nearSphere("location", new Point(new Position(latitudeLondon, longitudeLondon)))).iterator()
.toList();
assertThat(cities.size(), is(3));
assertThat(cities.get(0), is(london));
assertThat(cities.get(1), is(manchester));
assertThat(cities.get(2), is(sevilla));
}
@Test
public void shouldFindNearAPoint() {
// given
Datastore datastore = getDs();
City london = new City("London", new Point(new Position(51.5286416, -0.1015987)));
datastore.save(london);
City manchester = new City("Manchester", new Point(new Position(53.4722454, -2.2235922)));
datastore.save(manchester);
City sevilla = new City("Sevilla", new Point(new Position(37.3753708, -5.9550582)));
datastore.save(sevilla);
getDs().ensureIndexes();
final Point searchPoint = new Point(new Position(50, 0.1278));
List<City> cities = datastore.find(City.class)
.filter(near("location", searchPoint).maxDistance(200000.0)).iterator().toList();
assertThat(cities.size(), is(1));
assertThat(cities.get(0), is(london));
cities = datastore.find(City.class)
.filter(near("location", searchPoint).maxDistance(200000D)).iterator().toList();
assertThat(cities.size(), is(1));
assertThat(cities.get(0), is(london));
assertThat(datastore.find(City.class)
.filter(near("location", searchPoint)
.maxDistance(200000D)
.minDistance(195000D)).iterator().toList().size(), is(0));
assertThat(datastore.find(City.class)
.filter(nearSphere("location", searchPoint)
.maxDistance(200000D)
.minDistance(195000D)).iterator().toList().size(), is(0));
}
@Test
public void shouldFindRoutesCloseToAGivenPointWithinARadiusOfMeters() {
// given
Route sevilla = new Route("Spain", new LineString(asList(
new Position(37.40759155713022, -5.964911067858338),
new Position(37.40341208875179, -5.9643941558897495),
new Position(37.40297396667302, -5.970452763140202))));
getDs().save(sevilla);
Route newYork = new Route("New York", new LineString(asList(
new Position(40.75981395319104, -73.98302106186748),
new Position(40.7636824529618, -73.98049869574606),
new Position(40.76962974853814, -73.97964206524193))));
getDs().save(newYork);
Route london = new Route("London", new LineString(asList(
new Position(51.507780365645885, -0.21786745637655258),
new Position(51.50802478194237, -0.21474729292094707),
new Position(51.5086863655597, -0.20895397290587425))));
getDs().save(london);
getDs().ensureIndexes();
// when
List<Route> routes = getDs().find(Route.class)
.filter(near("route", new Point(new Position(51.5286416, -0.1015987)))
.maxDistance(20000.0)).iterator()
.toList();
// then
assertThat(routes.size(), is(1));
assertThat(routes.get(0), is(london));
}
@Test
public void shouldFindRoutesOrderedByDistanceFromAGivenPoint() {
// given
Route sevilla = new Route("Spain", new LineString(asList(new Position(37.40759155713022, -5.964911067858338),
new Position(37.40341208875179, -5.9643941558897495),
new Position(37.40297396667302, -5.970452763140202))));
getDs().save(sevilla);
Route newYork = new Route("New York", new LineString(asList(new Position(40.75981395319104, -73.98302106186748),
new Position(40.7636824529618, -73.98049869574606),
new Position(40.76962974853814, -73.97964206524193))));
getDs().save(newYork);
Route london = new Route("London", new LineString(asList(new Position(51.507780365645885, -0.21786745637655258),
new Position(51.50802478194237, -0.21474729292094707),
new Position(51.5086863655597, -0.20895397290587425))));
getDs().save(london);
getDs().ensureIndexes();
// when
List<Route> routes = getDs().find(Route.class)
.filter(near("route", new Point(new Position(51.5286416, -0.1015987)))).iterator()
.toList();
// then
assertThat(routes.size(), is(3));
assertThat(routes.get(0), is(london));
assertThat(routes.get(1), is(sevilla));
assertThat(routes.get(2), is(newYork));
}
@Test
public void testGeoWithinBox() {
getDs().ensureIndexes();
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);
}
@Test
public void testGeoWithinOutsideBox() {
getDs().ensureIndexes();
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);
}
@Test
public void testGeoWithinPolygon() {
getDs().ensureIndexes();
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);
}
@Test
public void testGeoWithinPolygon2() {
getDs().ensureIndexes();
final Place place1 = new Place("place1", new double[]{10, 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))
.tryNext();
Assert.assertNull(found);
}
@Test
public void testGeoWithinRadius() {
getDs().ensureIndexes();
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);
}
@Test
public void testGeoWithinRadius2() {
getDs().ensureIndexes();
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.5, 0.5)), 0.77)).iterator(new FindOptions().limit(1))
.next();
Assert.assertNotNull(found);
}