public static Point GetPixelWithScaleFactor(LatLong latLong, double scaleFactor, int tileSize)
        {
            double pixelX = MercatorProjection.LongitudeToPixelXWithScaleFactor(latLong.Longitude, scaleFactor, tileSize);
            double pixelY = MercatorProjection.LatitudeToPixelYWithScaleFactor(latLong.Latitude, scaleFactor, tileSize);

            return(new Point(pixelX, pixelY));
        }
        public virtual void PixelYToLatitudeTest()
        {
            foreach (int tileSize in TILE_SIZES)
            {
                for (sbyte zoomLevel = ZOOM_LEVEL_MIN; zoomLevel <= ZOOM_LEVEL_MAX; ++zoomLevel)
                {
                    long   mapSize  = MercatorProjection.GetMapSize(zoomLevel, tileSize);
                    double latitude = MercatorProjection.PixelYToLatitude(0, mapSize);
                    Assert.AreEqual(MercatorProjection.LATITUDE_MAX, latitude, 0);
                    latitude = MercatorProjection.PixelYToLatitudeWithScaleFactor(0, MercatorProjection.ZoomLevelToScaleFactor(zoomLevel), tileSize);
                    Assert.AreEqual(MercatorProjection.LATITUDE_MAX, latitude, 0);

                    latitude = MercatorProjection.PixelYToLatitude((float)mapSize / 2, mapSize);
                    Assert.AreEqual(0, latitude, 0);
                    mapSize  = MercatorProjection.GetMapSizeWithScaleFactor(MercatorProjection.ZoomLevelToScaleFactor(zoomLevel), tileSize);
                    latitude = MercatorProjection.PixelYToLatitudeWithScaleFactor((float)mapSize / 2, MercatorProjection.ZoomLevelToScaleFactor(zoomLevel), tileSize);
                    Assert.AreEqual(0, latitude, 0);

                    latitude = MercatorProjection.PixelYToLatitude(mapSize, mapSize);
                    Assert.AreEqual(MercatorProjection.LATITUDE_MIN, latitude, 0);
                    latitude = MercatorProjection.PixelYToLatitudeWithScaleFactor(mapSize, MercatorProjection.ZoomLevelToScaleFactor(zoomLevel), tileSize);
                    Assert.AreEqual(MercatorProjection.LATITUDE_MIN, latitude, 0);
                }

                VerifyInvalidPixelYToLatitude(-1, (sbyte)0, tileSize);
                VerifyInvalidPixelYToLatitude(tileSize + 1, (sbyte)0, tileSize);
            }
        }
        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));
        }
 public virtual void ZoomLevelToScaleFactorTest()
 {
     for (sbyte zoomLevel = ZOOM_LEVEL_MIN; zoomLevel <= ZOOM_LEVEL_MAX; ++zoomLevel)
     {
         double scaleFactor = MercatorProjection.ZoomLevelToScaleFactor(zoomLevel);
         Assert.AreEqual(zoomLevel, MercatorProjection.ScaleFactorToZoomLevel(scaleFactor), 0.0001f);
     }
 }
 public virtual void TileToPixelTest()
 {
     foreach (int tileSize in TILE_SIZES)
     {
         Assert.AreEqual(0, MercatorProjection.TileToPixel(0, tileSize));
         Assert.AreEqual(tileSize, MercatorProjection.TileToPixel(1, tileSize));
         Assert.AreEqual(tileSize * 2, MercatorProjection.TileToPixel(2, tileSize));
     }
 }
 public virtual void PixelYToTileYTest()
 {
     foreach (int tileSize in TILE_SIZES)
     {
         for (sbyte zoomLevel = ZOOM_LEVEL_MIN; zoomLevel <= ZOOM_LEVEL_MAX; ++zoomLevel)
         {
             Assert.AreEqual(0, MercatorProjection.PixelYToTileY(0, zoomLevel, tileSize));
             Assert.AreEqual(0, MercatorProjection.PixelYToTileY(0, MercatorProjection.ZoomLevelToScaleFactor(zoomLevel), tileSize));
         }
     }
 }
 public virtual void MetersToPixelTest()
 {
     foreach (int tileSize in TILE_SIZES)
     {
         Assert.True(MercatorProjection.MetersToPixels(10, 10.0, MercatorProjection.GetMapSize((sbyte)1, tileSize)) < 1);
         Assert.True(MercatorProjection.MetersToPixels((long)(40 * 10e7), 10.0, MercatorProjection.GetMapSize((sbyte)1, tileSize)) > 1);
         Assert.True(MercatorProjection.MetersToPixels(10, 10.0, MercatorProjection.GetMapSize((sbyte)20, tileSize)) > 1);
         Assert.True(MercatorProjection.MetersToPixels(10, 89.0, MercatorProjection.GetMapSize((sbyte)1, tileSize)) < 1);
         Assert.True(MercatorProjection.MetersToPixels((int)(40 * 10e3), 50, MercatorProjection.GetMapSize((sbyte)10, tileSize)) > 1);
     }
 }
 private static void VerifyInvalidPixelYToLatitude(double pixelY, sbyte zoomLevel, int tileSize)
 {
     try
     {
         MercatorProjection.PixelYToLatitude(pixelY, MercatorProjection.GetMapSize(zoomLevel, tileSize));
         Assert.Fail("pixelY: " + pixelY + ", zoomLevel: " + zoomLevel);
     }
     catch (System.ArgumentException)
     {
         Assert.True(true);
     }
 }
 private static void VerifyInvalidGetMapSize(sbyte zoomLevel, int tileSize)
 {
     try
     {
         MercatorProjection.GetMapSize(zoomLevel, tileSize);
         Assert.Fail("zoomLevel: " + zoomLevel);
     }
     catch (System.ArgumentException)
     {
         Assert.True(true);
     }
 }
 public virtual void GetMapSizeTest()
 {
     foreach (int tileSize in TILE_SIZES)
     {
         for (sbyte zoomLevel = ZOOM_LEVEL_MIN; zoomLevel <= ZOOM_LEVEL_MAX; ++zoomLevel)
         {
             long factor = (long)Math.Round(MercatorProjection.ZoomLevelToScaleFactor(zoomLevel));
             Assert.AreEqual(tileSize * factor, MercatorProjection.GetMapSize(zoomLevel, tileSize));
             Assert.AreEqual(MercatorProjection.GetMapSizeWithScaleFactor(MercatorProjection.ZoomLevelToScaleFactor(zoomLevel), tileSize), MercatorProjection.GetMapSize(zoomLevel, tileSize));
         }
         VerifyInvalidGetMapSize((sbyte)-1, tileSize);
     }
 }
        public virtual void LatitudeToTileYTest()
        {
            for (sbyte zoomLevel = ZOOM_LEVEL_MIN; zoomLevel <= ZOOM_LEVEL_MAX; ++zoomLevel)
            {
                long tileY = MercatorProjection.LatitudeToTileY(MercatorProjection.LATITUDE_MAX, zoomLevel);
                Assert.AreEqual(0, tileY);
                tileY = MercatorProjection.LatitudeToTileY(MercatorProjection.LATITUDE_MAX, MercatorProjection.ZoomLevelToScaleFactor(zoomLevel));
                Assert.AreEqual(0, tileY);

                tileY = MercatorProjection.LatitudeToTileY(MercatorProjection.LATITUDE_MIN, zoomLevel);
                Assert.AreEqual(Tile.GetMaxTileNumber(zoomLevel), tileY);
                tileY = MercatorProjection.LatitudeToTileY(MercatorProjection.LATITUDE_MIN, MercatorProjection.ZoomLevelToScaleFactor(zoomLevel));
                Assert.AreEqual(Tile.GetMaxTileNumber(zoomLevel), tileY);
            }
        }
        public virtual void LongitudeToTileXTest()
        {
            for (sbyte zoomLevel = ZOOM_LEVEL_MIN; zoomLevel <= ZOOM_LEVEL_MAX; ++zoomLevel)
            {
                long tileX = MercatorProjection.LongitudeToTileX(LatLongUtils.LONGITUDE_MIN, zoomLevel);
                Assert.AreEqual(0, tileX);
                tileX = MercatorProjection.LongitudeToTileX(LatLongUtils.LONGITUDE_MIN, MercatorProjection.ZoomLevelToScaleFactor(zoomLevel));
                Assert.AreEqual(0, tileX);

                tileX = MercatorProjection.LongitudeToTileX(LatLongUtils.LONGITUDE_MAX, zoomLevel);
                Assert.AreEqual(Tile.GetMaxTileNumber(zoomLevel), tileX);
                tileX = MercatorProjection.LongitudeToTileX(LatLongUtils.LONGITUDE_MAX, MercatorProjection.ZoomLevelToScaleFactor(zoomLevel));
                Assert.AreEqual(Tile.GetMaxTileNumber(zoomLevel), tileX);
            }
        }
        public virtual void TileYToLatitudeTest()
        {
            foreach (int tileSize in TILE_SIZES)
            {
                for (sbyte zoomLevel = ZOOM_LEVEL_MIN; zoomLevel <= ZOOM_LEVEL_MAX; ++zoomLevel)
                {
                    double latitude = MercatorProjection.TileYToLatitude(0, zoomLevel);
                    Assert.AreEqual(MercatorProjection.LATITUDE_MAX, latitude, 0);
                    latitude = MercatorProjection.TileYToLatitude(0, MercatorProjection.ZoomLevelToScaleFactor(zoomLevel));
                    Assert.AreEqual(MercatorProjection.LATITUDE_MAX, latitude, 0);

                    long tileY = MercatorProjection.GetMapSize(zoomLevel, tileSize) / tileSize;
                    latitude = MercatorProjection.TileYToLatitude(tileY, zoomLevel);
                    Assert.AreEqual(MercatorProjection.LATITUDE_MIN, latitude, 0);
                    tileY    = MercatorProjection.GetMapSizeWithScaleFactor(MercatorProjection.ZoomLevelToScaleFactor(zoomLevel), tileSize) / tileSize;
                    latitude = MercatorProjection.TileYToLatitude(tileY, MercatorProjection.ZoomLevelToScaleFactor(zoomLevel));
                    Assert.AreEqual(MercatorProjection.LATITUDE_MIN, latitude, 0);
                }
            }
        }
