Search in sources :

Example 71 with GeoPoint

use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.

the class GeoDistanceAggregationBuilder method parseGeoPoint.

private static GeoPoint parseGeoPoint(XContentParser parser) throws IOException {
    Token token = parser.currentToken();
    if (token == XContentParser.Token.VALUE_STRING) {
        GeoPoint point = new GeoPoint();
        point.resetFromString(parser.text());
        return point;
    }
    if (token == XContentParser.Token.START_ARRAY) {
        double lat = Double.NaN;
        double lon = Double.NaN;
        while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
            if (Double.isNaN(lon)) {
                lon = parser.doubleValue();
            } else if (Double.isNaN(lat)) {
                lat = parser.doubleValue();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "malformed [" + ORIGIN_FIELD.getPreferredName() + "]: a geo point array must be of the form [lon, lat]");
            }
        }
        return new GeoPoint(lat, lon);
    }
    if (token == XContentParser.Token.START_OBJECT) {
        String currentFieldName = null;
        double lat = Double.NaN;
        double lon = Double.NaN;
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                currentFieldName = parser.currentName();
            } else if (token == XContentParser.Token.VALUE_NUMBER) {
                if ("lat".equals(currentFieldName)) {
                    lat = parser.doubleValue();
                } else if ("lon".equals(currentFieldName)) {
                    lon = parser.doubleValue();
                }
            }
        }
        if (Double.isNaN(lat) || Double.isNaN(lon)) {
            throw new ParsingException(parser.getTokenLocation(), "malformed [" + currentFieldName + "] geo point object. either [lat] or [lon] (or both) are " + "missing");
        }
        return new GeoPoint(lat, lon);
    }
    // should not happen since we only parse geo points when we encounter a string, an object or an array
    throw new IllegalArgumentException("Unexpected token [" + token + "] while parsing geo point");
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) ParsingException(org.opensearch.common.ParsingException) Token(org.opensearch.common.xcontent.XContentParser.Token)

Example 72 with GeoPoint

use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.

the class GeoBoundsAggregator method getLeafCollector.

@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, LeafBucketCollector sub) {
    if (valuesSource == null) {
        return LeafBucketCollector.NO_OP_COLLECTOR;
    }
    final BigArrays bigArrays = context.bigArrays();
    final MultiGeoPointValues values = valuesSource.geoPointValues(ctx);
    return new LeafBucketCollectorBase(sub, values) {

        @Override
        public void collect(int doc, long bucket) throws IOException {
            if (bucket >= tops.size()) {
                long from = tops.size();
                tops = bigArrays.grow(tops, bucket + 1);
                tops.fill(from, tops.size(), Double.NEGATIVE_INFINITY);
                bottoms = bigArrays.resize(bottoms, tops.size());
                bottoms.fill(from, bottoms.size(), Double.POSITIVE_INFINITY);
                posLefts = bigArrays.resize(posLefts, tops.size());
                posLefts.fill(from, posLefts.size(), Double.POSITIVE_INFINITY);
                posRights = bigArrays.resize(posRights, tops.size());
                posRights.fill(from, posRights.size(), Double.NEGATIVE_INFINITY);
                negLefts = bigArrays.resize(negLefts, tops.size());
                negLefts.fill(from, negLefts.size(), Double.POSITIVE_INFINITY);
                negRights = bigArrays.resize(negRights, tops.size());
                negRights.fill(from, negRights.size(), Double.NEGATIVE_INFINITY);
            }
            if (values.advanceExact(doc)) {
                final int valuesCount = values.docValueCount();
                for (int i = 0; i < valuesCount; ++i) {
                    GeoPoint value = values.nextValue();
                    double top = tops.get(bucket);
                    if (value.lat() > top) {
                        top = value.lat();
                    }
                    double bottom = bottoms.get(bucket);
                    if (value.lat() < bottom) {
                        bottom = value.lat();
                    }
                    double posLeft = posLefts.get(bucket);
                    if (value.lon() >= 0 && value.lon() < posLeft) {
                        posLeft = value.lon();
                    }
                    double posRight = posRights.get(bucket);
                    if (value.lon() >= 0 && value.lon() > posRight) {
                        posRight = value.lon();
                    }
                    double negLeft = negLefts.get(bucket);
                    if (value.lon() < 0 && value.lon() < negLeft) {
                        negLeft = value.lon();
                    }
                    double negRight = negRights.get(bucket);
                    if (value.lon() < 0 && value.lon() > negRight) {
                        negRight = value.lon();
                    }
                    tops.set(bucket, top);
                    bottoms.set(bucket, bottom);
                    posLefts.set(bucket, posLeft);
                    posRights.set(bucket, posRight);
                    negLefts.set(bucket, negLeft);
                    negRights.set(bucket, negRight);
                }
            }
        }
    };
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) BigArrays(org.opensearch.common.util.BigArrays) LeafBucketCollectorBase(org.opensearch.search.aggregations.LeafBucketCollectorBase) MultiGeoPointValues(org.opensearch.index.fielddata.MultiGeoPointValues) GeoPoint(org.opensearch.common.geo.GeoPoint)

