/// <summary> /// Get tile envelope in WGS-84 coordinates /// </summary> /// <param name="x">x index</param> /// <param name="y">y index</param> /// <param name="zoom">zoom</param> /// <returns>Envelope in WGS-84</returns> private static Envelope GetTileEnvelope(int x, int y, int zoom) { var currTopLeftPixXY = TileCalculator.TileXYToTopLeftPixelXY(x, y); var currTopLeftCoord = TileCalculator.PixelXYToLatLong((int)currTopLeftPixXY.X, (int)currTopLeftPixXY.Y, zoom); var currBtmRightPixXY = TileCalculator.TileXYToBottomRightPixelXY(x, y); var currBtmRightCoord = TileCalculator.PixelXYToLatLong((int)currBtmRightPixXY.X, (int)currBtmRightPixXY.Y, zoom); return(new Envelope(currTopLeftCoord, currBtmRightCoord)); }
/// <summary> /// /// </summary> /// <param name="envelope"></param> /// <param name="bounds"></param> /// <returns></returns> public Tile[,] GetTiles(Envelope envelope, Rectangle bounds) { Coordinate mapTopLeft = envelope.TopLeft(); Coordinate mapBottomRight = envelope.BottomRight(); //Clip the coordinates so they are in the range of the web mercator projection mapTopLeft.Y = TileCalculator.Clip(mapTopLeft.Y, TileCalculator.MinLatitude, TileCalculator.MaxLatitude); mapTopLeft.X = TileCalculator.Clip(mapTopLeft.X, TileCalculator.MinLongitude, TileCalculator.MaxLongitude); mapBottomRight.Y = TileCalculator.Clip(mapBottomRight.Y, TileCalculator.MinLatitude, TileCalculator.MaxLatitude); mapBottomRight.X = TileCalculator.Clip(mapBottomRight.X, TileCalculator.MinLongitude, TileCalculator.MaxLongitude); int zoom = TileCalculator.DetermineZoomLevel(envelope, bounds); Point topLeftTileXY = TileCalculator.LatLongToTileXY(mapTopLeft, zoom); Point btmRightTileXY = TileCalculator.LatLongToTileXY(mapBottomRight, zoom); var tileMatrix = new Tile[(int)(btmRightTileXY.X - topLeftTileXY.X) + 1, (int)(btmRightTileXY.Y - topLeftTileXY.Y) + 1]; Parallel.For((int)topLeftTileXY.Y, (int)btmRightTileXY.Y + 1, y => Parallel.For((int)topLeftTileXY.X, (int)btmRightTileXY.X + 1, x => { var currTopLeftPixXY = TileCalculator.TileXYToTopLeftPixelXY(x, y); var currTopLeftCoord = TileCalculator.PixelXYToLatLong((int)currTopLeftPixXY.X, (int)currTopLeftPixXY.Y, zoom); var currBtmRightPixXY = TileCalculator.TileXYToBottomRightPixelXY(x, y); var currBtmRightCoord = TileCalculator.PixelXYToLatLong((int)currBtmRightPixXY.X, (int)currBtmRightPixXY.Y, zoom); var currEnv = new Envelope(currTopLeftCoord, currBtmRightCoord); var tile = GetTile(x, y, currEnv, zoom); tileMatrix[x - (int)topLeftTileXY.X, y - (int)topLeftTileXY.Y] = tile; } )); return(tileMatrix); }