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);
                }
            }
        }
        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 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 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);
                }
            }
        }
Example #8
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);
        }