@Test public void pixelYToLatitudeTest() { for (int tileSize : TILE_SIZES) { for (byte zoomLevel = ZOOM_LEVEL_MIN; zoomLevel <= ZOOM_LEVEL_MAX; ++zoomLevel) { double latitude = MercatorProjection.pixelYToLatitude(0, zoomLevel, tileSize); Assert.assertEquals(MercatorProjection.LATITUDE_MAX, latitude, 0); latitude = MercatorProjection.pixelYToLatitude(0, MercatorProjection.zoomLevelToScaleFactor(zoomLevel), tileSize); Assert.assertEquals(MercatorProjection.LATITUDE_MAX, latitude, 0); long mapSize = MercatorProjection.getMapSize(zoomLevel, tileSize); latitude = MercatorProjection.pixelYToLatitude((float) mapSize / 2, zoomLevel, tileSize); Assert.assertEquals(0, latitude, 0); mapSize = MercatorProjection.getMapSize(MercatorProjection.zoomLevelToScaleFactor(zoomLevel), tileSize); latitude = MercatorProjection.pixelYToLatitude((float) mapSize / 2, MercatorProjection.zoomLevelToScaleFactor(zoomLevel), tileSize); Assert.assertEquals(0, latitude, 0); latitude = MercatorProjection.pixelYToLatitude(mapSize, zoomLevel, tileSize); Assert.assertEquals(MercatorProjection.LATITUDE_MIN, latitude, 0); latitude = MercatorProjection.pixelYToLatitude(mapSize, MercatorProjection.zoomLevelToScaleFactor(zoomLevel), tileSize); Assert.assertEquals(MercatorProjection.LATITUDE_MIN, latitude, 0); } verifyInvalidPixelYToLatitude(-1, (byte) 0, tileSize); verifyInvalidPixelYToLatitude(tileSize + 1, (byte) 0, tileSize); } }
public static double pixelYToLatitude(double pixelY, double scaleFactor, int tileSize) { long mapSize = getMapSize(scaleFactor, tileSize); if (pixelY < 0 || pixelY > mapSize) { throw new IllegalArgumentException("invalid pixelY coordinate at zoom level " + scaleFactor + ": " + pixelY); } double y = 0.5 - (pixelY / mapSize); return 90 - 360 * Math.atan(Math.exp(-y * (2 * Math.PI))) / Math.PI; }
MercatorProjection { public static double pixelYToLatitude(double pixelY, double scaleFactor, int tileSize) { long mapSize = getMapSize(scaleFactor, tileSize); if (pixelY < 0 || pixelY > mapSize) { throw new IllegalArgumentException("invalid pixelY coordinate at zoom level " + scaleFactor + ": " + pixelY); } double y = 0.5 - (pixelY / mapSize); return 90 - 360 * Math.atan(Math.exp(-y * (2 * Math.PI))) / Math.PI; } }
MercatorProjection { public static double pixelYToLatitude(double pixelY, double scaleFactor, int tileSize) { long mapSize = getMapSize(scaleFactor, tileSize); if (pixelY < 0 || pixelY > mapSize) { throw new IllegalArgumentException("invalid pixelY coordinate at zoom level " + scaleFactor + ": " + pixelY); } double y = 0.5 - (pixelY / mapSize); return 90 - 360 * Math.atan(Math.exp(-y * (2 * Math.PI))) / Math.PI; } private  MercatorProjection(); }
MercatorProjection { public static double pixelYToLatitude(double pixelY, double scaleFactor, int tileSize) { long mapSize = getMapSize(scaleFactor, tileSize); if (pixelY < 0 || pixelY > mapSize) { throw new IllegalArgumentException("invalid pixelY coordinate at zoom level " + scaleFactor + ": " + pixelY); } double y = 0.5 - (pixelY / mapSize); return 90 - 360 * Math.atan(Math.exp(-y * (2 * Math.PI))) / Math.PI; } private  MercatorProjection(); static double calculateGroundResolution(double latitude, double scaleFactor, int tileSize); static double calculateGroundResolution(double latitude, byte zoomLevel, int tileSize); static LatLong fromPixels(double pixelX, double pixelY, double scaleFactor, int tileSize); static LatLong fromPixels(double pixelX, double pixelY, byte zoomLevel, int tileSize); static long getMapSize(double scaleFactor, int tileSize); static long getMapSize(byte zoomLevel, int tileSize); static Point getPixel(LatLong latLong, double scaleFactor, int tileSize); static Point getPixel(LatLong latLong, byte zoomLevel, int tileSize); static double latitudeToPixelY(double latitude, double scaleFactor, int tileSize); static double latitudeToPixelY(double latitude, byte zoomLevel, int tileSize); static long latitudeToTileY(double latitude, double scaleFactor); static long latitudeToTileY(double latitude, byte zoomLevel); static double longitudeToPixelX(double longitude, double scaleFactor, int tileSize); static double longitudeToPixelX(double longitude, byte zoomLevel, int tileSize); static long longitudeToTileX(double longitude, double scaleFactor); static long longitudeToTileX(double longitude, byte zoomLevel); static double metersToPixels(float meters, double latitude, double scaleFactor, int tileSize); static double metersToPixels(float meters, double latitude, byte zoom, int tileSize); static double pixelXToLongitude(double pixelX, double scaleFactor, int tileSize); static double pixelXToLongitude(double pixelX, byte zoomLevel, int tileSize); static long pixelXToTileX(double pixelX, double scaleFactor, int tileSize); static long pixelXToTileX(double pixelX, byte zoomLevel, int tileSize); static double pixelYToLatitude(double pixelY, double scaleFactor, int tileSize); static double pixelYToLatitude(double pixelY, byte zoomLevel, int tileSize); static long pixelYToTileY(double pixelY, double scaleFactor, int tileSize); static long pixelYToTileY(double pixelY, byte zoomLevel, int tileSize); static double scaleFactorToZoomLevel(double scaleFactor); static long tileToPixel(long tileNumber, int tileSize); static double tileXToLongitude(long tileX, double scaleFactor); static double tileXToLongitude(long tileX, byte zoomLevel); static double tileYToLatitude(long tileY, double scaleFactor); static double tileYToLatitude(long tileY, byte zoomLevel); static double zoomLevelToScaleFactor(byte zoomLevel); }
MercatorProjection { public static double pixelYToLatitude(double pixelY, double scaleFactor, int tileSize) { long mapSize = getMapSize(scaleFactor, tileSize); if (pixelY < 0 || pixelY > mapSize) { throw new IllegalArgumentException("invalid pixelY coordinate at zoom level " + scaleFactor + ": " + pixelY); } double y = 0.5 - (pixelY / mapSize); return 90 - 360 * Math.atan(Math.exp(-y * (2 * Math.PI))) / Math.PI; } private  MercatorProjection(); static double calculateGroundResolution(double latitude, double scaleFactor, int tileSize); static double calculateGroundResolution(double latitude, byte zoomLevel, int tileSize); static LatLong fromPixels(double pixelX, double pixelY, double scaleFactor, int tileSize); static LatLong fromPixels(double pixelX, double pixelY, byte zoomLevel, int tileSize); static long getMapSize(double scaleFactor, int tileSize); static long getMapSize(byte zoomLevel, int tileSize); static Point getPixel(LatLong latLong, double scaleFactor, int tileSize); static Point getPixel(LatLong latLong, byte zoomLevel, int tileSize); static double latitudeToPixelY(double latitude, double scaleFactor, int tileSize); static double latitudeToPixelY(double latitude, byte zoomLevel, int tileSize); static long latitudeToTileY(double latitude, double scaleFactor); static long latitudeToTileY(double latitude, byte zoomLevel); static double longitudeToPixelX(double longitude, double scaleFactor, int tileSize); static double longitudeToPixelX(double longitude, byte zoomLevel, int tileSize); static long longitudeToTileX(double longitude, double scaleFactor); static long longitudeToTileX(double longitude, byte zoomLevel); static double metersToPixels(float meters, double latitude, double scaleFactor, int tileSize); static double metersToPixels(float meters, double latitude, byte zoom, int tileSize); static double pixelXToLongitude(double pixelX, double scaleFactor, int tileSize); static double pixelXToLongitude(double pixelX, byte zoomLevel, int tileSize); static long pixelXToTileX(double pixelX, double scaleFactor, int tileSize); static long pixelXToTileX(double pixelX, byte zoomLevel, int tileSize); static double pixelYToLatitude(double pixelY, double scaleFactor, int tileSize); static double pixelYToLatitude(double pixelY, byte zoomLevel, int tileSize); static long pixelYToTileY(double pixelY, double scaleFactor, int tileSize); static long pixelYToTileY(double pixelY, byte zoomLevel, int tileSize); static double scaleFactorToZoomLevel(double scaleFactor); static long tileToPixel(long tileNumber, int tileSize); static double tileXToLongitude(long tileX, double scaleFactor); static double tileXToLongitude(long tileX, byte zoomLevel); static double tileYToLatitude(long tileY, double scaleFactor); static double tileYToLatitude(long tileY, byte zoomLevel); static double zoomLevelToScaleFactor(byte zoomLevel); static final double EARTH_CIRCUMFERENCE; static final double LATITUDE_MAX; static final double LATITUDE_MIN; }