GeoBoxMaker implements BoxMaker { @Override public <N extends Number & Comparable<N>> Box<N> make(Point<N> center, N radius) { double radDist = radiansFromUnits(radius.doubleValue()); double radLat = degreesToRadians(center.getX().doubleValue()); double radLon = degreesToRadians(center.getY().doubleValue()); double minLat = radLat - radDist; double maxLat = radLat + radDist; double minLon, maxLon; if (minLat > MIN_LAT && maxLat < MAX_LAT) { double deltaLon = Math.asin(Math.sin(radDist) / Math.cos(radLat)); minLon = radLon - deltaLon; maxLon = radLon + deltaLon; if (minLon < MIN_LON) { minLon += TWO_PI; } if (maxLon > MAX_LON) { maxLon -= TWO_PI; } } else { minLat = Math.max(minLat, MIN_LAT); maxLat = Math.min(maxLat, MAX_LAT); minLon = MIN_LON; maxLon = MAX_LON; } N zero = subtract(radius, radius); N minLatN = add(zero, radiansToDegrees(minLat)); N maxLatN = add(zero, radiansToDegrees(maxLat)); N minLonN = add(zero, radiansToDegrees(minLon)); N maxLonN = add(zero, radiansToDegrees(maxLon)); return new Box<N>( new Point<N>(minLatN,minLonN), new Point<N>(maxLatN,maxLonN)); } GeoBoxMaker(DistanceUnits units); @Override // http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates Box<N> make(Point<N> center,
            N radius); static double degreesToRadians(double degrees); static double radiansToDegrees(double radians); static double kilometersToRadians(double kilometers); static double milesToRadians(double miles);  }
@Test public void make_Degrees_equator() { GeoBoxMaker b = new GeoBoxMaker(DistanceUnits.DEGREES); Point<Double> p = Point.create(0D,0D); Box<Double> box = b.make(p, 1D); assertEquals(Box.create(-1D, -1D, 1D, 1D), box); }
@Test public void make_Degrees_here() { GeoBoxMaker b = new GeoBoxMaker(DistanceUnits.DEGREES); Point<Double> p = new Point<Double>(34.02886,-118.473993); Box<Double> box = b.make(p, 1.0D); assertEquals(Box.create(33.02886, -119.68064899173066, 35.02886, -117.26733700826932), box); }
@Test public void make_Kilometers_equator() { GeoBoxMaker b = new GeoBoxMaker(DistanceUnits.KILOMETERS); Point<Double> p = Point.create(0D,0D); Box<Double> box = b.make(p, 111D); Double almostOneDegree = 0.9982469825697909; assertEquals(Box.create(-almostOneDegree, -almostOneDegree, almostOneDegree, almostOneDegree), box); }
@Test public void make_Miles_equator() { GeoBoxMaker b = new GeoBoxMaker(DistanceUnits.MILES); Point<Double> p = Point.create(0D,0D); Box<Double> box = b.make(p, 69D); Double almostOneDegree = 0.998587720738237; assertEquals(Box.create(-almostOneDegree, -almostOneDegree, almostOneDegree, almostOneDegree), box); }