org.elasticsearch.common.geo.GeoUtils Java Examples

The following examples show how to use org.elasticsearch.common.geo.GeoUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GeolocationContextMapping.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private static final int parsePrecision(XContentParser parser) throws IOException, ElasticsearchParseException {
    switch (parser.currentToken()) {
    case VALUE_STRING:
        return GeoUtils.geoHashLevelsForPrecision(parser.text());
    case VALUE_NUMBER:
        switch (parser.numberType()) {
        case INT:
        case LONG:
            return parser.intValue();
        default:
            return GeoUtils.geoHashLevelsForPrecision(parser.doubleValue());
        }
    default:
        throw new ElasticsearchParseException("invalid precision value");
    }
}
 
Example #2
Source File: GeoPointFieldMapper.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
protected void parse(ParseContext context, GeoPoint point, String geoHash) throws IOException {
    if (ignoreMalformed.value() == false) {
        if (point.lat() > 90.0 || point.lat() < -90.0) {
            throw new IllegalArgumentException("illegal latitude value [" + point.lat() + "] for " + name());
        }
        if (point.lon() > 180.0 || point.lon() < -180) {
            throw new IllegalArgumentException("illegal longitude value [" + point.lon() + "] for " + name());
        }
    } else {
        // LUCENE WATCH: This will be folded back into Lucene's GeoPointField
        GeoUtils.normalizePoint(point);
    }
    if (fieldType().indexOptions() != IndexOptions.NONE || fieldType().stored()) {
        context.doc().add(new GeoPointField(fieldType().names().indexName(), point.lon(), point.lat(), fieldType() ));
    }
    super.parse(context, point, geoHash);
}
 
Example #3
Source File: GeolocationContextMapping.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
protected static Collection<String> parseSinglePointOrList(XContentParser parser) throws IOException {
    Token token = parser.currentToken();
    if(token == Token.START_ARRAY) {
        token = parser.nextToken();
        // Test if value is a single point in <code>[lon, lat]</code> format
        if(token == Token.VALUE_NUMBER) {
            double lon = parser.doubleValue();
            if(parser.nextToken() == Token.VALUE_NUMBER) {
                double lat = parser.doubleValue();
                if(parser.nextToken() == Token.END_ARRAY) {
                    return Collections.singleton(GeoHashUtils.stringEncode(lon, lat));
                } else {
                    throw new ElasticsearchParseException("only two values expected");
                }
            } else {
                throw new ElasticsearchParseException("latitue must be a numeric value");
            }
        } else {
            // otherwise it's a list of locations
            ArrayList<String> result = new ArrayList<>();
            while (token != Token.END_ARRAY) {
                result.add(GeoUtils.parseGeoPoint(parser).geohash());
                token = parser.nextToken(); //infinite loop without this line
            }
            return result;
        }
    } else {
        // or a single location
        return Collections.singleton(GeoUtils.parseGeoPoint(parser).geohash());
    }
}
 
Example #4
Source File: GeolocationContextMapping.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public static GeoQuery query(String name, double lat, double lon, String ... precisions) {
    int precisionInts[] = new int[precisions.length];
    for (int i = 0 ; i < precisions.length; i++) {
        precisionInts[i] = GeoUtils.geoHashLevelsForPrecision(precisions[i]);
    }
    return query(name, GeoHashUtils.stringEncode(lon, lat), precisionInts);
}
 
Example #5
Source File: GeolocationContextMapping.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Set the precision use o make suggestions
 *
 * @param meters
 *            precision as distance in meters
 * @return this
 */
public Builder precision(double meters) {
    int level = GeoUtils.geoHashLevelsForPrecision(meters);
    // Ceiling precision: we might return more results
    if (GeoUtils.geoHashCellSize(level) < meters) {
       level = Math.max(1, level - 1);
    }
    return precision(level);
}
 
Example #6
Source File: DecayFunctionParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private AbstractDistanceScoreFunction parseGeoVariable(String fieldName, XContentParser parser, QueryParseContext parseContext,
        GeoPointFieldMapper.GeoPointFieldType fieldType, MultiValueMode mode) throws IOException {
    XContentParser.Token token;
    String parameterName = null;
    GeoPoint origin = new GeoPoint();
    String scaleString = null;
    String offsetString = "0km";
    double decay = 0.5;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            parameterName = parser.currentName();
        } else if (parameterName.equals(DecayFunctionBuilder.SCALE)) {
            scaleString = parser.text();
        } else if (parameterName.equals(DecayFunctionBuilder.ORIGIN)) {
            origin = GeoUtils.parseGeoPoint(parser);
        } else if (parameterName.equals(DecayFunctionBuilder.DECAY)) {
            decay = parser.doubleValue();
        } else if (parameterName.equals(DecayFunctionBuilder.OFFSET)) {
            offsetString = parser.text();
        } else {
            throw new ElasticsearchParseException("parameter [{}] not supported!", parameterName);
        }
    }
    if (origin == null || scaleString == null) {
        throw new ElasticsearchParseException("[{}] and [{}] must be set for geo fields.", DecayFunctionBuilder.ORIGIN, DecayFunctionBuilder.SCALE);
    }
    double scale = DistanceUnit.DEFAULT.parse(scaleString, DistanceUnit.DEFAULT);
    double offset = DistanceUnit.DEFAULT.parse(offsetString, DistanceUnit.DEFAULT);
    IndexGeoPointFieldData indexFieldData = parseContext.getForField(fieldType);
    return new GeoFieldDataScoreFunction(origin, scale, decay, offset, getDecayFunction(), indexFieldData, mode);

}
 
