Search in sources :

Example 21 with Distance

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

the class MongoQueryCreatorUnitTests method shouldCreateNearSphereQueryForSphericalPropertyHavingDistanceWithDefaultMetric.

// DATAMONGO-1110
@Test
public void shouldCreateNearSphereQueryForSphericalPropertyHavingDistanceWithDefaultMetric() {
    Point point = new Point(1.0, 1.0);
    Distance distance = new Distance(1.0);
    PartTree tree = new PartTree("findByAddress2dSphere_GeoNear", User.class);
    MongoQueryCreator creator = new MongoQueryCreator(tree, getAccessor(converter, point, distance), context);
    Query query = creator.createQuery();
    assertThat(query, is(query(where("address2dSphere.geo").nearSphere(point).maxDistance(1.0))));
}
Also used : Query(org.springframework.data.mongodb.core.query.Query) Point(org.springframework.data.geo.Point) GeoJsonPoint(org.springframework.data.mongodb.core.geo.GeoJsonPoint) PartTree(org.springframework.data.repository.query.parser.PartTree) Distance(org.springframework.data.geo.Distance) Test(org.junit.Test)

Example 22 with Distance

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

the class MongoQueryCreatorUnitTests method bindsMetricDistanceParameterToNearSphereCorrectly.

@Test
public void bindsMetricDistanceParameterToNearSphereCorrectly() throws Exception {
    Point point = new Point(10, 20);
    Distance distance = new Distance(2.5, Metrics.KILOMETERS);
    Query query = query(where("location").nearSphere(point).maxDistance(distance.getNormalizedValue()).and("firstname").is("Dave"));
    assertBindsDistanceToQuery(point, distance, query);
}
Also used : Query(org.springframework.data.mongodb.core.query.Query) Point(org.springframework.data.geo.Point) GeoJsonPoint(org.springframework.data.mongodb.core.geo.GeoJsonPoint) Distance(org.springframework.data.geo.Distance) Test(org.junit.Test)

Example 23 with Distance

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

the class ReactiveMongoQueryExecutionUnitTests method geoNearExecutionShouldApplyQuerySettings.

// DATAMONGO-1444
@Test
public void geoNearExecutionShouldApplyQuerySettings() throws Exception {
    Method geoNear = ClassUtils.getMethod(GeoRepo.class, "geoNear");
    Query query = new Query();
    when(parameterAccessor.getGeoNearLocation()).thenReturn(new Point(1, 2));
    when(parameterAccessor.getDistanceRange()).thenReturn(new Range<>(new Distance(10), new Distance(15)));
    when(parameterAccessor.getPageable()).thenReturn(PageRequest.of(1, 10));
    new GeoNearExecution(operations, parameterAccessor, ClassTypeInformation.fromReturnTypeOf(geoNear)).execute(query, Person.class, "person");
    ArgumentCaptor<NearQuery> queryArgumentCaptor = ArgumentCaptor.forClass(NearQuery.class);
    verify(operations).geoNear(queryArgumentCaptor.capture(), eq(Person.class), eq("person"));
    NearQuery nearQuery = queryArgumentCaptor.getValue();
    assertThat(nearQuery.toDocument().get("near"), is(equalTo(Arrays.asList(1d, 2d))));
    assertThat(nearQuery.getSkip(), is(10L));
    assertThat(nearQuery.getMinDistance(), is(equalTo(new Distance(10))));
    assertThat(nearQuery.getMaxDistance(), is(equalTo(new Distance(15))));
}
Also used : NearQuery(org.springframework.data.mongodb.core.query.NearQuery) NearQuery(org.springframework.data.mongodb.core.query.NearQuery) Query(org.springframework.data.mongodb.core.query.Query) GeoNearExecution(org.springframework.data.mongodb.repository.query.ReactiveMongoQueryExecution.GeoNearExecution) Method(java.lang.reflect.Method) Point(org.springframework.data.geo.Point) Person(org.springframework.data.mongodb.repository.Person) Distance(org.springframework.data.geo.Distance) Test(org.junit.Test)

Example 24 with Distance

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

the class GeoConvertersUnitTests method convertsSphereToDocumentAndBackCorrectlyWithKilometerDistance.

// DATAMONGO-858
@Test
public void convertsSphereToDocumentAndBackCorrectlyWithKilometerDistance() {
    Distance radius = new Distance(3, Metrics.KILOMETERS);
    Sphere sphere = new Sphere(new Point(1, 2), radius);
    Document document = SphereToDocumentConverter.INSTANCE.convert(sphere);
    Sphere result = DocumentToSphereConverter.INSTANCE.convert(document);
    assertThat(result, is(sphere));
    assertThat(result.getRadius(), is(radius));
    assertThat(result.getClass().equals(org.springframework.data.mongodb.core.geo.Sphere.class), is(true));
}
Also used : Sphere(org.springframework.data.mongodb.core.geo.Sphere) Point(org.springframework.data.geo.Point) Document(org.bson.Document) Distance(org.springframework.data.geo.Distance) Test(org.junit.Test)

Example 25 with Distance

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

the class MappingMongoConverterUnitTests method shouldWriteEntityWithGeoShapeCorrectly.

// DATAMONGO-858
@Test
public void shouldWriteEntityWithGeoShapeCorrectly() {
    ClassWithGeoShape object = new ClassWithGeoShape();
    Sphere sphere = new Sphere(new Point(1, 2), 3);
    Distance radius = sphere.getRadius();
    object.shape = sphere;
    org.bson.Document document = new org.bson.Document();
    converter.write(object, document);
    assertThat(document, is(notNullValue()));
    assertThat(document.get("shape"), is(instanceOf(org.bson.Document.class)));
    assertThat(document.get("shape"), is((Object) new org.bson.Document("center", new org.bson.Document("x", sphere.getCenter().getX()).append("y", sphere.getCenter().getY())).append("radius", radius.getNormalizedValue()).append("metric", radius.getMetric().toString())));
}
Also used : Sphere(org.springframework.data.mongodb.core.geo.Sphere) Point(org.springframework.data.geo.Point) Document(org.springframework.data.mongodb.core.mapping.Document) Distance(org.springframework.data.geo.Distance) Test(org.junit.Test)

Aggregations

Distance (org.springframework.data.geo.Distance)28 Point (org.springframework.data.geo.Point)27 Test (org.junit.Test)25 GeoJsonPoint (org.springframework.data.mongodb.core.geo.GeoJsonPoint)12 Query (org.springframework.data.mongodb.core.query.Query)7 Document (org.springframework.data.mongodb.core.mapping.Document)6 Document (org.bson.Document)5 Circle (org.springframework.data.geo.Circle)5 Metric (org.springframework.data.geo.Metric)5 Sphere (org.springframework.data.mongodb.core.geo.Sphere)5 PartTree (org.springframework.data.repository.query.parser.PartTree)4 Arrays (java.util.Arrays)3 Sort (org.springframework.data.domain.Sort)3 Metrics (org.springframework.data.geo.Metrics)3 Method (java.lang.reflect.Method)2 BlockingQueue (java.util.concurrent.BlockingQueue)2 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)2 TimeUnit (java.util.concurrent.TimeUnit)2 NoArgsConstructor (lombok.NoArgsConstructor)2 Matchers (org.hamcrest.Matchers)2