Search in sources :

Example 1 with Shape

use of org.springframework.data.geo.Shape 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 Shape

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

the class MongoConvertersUnitTests method convertsCircleToDocumentAndBackCorrectly.

// DATAMONGO-858
@Test
public void convertsCircleToDocumentAndBackCorrectly() {
    Circle circle = new Circle(new Point(1, 2), 3);
    Document document = GeoConverters.CircleToDocumentConverter.INSTANCE.convert(circle);
    Shape shape = GeoConverters.DocumentToCircleConverter.INSTANCE.convert(document);
    assertThat(shape, is((org.springframework.data.geo.Shape) circle));
}
Also used : Circle(org.springframework.data.geo.Circle) Shape(org.springframework.data.geo.Shape) Point(org.springframework.data.geo.Point) Document(org.bson.Document) Test(org.junit.Test)

Example 3 with Shape

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

the class MongoQueryCreator method from.

/**
 * Populates the given {@link CriteriaDefinition} depending on the {@link Part} given.
 *
 * @param part
 * @param property
 * @param criteria
 * @param parameters
 * @return
 */
private Criteria from(Part part, MongoPersistentProperty property, Criteria criteria, Iterator<Object> parameters) {
    Type type = part.getType();
    switch(type) {
        case AFTER:
        case GREATER_THAN:
            return criteria.gt(parameters.next());
        case GREATER_THAN_EQUAL:
            return criteria.gte(parameters.next());
        case BEFORE:
        case LESS_THAN:
            return criteria.lt(parameters.next());
        case LESS_THAN_EQUAL:
            return criteria.lte(parameters.next());
        case BETWEEN:
            return criteria.gt(parameters.next()).lt(parameters.next());
        case IS_NOT_NULL:
            return criteria.ne(null);
        case IS_NULL:
            return criteria.is(null);
        case NOT_IN:
            return criteria.nin(nextAsArray(parameters));
        case IN:
            return criteria.in(nextAsArray(parameters));
        case LIKE:
        case STARTING_WITH:
        case ENDING_WITH:
        case CONTAINING:
            return createContainingCriteria(part, property, criteria, parameters);
        case NOT_LIKE:
            return createContainingCriteria(part, property, criteria.not(), parameters);
        case NOT_CONTAINING:
            return createContainingCriteria(part, property, criteria.not(), parameters);
        case REGEX:
            return criteria.regex(parameters.next().toString());
        case EXISTS:
            return criteria.exists((Boolean) parameters.next());
        case TRUE:
            return criteria.is(true);
        case FALSE:
            return criteria.is(false);
        case NEAR:
            Range<Distance> range = accessor.getDistanceRange();
            Optional<Distance> distance = range.getUpperBound().getValue();
            Optional<Distance> minDistance = range.getLowerBound().getValue();
            Point point = accessor.getGeoNearLocation();
            Point pointToUse = point == null ? nextAs(parameters, Point.class) : point;
            boolean isSpherical = isSpherical(property);
            return distance.map(it -> {
                if (isSpherical || !Metrics.NEUTRAL.equals(it.getMetric())) {
                    criteria.nearSphere(pointToUse);
                } else {
                    criteria.near(pointToUse);
                }
                criteria.maxDistance(it.getNormalizedValue());
                minDistance.ifPresent(min -> criteria.minDistance(min.getNormalizedValue()));
                return criteria;
            }).orElseGet(() -> isSpherical ? criteria.nearSphere(pointToUse) : criteria.near(pointToUse));
        case WITHIN:
            Object parameter = parameters.next();
            return criteria.within((Shape) parameter);
        case SIMPLE_PROPERTY:
            return isSimpleComparisionPossible(part) ? criteria.is(parameters.next()) : createLikeRegexCriteriaOrThrow(part, property, criteria, parameters, false);
        case NEGATING_SIMPLE_PROPERTY:
            return isSimpleComparisionPossible(part) ? criteria.ne(parameters.next()) : createLikeRegexCriteriaOrThrow(part, property, criteria, parameters, true);
        default:
            throw new IllegalArgumentException("Unsupported keyword!");
    }
}
Also used : MongoRegexCreator(org.springframework.data.mongodb.core.query.MongoRegexCreator) Arrays(java.util.Arrays) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) IgnoreCaseType(org.springframework.data.repository.query.parser.Part.IgnoreCaseType) Metrics(org.springframework.data.geo.Metrics) LoggerFactory(org.slf4j.LoggerFactory) Shape(org.springframework.data.geo.Shape) Type(org.springframework.data.repository.query.parser.Part.Type) MappingContext(org.springframework.data.mapping.context.MappingContext) Part(org.springframework.data.repository.query.parser.Part) Distance(org.springframework.data.geo.Distance) AbstractQueryCreator(org.springframework.data.repository.query.parser.AbstractQueryCreator) Sort(org.springframework.data.domain.Sort) MatchMode(org.springframework.data.mongodb.core.query.MongoRegexCreator.MatchMode) Point(org.springframework.data.geo.Point) Logger(org.slf4j.Logger) ClassUtils(org.springframework.util.ClassUtils) Iterator(java.util.Iterator) GeoSpatialIndexType(org.springframework.data.mongodb.core.index.GeoSpatialIndexType) PotentiallyConvertingIterator(org.springframework.data.mongodb.repository.query.ConvertingParameterAccessor.PotentiallyConvertingIterator) PartTree(org.springframework.data.repository.query.parser.PartTree) Collection(java.util.Collection) Range(org.springframework.data.domain.Range) Criteria(org.springframework.data.mongodb.core.query.Criteria) Query(org.springframework.data.mongodb.core.query.Query) PersistentPropertyPath(org.springframework.data.mapping.context.PersistentPropertyPath) CriteriaDefinition(org.springframework.data.mongodb.core.query.CriteriaDefinition) Optional(java.util.Optional) PropertyPath(org.springframework.data.mapping.PropertyPath) GeoSpatialIndexed(org.springframework.data.mongodb.core.index.GeoSpatialIndexed) Assert(org.springframework.util.Assert) IgnoreCaseType(org.springframework.data.repository.query.parser.Part.IgnoreCaseType) Type(org.springframework.data.repository.query.parser.Part.Type) GeoSpatialIndexType(org.springframework.data.mongodb.core.index.GeoSpatialIndexType) Point(org.springframework.data.geo.Point) Distance(org.springframework.data.geo.Distance)

