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);
}
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))));
}
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()));
}
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");
}
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;
}
Aggregations