void startAnimationMove(LatLong latLong) { // TODO is this properly synchronized? mapSize = MercatorProjection.getMapSize(zoomLevel, displayModel.getTileSize()); targetPixelX = MercatorProjection.longitudeToPixelX(latLong.longitude, mapSize); targetPixelY = MercatorProjection.latitudeToPixelY(latLong.latitude, mapSize); moveSteps = DEFAULT_MOVE_STEPS; synchronized (this) { notify(); } }
public static Set<Tile> getTiles(BoundingBox boundingBox, byte zoomLevel, int tileSize) { int tileLeft = MercatorProjection.longitudeToTileX(boundingBox.minLongitude, zoomLevel); int tileTop = MercatorProjection.latitudeToTileY(boundingBox.maxLatitude, zoomLevel); int tileRight = MercatorProjection.longitudeToTileX(boundingBox.maxLongitude, zoomLevel); int tileBottom = MercatorProjection.latitudeToTileY(boundingBox.minLatitude, zoomLevel); Set<Tile> tiles = new HashSet<Tile>(); for (int tileY = tileTop; tileY <= tileBottom; ++tileY) { for (int tileX = tileLeft; tileX <= tileRight; ++tileX) { tiles.add(new Tile(tileX, tileY, zoomLevel, tileSize)); } } return tiles; }
private static double calculatePriority(Tile tile, MapPosition mapPosition, int tileSize) { double tileLatitude = MercatorProjection.tileYToLatitude(tile.tileY, tile.zoomLevel); double tileLongitude = MercatorProjection.tileXToLongitude(tile.tileX, tile.zoomLevel); int halfTileSize = tileSize / 2; long mapSize = MercatorProjection.getMapSize(mapPosition.zoomLevel, tileSize); double tilePixelX = MercatorProjection.longitudeToPixelX(tileLongitude, mapSize) + halfTileSize; double tilePixelY = MercatorProjection.latitudeToPixelY(tileLatitude, mapSize) + halfTileSize; LatLong latLong = mapPosition.latLong; double mapPixelX = MercatorProjection.longitudeToPixelX(latLong.longitude, mapSize); double mapPixelY = MercatorProjection.latitudeToPixelY(latLong.latitude, mapSize); double diffPixel = Math.hypot(tilePixelX - mapPixelX, tilePixelY - mapPixelY); int diffZoom = Math.abs(tile.zoomLevel - mapPosition.zoomLevel); return diffPixel + PENALTY_PER_ZOOM_LEVEL * tileSize * diffZoom; }
/** * Converts a tile X number at a certain zoom level to a longitude coordinate. * * @param tileX the tile X number that should be converted. * @param zoomLevel the zoom level at which the number should be converted. * @return the longitude value of the tile X number. */ public static double tileXToLongitude(long tileX, byte zoomLevel) { return pixelXToLongitude(tileX * DUMMY_TILE_SIZE, getMapSize(zoomLevel, DUMMY_TILE_SIZE)); }
/** * Converts a longitude coordinate (in degrees) to the tile X number at a certain zoom level. * * @param longitude the longitude coordinate that should be converted. * @param zoomLevel the zoom level at which the coordinate should be converted. * @return the tile X number of the longitude value. */ public static int longitudeToTileX(double longitude, byte zoomLevel) { return pixelXToTileX(longitudeToPixelX(longitude, zoomLevel, DUMMY_TILE_SIZE), zoomLevel, DUMMY_TILE_SIZE); }
/** * 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)); }
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); }
/** * Gets the geographic extend of this Tile as a BoundingBox. * * @return boundaries of this tile. */ public BoundingBox getBoundingBox() { if (this.boundingBox == null) { double minLatitude = Math.max(MercatorProjection.LATITUDE_MIN, MercatorProjection.tileYToLatitude(tileY + 1, zoomLevel)); double minLongitude = Math.max(-180, MercatorProjection.tileXToLongitude(this.tileX, zoomLevel)); double maxLatitude = Math.min(MercatorProjection.LATITUDE_MAX, MercatorProjection.tileYToLatitude(this.tileY, zoomLevel)); double maxLongitude = Math.min(180, MercatorProjection.tileXToLongitude(tileX + 1, zoomLevel)); if (maxLongitude == -180) { // fix for dateline crossing, where the right tile starts at -180 and causes an invalid bbox maxLongitude = 180; } this.boundingBox = new BoundingBox(minLatitude, minLongitude, maxLatitude, maxLongitude); } return this.boundingBox; }
@Override public void draw(BoundingBox boundingBox, byte zoomLevel, Canvas canvas, Point topLeftPoint) { long tileLeft = MercatorProjection.longitudeToTileX(boundingBox.minLongitude, zoomLevel); long tileTop = MercatorProjection.latitudeToTileY(boundingBox.maxLatitude, zoomLevel); long tileRight = MercatorProjection.longitudeToTileX(boundingBox.maxLongitude, zoomLevel); long tileBottom = MercatorProjection.latitudeToTileY(boundingBox.minLatitude, zoomLevel); int tileSize = this.displayModel.getTileSize(); int pixelX1 = (int) (MercatorProjection.tileToPixel(tileLeft, tileSize) - topLeftPoint.x); int pixelY1 = (int) (MercatorProjection.tileToPixel(tileTop, tileSize) - topLeftPoint.y); int pixelX2 = (int) (MercatorProjection.tileToPixel(tileRight, tileSize) - topLeftPoint.x + tileSize); int pixelY2 = (int) (MercatorProjection.tileToPixel(tileBottom, tileSize) - topLeftPoint.y + tileSize); for (int lineX = pixelX1; lineX <= pixelX2 + 1; lineX += tileSize) { canvas.drawLine(lineX, pixelY1, lineX, pixelY2, this.paintBack); } for (int lineY = pixelY1; lineY <= pixelY2 + 1; lineY += tileSize) { canvas.drawLine(pixelX1, lineY, pixelX2, lineY, this.paintBack); } for (int lineX = pixelX1; lineX <= pixelX2 + 1; lineX += tileSize) { canvas.drawLine(lineX, pixelY1, lineX, pixelY2, this.paintFront); } for (int lineY = pixelY1; lineY <= pixelY2 + 1; lineY += tileSize) { canvas.drawLine(pixelX1, lineY, pixelX2, lineY, this.paintFront); } } }
public static Point getPixel(LatLong latLong, long mapSize) { double pixelX = MercatorProjection.longitudeToPixelX(latLong.longitude, mapSize); double pixelY = MercatorProjection.latitudeToPixelY(latLong.latitude, mapSize); return new Point(pixelX, pixelY); }
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); }
@Test public void longitudeToPixelXTest() { for (int tileSize : TILE_SIZES) { for (byte zoomLevel = ZOOM_LEVEL_MIN; zoomLevel <= ZOOM_LEVEL_MAX; ++zoomLevel) { long mapSize = MercatorProjection.getMapSize(zoomLevel, tileSize); double pixelX = MercatorProjection.longitudeToPixelX(LatLongUtils.LONGITUDE_MIN, mapSize); Assert.assertEquals(0, pixelX, 0); pixelX = MercatorProjection.longitudeToPixelXWithScaleFactor(LatLongUtils.LONGITUDE_MIN, MercatorProjection.zoomLevelToScaleFactor(zoomLevel), tileSize); Assert.assertEquals(0, pixelX, 0); pixelX = MercatorProjection.longitudeToPixelX(0, mapSize); Assert.assertEquals((float) mapSize / 2, pixelX, 0); mapSize = MercatorProjection.getMapSizeWithScaleFactor(MercatorProjection.zoomLevelToScaleFactor(zoomLevel), tileSize); pixelX = MercatorProjection.longitudeToPixelXWithScaleFactor(0, MercatorProjection.zoomLevelToScaleFactor(zoomLevel), tileSize); Assert.assertEquals((float) mapSize / 2, pixelX, 0); pixelX = MercatorProjection.longitudeToPixelX(LatLongUtils.LONGITUDE_MAX, mapSize); Assert.assertEquals(mapSize, pixelX, 0); pixelX = MercatorProjection.longitudeToPixelXWithScaleFactor(LatLongUtils.LONGITUDE_MAX, MercatorProjection.zoomLevelToScaleFactor(zoomLevel), tileSize); Assert.assertEquals(mapSize, pixelX, 0); } } }
@Test public void tileXToLongitudeTest() { for (int tileSize : TILE_SIZES) { for (byte zoomLevel = ZOOM_LEVEL_MIN; zoomLevel <= ZOOM_LEVEL_MAX; ++zoomLevel) { double longitude = MercatorProjection.tileXToLongitude(0, zoomLevel); Assert.assertEquals(LatLongUtils.LONGITUDE_MIN, longitude, 0); longitude = MercatorProjection.tileXToLongitudeWithScaleFactor(0, MercatorProjection.zoomLevelToScaleFactor(zoomLevel)); Assert.assertEquals(LatLongUtils.LONGITUDE_MIN, longitude, 0); long tileX = MercatorProjection.getMapSize(zoomLevel, tileSize) / tileSize; longitude = MercatorProjection.tileXToLongitude(tileX, zoomLevel); Assert.assertEquals(LatLongUtils.LONGITUDE_MAX, longitude, 0); tileX = MercatorProjection.getMapSizeWithScaleFactor(MercatorProjection.zoomLevelToScaleFactor(zoomLevel), tileSize) / tileSize; longitude = MercatorProjection.tileXToLongitudeWithScaleFactor(tileX, MercatorProjection.zoomLevelToScaleFactor(zoomLevel)); Assert.assertEquals(LatLongUtils.LONGITUDE_MAX, longitude, 0); } } }
@Test public void tileYToLatitudeTest() { for (int tileSize : TILE_SIZES) { for (byte zoomLevel = ZOOM_LEVEL_MIN; zoomLevel <= ZOOM_LEVEL_MAX; ++zoomLevel) { double latitude = MercatorProjection.tileYToLatitude(0, zoomLevel); Assert.assertEquals(MercatorProjection.LATITUDE_MAX, latitude, 0); latitude = MercatorProjection.tileYToLatitudeWithScaleFactor(0, MercatorProjection.zoomLevelToScaleFactor(zoomLevel)); Assert.assertEquals(MercatorProjection.LATITUDE_MAX, latitude, 0); long tileY = MercatorProjection.getMapSize(zoomLevel, tileSize) / tileSize; latitude = MercatorProjection.tileYToLatitude(tileY, zoomLevel); Assert.assertEquals(MercatorProjection.LATITUDE_MIN, latitude, 0); tileY = MercatorProjection.getMapSizeWithScaleFactor(MercatorProjection.zoomLevelToScaleFactor(zoomLevel), tileSize) / tileSize; latitude = MercatorProjection.tileYToLatitudeWithScaleFactor(tileY, MercatorProjection.zoomLevelToScaleFactor(zoomLevel)); Assert.assertEquals(MercatorProjection.LATITUDE_MIN, latitude, 0); } } }
@Test public void latitudeToPixelYTest() { for (int tileSize : TILE_SIZES) { for (byte zoomLevel = ZOOM_LEVEL_MIN; zoomLevel <= ZOOM_LEVEL_MAX; ++zoomLevel) { long mapSize = MercatorProjection.getMapSize(zoomLevel, tileSize); double pixelY = MercatorProjection.latitudeToPixelY(MercatorProjection.LATITUDE_MAX, mapSize); Assert.assertEquals(0, pixelY, 0); pixelY = MercatorProjection.latitudeToPixelYWithScaleFactor(MercatorProjection.LATITUDE_MAX, MercatorProjection.zoomLevelToScaleFactor(zoomLevel), tileSize); Assert.assertEquals(0, pixelY, 0); pixelY = MercatorProjection.latitudeToPixelY(0, mapSize); Assert.assertEquals((float) mapSize / 2, pixelY, 0); pixelY = MercatorProjection.latitudeToPixelYWithScaleFactor(0, MercatorProjection.zoomLevelToScaleFactor(zoomLevel), tileSize); Assert.assertEquals((float) mapSize / 2, pixelY, 0); pixelY = MercatorProjection.latitudeToPixelY(MercatorProjection.LATITUDE_MIN, mapSize); Assert.assertEquals(mapSize, pixelY, 0); pixelY = MercatorProjection.latitudeToPixelYWithScaleFactor(MercatorProjection.LATITUDE_MIN, MercatorProjection.zoomLevelToScaleFactor(zoomLevel), tileSize); Assert.assertEquals(mapSize, pixelY, 0); } } }
@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); } }
@Test public void pixelXToLongitudeTest() { for (int tileSize : TILE_SIZES) { for (byte zoomLevel = ZOOM_LEVEL_MIN; zoomLevel <= ZOOM_LEVEL_MAX; ++zoomLevel) { long mapSize = MercatorProjection.getMapSize(zoomLevel, tileSize); double longitude = MercatorProjection.pixelXToLongitude(0, mapSize); Assert.assertEquals(LatLongUtils.LONGITUDE_MIN, longitude, 0); longitude = MercatorProjection.pixelXToLongitudeWithScaleFactor(0, MercatorProjection.zoomLevelToScaleFactor(zoomLevel), tileSize); Assert.assertEquals(LatLongUtils.LONGITUDE_MIN, longitude, 0); longitude = MercatorProjection.pixelXToLongitude((float) mapSize / 2, mapSize); Assert.assertEquals(0, longitude, 0); mapSize = MercatorProjection.getMapSizeWithScaleFactor(MercatorProjection.zoomLevelToScaleFactor(zoomLevel), tileSize); longitude = MercatorProjection.pixelXToLongitudeWithScaleFactor((float) mapSize / 2, MercatorProjection.zoomLevelToScaleFactor(zoomLevel), tileSize); Assert.assertEquals(0, longitude, 0); longitude = MercatorProjection.pixelXToLongitude(mapSize, mapSize); Assert.assertEquals(LatLongUtils.LONGITUDE_MAX, longitude, 0); longitude = MercatorProjection.pixelXToLongitudeWithScaleFactor(mapSize, MercatorProjection.zoomLevelToScaleFactor(zoomLevel), tileSize); Assert.assertEquals(LatLongUtils.LONGITUDE_MAX, longitude, 0); } verifyInvalidPixelXToLongitude(-1, (byte) 0, tileSize); verifyInvalidPixelXToLongitude(tileSize + 1, (byte) 0, tileSize); } }
/** * Calculates the required length and value of the scalebar * * @param unitAdapter the DistanceUnitAdapter to calculate for * @return a {@link ScaleBarLengthAndValue} object containing the required scaleBarLength and scaleBarValue */ protected ScaleBarLengthAndValue calculateScaleBarLengthAndValue(DistanceUnitAdapter unitAdapter) { this.prevMapPosition = this.mapViewPosition.getMapPosition(); double groundResolution = MercatorProjection.calculateGroundResolution(this.prevMapPosition.latLong.latitude, MercatorProjection.getMapSize(this.prevMapPosition.zoomLevel, this.displayModel.getTileSize())); groundResolution = groundResolution / unitAdapter.getMeterRatio(); int[] scaleBarValues = unitAdapter.getScaleBarValues(); int scaleBarLength = 0; int mapScaleValue = 0; for (int scaleBarValue : scaleBarValues) { mapScaleValue = scaleBarValue; scaleBarLength = (int) (mapScaleValue / groundResolution); if (scaleBarLength < (this.mapScaleBitmap.getWidth() - 10)) { break; } } return new ScaleBarLengthAndValue(scaleBarLength, mapScaleValue); }
/** * Converts a longitude coordinate (in degrees) to a pixel X coordinate at a certain zoom level. * * @param longitude the longitude coordinate that should be converted. * @param zoomLevel the zoom level at which the coordinate should be converted. * @param tileSize the tile size * @return the pixel X coordinate of the longitude value. */ public static double longitudeToPixelX(double longitude, byte zoomLevel, int tileSize) { long mapSize = getMapSize(zoomLevel, tileSize); return (longitude + 180) / 360 * mapSize; }
/** * @return the non-negative radius of this circle in pixels. */ protected int getRadiusInPixels(double latitude, byte zoomLevel) { return (int) MercatorProjection.metersToPixels(this.radius, latitude, MercatorProjection.getMapSize(zoomLevel, displayModel.getTileSize())); }