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));
}
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));
}
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);
}
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);
}
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());
}
Aggregations