下面列出了怎么用com.mongodb.client.model.geojson.Position的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void shouldRetrieveGeoJsonLineString() {
// given
Route route = new Route("My Route", new LineString(asList(
new Position(1, 2),
new Position(3, 5),
new Position(19, 13))));
getDs().save(route);
// when
Route found = getDs().find(Route.class)
.filter(eq("name", "My Route")).iterator(new FindOptions().limit(1)).tryNext();
// then
assertThat(found, is(notNullValue()));
assertThat(found, is(route));
}
@Test
public void shouldRetrieveGeoJsonMultiLineString() {
// given
String name = "Many Paths";
Paths paths = new Paths(name, new MultiLineString(
asList(
asList(
new Position(1, 2),
new Position(3, 5),
new Position(19, 13)),
asList(
new Position(1.5, 2.0),
new Position(1.9, 2.0),
new Position(1.9, 1.8),
new Position(1.5, 2.0)))));
getDs().save(paths);
// when
Paths found = getDs().find(Paths.class).filter(eq("name", name)).iterator(new FindOptions().limit(1)).tryNext();
// then
assertThat(found, is(notNullValue()));
assertThat(found, is(paths));
}
@Test
public void shouldRetrieveGeoJsonMultiPoint() {
// given
String name = "My stores";
Stores stores = new Stores(name, new MultiPoint(asList(
new Position(1, 2),
new Position(3, 5),
new Position(19, 13))));
getDs().save(stores);
// when
Stores found = getDs().find(Stores.class).filter(eq("name", name)).iterator(new FindOptions().limit(1)).tryNext();
// then
assertThat(found, is(notNullValue()));
assertThat(found, is(stores));
}
@Test
public void shouldRetrieveGeoJsonPolygon() {
// given
Area area = new Area("The Area", new Polygon(asList(
new Position(2.0, 1.1),
new Position(3.5, 2.3),
new Position(1.0, 3.7),
new Position(2.0, 1.1))));
getDs().save(area);
// when
Area found = getDs().find(Area.class)
.filter(eq("name", "The Area")).iterator(new FindOptions().limit(1))
.tryNext();
// then
assertThat(found, is(notNullValue()));
assertThat(found, is(area));
}
@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());
}
@Test
public void givenNearbyLocation_whenSearchUsingIntersect_thenFound() {
ArrayList<Position> positions = new ArrayList<Position>();
positions.add(new Position(-0.1439, 51.4952));
positions.add(new Position(-0.1346, 51.4978));
positions.add(new Position(-0.2177, 51.5135));
positions.add(new Position(-0.1439, 51.4952));
Polygon geometry = new Polygon(positions);
FindIterable<Document> result = collection.find(Filters.geoIntersects("location", geometry));
assertNotNull(result.first());
assertEquals("Hyde Park", result.first().get("name"));
}
@Override
@SuppressWarnings({"unchecked"})
public com.mongodb.client.model.geojson.Polygon convert(final CoordinateReferenceSystem crs) {
final List<List<Position>> lists = GeoJson.convertLineStrings(interiorBoundaries);
final List[] holeArray = lists.toArray(new List[0]);
return new com.mongodb.client.model.geojson.Polygon(crs != null ? crs.convert() : null,
new PolygonCoordinates(exteriorBoundary.convert().getCoordinates(), holeArray));
}
/**
* @param values the values to convert
* @morphia.internal
* @return the converted values
*/
public static List<Position> convertPoints(final List<Point> values) {
final ArrayList<Position> positions = new ArrayList<Position>();
for (final Point point : values) {
positions.add(new Position(point.getLongitude(), point.getLatitude()));
}
return positions;
}
/**
* @param values the values to convert
* @morphia.internal
* @return the converted values
*/
public static List<List<Position>> convertLineStrings(final List<LineString> values) {
final List<List<Position>> positions = new ArrayList<List<Position>>();
for (final LineString line : values) {
positions.add(convertPoints(line.getCoordinates()));
}
return positions;
}
private void encodePosition(final BsonWriter writer, final Position value) {
writer.writeStartArray();
for (double number : value.getValues()) {
writer.writeDouble(number);
}
writer.writeEndArray();
}
private void encodePosition(final BsonWriter writer, final Position value) {
writer.writeStartArray();
for (double number : value.getValues()) {
writer.writeDouble(number);
}
writer.writeEndArray();
}
@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 shouldRetrieveGeoJsonMultiRingPolygon() {
// given
String polygonName = "A polygon with holes";
Polygon polygonWithHoles = new Polygon(
asList(
new Position(1.1, 2.0),
new Position(2.3, 3.5),
new Position(3.7, 1.0),
new Position(1.1, 2.0)),
asList(
new Position(1.5, 2.0),
new Position(1.9, 2.0),
new Position(1.9, 1.8),
new Position(1.5, 2.0)),
asList(
new Position(2.2, 2.1),
new Position(2.4, 1.9),
new Position(2.4, 1.7),
new Position(2.1, 1.8),
new Position(2.2, 2.1)));
Area area = new Area(polygonName, polygonWithHoles);
getDs().save(area);
// when
Area found = getDs().find(Area.class)
.filter(eq("name", polygonName)).iterator(new FindOptions().limit(1))
.tryNext();
// then
assertThat(found, is(notNullValue()));
assertThat(found, is(area));
}
@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 shouldFindAreasThatALineCrosses() {
// 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
MongoCursor<Area> areaContainingPoint = getDs().find(Area.class)
.filter(geoIntersects("area", new LineString(asList(
new Position(37.4056048, -5.9666089),
new Position(37.404497, -5.9640557))))).iterator();
// then
assertThat(areaContainingPoint.next(), is(sevilla));
assertFalse(areaContainingPoint.hasNext());
}
@Test
public void shouldFindRoutesThatALineCrosses() {
// given
Route sevilla = new Route("Spain", new LineString(asList(
new Position(37.4045286, -5.9642332),
new Position(37.4061095, -5.9645765))));
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
MongoCursor<Route> route = getDs().find(Route.class)
.filter(geoIntersects("route", new LineString(asList(
new Position(37.4043709, -5.9643244),
new Position(37.4045286, -5.9642332))))).iterator();
// then
assertThat(route.next(), is(sevilla));
assertFalse(route.hasNext());
}
@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));
}