Example 4 with Shape

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

the class MongoConvertersUnitTests method convertsBoxToDocumentAndBackCorrectly.

// DATAMONGO-858
@Test
public void convertsBoxToDocumentAndBackCorrectly() {
    Box box = new Box(new Point(1, 2), new Point(3, 4));
    Document document = GeoConverters.BoxToDocumentConverter.INSTANCE.convert(box);
    Shape shape = GeoConverters.DocumentToBoxConverter.INSTANCE.convert(document);
    assertThat(shape, is((org.springframework.data.geo.Shape) box));
}
Also used : Shape(org.springframework.data.geo.Shape) Box(org.springframework.data.geo.Box) Point(org.springframework.data.geo.Point) Document(org.bson.Document) Test(org.junit.Test)

Example 5 with Shape

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

the class MongoConvertersUnitTests 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 = GeoConverters.PolygonToDocumentConverter.INSTANCE.convert(polygon);
    Shape shape = GeoConverters.DocumentToPolygonConverter.INSTANCE.convert(document);
    assertThat(shape, is((org.springframework.data.geo.Shape) polygon));
}
Also used : Shape(org.springframework.data.geo.Shape) Point(org.springframework.data.geo.Point) Polygon(org.springframework.data.geo.Polygon) Document(org.bson.Document) Test(org.junit.Test)

Aggregations

Point (org.springframework.data.geo.Point)6 Shape (org.springframework.data.geo.Shape)6 Test (org.junit.Test)5 Document (org.bson.Document)4 Polygon (org.springframework.data.geo.Polygon)2 Query (org.springframework.data.mongodb.core.query.Query)2 PartTree (org.springframework.data.repository.query.parser.PartTree)2 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 Optional (java.util.Optional)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 Range (org.springframework.data.domain.Range)1 Sort (org.springframework.data.domain.Sort)1 Box (org.springframework.data.geo.Box)1 Circle (org.springframework.data.geo.Circle)1 Distance (org.springframework.data.geo.Distance)1 Metrics (org.springframework.data.geo.Metrics)1 PropertyPath (org.springframework.data.mapping.PropertyPath)1