Skip to content

Commit 0c1da76

Browse files
committed
geospatial mongoldb examples
1 parent 6d3a51b commit 0c1da76

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.baeldung.geo;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertNull;
6+
7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
11+
import org.bson.Document;
12+
import org.junit.Before;
13+
import org.junit.Test;
14+
15+
import com.mongodb.MongoClient;
16+
import com.mongodb.client.FindIterable;
17+
import com.mongodb.client.MongoCollection;
18+
import com.mongodb.client.MongoDatabase;
19+
import com.mongodb.client.model.Filters;
20+
import com.mongodb.client.model.Indexes;
21+
import com.mongodb.client.model.geojson.Point;
22+
import com.mongodb.client.model.geojson.Polygon;
23+
import com.mongodb.client.model.geojson.Position;
24+
25+
public class MongoGeospatialIntegrationTest {
26+
27+
private MongoClient mongoClient;
28+
private MongoDatabase db;
29+
private MongoCollection<Document> collection;
30+
31+
@Before
32+
public void setup() {
33+
if (mongoClient == null) {
34+
mongoClient = new MongoClient();
35+
db = mongoClient.getDatabase("myMongoDb");
36+
collection = db.getCollection("places");
37+
collection.deleteMany(new Document());
38+
collection.createIndex(Indexes.geo2dsphere("location"));
39+
collection.insertOne(Document.parse("{'name':'Big Ben','location': {'coordinates':[-0.1268194,51.5007292],'type':'Point'}}"));
40+
collection.insertOne(Document.parse("{'name':'Hyde Park','location': {'coordinates': [[[-0.159381,51.513126],[-0.189615,51.509928],[-0.187373,51.502442], [-0.153019,51.503464],[-0.159381,51.513126]]],'type':'Polygon'}}"));
41+
}
42+
}
43+
44+
@Test
45+
public void givenNearbyLocation_whenSearchNearby_thenFound() {
46+
Point currentLoc = new Point(new Position(-0.126821, 51.495885));
47+
FindIterable<Document> result = collection.find(Filters.near("location", currentLoc, 1000.0, 10.0));
48+
49+
assertNotNull(result.first());
50+
assertEquals("Big Ben", result.first().get("name"));
51+
}
52+
53+
@Test
54+
public void givenFarLocation_whenSearchNearby_thenNotFound() {
55+
Point currentLoc = new Point(new Position(-0.5243333, 51.4700223));
56+
FindIterable<Document> result = collection.find(Filters.near("location", currentLoc, 5000.0, 10.0));
57+
58+
assertNull(result.first());
59+
}
60+
61+
@Test
62+
public void givenNearbyLocation_whenSearchWithinCircleSphere_thenFound() {
63+
double distanceInRad = 5.0 / 6371;
64+
FindIterable<Document> result = collection.find(Filters.geoWithinCenterSphere("location", -0.1435083, 51.4990956, distanceInRad));
65+
66+
assertNotNull(result.first());
67+
assertEquals("Big Ben", result.first().get("name"));
68+
}
69+
70+
@Test
71+
public void givenNearbyLocation_whenSearchWithinBox_thenFound() {
72+
double lowerLeftX = -0.1427638;
73+
double lowerLeftY = 51.4991288;
74+
double upperRightX = -0.1256209;
75+
double upperRightY = 51.5030272;
76+
77+
FindIterable<Document> result = collection.find(Filters.geoWithinBox("location", lowerLeftX, lowerLeftY, upperRightX, upperRightY));
78+
79+
assertNotNull(result.first());
80+
assertEquals("Big Ben", result.first().get("name"));
81+
}
82+
83+
@Test
84+
public void givenNearbyLocation_whenSearchWithinPolygon_thenFound() {
85+
ArrayList<List<Double>> points = new ArrayList<List<Double>>();
86+
points.add(Arrays.asList(-0.1439, 51.4952)); // victoria station
87+
points.add(Arrays.asList(-0.1121, 51.4989));// Lambeth North
88+
points.add(Arrays.asList(-0.13, 51.5163));// Tottenham Court Road
89+
points.add(Arrays.asList(-0.1439, 51.4952)); // victoria station
90+
FindIterable<Document> result = collection.find(Filters.geoWithinPolygon("location", points));
91+
92+
assertNotNull(result.first());
93+
assertEquals("Big Ben", result.first().get("name"));
94+
}
95+
96+
@Test
97+
public void givenNearbyLocation_whenSearchUsingIntersect_thenFound() {
98+
ArrayList<Position> positions = new ArrayList<Position>();
99+
positions.add(new Position(-0.1439, 51.4952));
100+
positions.add(new Position(-0.1346, 51.4978));
101+
positions.add(new Position(-0.2177, 51.5135));
102+
positions.add(new Position(-0.1439, 51.4952));
103+
Polygon geometry = new Polygon(positions);
104+
FindIterable<Document> result = collection.find(Filters.geoIntersects("location", geometry));
105+
106+
assertNotNull(result.first());
107+
assertEquals("Hyde Park", result.first().get("name"));
108+
}
109+
110+
}

0 commit comments

Comments
 (0)