Example #7
Source File: GeoShapeFieldMapper.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private static int getLevels(int treeLevels, double precisionInMeters, int defaultLevels, boolean geoHash) {
    if (treeLevels > 0 || precisionInMeters >= 0) {
        return Math.max(treeLevels, precisionInMeters >= 0 ? (geoHash ? GeoUtils.geoHashLevelsForPrecision(precisionInMeters)
            : GeoUtils.quadTreeLevelsForPrecision(precisionInMeters)) : 0);
    }
    return defaultLevels;
}
 
Example #8
Source File: InternalGeoPointClustering.java    From elasticsearch-aggregation-geoclustering with Apache License 2.0 5 votes vote down vote up
private void computeDistance(Bucket bucket, Bucket potentialNeighbor, List<Bucket> revisit, ReduceContext reduceContext) {
    if (potentialNeighbor.visited) {
        return;
    }

    double neighborDistance = GeoUtils.arcDistance(
            bucket.centroid.lat(),
            bucket.centroid.lon(),
            potentialNeighbor.centroid.lat(),
            potentialNeighbor.centroid.lon()
    );

    double avgLat = (bucket.centroid.lat() + potentialNeighbor.centroid.lat()) / 2;

    double fixedRadius = radius * Math.cos(DistanceUtils.toRadians(avgLat));

    if (neighborDistance <= fixedRadius) {
        potentialNeighbor.visited = true;
        long mergedDocCount = bucket.docCount + potentialNeighbor.docCount;
        double newCentroidLat = (bucket.centroid.getLat() * bucket.docCount +
                potentialNeighbor.centroid.getLat() * potentialNeighbor.docCount) / mergedDocCount;
        double newCentroidLon = (bucket.centroid.getLon() * bucket.docCount +
                potentialNeighbor.centroid.getLon() * potentialNeighbor.docCount) / mergedDocCount;
        bucket.centroid = new GeoPoint(newCentroidLat, newCentroidLon);

        bucket.docCount = mergedDocCount;
        List<InternalAggregations> aggregationsList = new ArrayList<>();
        aggregationsList.add(bucket.aggregations);
        aggregationsList.add(potentialNeighbor.aggregations);
        bucket.aggregations = InternalAggregations.reduce(aggregationsList, reduceContext);
        bucket.geohashesList.add(potentialNeighbor.geohashAsLong);
    } else if (revisit != null && ratio > 0 && neighborDistance / fixedRadius < ratio ) {
        revisit.add(potentialNeighbor);
    }
}
 
Example #9
Source File: GeoShapeFieldMapper.java    From crate with Apache License 2.0 5 votes vote down vote up
private static int getLevels(int treeLevels, double precisionInMeters, int defaultLevels, boolean geoHash) {
    if (treeLevels > 0 || precisionInMeters >= 0) {
        return Math.max(treeLevels, precisionInMeters >= 0 ? (geoHash ? GeoUtils.geoHashLevelsForPrecision(precisionInMeters)
            : GeoUtils.quadTreeLevelsForPrecision(precisionInMeters)) : 0);
    }
    return defaultLevels;
}
 
Example #10
Source File: GeoPointFieldMapper.java    From crate with Apache License 2.0 5 votes vote down vote up
protected void parse(ParseContext context, GeoPoint point) throws IOException {

        if (ignoreMalformed.value() == false) {
            if (point.lat() > 90.0 || point.lat() < -90.0) {
                throw new IllegalArgumentException("illegal latitude value [" + point.lat() + "] for " + name());
            }
            if (point.lon() > 180.0 || point.lon() < -180) {
                throw new IllegalArgumentException("illegal longitude value [" + point.lon() + "] for " + name());
            }
        } else {
            GeoUtils.normalizePoint(point);
        }
        if (fieldType().indexOptions() != IndexOptions.NONE) {
            context.doc().add(new LatLonPoint(fieldType().name(), point.lat(), point.lon()));
        }
        if (fieldType().stored()) {
            context.doc().add(new StoredField(fieldType().name(), point.toString()));
        }
        if (fieldType.hasDocValues()) {
            context.doc().add(new LatLonDocValuesField(fieldType().name(), point.lat(), point.lon()));
        } else if (fieldType().stored() || fieldType().indexOptions() != IndexOptions.NONE) {
            List<IndexableField> fields = new ArrayList<>(1);
            createFieldNamesField(context, fields);
            for (IndexableField field : fields) {
                context.doc().add(field);
            }
        }
        // if the mapping contains multifields then use the geohash string
        if (multiFields.iterator().hasNext()) {
            multiFields.parse(this, context.createExternalValueContext(point.geohash()));
        }
    }
 
