public double[] getLats() { List<GeoPoint> points = getValues(); double[] lats = new double[points.size()]; for (int i = 0; i < points.size(); i++) { lats[i] = points.get(i).lat(); } return lats; }
public double getLat() { return getValue().lat(); }
@Override public GeoPoint get(int index) { final GeoPoint point = values[index]; return new GeoPoint(point.lat(), point.lon()); }
@Override public Object getProperty(List<String> path) { if (path.isEmpty()) { return this; } else if (path.size() == 1) { String coordinate = path.get(0); switch (coordinate) { case "value": return centroid; case "lat": return centroid.lat(); case "lon": return centroid.lon(); case "count": return count; default: throw new IllegalArgumentException("Found unknown path element [" + coordinate + "] in [" + getName() + "]"); } } else { throw new IllegalArgumentException("path not supported for [" + getName() + "]: " + path); } }
/** * Writes the given {@link GeoPoint} to the stream */ public void writeGeoPoint(GeoPoint geoPoint) throws IOException { writeDouble(geoPoint.lat()); writeDouble(geoPoint.lon()); }
public double planeDistance(double lat, double lon) { GeoPoint point = getValue(); return GeoUtils.planeDistance(point.lat(), point.lon(), lat, lon); }
public double arcDistance(double lat, double lon) { GeoPoint point = getValue(); return GeoUtils.arcDistance(point.lat(), point.lon(), lat, lon); }
/** * Normalize the geo {@code Point} for the given coordinates to lie within * their respective normalized ranges. * <p> * You can control which coordinate gets normalized with the two flags. * <p> * Note: A shift of 180° is applied in the longitude if necessary, * in order to normalize properly the latitude. * If normalizing latitude but not longitude, it is assumed that * the longitude is in the form x+k*360, with x in ]-180;180], * and k is meaningful to the application. * Therefore x will be adjusted while keeping k preserved. * * @param point The point to normalize in-place. * @param normLat Whether to normalize latitude or leave it as is. * @param normLon Whether to normalize longitude. */ public static void normalizePoint(GeoPoint point, boolean normLat, boolean normLon) { double[] pt = {point.lon(), point.lat()}; normalizePoint(pt, normLon, normLat); point.reset(pt[1], pt[0]); }
@Override public boolean advanceExact(int docId) throws IOException { if (geoPointValues.advanceExact(docId)) { int n = geoPointValues.docValueCount(); resize(n); for (int i = 0; i < n; i++) { GeoPoint other = geoPointValues.nextValue(); double distance = distFunction.calculate( origin.lat(), origin.lon(), other.lat(), other.lon(), DistanceUnit.METERS); values[i] = Math.max(0.0d, distance - offset); } sort(); return true; } else { return false; } } }), 0);
@Override public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException { GeoPoint topLeft = topLeft(); GeoPoint bottomRight = bottomRight(); if (topLeft != null) { builder.startObject(BOUNDS_FIELD.getPreferredName()); builder.startObject(TOP_LEFT_FIELD.getPreferredName()); builder.field(LAT_FIELD.getPreferredName(), topLeft.lat()); builder.field(LON_FIELD.getPreferredName(), topLeft.lon()); builder.endObject(); builder.startObject(BOTTOM_RIGHT_FIELD.getPreferredName()); builder.field(LAT_FIELD.getPreferredName(), bottomRight.lat()); builder.field(LON_FIELD.getPreferredName(), bottomRight.lon()); builder.endObject(); builder.endObject(); } return builder; }
@Override public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException { if (centroid != null) { builder.startObject(Fields.CENTROID.getPreferredName()); { builder.field(Fields.CENTROID_LAT.getPreferredName(), centroid.lat()); builder.field(Fields.CENTROID_LON.getPreferredName(), centroid.lon()); } builder.endObject(); } builder.field(Fields.COUNT.getPreferredName(), count); return builder; }
@Override public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException { if (centroid != null) { builder.startObject(Fields.CENTROID.getPreferredName()); { builder.field(Fields.CENTROID_LAT.getPreferredName(), centroid.lat()); builder.field(Fields.CENTROID_LON.getPreferredName(), centroid.lon()); } builder.endObject(); } builder.field(Fields.COUNT.getPreferredName(), count); return builder; }
public double geohashDistance(String geohash) { GeoPoint point = getValue(); return GeoUtils.arcDistance(point.lat(), point.lon(), GeoHashUtils.decodeLatitude(geohash), GeoHashUtils.decodeLongitude(geohash)); }
@Override protected void innerWriteTo(StreamOutput out) throws IOException { out.writeDouble(origin.lat()); out.writeDouble(origin.lon()); out.writeVInt(ranges.size()); for (Range range : ranges) { range.writeTo(out); } out.writeBoolean(keyed); distanceType.writeTo(out); unit.writeTo(out); }
@Override protected void doXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(NAME); builder.startObject(fieldName); builder.startArray(POINTS_FIELD.getPreferredName()); for (GeoPoint point : shell) { builder.startArray().value(point.lon()).value(point.lat()).endArray(); } builder.endArray(); builder.endObject(); builder.field(VALIDATION_METHOD.getPreferredName(), validationMethod); builder.field(IGNORE_UNMAPPED_FIELD.getPreferredName(), ignoreUnmapped); printBoostAndQueryName(builder); builder.endObject(); }
@Override public boolean advanceExact(int target) throws IOException { if (geoPointValues.advanceExact(target)) { resize(geoPointValues.docValueCount() * fromPoints.length); int v = 0; for (int i = 0; i < geoPointValues.docValueCount(); ++i) { final GeoPoint point = geoPointValues.nextValue(); for (GeoPoint from : fromPoints) { values[v] = distance.calculate(from.lat(), from.lon(), point.lat(), point.lon(), unit); v++; } } sort(); return true; } else { return false; } } };
@Override protected void doWriteTo(StreamOutput out) throws IOException { out.writeVLong(count); if (centroid != null) { out.writeBoolean(true); // should we just write lat and lon separately? out.writeLong(encodeLatLon(centroid.lat(), centroid.lon())); } else { out.writeBoolean(false); } }
@Override protected void doXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(NAME); builder.startArray(fieldName).value(center.lon()).value(center.lat()).endArray(); builder.field(DISTANCE_FIELD.getPreferredName(), distance); builder.field(DISTANCE_TYPE_FIELD.getPreferredName(), geoDistance.name().toLowerCase(Locale.ROOT)); builder.field(VALIDATION_METHOD_FIELD.getPreferredName(), validationMethod); builder.field(IGNORE_UNMAPPED_FIELD.getPreferredName(), ignoreUnmapped); printBoostAndQueryName(builder); builder.endObject(); }
@Override public void setNextDocId(int docId) throws IOException { if (in.advanceExact(docId)) { resize(in.docValueCount()); for (int i = 0; i < count; i++) { GeoPoint point = in.nextValue(); values[i] = new GeoPoint(point.lat(), point.lon()); } } else { resize(0); } }