Example usage for com.google.common.geometry S2LatLng getEarthDistance

List of usage examples for com.google.common.geometry S2LatLng getEarthDistance

Introduction

In this page you can find the example usage for com.google.common.geometry S2LatLng getEarthDistance.

Prototype

public double getEarthDistance(final S2LatLng o) 

Source Link

Document

Returns the surface distance to the given point assuming the default Earth radius of #EARTH_RADIUS_METERS .

Usage

From source file:com.grublr.geo.s2.internal.S2Util.java

/**
 * An utility method to get a bounding box of latitude and longitude from a given GeoQueryRequest.
 * /*ww w. j a  v a 2  s  .  c om*/
 * @param geoQueryRequest
 *            It contains all of the necessary information to form a latitude and longitude box.
 * 
 * */
public static S2LatLngRect getBoundingLatLngRect(GeoQueryRequest geoQueryRequest) {
    if (geoQueryRequest instanceof QueryRectangleRequest) {
        QueryRectangleRequest queryRectangleRequest = (QueryRectangleRequest) geoQueryRequest;

        GeoPoint minPoint = queryRectangleRequest.getMinPoint();
        GeoPoint maxPoint = queryRectangleRequest.getMaxPoint();

        S2LatLngRect latLngRect = null;

        if (minPoint != null && maxPoint != null) {
            S2LatLng minLatLng = S2LatLng.fromDegrees(minPoint.getLatitude(), minPoint.getLongitude());
            S2LatLng maxLatLng = S2LatLng.fromDegrees(maxPoint.getLatitude(), maxPoint.getLongitude());

            latLngRect = new S2LatLngRect(minLatLng, maxLatLng);
        }

        return latLngRect;
    } else if (geoQueryRequest instanceof QueryRadiusRequest) {
        QueryRadiusRequest queryRadiusRequest = (QueryRadiusRequest) geoQueryRequest;

        GeoPoint centerPoint = queryRadiusRequest.getCenterPoint();
        double radiusInMeter = queryRadiusRequest.getRadiusInMeter();

        S2LatLng centerLatLng = S2LatLng.fromDegrees(centerPoint.getLatitude(), centerPoint.getLongitude());

        double latReferenceUnit = centerPoint.getLatitude() > 0.0 ? -1.0 : 1.0;
        S2LatLng latReferenceLatLng = S2LatLng.fromDegrees(centerPoint.getLatitude() + latReferenceUnit,
                centerPoint.getLongitude());
        double lngReferenceUnit = centerPoint.getLongitude() > 0.0 ? -1.0 : 1.0;
        S2LatLng lngReferenceLatLng = S2LatLng.fromDegrees(centerPoint.getLatitude(),
                centerPoint.getLongitude() + lngReferenceUnit);

        double latForRadius = radiusInMeter / centerLatLng.getEarthDistance(latReferenceLatLng);
        double lngForRadius = radiusInMeter / centerLatLng.getEarthDistance(lngReferenceLatLng);

        S2LatLng minLatLng = S2LatLng.fromDegrees(centerPoint.getLatitude() - latForRadius,
                centerPoint.getLongitude() - lngForRadius);
        S2LatLng maxLatLng = S2LatLng.fromDegrees(centerPoint.getLatitude() + latForRadius,
                centerPoint.getLongitude() + lngForRadius);

        return new S2LatLngRect(minLatLng, maxLatLng);
    }

    return null;
}

From source file:com.grublr.geo.GeoDataManager.java

/**
 * Filter out any points outside of the queried area from the input list.
 * /*from  w w  w  . ja  v  a  2  s.c  om*/
 * @param list
 *            List of items return by Amazon DynamoDB. It may contains points outside of the actual area queried.
 * 
 * @param latLngRect
 *            Queried area. Any points outside of this area need to be discarded.
 * 
 * @return List of items within the queried area.
 */
private List<Map<String, AttributeValue>> filter(List<Map<String, AttributeValue>> list,
        GeoQueryRequest geoQueryRequest) {

    List<Map<String, AttributeValue>> result = new ArrayList<Map<String, AttributeValue>>();

    S2LatLngRect latLngRect = null;
    S2LatLng centerLatLng = null;
    double radiusInMeter = 0;
    if (geoQueryRequest instanceof QueryRectangleRequest) {
        latLngRect = S2Util.getBoundingLatLngRect(geoQueryRequest);
    } else if (geoQueryRequest instanceof QueryRadiusRequest) {
        GeoPoint centerPoint = ((QueryRadiusRequest) geoQueryRequest).getCenterPoint();
        centerLatLng = S2LatLng.fromDegrees(centerPoint.getLatitude(), centerPoint.getLongitude());

        radiusInMeter = ((QueryRadiusRequest) geoQueryRequest).getRadiusInMeter();
    }

    for (Map<String, AttributeValue> item : list) {
        String geoJson = item.get(config.getGeoJsonAttributeName()).getS();
        GeoPoint geoPoint = GeoJsonMapper.geoPointFromString(geoJson);

        S2LatLng latLng = S2LatLng.fromDegrees(geoPoint.getLatitude(), geoPoint.getLongitude());
        if (latLngRect != null && latLngRect.contains(latLng)) {
            result.add(item);
        } else if (centerLatLng != null && radiusInMeter > 0
                && centerLatLng.getEarthDistance(latLng) <= radiusInMeter) {
            result.add(item);
        }
    }

    return result;
}