Example #11
Source File: GeoPointFieldMapper.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Parses geopoint represented as an object or an array, ignores malformed geopoints if needed
 */
private void parseGeoPointIgnoringMalformed(ParseContext context, GeoPoint sparse) throws IOException {
    try {
        parse(context, GeoUtils.parseGeoPoint(context.parser(), sparse));
    } catch (ElasticsearchParseException e) {
        if (ignoreMalformed.value() == false) {
            throw e;
        }
        context.addIgnoredField(fieldType.name());
    }
}
 
Example #12
Source File: DistanceFunction.java    From crate with Apache License 2.0 5 votes vote down vote up
public static Double evaluate(Input<Point> arg1, Input<Point> arg2) {
    Point value1 = arg1.value();
    if (value1 == null) {
        return null;
    }
    Point value2 = arg2.value();
    if (value2 == null) {
        return null;
    }
    return GeoUtils.arcDistance(value1.getY(), value1.getX(), value2.getY(), value2.getX());
}
 
Example #13
Source File: GeohashCellQuery.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
    XContentParser parser = parseContext.parser();

    String fieldName = null;
    String geohash = null;
    int levels = -1;
    boolean neighbors = false;


    XContentParser.Token token;
    if ((token = parser.currentToken()) != Token.START_OBJECT) {
        throw new ElasticsearchParseException("failed to parse [{}] query. expected an object but found [{}] instead", NAME, token);
    }

    while ((token = parser.nextToken()) != Token.END_OBJECT) {
        if (token == Token.FIELD_NAME) {
            String field = parser.currentName();

            if (parseContext.isDeprecatedSetting(field)) {
                // skip
            } else if (PRECISION.equals(field)) {
                token = parser.nextToken();
                if(token == Token.VALUE_NUMBER) {
                    levels = parser.intValue();
                } else if(token == Token.VALUE_STRING) {
                    double meters = DistanceUnit.parse(parser.text(), DistanceUnit.DEFAULT, DistanceUnit.METERS);
                    levels = GeoUtils.geoHashLevelsForPrecision(meters);
                }
            } else if (NEIGHBORS.equals(field)) {
                parser.nextToken();
                neighbors = parser.booleanValue();
            } else {
                fieldName = field;
                token = parser.nextToken();
                if(token == Token.VALUE_STRING) {
                    // A string indicates either a gehash or a lat/lon string
                    String location = parser.text();
                    if(location.indexOf(",")>0) {
                        geohash = GeoUtils.parseGeoPoint(parser).geohash();
                    } else {
                        geohash = location;
                    }
                } else {
                    geohash = GeoUtils.parseGeoPoint(parser).geohash();
                }
            }
        } else {
            throw new ElasticsearchParseException("failed to parse [{}] query. unexpected token [{}]", NAME, token);
        }
    }

    if (geohash == null) {
        throw new QueryParsingException(parseContext, "failed to parse [{}] query. missing geohash value", NAME);
    }

    MappedFieldType fieldType = parseContext.fieldMapper(fieldName);
    if (fieldType == null) {
        throw new QueryParsingException(parseContext, "failed to parse [{}] query. missing [{}] field [{}]", NAME, BaseGeoPointFieldMapper.CONTENT_TYPE, fieldName);
    }

    if (!(fieldType instanceof BaseGeoPointFieldMapper.GeoPointFieldType)) {
        throw new QueryParsingException(parseContext, "failed to parse [{}] query. field [{}] is not a geo_point field", NAME, fieldName);
    }

    BaseGeoPointFieldMapper.GeoPointFieldType geoFieldType = ((BaseGeoPointFieldMapper.GeoPointFieldType) fieldType);
    if (!geoFieldType.isGeoHashPrefixEnabled()) {
        throw new QueryParsingException(parseContext, "failed to parse [{}] query. [geohash_prefix] is not enabled for field [{}]", NAME, fieldName);
    }

    if(levels > 0) {
        int len = Math.min(levels, geohash.length());
        geohash = geohash.substring(0, len);
    }

    Query filter;
    if (neighbors) {
        filter = create(parseContext, geoFieldType, geohash, GeoHashUtils.addNeighbors(geohash, new ArrayList<CharSequence>(8)));
    } else {
        filter = create(parseContext, geoFieldType, geohash, null);
    }

    return filter;
}
 