Esempio n. 15
0
        /// <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);
        }
 /// <summary>
 /// Converts meters to pixels at latitude for zoom-level.
 /// </summary>
 /// <param name="meters">
 ///            the meters to convert </param>
 /// <param name="latitude">
 ///            the latitude for the conversion. </param>
 /// <param name="mapSize">
 ///            precomputed size of map. </param>
 /// <returns> pixels that represent the meters at the given zoom-level and latitude. </returns>
 public static double MetersToPixels(float meters, double latitude, long mapSize)
 {
     return(meters / MercatorProjection.CalculateGroundResolution(latitude, mapSize));
 }
 /// <summary>
 /// Converts meters to pixels at latitude for zoom-level.
 /// </summary>
 /// <param name="meters">
 ///            the meters to convert </param>
 /// <param name="latitude">
 ///            the latitude for the conversion. </param>
 /// <param name="scaleFactor">
 ///            the scale factor for the conversion. </param>
 /// <returns> pixels that represent the meters at the given zoom-level and latitude. </returns>
 public static double MetersToPixelsWithScaleFactor(float meters, double latitude, double scaleFactor, int tileSize)
 {
     return(meters / MercatorProjection.CalculateGroundResolutionWithScaleFactor(latitude, scaleFactor, tileSize));
 }