Search in sources :

Example 61 with GeoPoint

use of org.elasticsearch.common.geo.GeoPoint in project geocluster-facet by zenobase.

the class GeoCluster method readFrom.

public static GeoCluster readFrom(StreamInput in) throws IOException {
    int size = in.readVInt();
    GeoPoint center = GeoPoints.readFrom(in);
    GeoBoundingBox bounds = size > 1 ? GeoBoundingBox.readFrom(in) : new GeoBoundingBox(center, center);
    return new GeoCluster(size, center, bounds);
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) GeoPoint(org.elasticsearch.common.geo.GeoPoint)

Example 62 with GeoPoint

use of org.elasticsearch.common.geo.GeoPoint in project geocluster-facet by zenobase.

the class GeoClusterReducerTests method testReduceAll.

@Test
public void testReduceAll() {
    GeoClusterReducer reducer = new GeoClusterReducer(1.0);
    List<GeoCluster> clusters = Lists.newArrayList();
    assertThat(reducer.reduce(clusters).size(), equalTo(0));
    clusters.add(new GeoCluster(DENVER));
    assertThat("Cluster after adding Denver", reducer.reduce(clusters), hasItems(new GeoCluster(1, DENVER, new GeoBoundingBox(DENVER))));
    clusters.add(new GeoCluster(DENVER));
    assertThat("Cluster after adding Denver again", reducer.reduce(clusters), hasItems(new GeoCluster(2, DENVER, new GeoBoundingBox(DENVER))));
    clusters.add(new GeoCluster(SAN_DIEGO));
    assertThat("Cluster after adding San Diego", reducer.reduce(clusters), hasItems(new GeoCluster(3, new GeoPoint(37.4400, -108.9567), new GeoBoundingBox(DENVER).extend(SAN_DIEGO))));
    clusters.add(new GeoCluster(LAS_VEGAS));
    assertThat("Cluster after adding Las Vegas", reducer.reduce(clusters), hasItems(new GeoCluster(4, new GeoPoint(37.1000, -110.5100), new GeoBoundingBox(DENVER).extend(SAN_DIEGO).extend(LAS_VEGAS))));
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) Test(org.testng.annotations.Test)

Example 63 with GeoPoint

use of org.elasticsearch.common.geo.GeoPoint in project geocluster-facet by zenobase.

the class GeoPointsTests method testReadFromWriteTo.

@Test
public void testReadFromWriteTo() throws IOException {
    BytesStreamOutput out = new BytesStreamOutput();
    GeoPoints.writeTo(LAS_VEGAS, out);
    BytesStreamInput in = new BytesStreamInput(out.bytes());
    GeoPoint point = GeoPoints.readFrom(in);
    assertThat("Latitude", point.lat(), equalTo(LAS_VEGAS.lat()));
    assertThat("Longitude", point.lon(), equalTo(LAS_VEGAS.lon()));
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) BytesStreamInput(org.elasticsearch.common.io.stream.BytesStreamInput) Test(org.testng.annotations.Test)

Example 64 with GeoPoint

use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.

the class GeoDistanceAggregationBuilder method parseGeoPoint.

private static GeoPoint parseGeoPoint(XContentParser parser, QueryParseContext context) 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.elasticsearch.common.geo.GeoPoint) ParsingException(org.elasticsearch.common.ParsingException) Token(org.elasticsearch.common.xcontent.XContentParser.Token)

Example 65 with GeoPoint

use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.

the class InternalGeoBounds method doXContentBody.

@Override
public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
    GeoPoint topLeft = topLeft();
    GeoPoint bottomRight = bottomRight();
    if (topLeft != null) {
        builder.startObject("bounds");
        builder.startObject("top_left");
        builder.field("lat", topLeft.lat());
        builder.field("lon", topLeft.lon());
        builder.endObject();
        builder.startObject("bottom_right");
        builder.field("lat", bottomRight.lat());
        builder.field("lon", bottomRight.lon());
        builder.endObject();
        builder.endObject();
    }
    return builder;
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint)

Aggregations

GeoPoint (org.elasticsearch.common.geo.GeoPoint)122 SearchResponse (org.elasticsearch.action.search.SearchResponse)40 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)27 ArrayList (java.util.ArrayList)20 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)15 XContentParser (org.elasticsearch.common.xcontent.XContentParser)9 GeoBounds (org.elasticsearch.search.aggregations.metrics.geobounds.GeoBounds)9 Range (org.elasticsearch.search.aggregations.bucket.range.Range)8 GeoCentroid (org.elasticsearch.search.aggregations.metrics.geocentroid.GeoCentroid)8 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)7 Version (org.elasticsearch.Version)7 Bucket (org.elasticsearch.search.aggregations.bucket.range.Range.Bucket)7 Settings (org.elasticsearch.common.settings.Settings)6 MultiGeoPointValues (org.elasticsearch.index.fielddata.MultiGeoPointValues)6 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)5 ParsingException (org.elasticsearch.common.ParsingException)5 SearchHit (org.elasticsearch.search.SearchHit)5 Test (org.testng.annotations.Test)5 HashSet (java.util.HashSet)4 DistanceUnit (org.elasticsearch.common.unit.DistanceUnit)4