/// <summary> /// /// </summary> /// <param name="tile"></param> /// <returns></returns> public bool Put(Tile tile) { try { string pathToTile = Path.Combine(_fullPath, tile.ZoomLevel.ToString()); pathToTile = Path.Combine(pathToTile, tile.X.ToString()); Directory.CreateDirectory(pathToTile); tile.Bitmap.Save(Path.Combine(pathToTile, tile.Y.ToString())); return true; } catch { return false; } }
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; }
/// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="envelope"></param> /// <param name="zoom"></param> /// <returns></returns> public Tile GetTile(int x, int y, Envelope envelope, int zoom) { Bitmap bitmap = _tileCache.Get(zoom, x, y); if (null == bitmap) { bitmap = GetViaBrutile(x, y, zoom, envelope); } if (null != bitmap) { var tile = new Tile(x, y, zoom, envelope, bitmap); return tile; } try { string url = _tileServerUrl; if (url.Contains("{key}")) { string quadKey = TileCalculator.TileXYToBingQuadKey(x, y, zoom); url = url.Replace("{key}", quadKey); } else { url = url.Replace("{zoom}", zoom.ToString()); url = url.Replace("{x}", x.ToString()); url = url.Replace("{y}", y.ToString()); } var client = new WebClient(); var stream = client.OpenRead(url); if (stream != null) bitmap = new Bitmap(stream); var tile = new Tile(x, y, zoom, envelope, bitmap); if (stream != null) { stream.Flush(); stream.Close(); } //Put the tile in the cache _tileCache.Put(tile); return tile; } catch (Exception ex) { // We may see a 400 (Bad Request) when the user is zoomed in too far. Debug.WriteLine(ex.Message); //Return a No Data Available tile var noDataTile = new Tile(x, y, zoom, envelope, resources.NoDataTile); return noDataTile; } }
/// <summary> /// Takes a 2-dimensional array of tiles and stitches it into a single Bitmap for display on the Map /// </summary> /// <param name="tiles">2-dimensional array of tiles, [x by y]</param> /// <returns>Bitmap of the tiles stitched together</returns> public static Bitmap StitchTiles(Tile[,] tiles) { var width = tiles.GetLength(0) * 256; var height = tiles.GetLength(1) * 256; //create a bitmap to hold the combined image var finalImage = new Bitmap(width, height); //get a graphics object from the image so we can draw on it using (Graphics g = Graphics.FromImage(finalImage)) { //set background color g.Clear(Color.Black); //go through each image and "draw" it on the final image for (var y = 0; y < tiles.GetLength(1); y++) { for (var x = 0; x < tiles.GetLength(0); x++) { if (tiles[x, y] != null) { var tile = tiles[x, y].Bitmap; g.DrawImage(tile, new Rectangle(x * 256, y * 256, tile.Width, tile.Height)); } } } } return finalImage; }
private Tile GetTile(int x, int y, Envelope envelope, int zoom) { var bitmap = GetViaBrutile(x, y, zoom, envelope); if (bitmap != null) { return new Tile(x, y, zoom, envelope, bitmap); } try { var url = _tileServerUrl; if (url == null) { var noDataTile = new Tile(x, y, zoom, envelope, Resources.nodata); return noDataTile; } if (url.Contains("{key}")) { var quadKey = TileCalculator.TileXYToBingQuadKey(x, y, zoom); url = url.Replace("{key}", quadKey); } else { url = url.Replace("{zoom}", zoom.ToString()); url = url.Replace("{x}", x.ToString()); url = url.Replace("{y}", y.ToString()); } var client = new WebClient(); var stream = client.OpenRead(url); if (stream != null) bitmap = new Bitmap(stream); var tile = new Tile(x, y, zoom, envelope, bitmap); if (stream != null) { stream.Flush(); stream.Close(); } return tile; } catch (Exception ex) { Debug.WriteLine(ex.Message); return new Tile(x, y, zoom, envelope, Resources.nodata); } }