Example 73 with GeoPoint

use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.

the class GeoCentroidAggregator method buildAggregation.

@Override
public InternalAggregation buildAggregation(long bucket) {
    if (valuesSource == null || bucket >= counts.size()) {
        return buildEmptyAggregation();
    }
    final long bucketCount = counts.get(bucket);
    final GeoPoint bucketCentroid = (bucketCount > 0) ? new GeoPoint(latSum.get(bucket) / bucketCount, lonSum.get(bucket) / bucketCount) : null;
    return new InternalGeoCentroid(name, bucketCentroid, bucketCount, metadata());
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint)

Example 74 with GeoPoint

use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.

the class QueryDSLDocumentationTests method testGeoPolygon.

public void testGeoPolygon() {
    // tag::geo_polygon
    // <1>
    List<GeoPoint> points = new ArrayList<>();
    points.add(new GeoPoint(40, -70));
    points.add(new GeoPoint(30, -80));
    points.add(new GeoPoint(20, -90));
    // <2>
    geoPolygonQuery("pin.location", points);
// end::geo_polygon
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) ArrayList(java.util.ArrayList)

Example 75 with GeoPoint

use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.

the class GeoHashTests method testGeohashAsLongRoutines.

public void testGeohashAsLongRoutines() {
    final GeoPoint expected = new GeoPoint();
    final GeoPoint actual = new GeoPoint();
    // String based counterpart
    for (double lat = -90; lat < 90; lat++) {
        for (double lng = -180; lng < 180; lng++) {
            for (int p = 1; p <= 12; p++) {
                long geoAsLong = Geohash.longEncode(lng, lat, p);
                // string encode from geohashlong encoded location
                String geohashFromLong = Geohash.stringEncode(geoAsLong);
                // string encode from full res lat lon
                String geohash = Geohash.stringEncode(lng, lat, p);
                // ensure both strings are the same
                assertEquals(geohash, geohashFromLong);
                // decode from the full-res geohash string
                expected.resetFromGeoHash(geohash);
                // decode from the geohash encoded long
                actual.resetFromGeoHash(geoAsLong);
                assertEquals(expected, actual);
            }
        }
    }
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) GeoPoint(org.opensearch.common.geo.GeoPoint)

Aggregations

GeoPoint (org.opensearch.common.geo.GeoPoint)150 SearchResponse (org.opensearch.action.search.SearchResponse)41 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)27 ArrayList (java.util.ArrayList)23 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)19 Matchers.containsString (org.hamcrest.Matchers.containsString)11 XContentParser (org.opensearch.common.xcontent.XContentParser)10 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)10 LatLonPoint (org.apache.lucene.document.LatLonPoint)8 Version (org.opensearch.Version)8 IOException (java.io.IOException)7 LatLonDocValuesField (org.apache.lucene.document.LatLonDocValuesField)7 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)7 OpenSearchParseException (org.opensearch.OpenSearchParseException)7 Range (org.opensearch.search.aggregations.bucket.range.Range)6 Bucket (org.opensearch.search.aggregations.bucket.range.Range.Bucket)6 Map (java.util.Map)5 Document (org.apache.lucene.document.Document)5 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)5 Query (org.apache.lucene.search.Query)5