public BitmapImage GetTile(GeometryInfo GeoInfo, int x, int y, int zoom) { String key = zoom.ToString() + ',' + x.ToString() + ',' + y.ToString(); if (PreLoadedTiles.ContainsKey(key)) { // We already have the tile in our cache... Object TileContent = PreLoadedTiles [key]; if ((TileContent == null) || (TileContent.GetType () != typeof (BitmapImage))) { BitmapImage Tile = new BitmapImage(); MemoryStream TileStream = new MemoryStream((Byte[])TileContent); Tile.BeginInit (); Tile.StreamSource = (MemoryStream)TileStream; Tile.EndInit (); lock (PreLoadedTiles) { PreLoadedTiles [key] = Tile; } } return (BitmapImage) (PreLoadedTiles[key]); } TileDownLoadDescription DownLoadDescription = new TileDownLoadDescription () {GeoInfo = GeoInfo, Key = key}; if (GeoInfo.RunTileLoadingSynchron) { Download(DownLoadDescription); if (PreLoadedTiles.ContainsKey(key)) { // We already have the tile in our cache... Object TileContent = PreLoadedTiles[key]; if (TileContent.GetType() != typeof(BitmapImage)) { BitmapImage Tile = new BitmapImage(); MemoryStream TileStream = new MemoryStream((Byte[])TileContent); Tile.BeginInit(); Tile.StreamSource = (MemoryStream)TileStream; Tile.EndInit(); lock (PreLoadedTiles) { PreLoadedTiles[key] = Tile; } } return (BitmapImage)(PreLoadedTiles[key]); } return null; } BackgroundWorker DownloadThread = new BackgroundWorker (); DownloadThread.RunWorkerCompleted += DownloadThread_RunWorkerCompleted; DownloadThread.DoWork += DownloadThread_DoWork; DownloadThread.RunWorkerAsync(DownLoadDescription); return null; }
private void Download (TileDownLoadDescription DownLoadDescription) { try { if (DownLoadDescription == null) return; //string[] vals = DownLoadDescription.Key.Split(','); //string url = "http://tile.openstreetmap.org/" + vals[0] + "/" + vals[1] + "/" + vals[2] + ".png"; WebClient Client = new WebClient(); System.IO.Stream TileIOStream = Client.OpenRead(DownLoadDescription.Url); MemoryStream TileStream = new MemoryStream(); TileIOStream.CopyTo(TileStream); //BitmapImage tile = new BitmapImage(new Uri(url)); //stream.Flush(); //stream.Close(); if (DownLoadDescription.GeoInfo.RunTileLoadingSynchron) { PreLoadedTiles[DownLoadDescription.Key] = TileStream.ToArray(); TileStream.Close(); } else { lock (PreLoadedTiles) { PreLoadedTiles[DownLoadDescription.Key] = TileStream.ToArray(); TileStream.Close(); } } // TODO: If we've reached the (as yet undefined) limit of how // many tiles we should be caching, we should remove the least // recently used one. if (NewDataAvailable != null) NewDataAvailable(DownLoadDescription.GeoInfo); } catch (Exception) { lock (PreLoadedTiles) { PreLoadedTiles[DownLoadDescription.Key] = null; } if (NewDataAvailable != null) NewDataAvailable(DownLoadDescription.GeoInfo); } finally { mDownloadTile = null; } }