Search in sources :

Example 1 with Distance

use of org.springframework.data.geo.Distance in project tutorials by eugenp.

the class CampusServiceImplIntegrationTest method whenFindByLocationNearNewYorkCity_thenResultContainsColumbia.

@Test
public final void whenFindByLocationNearNewYorkCity_thenResultContainsColumbia() throws Exception {
    Set<Campus> campuses = campusService.findByLocationNear(NewYorkCity, new Distance(1, Metrics.NEUTRAL));
    assertFalse(campuses.isEmpty());
    assertTrue(campuses.contains(Columbia));
    assertFalse(campuses.contains(Harvard));
}
Also used : Campus(org.baeldung.spring.data.couchbase.model.Campus) Distance(org.springframework.data.geo.Distance) Test(org.junit.Test) MultiBucketIntegationTest(org.baeldung.spring.data.couchbase2b.MultiBucketIntegationTest)

Example 2 with Distance

use of org.springframework.data.geo.Distance in project tutorials by eugenp.

the class CampusServiceImplIntegrationTest method whenFindByLocationNearBoston_thenResultContainsHarvard.

@Test
public final void whenFindByLocationNearBoston_thenResultContainsHarvard() throws Exception {
    Set<Campus> campuses = campusService.findByLocationNear(Boston, new Distance(1, Metrics.NEUTRAL));
    assertFalse(campuses.isEmpty());
    assertTrue(campuses.contains(Harvard));
    assertFalse(campuses.contains(Columbia));
}
Also used : Campus(org.baeldung.spring.data.couchbase.model.Campus) Distance(org.springframework.data.geo.Distance) Test(org.junit.Test) MultiBucketIntegationTest(org.baeldung.spring.data.couchbase2b.MultiBucketIntegationTest)

Example 3 with Distance

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

the class MongoTemplate method geoNear.

public <T> GeoResults<T> geoNear(NearQuery near, Class<?> domainType, String collectionName, Class<T> returnType) {
    if (near == null) {
        throw new InvalidDataAccessApiUsageException("NearQuery must not be null!");
    }
    if (domainType == null) {
        throw new InvalidDataAccessApiUsageException("Entity class must not be null!");
    }
    Assert.notNull(collectionName, "CollectionName must not be null!");
    Assert.notNull(returnType, "ReturnType must not be null!");
    String collection = StringUtils.hasText(collectionName) ? collectionName : getCollectionName(domainType);
    String distanceField = operations.nearQueryDistanceFieldName(domainType);
    Aggregation $geoNear = TypedAggregation.newAggregation(domainType, Aggregation.geoNear(near, distanceField)).withOptions(AggregationOptions.builder().collation(near.getCollation()).build());
    AggregationResults<Document> results = aggregate($geoNear, collection, Document.class);
    EntityProjection<T, ?> projection = operations.introspectProjection(returnType, domainType);
    DocumentCallback<GeoResult<T>> callback = new GeoNearResultDocumentCallback<>(distanceField, new ProjectingReadCallback<>(mongoConverter, projection, collection), near.getMetric());
    List<GeoResult<T>> result = new ArrayList<>();
    BigDecimal aggregate = BigDecimal.ZERO;
    for (Document element : results) {
        GeoResult<T> geoResult = callback.doWith(element);
        aggregate = aggregate.add(BigDecimal.valueOf(geoResult.getDistance().getValue()));
        result.add(geoResult);
    }
    Distance avgDistance = new Distance(result.size() == 0 ? 0 : aggregate.divide(new BigDecimal(result.size()), RoundingMode.HALF_UP).doubleValue(), near.getMetric());
    return new GeoResults<>(result, avgDistance);
}
Also used : GeoResults(org.springframework.data.geo.GeoResults) Document(org.bson.Document) BigDecimal(java.math.BigDecimal) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) TypedAggregation(org.springframework.data.mongodb.core.aggregation.TypedAggregation) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) GeoResult(org.springframework.data.geo.GeoResult) Distance(org.springframework.data.geo.Distance)

Example 4 with Distance

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

the class MongoParametersParameterAccessor method getDistanceRange.

public Range<Distance> getDistanceRange() {
    MongoParameters mongoParameters = method.getParameters();
    int rangeIndex = mongoParameters.getRangeIndex();
    if (rangeIndex != -1) {
        return getValue(rangeIndex);
    }
    int maxDistanceIndex = mongoParameters.getMaxDistanceIndex();
    Bound<Distance> maxDistance = maxDistanceIndex == -1 ? Bound.unbounded() : Bound.inclusive((Distance) getValue(maxDistanceIndex));
    return Range.of(Bound.unbounded(), maxDistance);
}
Also used : Point(org.springframework.data.geo.Point) Distance(org.springframework.data.geo.Distance)

Example 5 with Distance

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

the class GeoNearOperationUnitTests method rendersMaxDistanceCorrectly.

// DATAMONGO-2264
@Test
public void rendersMaxDistanceCorrectly() {
    NearQuery query = NearQuery.near(10.0, 20.0).maxDistance(new Distance(30.0));
    assertThat(new GeoNearOperation(query, "distance").toPipelineStages(Aggregation.DEFAULT_CONTEXT)).containsExactly($geoNear().near(10.0, 20.0).maxDistance(30.0).doc());
}
Also used : NearQuery(org.springframework.data.mongodb.core.query.NearQuery) Distance(org.springframework.data.geo.Distance) Test(org.junit.jupiter.api.Test)

Aggregations

Distance (org.springframework.data.geo.Distance)44 Test (org.junit.jupiter.api.Test)34 Point (org.springframework.data.geo.Point)32 GeoJsonPoint (org.springframework.data.mongodb.core.geo.GeoJsonPoint)17 Query (org.springframework.data.mongodb.core.query.Query)9 Test (org.junit.Test)6 Product (com.nixmash.blog.solr.model.Product)5 Document (org.bson.Document)5 Circle (org.springframework.data.geo.Circle)5 Document (org.springframework.data.mongodb.core.mapping.Document)5 PartTree (org.springframework.data.repository.query.parser.PartTree)5 Sphere (org.springframework.data.mongodb.core.geo.Sphere)4 Arrays (java.util.Arrays)3 Sort (org.springframework.data.domain.Sort)3 Metrics (org.springframework.data.geo.Metrics)3 MongoClient (com.mongodb.reactivestreams.client.MongoClient)2 Method (java.lang.reflect.Method)2 Map (java.util.Map)2 BlockingQueue (java.util.concurrent.BlockingQueue)2 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)2