Search in sources :

Example 1 with Polygon

use of org.springframework.data.geo.Polygon in project spring-data-mongodb by spring-projects.

the class MongoQueryCreatorUnitTests method shouldCreateWithinQueryCorrectly.

// DATAMONGO-1136
@Test
public void shouldCreateWithinQueryCorrectly() {
    Point first = new Point(1, 1);
    Point second = new Point(2, 2);
    Point third = new Point(3, 3);
    Shape shape = new Polygon(first, second, third);
    PartTree tree = new PartTree("findByAddress_GeoWithin", User.class);
    MongoQueryCreator creator = new MongoQueryCreator(tree, getAccessor(converter, shape), context);
    Query query = creator.createQuery();
    assertThat(query, is(query(where("address.geo").within(shape))));
}
Also used : Shape(org.springframework.data.geo.Shape) Query(org.springframework.data.mongodb.core.query.Query) Point(org.springframework.data.geo.Point) GeoJsonPoint(org.springframework.data.mongodb.core.geo.GeoJsonPoint) Polygon(org.springframework.data.geo.Polygon) PartTree(org.springframework.data.repository.query.parser.PartTree) Test(org.junit.Test)

Example 2 with Polygon

use of org.springframework.data.geo.Polygon in project spring-data-mongodb by spring-projects.

the class AbstractGeoSpatialTests method withinPolygon.

@Test
public void withinPolygon() {
    Point first = new Point(-73.99756, 40.73083);
    Point second = new Point(-73.99756, 40.741404);
    Point third = new Point(-73.988135, 40.741404);
    Point fourth = new Point(-73.988135, 40.73083);
    Polygon polygon = new Polygon(first, second, third, fourth);
    List<Venue> venues = template.find(query(where("location").within(polygon)), Venue.class);
    assertThat(venues.size(), is(4));
}
Also used : Venue(org.springframework.data.mongodb.core.Venue) Point(org.springframework.data.geo.Point) Polygon(org.springframework.data.geo.Polygon) Test(org.junit.Test)

Example 3 with Polygon

use of org.springframework.data.geo.Polygon in project spring-data-mongodb by spring-projects.

the class GeoConvertersUnitTests method convertsPolygonToDocumentAndBackCorrectly.

// DATAMONGO-858
@Test
public void convertsPolygonToDocumentAndBackCorrectly() {
    Polygon polygon = new Polygon(new Point(1, 2), new Point(2, 3), new Point(3, 4), new Point(5, 6));
    Document document = PolygonToDocumentConverter.INSTANCE.convert(polygon);
    Polygon result = DocumentToPolygonConverter.INSTANCE.convert(document);
    assertThat(result, is(polygon));
    assertThat(result.getClass().equals(Polygon.class), is(true));
}
Also used : Point(org.springframework.data.geo.Point) Polygon(org.springframework.data.geo.Polygon) Document(org.bson.Document) Test(org.junit.Test)

Example 4 with Polygon

use of org.springframework.data.geo.Polygon in project spring-data-mongodb by spring-projects.

the class AbstractPersonRepositoryIntegrationTests method findsPeopleByLocationWithinPolygon.

@Test
public void findsPeopleByLocationWithinPolygon() {
    Point point = new Point(-73.99171, 40.738868);
    dave.setLocation(point);
    repository.save(dave);
    Point first = new Point(-78.99171, 35.738868);
    Point second = new Point(-78.99171, 45.738868);
    Point third = new Point(-68.99171, 45.738868);
    Point fourth = new Point(-68.99171, 35.738868);
    List<Person> result = repository.findByLocationWithin(new Polygon(first, second, third, fourth));
    assertThat(result.size(), is(1));
    assertThat(result, hasItem(dave));
}
Also used : Point(org.springframework.data.geo.Point) GeoJsonPoint(org.springframework.data.mongodb.core.geo.GeoJsonPoint) Polygon(org.springframework.data.geo.Polygon) Test(org.junit.Test)

Example 5 with Polygon

use of org.springframework.data.geo.Polygon in project spring-data-mongodb by spring-projects.

the class MappingMongoConverterUnitTests method shouldWriteEntityWithGeoPolygonCorrectly.

// DATAMONGO-858
@Test
public void shouldWriteEntityWithGeoPolygonCorrectly() {
    ClassWithGeoPolygon object = new ClassWithGeoPolygon();
    object.polygon = new Polygon(new Point(1, 2), new Point(3, 4), new Point(4, 5));
    org.bson.Document document = new org.bson.Document();
    converter.write(object, document);
    assertThat(document, is(notNullValue()));
    assertThat(document.get("polygon"), is(instanceOf(org.bson.Document.class)));
    org.bson.Document polygonDoc = (org.bson.Document) document.get("polygon");
    @SuppressWarnings("unchecked") List<org.bson.Document> points = (List<org.bson.Document>) polygonDoc.get("points");
    assertThat(points, hasSize(3));
    assertThat(points, Matchers.<org.bson.Document>hasItems(toDocument(object.polygon.getPoints().get(0)), toDocument(object.polygon.getPoints().get(1)), toDocument(object.polygon.getPoints().get(2))));
}
Also used : BasicDBList(com.mongodb.BasicDBList) List(java.util.List) ArrayList(java.util.ArrayList) Point(org.springframework.data.geo.Point) Polygon(org.springframework.data.geo.Polygon) Document(org.springframework.data.mongodb.core.mapping.Document) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)7 Point (org.springframework.data.geo.Point)7 Polygon (org.springframework.data.geo.Polygon)7 Document (org.bson.Document)2 Shape (org.springframework.data.geo.Shape)2 GeoJsonPoint (org.springframework.data.mongodb.core.geo.GeoJsonPoint)2 Document (org.springframework.data.mongodb.core.mapping.Document)2 BasicDBList (com.mongodb.BasicDBList)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Venue (org.springframework.data.mongodb.core.Venue)1 Query (org.springframework.data.mongodb.core.query.Query)1 PartTree (org.springframework.data.repository.query.parser.PartTree)1