/** * Convert a value into meters * * @param distance distance in this unit * @return value in meters */ public double toMeters(double distance) { return convert(distance, this, DistanceUnit.METERS); }
/** * Set the precision use o make suggestions * * @param precision * precision value * @param unit * {@link DistanceUnit} to use * @return this */ public Builder precision(double precision, DistanceUnit unit) { return precision(unit.toMeters(precision)); }
/** * Set the radius of the circle * @param radius value of the circles radius * @param unit unit name of the radius value (see {@link DistanceUnit}) * @return this */ public CircleBuilder radius(double radius, String unit) { return radius(radius, DistanceUnit.fromString(unit)); }
@Override public double getDistance() { String geohash = (String)((ElasticsearchDocument)getDocument()).getSource().get(geoPointField); GeoPoint dstPoint = GeoPoint.fromGeohash(geohash); double unitDist = GeoDistance.ARC.calculate(srcPoint.getLat(), srcPoint.getLon(), dstPoint.getLat(), dstPoint.getLon(), unit); double distance; if (GEOF.UOM_METRE.equals(units)) { distance = unit.toMeters(unitDist); } else if (GEOF.UOM_DEGREE.equals(units)) { distance = unitDist / unit.getDistancePerDegree(); } else if (GEOF.UOM_RADIAN.equals(units)) { distance = DistanceUtils.dist2Radians(unit.convert(unitDist, DistanceUnit.KILOMETERS), DistanceUtils.EARTH_MEAN_RADIUS_KM); } else if (GEOF.UOM_UNITY.equals(units)) { distance = unit.convert(unitDist, DistanceUnit.KILOMETERS) / (Math.PI * DistanceUtils.EARTH_MEAN_RADIUS_KM); } else { throw new UnsupportedOperationException("Unsupported units: " + units); } return distance; } }
unit = DistanceUnit.fromString(parser.text()); } else if (currentFieldName.equals("distance_type") || currentFieldName.equals("distanceType")) { geoDistance = GeoDistance.fromString(parser.text()); if (vFrom != null) { if (vFrom instanceof Number) { from = unit.toMeters(((Number) vFrom).doubleValue()); } else { from = DistanceUnit.parse((String) vFrom, unit, DistanceUnit.DEFAULT); to = unit.toMeters(((Number) vTo).doubleValue()); } else { to = DistanceUnit.parse((String) vTo, unit, DistanceUnit.DEFAULT);
/** * Parses a given distance and converts it to this unit. * * @param distance String defining a distance (value and unit) * @param defaultUnit unit to expect if none if provided * @return parsed distance */ public double parse(String distance, DistanceUnit defaultUnit) { return parse(distance, defaultUnit, this); }
@Override public int hashCode() { return Objects.hash(center, radius, unit.ordinal()); }
/** * Convert a value to a distance string * * @param distance value to convert * @return String representation of the distance */ public String toString(double distance) { return distance + toString(); }
/** * Parses the suffix of a given distance string and return the corresponding {@link DistanceUnit} * * @param distance string representing a distance * @param defaultUnit default unit to use, if no unit is provided by the string * @return unit of the given distance */ public static DistanceUnit parseUnit(String distance, DistanceUnit defaultUnit) { for (DistanceUnit unit : values()) { for (String name : unit.names) { if(distance.endsWith(name)) { return unit; } } } return defaultUnit; }
@Override public void writeTo(StreamOutput out) throws IOException { writeCoordinateTo(center, out); out.writeDouble(radius); unit.writeTo(out); }
/** * Read from a stream. */ public CircleBuilder(StreamInput in) throws IOException { center(readFromStream(in)); radius(in.readDouble(), DistanceUnit.readFromStream(in)); }
@Override public Circle buildS4J() { return SPATIAL_CONTEXT.makeCircle(center.x, center.y, 360 * radius / unit.getEarthCircumference()); }
/** * Set the precision use o make suggestions * * @param precision * precision as distance with {@link DistanceUnit}. Default: * meters * @return this */ public Builder precision(String precision) { return precision(DistanceUnit.parse(precision, DistanceUnit.METERS, DistanceUnit.METERS)); }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null || getClass() != obj.getClass()) { return false; } CircleBuilder other = (CircleBuilder) obj; return Objects.equals(center, other.center) && Objects.equals(radius, other.radius) && Objects.equals(unit.ordinal(), other.unit.ordinal()); } }
@Override public String toString() { return unit.toString(value); }
if (from != null) { if (from instanceof Number) { fromValue = unit.toMeters(((Number) from).doubleValue()); } else { fromValue = DistanceUnit.parse((String) from, unit, DistanceUnit.DEFAULT); toValue = unit.toMeters(((Number) to).doubleValue()); } else { toValue = DistanceUnit.parse((String) to, unit, DistanceUnit.DEFAULT);
/** * Read a {@link DistanceUnit} from a {@link StreamInput}. * * @param in {@link StreamInput} to read the {@link DistanceUnit} from * @return {@link DistanceUnit} read from the {@link StreamInput} * @throws IOException if no unit can be read from the {@link StreamInput} * @throws IllegalArgumentException if no matching {@link DistanceUnit} can be found */ public static DistanceUnit readFromStream(StreamInput in) throws IOException { byte b = in.readByte(); if (b < 0 || b >= values().length) { throw new IllegalArgumentException("No type for distance unit matching [" + b + "]"); } return values()[b]; }
@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); }
/** * Read from a stream. */ public GeoDistanceAggregationBuilder(StreamInput in) throws IOException { super(in, InternalGeoDistance.FACTORY.getValueSourceType(), InternalGeoDistance.FACTORY.getValueType()); origin = new GeoPoint(in.readDouble(), in.readDouble()); int size = in.readVInt(); ranges = new ArrayList<>(size); for (int i = 0; i < size; i++) { ranges.add(new Range(in)); } keyed = in.readBoolean(); distanceType = GeoDistance.readFromStream(in); unit = DistanceUnit.readFromStream(in); }