Example #14
Source File: BaseGeoPointFieldMapper.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public Mapper.Builder<?, ?> parse(String name, Map<String, Object> node, ParserContext parserContext) throws MapperParsingException {
    Builder builder;
    if (parserContext.indexVersionCreated().before(Version.V_2_2_0)) {
        builder = new GeoPointFieldMapperLegacy.Builder(name);
    } else {
        builder = new GeoPointFieldMapper.Builder(name);
    }
    parseField(builder, name, node, parserContext);

    for (Iterator<Map.Entry<String, Object>> iterator = node.entrySet().iterator(); iterator.hasNext();) {
        Map.Entry<String, Object> entry = iterator.next();
        String propName = Strings.toUnderscoreCase(entry.getKey());
        Object propNode = entry.getValue();
        if (propName.equals("lat_lon")) {
            deprecationLogger.deprecated(CONTENT_TYPE + " lat_lon parameter is deprecated and will be removed "
                + "in the next major release");
            builder.enableLatLon(XContentMapValues.nodeBooleanValue(propNode));
            iterator.remove();
        } else if (propName.equals("precision_step")) {
            deprecationLogger.deprecated(CONTENT_TYPE + " precision_step parameter is deprecated and will be removed "
                + "in the next major release");
            builder.precisionStep(XContentMapValues.nodeIntegerValue(propNode));
            iterator.remove();
        } else if (propName.equals("geohash")) {
            builder.enableGeoHash(XContentMapValues.nodeBooleanValue(propNode));
            iterator.remove();
        } else if (propName.equals("geohash_prefix")) {
            builder.geoHashPrefix(XContentMapValues.nodeBooleanValue(propNode));
            if (XContentMapValues.nodeBooleanValue(propNode)) {
                builder.enableGeoHash(true);
            }
            iterator.remove();
        } else if (propName.equals("geohash_precision")) {
            if (propNode instanceof Integer) {
                builder.geoHashPrecision(XContentMapValues.nodeIntegerValue(propNode));
            } else {
                builder.geoHashPrecision(GeoUtils.geoHashLevelsForPrecision(propNode.toString()));
            }
            iterator.remove();
        } else if (propName.equals(Names.IGNORE_MALFORMED)) {
            builder.ignoreMalformed(XContentMapValues.nodeBooleanValue(propNode));
            iterator.remove();
        } else if (parseMultiField(builder, name, parserContext, propName, propNode)) {
            iterator.remove();
        }
    }

    if (builder instanceof GeoPointFieldMapperLegacy.Builder) {
        return GeoPointFieldMapperLegacy.parse((GeoPointFieldMapperLegacy.Builder) builder, node, parserContext);
    }

    return (GeoPointFieldMapper.Builder) builder;
}
 
Example #15
Source File: DistanceQuery.java    From crate with Apache License 2.0 4 votes vote down vote up
/**
 * Create a LatLonPoint distance query.
 *
 *
 * <pre>
 *          , - ~ ~ ~ - ,
 *      , '               ' ,
 *    ,                       ,     X = lonLat coordinates
 *   ,                         ,
 *  ,              [distance]   ,
 *  ,            p<------------>X
 *  ,            ^              ,
 *   ,           |             ,
 *    ,      [columnName]     ,
 *      ,     point        , '
 *        ' - , _ _ _ ,  '
 *
 *  lt and lte -> match everything WITHIN distance
 *  gt and gte -> match everything OUTSIDE distance
 *
 *  eq distance ~ 0 -> match everything within distance + tolerance
 *
 *  eq distance > 0 -> build two circles, one slightly smaller, one slightly larger
 *                          distance must not be within the smaller distance but within the larger.
 * </pre>
 */
private static Query esV5DistanceQuery(Function parentFunction,
                                       LuceneQueryBuilder.Context context,
                                       String parentOperatorName,
                                       String columnName,
                                       Double distance,
                                       Point lonLat) {
    switch (parentOperatorName) {
        // We documented that using distance in the WHERE clause utilizes the index which isn't precise so treating
        // lte & lt the same should be acceptable
        case LteOperator.NAME:
        case LtOperator.NAME:
            return LatLonPoint.newDistanceQuery(columnName, lonLat.getY(), lonLat.getX(), distance);
        case GteOperator.NAME:
            if (distance - GeoUtils.TOLERANCE <= 0.0d) {
                return Queries.newMatchAllQuery();
            }
            // fall through
        case GtOperator.NAME:
            return Queries.not(LatLonPoint.newDistanceQuery(columnName, lonLat.getY(), lonLat.getX(), distance));
        case EqOperator.NAME:
            return eqDistance(parentFunction, context, columnName, distance, lonLat);
        default:
            return null;
    }
}