Example usage for org.apache.lucene.geo GeoUtils checkLatitude

List of usage examples for org.apache.lucene.geo GeoUtils checkLatitude

Introduction

In this page you can find the example usage for org.apache.lucene.geo GeoUtils checkLatitude.

Prototype

public static void checkLatitude(double latitude) 

Source Link

Document

validates latitude value is within standard +/-90 coordinate bounds

Usage

From source file:perf.IndexAndSearchOpenStreetMaps.java

License:Apache License

/** Makes an n-gon, centered at the provided lat/lon, and each vertex approximately
 *  distanceMeters away from the center.
 *
 * Do not invoke me across the dateline or a pole!! */
private static double[][] makeRegularPoly(double centerLat, double centerLon, double radiusMeters, int gons) {

    // System.out.println("MAKE POLY: centerLat=" + centerLat + " centerLon=" + centerLon + " radiusMeters=" + radiusMeters + " gons=" + gons);

    double[][] result = new double[2][];
    result[0] = new double[gons + 1];
    result[1] = new double[gons + 1];
    //System.out.println("make gon=" + gons);
    for (int i = 0; i < gons; i++) {
        double angle = 360.0 - i * (360.0 / gons);
        //System.out.println("  angle " + angle);
        double x = Math.cos(Math.toRadians(angle));
        double y = Math.sin(Math.toRadians(angle));
        double factor = 2.0;
        double step = 1.0;
        int last = 0;

        //System.out.println("angle " + angle + " slope=" + slope);
        // Iterate out along one spoke until we hone in on the point that's nearly exactly radiusMeters from the center:
        while (true) {
            double lat = centerLat + y * factor;
            GeoUtils.checkLatitude(lat);
            double lon = centerLon + x * factor;
            GeoUtils.checkLongitude(lon);
            double distanceMeters = SloppyMath.haversinMeters(centerLat, centerLon, lat, lon);

            //System.out.println("  iter lat=" + lat + " lon=" + lon + " distance=" + distanceMeters + " vs " + radiusMeters);
            if (Math.abs(distanceMeters - radiusMeters) < 0.1) {
                // Within 10 cm: close enough!
                result[0][i] = lat;//from w  w  w .  j  a va  2s.  c o  m
                result[1][i] = lon;
                break;
            }

            if (distanceMeters > radiusMeters) {
                // too big
                //System.out.println("    smaller");
                factor -= step;
                if (last == 1) {
                    //System.out.println("      half-step");
                    step /= 2.0;
                }
                last = -1;
            } else if (distanceMeters < radiusMeters) {
                // too small
                //System.out.println("    bigger");
                factor += step;
                if (last == -1) {
                    //System.out.println("      half-step");
                    step /= 2.0;
                }
                last = 1;
            }
        }
    }

    // close poly
    result[0][gons] = result[0][0];
    result[1][gons] = result[1][0];

    //System.out.println("  polyLats=" + Arrays.toString(result[0]));
    //System.out.println("  polyLons=" + Arrays.toString(result[1]));

    return result;
}