Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions core/src/main/java/org/elasticsearch/common/Numbers.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,12 @@ public static byte[] doubleToBytes(double val) {
return longToBytes(Double.doubleToRawLongBits(val));
}

/** Returns true if value is neither NaN nor infinite. */
public static boolean isValidDouble(double value) {
if (Double.isNaN(value) || Double.isInfinite(value)) {
return false;
}
return true;
}

}
46 changes: 34 additions & 12 deletions core/src/main/java/org/elasticsearch/common/geo/GeoDistance.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,27 @@

import org.apache.lucene.util.Bits;
import org.apache.lucene.util.SloppyMath;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.index.fielddata.FieldData;
import org.elasticsearch.index.fielddata.GeoPointValues;
import org.elasticsearch.index.fielddata.MultiGeoPointValues;
import org.elasticsearch.index.fielddata.NumericDoubleValues;
import org.elasticsearch.index.fielddata.SortedNumericDoubleValues;
import org.elasticsearch.index.fielddata.SortingNumericDoubleValues;

import java.io.IOException;
import java.util.Locale;

/**
* Geo distance calculation.
*/
public enum GeoDistance {
public enum GeoDistance implements Writeable<GeoDistance> {
/**
* Calculates distance as points on a plane. Faster, but less accurate than {@link #ARC}.
*/
PLANE() {
PLANE {
@Override
public double calculate(double sourceLatitude, double sourceLongitude, double targetLatitude, double targetLongitude, DistanceUnit unit) {
double px = targetLongitude - sourceLongitude;
Expand All @@ -60,7 +63,7 @@ public FixedSourceDistance fixedSourceDistance(double sourceLatitude, double sou
/**
* Calculates distance factor.
*/
FACTOR() {
FACTOR {
@Override
public double calculate(double sourceLatitude, double sourceLongitude, double targetLatitude, double targetLongitude, DistanceUnit unit) {
double longitudeDifference = targetLongitude - sourceLongitude;
Expand All @@ -82,7 +85,7 @@ public FixedSourceDistance fixedSourceDistance(double sourceLatitude, double sou
/**
* Calculates distance as points on a globe.
*/
ARC() {
ARC {
@Override
public double calculate(double sourceLatitude, double sourceLongitude, double targetLatitude, double targetLongitude, DistanceUnit unit) {
double x1 = sourceLatitude * Math.PI / 180D;
Expand All @@ -109,7 +112,7 @@ public FixedSourceDistance fixedSourceDistance(double sourceLatitude, double sou
* Calculates distance as points on a globe in a sloppy way. Close to the pole areas the accuracy
* of this function decreases.
*/
SLOPPY_ARC() {
SLOPPY_ARC {

@Override
public double normalize(double distance, DistanceUnit unit) {
Expand All @@ -127,12 +130,31 @@ public FixedSourceDistance fixedSourceDistance(double sourceLatitude, double sou
}
};

/** Returns a GeoDistance object as read from the StreamInput. */
@Override
public GeoDistance readFrom(StreamInput in) throws IOException {
int ord = in.readVInt();
if (ord < 0 || ord >= values().length) {
throw new IOException("Unknown GeoDistance ordinal [" + ord + "]");
}
return GeoDistance.values()[ord];
}

public static GeoDistance readGeoDistanceFrom(StreamInput in) throws IOException {
return DEFAULT.readFrom(in);
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(this.ordinal());
}

/**
* Default {@link GeoDistance} function. This method should be used, If no specific function has been selected.
* This is an alias for <code>SLOPPY_ARC</code>
*/
public static final GeoDistance DEFAULT = SLOPPY_ARC;
public static final GeoDistance DEFAULT = SLOPPY_ARC;

public abstract double normalize(double distance, DistanceUnit unit);

public abstract double calculate(double sourceLatitude, double sourceLongitude, double targetLatitude, double targetLongitude, DistanceUnit unit);
Expand Down Expand Up @@ -180,14 +202,14 @@ public static DistanceBoundingCheck distanceBoundingCheck(double sourceLatitude,

/**
* Get a {@link GeoDistance} according to a given name. Valid values are
*
*
* <ul>
* <li><b>plane</b> for <code>GeoDistance.PLANE</code></li>
* <li><b>sloppy_arc</b> for <code>GeoDistance.SLOPPY_ARC</code></li>
* <li><b>factor</b> for <code>GeoDistance.FACTOR</code></li>
* <li><b>arc</b> for <code>GeoDistance.ARC</code></li>
* </ul>
*
*
* @param name name of the {@link GeoDistance}
* @return a {@link GeoDistance}
*/
Expand Down Expand Up @@ -336,7 +358,7 @@ public double calculate(double targetLatitude, double targetLongitude) {

/**
* Basic implementation of {@link FixedSourceDistance}. This class keeps the basic parameters for a distance
* functions based on a fixed source. Namely latitude, longitude and unit.
* functions based on a fixed source. Namely latitude, longitude and unit.
*/
public static abstract class FixedSourceDistanceBase implements FixedSourceDistance {
protected final double sourceLatitude;
Expand All @@ -349,7 +371,7 @@ public FixedSourceDistanceBase(double sourceLatitude, double sourceLongitude, Di
this.unit = unit;
}
}

public static class ArcFixedSourceDistance extends FixedSourceDistanceBase {

public ArcFixedSourceDistance(double sourceLatitude, double sourceLongitude, DistanceUnit unit) {
Expand Down
32 changes: 28 additions & 4 deletions core/src/main/java/org/elasticsearch/common/geo/GeoPoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

package org.elasticsearch.common.geo;

import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import java.io.IOException;


import org.apache.lucene.util.BitUtil;
import org.apache.lucene.util.XGeoHashUtils;
Expand All @@ -27,11 +32,14 @@
/**
*
*/
public final class GeoPoint {
public final class GeoPoint implements Writeable<GeoPoint> {

private double lat;
private double lon;
private final static double TOLERANCE = XGeoUtils.TOLERANCE;

// for serialization purposes
private static final GeoPoint PROTOTYPE = new GeoPoint(Double.NaN, Double.NaN);

public GeoPoint() {
}
Expand Down Expand Up @@ -152,8 +160,7 @@ public String toString() {
}

public static GeoPoint parseFromLatLon(String latLon) {
GeoPoint point = new GeoPoint();
point.resetFromString(latLon);
GeoPoint point = new GeoPoint(latLon);
return point;
}

Expand All @@ -168,4 +175,21 @@ public static GeoPoint fromGeohash(long geohashLong) {
public static GeoPoint fromIndexLong(long indexLong) {
return new GeoPoint().resetFromIndexHash(indexLong);
}
}

@Override
public GeoPoint readFrom(StreamInput in) throws IOException {
double lat = in.readDouble();
double lon = in.readDouble();
return new GeoPoint(lat, lon);
}

public static GeoPoint readGeoPointFrom(StreamInput in) throws IOException {
return PROTOTYPE.readFrom(in);
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeDouble(lat);
out.writeDouble(lon);
}
}
27 changes: 26 additions & 1 deletion core/src/main/java/org/elasticsearch/common/geo/GeoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,19 @@
*/
public class GeoUtils {

/** Maximum valid latitude in degrees. */
public static final double MAX_LAT = 90.0;
/** Minimum valid latitude in degrees. */
public static final double MIN_LAT = -90.0;
/** Maximum valid longitude in degrees. */
public static final double MAX_LON = 180.0;
/** Minimum valid longitude in degrees. */
public static final double MIN_LON = -180.0;

public static final String LATITUDE = GeoPointFieldMapper.Names.LAT;
public static final String LONGITUDE = GeoPointFieldMapper.Names.LON;
public static final String GEOHASH = GeoPointFieldMapper.Names.GEOHASH;

/** Earth ellipsoid major axis defined by WGS 84 in meters */
public static final double EARTH_SEMI_MAJOR_AXIS = 6378137.0; // meters (WGS 84)

Expand All @@ -56,6 +65,22 @@ public class GeoUtils {
/** Earth ellipsoid polar distance in meters */
public static final double EARTH_POLAR_DISTANCE = Math.PI * EARTH_SEMI_MINOR_AXIS;

/** Returns true if latitude is actually a valid latitude value.*/
public static boolean isValidLatitude(double latitude) {
if (Double.isNaN(latitude) || Double.isInfinite(latitude) || latitude < GeoUtils.MIN_LAT || latitude > GeoUtils.MAX_LAT) {
return false;
}
return true;
}

/** Returns true if longitude is actually a valid longitude value. */
public static boolean isValidLongitude(double longitude) {
if (Double.isNaN(longitude) || Double.isNaN(longitude) || longitude < GeoUtils.MIN_LON || longitude > GeoUtils.MAX_LON) {
return false;
}
return true;
}

/**
* Return an approximate value of the diameter of the earth (in meters) at the given latitude (in radians).
*/
Expand Down
Loading