Example usage for org.springframework.data.elasticsearch.core.geo GeoPoint fromPoint

List of usage examples for org.springframework.data.elasticsearch.core.geo GeoPoint fromPoint

Introduction

In this page you can find the example usage for org.springframework.data.elasticsearch.core.geo GeoPoint fromPoint.

Prototype

public static GeoPoint fromPoint(Point point) 

Source Link

Document

build a GeoPoint from a org.springframework.data.geo.Point

Usage

From source file:com.github.vanroy.springdata.jest.CriteriaFilterProcessor.java

private QueryBuilder processCriteriaEntry(OperationKey key, Object value, String fieldName) {
    if (value == null) {
        return null;
    }/*ww w .j  a v a  2  s.  c o m*/
    QueryBuilder filter = null;

    switch (key) {
    case WITHIN: {
        GeoDistanceQueryBuilder geoDistanceQueryBuilder = QueryBuilders.geoDistanceQuery(fieldName);

        Assert.isTrue(value instanceof Object[],
                "Value of a geo distance filter should be an array of two values.");
        Object[] valArray = (Object[]) value;
        Assert.noNullElements(valArray, "Geo distance filter takes 2 not null elements array as parameter.");
        Assert.isTrue(valArray.length == 2, "Geo distance filter takes a 2-elements array as parameter.");
        Assert.isTrue(
                valArray[0] instanceof GeoPoint || valArray[0] instanceof String
                        || valArray[0] instanceof Point,
                "First element of a geo distance filter must be a GeoPoint, a Point or a String");
        Assert.isTrue(valArray[1] instanceof String || valArray[1] instanceof Distance,
                "Second element of a geo distance filter must be a String or a Distance");

        StringBuilder dist = new StringBuilder();

        if (valArray[1] instanceof Distance) {
            extractDistanceString((Distance) valArray[1], dist);
        } else {
            dist.append((String) valArray[1]);
        }

        if (valArray[0] instanceof GeoPoint) {
            GeoPoint loc = (GeoPoint) valArray[0];
            geoDistanceQueryBuilder.lat(loc.getLat()).lon(loc.getLon()).distance(dist.toString())
                    .geoDistance(GeoDistance.PLANE);
        } else if (valArray[0] instanceof Point) {
            GeoPoint loc = GeoPoint.fromPoint((Point) valArray[0]);
            geoDistanceQueryBuilder.lat(loc.getLat()).lon(loc.getLon()).distance(dist.toString())
                    .geoDistance(GeoDistance.PLANE);
        } else {
            String loc = (String) valArray[0];
            if (loc.contains(",")) {
                String c[] = loc.split(",");
                geoDistanceQueryBuilder.lat(Double.parseDouble(c[0])).lon(Double.parseDouble(c[1]))
                        .distance(dist.toString()).geoDistance(GeoDistance.PLANE);
            } else {
                geoDistanceQueryBuilder.geohash(loc).distance(dist.toString()).geoDistance(GeoDistance.PLANE);
            }
        }
        filter = geoDistanceQueryBuilder;

        break;
    }

    case BBOX: {
        filter = QueryBuilders.geoBoundingBoxQuery(fieldName);

        Assert.isTrue(value instanceof Object[],
                "Value of a boundedBy filter should be an array of one or two values.");
        Object[] valArray = (Object[]) value;
        Assert.noNullElements(valArray, "Geo boundedBy filter takes a not null element array as parameter.");

        if (valArray.length == 1) {
            //GeoEnvelop
            oneParameterBBox((GeoBoundingBoxQueryBuilder) filter, valArray[0]);
        } else if (valArray.length == 2) {
            //2x GeoPoint
            //2x String
            twoParameterBBox((GeoBoundingBoxQueryBuilder) filter, valArray);
        } else {
            //error
            Assert.isTrue(false,
                    "Geo distance filter takes a 1-elements array(GeoBox) or 2-elements array(GeoPoints or Strings(format lat,lon or geohash)).");
        }
        break;
    }
    }

    return filter;
}