public virtual void LatitudeToPixelYTest() { foreach (int tileSize in TILE_SIZES) { for (sbyte zoomLevel = ZOOM_LEVEL_MIN; zoomLevel <= ZOOM_LEVEL_MAX; ++zoomLevel) { long mapSize = MercatorProjection.GetMapSize(zoomLevel, tileSize); double pixelY = MercatorProjection.LatitudeToPixelY(MercatorProjection.LATITUDE_MAX, mapSize); Assert.AreEqual(0, pixelY, 0); pixelY = MercatorProjection.LatitudeToPixelYWithScaleFactor(MercatorProjection.LATITUDE_MAX, MercatorProjection.ZoomLevelToScaleFactor(zoomLevel), tileSize); Assert.AreEqual(0, pixelY, 0); pixelY = MercatorProjection.LatitudeToPixelY(0, mapSize); Assert.AreEqual((float)mapSize / 2, pixelY, 0); pixelY = MercatorProjection.LatitudeToPixelYWithScaleFactor(0, MercatorProjection.ZoomLevelToScaleFactor(zoomLevel), tileSize); Assert.AreEqual((float)mapSize / 2, pixelY, 0); pixelY = MercatorProjection.LatitudeToPixelY(MercatorProjection.LATITUDE_MIN, mapSize); Assert.AreEqual(mapSize, pixelY, 0); pixelY = MercatorProjection.LatitudeToPixelYWithScaleFactor(MercatorProjection.LATITUDE_MIN, MercatorProjection.ZoomLevelToScaleFactor(zoomLevel), tileSize); Assert.AreEqual(mapSize, pixelY, 0); } } }
/// <summary> /// Calculates the absolute pixel position for a zoom level and tile size relative to origin /// </summary> /// <param name="latLong"> </param> /// <param name="mapSize"> precomputed size of map. </param> /// <returns> the relative pixel position to the origin values (e.g. for a tile) </returns> public static Point GetPixelRelative(LatLong latLong, long mapSize, double x, double y) { double pixelX = MercatorProjection.LongitudeToPixelX(latLong.Longitude, mapSize) - x; double pixelY = MercatorProjection.LatitudeToPixelY(latLong.Latitude, mapSize) - y; return(new Point(pixelX, pixelY)); }
/// <summary> /// Calculates the zoom level that allows to display the <seealso cref="BoundingBox"/> on a view with the <seealso cref="Dimension"/> and /// tile size. /// </summary> /// <param name="dimension"> /// the <seealso cref="Dimension"/> of the view. </param> /// <param name="boundingBox"> /// the <seealso cref="BoundingBox"/> to display. </param> /// <param name="tileSize"> /// the size of the tiles. </param> /// <returns> the zoom level that allows to display the <seealso cref="BoundingBox"/> on a view with the <seealso cref="Dimension"/> and /// tile size. </returns> public static sbyte ZoomForBounds(Dimension dimension, BoundingBox boundingBox, int tileSize) { long mapSize = MercatorProjection.GetMapSize((sbyte)0, tileSize); double pixelXMax = MercatorProjection.LongitudeToPixelX(boundingBox.MaxLongitude, mapSize); double pixelXMin = MercatorProjection.LongitudeToPixelX(boundingBox.MinLongitude, mapSize); double zoomX = -Math.Log(Math.Abs(pixelXMax - pixelXMin) / dimension.Width) / Math.Log(2); double pixelYMax = MercatorProjection.LatitudeToPixelY(boundingBox.MaxLatitude, mapSize); double pixelYMin = MercatorProjection.LatitudeToPixelY(boundingBox.MinLatitude, mapSize); double zoomY = -Math.Log(Math.Abs(pixelYMax - pixelYMin) / dimension.Height) / Math.Log(2); double zoom = Math.Floor(Math.Min(zoomX, zoomY)); if (zoom < 0) { return(0); } if (zoom > sbyte.MaxValue) { return(sbyte.MaxValue); } return((sbyte)zoom); }