private void SetTileToMemory(uOSMTile tile) { if (!memTiles.ContainsKey(tile.Key)) { memTiles.Add(tile.Key, tile); if (memTiles.Count > MaxMemTiles) { memTiles.Remove(memTiles.Keys.First()); } } }
private bool TryLoadTileImage(uOSMTile tile, out Image tileImage) { bool result = false; string tilePath = Path.Combine(DB_Path, uOSMTile.GetRelativePathAndFileName(tile)); tileImage = null; if (File.Exists(tilePath)) { try { tileImage = Image.FromFile(tilePath); result = true; } catch { } } return(result); }
private bool TrySaveTile(uOSMTile tile) { bool result = false; string tilePath = Path.Combine(DB_Path, uOSMTile.GetRelativePathAndFileName(tile)); string tileDir = Path.GetDirectoryName(tilePath); try { if (!Directory.Exists(tileDir)) { Directory.CreateDirectory(tileDir); } tile.Tile.Save(tilePath); result = true; } catch { } return(result); }
public List <uOSMTile> GetTiles(int z, int fromx, int tox, int fromy, int toy) { List <uOSMTile> result = new List <uOSMTile>(); for (int x = fromx; x <= tox; x++) { for (int y = fromy; y <= toy; y++) { uOSMTile tile = new uOSMTile(z, x, y); if (IsTileInMemory(tile)) { tile.Tile = new Bitmap(memTiles[tile.Key].Tile); } else { if (TryLoadTileImage(tile, out tile.Tile)) { SetTileToMemory(tile); } else { if (TryDownloadTileImage(tile, out tile.Tile)) { SetTileToMemory(tile); TrySaveTile(tile); } else { tile.Tile = (Bitmap)errImage.Clone(); } } } result.Add(tile); } } return(result); }
private bool TryDownloadTileImage(uOSMTile tile, out Image tileImage) { bool result = false; string url = string.Format(CultureInfo.InvariantCulture, "{0}/{1}", GetNextServer(), uOSMTile.GetRelativeUrl(tile)); tileImage = null; byte[] bytes = null; try { bytes = httpClient.GetByteArrayAsync(url).ConfigureAwait(false).GetAwaiter().GetResult(); } catch { } if (bytes != null) { using (MemoryStream ms = new MemoryStream(bytes)) { tileImage = Image.FromStream(ms); result = true; } } return(result); }
private bool IsTileInMemory(uOSMTile tile) { return(memTiles.ContainsKey(tile.Key)); }
public static string GetRelativeUrl(uOSMTile tile) { return(string.Format(CultureInfo.InvariantCulture, "{0}/{1}/{2}.png", tile.Z, tile.X, tile.Y)); }
public static string GetRelativePathAndFileName(uOSMTile tile) { return(string.Format(CultureInfo.InvariantCulture, "{0}\\{1}_{2}.png", tile.Z, tile.X, tile.Y)); }