@Test public void zoomForBoundsTest() { Dimension[] dimensions = { new Dimension(200, 300), new Dimension(500, 400), new Dimension(1000, 600), new Dimension(3280, 1780), new Dimension(100, 200), new Dimension(500, 200) }; BoundingBox[] boundingBoxes = { new BoundingBox(12.2, 0, 34.3, 120), new BoundingBox(-30, 20, 30, 30), new BoundingBox(20.3, 100, 30.4, 120), new BoundingBox(4.4, 2, 4.5, 2.2), new BoundingBox(50.43, 12.23, 50.44, 12.24), new BoundingBox(50.43, 12, 50.44, 40) }; int[] tileSizes = { 256, 512, 500, 620, 451 }; byte[] results = { 2, 1, 1, 1, 1, 3, 2, 2, 2, 2, 4, 4, 4, 4, 4, 10, 10, 10, 10, 10, 14, 13, 14, 13, 14, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 3, 3, 3, 2, 3, 5, 4, 4, 4, 4, 11, 10, 11, 10, 11, 14, 14, 14, 14, 14, 5, 4, 4, 4, 4, 5, 3, 3, 3, 3, 4, 3, 3, 3, 3, 6, 5, 5, 5, 5, 13, 11, 12, 11, 12, 15, 14, 14, 14, 14, 7, 5, 5, 5, 5, 10, 7, 7, 6, 7, 9, 5, 5, 5, 6, 11, 8, 8, 7, 8, 17, 14, 14, 13, 14, 20, 16, 16, 16, 17, 16, 9, 9, 8, 10, 1, 1, 1, 1, 1, 3, 2, 2, 2, 2, 4, 4, 4, 4, 4, 10, 10, 10, 10, 10, 14, 13, 13, 13, 13, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 3, 2, 2, 2, 2, 5, 4, 4, 4, 4, 11, 10, 11, 10, 11, 14, 13, 13, 13, 13, 5, 4, 4, 4, 4 }; int i = 0; for (Dimension dimension : dimensions) { for (BoundingBox boundingBox : boundingBoxes) { for (int tileSize : tileSizes) { Assert.assertEquals(results[i], LatLongUtils.zoomForBounds(dimension, boundingBox, tileSize)); ++i; } } } }
public static byte zoomForBounds(Dimension dimension, BoundingBox boundingBox, int tileSize) { double dxMax = MercatorProjection.longitudeToPixelX(boundingBox.maxLongitude, (byte) 0, tileSize) / tileSize; double dxMin = MercatorProjection.longitudeToPixelX(boundingBox.minLongitude, (byte) 0, tileSize) / tileSize; double zoomX = Math.floor(-Math.log(3.8) * Math.log(Math.abs(dxMax - dxMin)) + (float) dimension.width / tileSize); double dyMax = MercatorProjection.latitudeToPixelY(boundingBox.maxLatitude, (byte) 0, tileSize) / tileSize; double dyMin = MercatorProjection.latitudeToPixelY(boundingBox.minLatitude, (byte) 0, tileSize) / tileSize; double zoomY = Math.floor(-Math.log(3.8) * Math.log(Math.abs(dyMax - dyMin)) + (float) dimension.height / tileSize); return (byte) Math.min(zoomX, zoomY); }
LatLongUtils { public static byte zoomForBounds(Dimension dimension, BoundingBox boundingBox, int tileSize) { double dxMax = MercatorProjection.longitudeToPixelX(boundingBox.maxLongitude, (byte) 0, tileSize) / tileSize; double dxMin = MercatorProjection.longitudeToPixelX(boundingBox.minLongitude, (byte) 0, tileSize) / tileSize; double zoomX = Math.floor(-Math.log(3.8) * Math.log(Math.abs(dxMax - dxMin)) + (float) dimension.width / tileSize); double dyMax = MercatorProjection.latitudeToPixelY(boundingBox.maxLatitude, (byte) 0, tileSize) / tileSize; double dyMin = MercatorProjection.latitudeToPixelY(boundingBox.minLatitude, (byte) 0, tileSize) / tileSize; double zoomY = Math.floor(-Math.log(3.8) * Math.log(Math.abs(dyMax - dyMin)) + (float) dimension.height / tileSize); return (byte) Math.min(zoomX, zoomY); } }
LatLongUtils { public static byte zoomForBounds(Dimension dimension, BoundingBox boundingBox, int tileSize) { double dxMax = MercatorProjection.longitudeToPixelX(boundingBox.maxLongitude, (byte) 0, tileSize) / tileSize; double dxMin = MercatorProjection.longitudeToPixelX(boundingBox.minLongitude, (byte) 0, tileSize) / tileSize; double zoomX = Math.floor(-Math.log(3.8) * Math.log(Math.abs(dxMax - dxMin)) + (float) dimension.width / tileSize); double dyMax = MercatorProjection.latitudeToPixelY(boundingBox.maxLatitude, (byte) 0, tileSize) / tileSize; double dyMin = MercatorProjection.latitudeToPixelY(boundingBox.minLatitude, (byte) 0, tileSize) / tileSize; double zoomY = Math.floor(-Math.log(3.8) * Math.log(Math.abs(dyMax - dyMin)) + (float) dimension.height / tileSize); return (byte) Math.min(zoomX, zoomY); } private  LatLongUtils(); }
LatLongUtils { public static byte zoomForBounds(Dimension dimension, BoundingBox boundingBox, int tileSize) { double dxMax = MercatorProjection.longitudeToPixelX(boundingBox.maxLongitude, (byte) 0, tileSize) / tileSize; double dxMin = MercatorProjection.longitudeToPixelX(boundingBox.minLongitude, (byte) 0, tileSize) / tileSize; double zoomX = Math.floor(-Math.log(3.8) * Math.log(Math.abs(dxMax - dxMin)) + (float) dimension.width / tileSize); double dyMax = MercatorProjection.latitudeToPixelY(boundingBox.maxLatitude, (byte) 0, tileSize) / tileSize; double dyMin = MercatorProjection.latitudeToPixelY(boundingBox.minLatitude, (byte) 0, tileSize) / tileSize; double zoomY = Math.floor(-Math.log(3.8) * Math.log(Math.abs(dyMax - dyMin)) + (float) dimension.height / tileSize); return (byte) Math.min(zoomX, zoomY); } private  LatLongUtils(); static int degreesToMicrodegrees(double coordinate); static LatLong fromString(String latLongString); static double latitudeDistance(int meters); static double longitudeDistance(int meters, double latitude); static double microdegreesToDegrees(int coordinate); static double[] parseCoordinateString(String coordinatesString, int numberOfCoordinates); static void validateLatitude(double latitude); static void validateLongitude(double longitude); static byte zoomForBounds(Dimension dimension, BoundingBox boundingBox, int tileSize); }
LatLongUtils { public static byte zoomForBounds(Dimension dimension, BoundingBox boundingBox, int tileSize) { double dxMax = MercatorProjection.longitudeToPixelX(boundingBox.maxLongitude, (byte) 0, tileSize) / tileSize; double dxMin = MercatorProjection.longitudeToPixelX(boundingBox.minLongitude, (byte) 0, tileSize) / tileSize; double zoomX = Math.floor(-Math.log(3.8) * Math.log(Math.abs(dxMax - dxMin)) + (float) dimension.width / tileSize); double dyMax = MercatorProjection.latitudeToPixelY(boundingBox.maxLatitude, (byte) 0, tileSize) / tileSize; double dyMin = MercatorProjection.latitudeToPixelY(boundingBox.minLatitude, (byte) 0, tileSize) / tileSize; double zoomY = Math.floor(-Math.log(3.8) * Math.log(Math.abs(dyMax - dyMin)) + (float) dimension.height / tileSize); return (byte) Math.min(zoomX, zoomY); } private  LatLongUtils(); static int degreesToMicrodegrees(double coordinate); static LatLong fromString(String latLongString); static double latitudeDistance(int meters); static double longitudeDistance(int meters, double latitude); static double microdegreesToDegrees(int coordinate); static double[] parseCoordinateString(String coordinatesString, int numberOfCoordinates); static void validateLatitude(double latitude); static void validateLongitude(double longitude); static byte zoomForBounds(Dimension dimension, BoundingBox boundingBox, int tileSize); static final double EQUATORIAL_RADIUS; static final double LATITUDE_MAX; static final double LATITUDE_MIN; static final double LONGITUDE_MAX; static final double LONGITUDE_MIN; }