/** * Converts a tile Y number at a certain zoom level to a latitude coordinate. * * @param tileY the tile Y number that should be converted. * @param zoomLevel the zoom level at which the number should be converted. * @return the latitude value of the tile Y number. */ public static double tileYToLatitude(long tileY, byte zoomLevel) { return pixelYToLatitude(tileY * DUMMY_TILE_SIZE, getMapSize(zoomLevel, DUMMY_TILE_SIZE)); }
@Test public void pixelYToLatitudeTest() { for (int tileSize : TILE_SIZES) { for (byte zoomLevel = ZOOM_LEVEL_MIN; zoomLevel <= ZOOM_LEVEL_MAX; ++zoomLevel) { long mapSize = MercatorProjection.getMapSize(zoomLevel, tileSize); double latitude = MercatorProjection.pixelYToLatitude(0, mapSize); Assert.assertEquals(MercatorProjection.LATITUDE_MAX, latitude, 0); latitude = MercatorProjection.pixelYToLatitudeWithScaleFactor(0, MercatorProjection.zoomLevelToScaleFactor(zoomLevel), tileSize); Assert.assertEquals(MercatorProjection.LATITUDE_MAX, latitude, 0); latitude = MercatorProjection.pixelYToLatitude((float) mapSize / 2, mapSize); Assert.assertEquals(0, latitude, 0); mapSize = MercatorProjection.getMapSizeWithScaleFactor(MercatorProjection.zoomLevelToScaleFactor(zoomLevel), tileSize); latitude = MercatorProjection.pixelYToLatitudeWithScaleFactor((float) mapSize / 2, MercatorProjection.zoomLevelToScaleFactor(zoomLevel), tileSize); Assert.assertEquals(0, latitude, 0); latitude = MercatorProjection.pixelYToLatitude(mapSize, mapSize); Assert.assertEquals(MercatorProjection.LATITUDE_MIN, latitude, 0); latitude = MercatorProjection.pixelYToLatitudeWithScaleFactor(mapSize, MercatorProjection.zoomLevelToScaleFactor(zoomLevel), tileSize); Assert.assertEquals(MercatorProjection.LATITUDE_MIN, latitude, 0); } verifyInvalidPixelYToLatitude(-1, (byte) 0, tileSize); verifyInvalidPixelYToLatitude(tileSize + 1, (byte) 0, tileSize); } }
private static double deltaLat(double deltaPixel, double lat, byte zoom, int tileSize) { long mapSize = MercatorProjection.getMapSize(zoom, tileSize); double pixelY = MercatorProjection.latitudeToPixelY(lat, mapSize); double lat2 = MercatorProjection.pixelYToLatitude(pixelY + deltaPixel, mapSize); return Math.abs(lat2 - lat); }
/** * Get LatLong from Pixels. */ public static LatLong fromPixels(double pixelX, double pixelY, long mapSize) { return new LatLong(pixelYToLatitude(pixelY, mapSize), pixelXToLongitude(pixelX, mapSize)); }
public static BoundingBox getBoundingBox(MapPosition mapPosition, Dimension canvasDimension, int tileSize) { long mapSize = MercatorProjection.getMapSize(mapPosition.zoomLevel, tileSize); double pixelX = MercatorProjection.longitudeToPixelX(mapPosition.latLong.longitude, mapSize); double pixelY = MercatorProjection.latitudeToPixelY(mapPosition.latLong.latitude, mapSize); int halfCanvasWidth = canvasDimension.width / 2; int halfCanvasHeight = canvasDimension.height / 2; double pixelXMin = Math.max(0, pixelX - halfCanvasWidth); double pixelYMin = Math.max(0, pixelY - halfCanvasHeight); double pixelXMax = Math.min(mapSize, pixelX + halfCanvasWidth); double pixelYMax = Math.min(mapSize, pixelY + halfCanvasHeight); double minLatitude = MercatorProjection.pixelYToLatitude(pixelYMax, mapSize); double minLongitude = MercatorProjection.pixelXToLongitude(pixelXMin, mapSize); double maxLatitude = MercatorProjection.pixelYToLatitude(pixelYMin, mapSize); double maxLongitude = MercatorProjection.pixelXToLongitude(pixelXMax, mapSize); return new BoundingBox(minLatitude, minLongitude, maxLatitude, maxLongitude); }
/** * Moves the center position of the map by the given amount of pixels. * * @param moveHorizontal the amount of pixels to move this MapViewPosition horizontally. * @param moveVertical the amount of pixels to move this MapViewPosition vertically. * @param zoomLevelDiff the difference in desired zoom level. * @param animated whether the move should be animated. */ public void moveCenterAndZoom(double moveHorizontal, double moveVertical, byte zoomLevelDiff, boolean animated) { synchronized (this) { long mapSize = MercatorProjection.getMapSize(this.zoomLevel, this.displayModel.getTileSize()); double pixelX = MercatorProjection.longitudeToPixelX(this.longitude, mapSize) - moveHorizontal; double pixelY = MercatorProjection.latitudeToPixelY(this.latitude, mapSize) - moveVertical; pixelX = Math.min(Math.max(0, pixelX), mapSize); pixelY = Math.min(Math.max(0, pixelY), mapSize); double newLatitude = MercatorProjection.pixelYToLatitude(pixelY, mapSize); double newLongitude = MercatorProjection.pixelXToLongitude(pixelX, mapSize); setCenterInternal(newLatitude, newLongitude); setZoomLevelInternal(this.zoomLevel + zoomLevelDiff, animated); } notifyObservers(); }
private static void verifyInvalidPixelYToLatitude(double pixelY, byte zoomLevel, int tileSize) { try { MercatorProjection.pixelYToLatitude(pixelY, MercatorProjection.getMapSize(zoomLevel, tileSize)); Assert.fail("pixelY: " + pixelY + ", zoomLevel: " + zoomLevel); } catch (IllegalArgumentException e) { Assert.assertTrue(true); } }
double maptileTopLat = MercatorProjection.pixelYToLatitude((long) origin.y, tile.mapSize); double maptileLeftLng = MercatorProjection.pixelXToLongitude((long) origin.x, tile.mapSize); double maptileBottomLat = MercatorProjection.pixelYToLatitude((long) origin.y + tile.tileSize, tile.mapSize); double maptileRightLng = MercatorProjection.pixelXToLongitude((long) origin.x + tile.tileSize, tile.mapSize);
return new LatLong(MercatorProjection.pixelYToLatitude(pixelY + y, mapSize), MercatorProjection.pixelXToLongitude(pixelX + x, mapSize)); } catch (Exception e) {
/** * Converts a tile Y number at a certain zoom level to a latitude coordinate. * * @param tileY the tile Y number that should be converted. * @param zoomLevel the zoom level at which the number should be converted. * @return the latitude value of the tile Y number. */ public static double tileYToLatitude(long tileY, byte zoomLevel) { return pixelYToLatitude(tileY * DUMMY_TILE_SIZE, getMapSize(zoomLevel, DUMMY_TILE_SIZE)); }
/** * Get LatLong from Pixels. */ public static LatLong fromPixels(double pixelX, double pixelY, long mapSize) { return new LatLong(pixelYToLatitude(pixelY, mapSize), pixelXToLongitude(pixelX, mapSize)); }
public static BoundingBox getBoundingBox(MapPosition mapPosition, Dimension canvasDimension, int tileSize) { long mapSize = MercatorProjection.getMapSize(mapPosition.zoomLevel, tileSize); double pixelX = MercatorProjection.longitudeToPixelX(mapPosition.latLong.longitude, mapSize); double pixelY = MercatorProjection.latitudeToPixelY(mapPosition.latLong.latitude, mapSize); int halfCanvasWidth = canvasDimension.width / 2; int halfCanvasHeight = canvasDimension.height / 2; double pixelXMin = Math.max(0, pixelX - halfCanvasWidth); double pixelYMin = Math.max(0, pixelY - halfCanvasHeight); double pixelXMax = Math.min(mapSize, pixelX + halfCanvasWidth); double pixelYMax = Math.min(mapSize, pixelY + halfCanvasHeight); double minLatitude = MercatorProjection.pixelYToLatitude(pixelYMax, mapSize); double minLongitude = MercatorProjection.pixelXToLongitude(pixelXMin, mapSize); double maxLatitude = MercatorProjection.pixelYToLatitude(pixelYMin, mapSize); double maxLongitude = MercatorProjection.pixelXToLongitude(pixelXMax, mapSize); return new BoundingBox(minLatitude, minLongitude, maxLatitude, maxLongitude); }
/** * Moves the center position of the map by the given amount of pixels. * * @param moveHorizontal the amount of pixels to move this MapViewPosition horizontally. * @param moveVertical the amount of pixels to move this MapViewPosition vertically. * @param zoomLevelDiff the difference in desired zoom level. * @param animated whether the move should be animated. */ public void moveCenterAndZoom(double moveHorizontal, double moveVertical, byte zoomLevelDiff, boolean animated) { synchronized (this) { long mapSize = MercatorProjection.getMapSize(this.zoomLevel, this.displayModel.getTileSize()); double pixelX = MercatorProjection.longitudeToPixelX(this.longitude, mapSize) - moveHorizontal; double pixelY = MercatorProjection.latitudeToPixelY(this.latitude, mapSize) - moveVertical; pixelX = Math.min(Math.max(0, pixelX), mapSize); pixelY = Math.min(Math.max(0, pixelY), mapSize); double newLatitude = MercatorProjection.pixelYToLatitude(pixelY, mapSize); double newLongitude = MercatorProjection.pixelXToLongitude(pixelX, mapSize); setCenterInternal(newLatitude, newLongitude); setZoomLevelInternal(this.zoomLevel + zoomLevelDiff, animated); } notifyObservers(); }
double maptileTopLat = MercatorProjection.pixelYToLatitude((long) origin.y, tile.mapSize); double maptileLeftLng = MercatorProjection.pixelXToLongitude((long) origin.x, tile.mapSize); double maptileBottomLat = MercatorProjection.pixelYToLatitude((long) origin.y + tile.tileSize, tile.mapSize); double maptileRightLng = MercatorProjection.pixelXToLongitude((long) origin.x + tile.tileSize, tile.mapSize);
return new LatLong(MercatorProjection.pixelYToLatitude(pixelY + y, mapSize), MercatorProjection.pixelXToLongitude(pixelX + x, mapSize)); } catch (Exception e) {