private void SetTileSources(Rect imageRect, MultiScaleTileSource source) { double tileWidth = this.TileSize.Width * this.scaleFactor; double tileHeight = this.TileSize.Height * this.scaleFactor; int startTileX = (int)(imageRect.X / tileWidth); int startTileY = (int)(imageRect.Y / tileHeight); int endTileX = (int)Math.Ceiling(imageRect.Right / tileWidth) - 1; int endTileY = (int)Math.Ceiling(imageRect.Bottom / tileWidth) - 1; foreach (MapTile tile in this.mainCanvas.Children) { int tileX = startTileX + tile.RelativeTileId.X; int tileY = startTileY + tile.RelativeTileId.Y; if (tileX > endTileX || tileY > endTileY) { if (tile.Source != null) { tile.Source = null; } continue; } TileId tileId = new TileId(this.requestZoomLevel, tileX, tileY); this.SetTileSource(tile, tileId, source); } }
internal void UpdateRequest(TileSource e) { if (!this.abort) { lock (this.requestsSyncronizer) { TileId key = e.TileId; if (this.requests.ContainsKey(key)) { TileSource source = this.requests[key]; if (source != null) { this.tileCacheSize -= this.requests[key].Size; } this.tileCacheSize += e.Size; this.requests[key] = e; } } this.processStart = true; ManualResetEvent processManager = this.processRequest; if (processManager != null) { processManager.Set(); } } }
internal void RemoveRequest(TileId key) { lock (this.requestsSyncronizer) { if (this.requests.ContainsKey(key)) { this.RemoveTile(key); } } }
internal TileDownloader(TileId tileId, Stream stream, Action <TileDownloader> assyncImageReader) : this(tileId, assyncImageReader) { this.tileBodyPart = new byte[TileBodyPartLength]; this.stream = stream; if (stream.CanSeek) { stream.Position = 0; } }
/// <summary> /// Invalidates specified tile layers. /// </summary> /// <param name="tileLevel">Tile level.</param> /// <param name="tilePositionX">X position of the tile.</param> /// <param name="tilePositionY">Y position of the tile.</param> /// <param name="tileLayer">Layer of the tile.</param> public virtual void InvalidateTileLayer(int tileLevel, int tilePositionX, int tilePositionY, int tileLayer) { TilesDownloadManager manager = this.downloadManager; if (manager != null) { TileId tileId = new TileId(tileLevel, tilePositionX, tilePositionY); manager.RemoveRequest(tileId); manager.Refresh(); } }
internal TileSource GetTileSource(TileId tileId) { TileSource tileSource = null; TilesDownloadManager manager = this.downloadManager; if (manager != null) { tileSource = manager.GetTileSource(tileId); } return(tileSource); }
private TileId ReduceTileId(TileId tileId) { TileId result = null; int level = tileId.Level - 1; if (level > this.zoomLevelShift) { result = new TileId(level, tileId.X >> 1, tileId.Y >> 1); } return(result); }
private void RemoveTile(TileId key) { TileSource tileSource = this.requests[key]; if (tileSource != null) { this.tileCacheSize -= tileSource.Size; tileSource.Dispose(); } this.requestList.Remove(key); this.requests.Remove(key); }
internal void DownloadTile(TileId tileId) { IList <object> objects = null; using (ManualResetEvent complete = new ManualResetEvent(false)) { this.Dispatcher.BeginInvoke(new Action(() => { objects = this.GetTileLayers(tileId.Level, tileId.X, tileId.Y); complete.Set(); })); complete.WaitOne(); } if (objects.Count > 0) { Uri uri = objects[0] as Uri; Stream stream = objects[0] as Stream; if (uri != null) { TileDownloadState e = new TileDownloadState() { TileId = tileId, Uri = uri, Callback = this.AssyncImageCacheReader }; this.GetCachedTileAsync(tileId.Level, tileId.X, tileId.Y, e.RaiseEvent); } else if (stream != null) { this.Dispatcher.BeginInvoke(new Action(() => { TileDownloader tile = new TileDownloader(tileId, stream, this.AssyncImageReader); tile.StartDownload(); })); } else { TileDownloadState e = new TileDownloadState() { TileId = tileId }; this.UpdateTile(e); } } }
private bool RequestTile(int requestLevel, int tileX, int tileY) { TileId key = new TileId(requestLevel, tileX, tileY); bool download = this.UpdateCachedTile(key); if (download) { MultiScaleTileSource source = this.Source; if (source != null) { source.DownloadTile(key); } } return(download); }
internal TileSource GetTileSource(TileId tileId) { TileSource tileSource = null; if (!this.abort) { lock (this.requestsSyncronizer) { if (this.requests.ContainsKey(tileId)) { tileSource = this.requests[tileId] as TileSource; } } } return(tileSource); }
private void SetTileSource(MapTile tile, TileId tileId, MultiScaleTileSource source) { bool refresh = false; if (!tileId.Equals(tile.TileId)) { tile.TileId = tileId; refresh = true; } while (true) { TileSource tileSource = source.GetTileSource(tileId); if (tileSource != null) { if (tile.Source != tileSource || refresh) { tile.Source = tileSource; } if (tileSource.IsUsed) { this.usedTiles.Add(tileSource); this.unusedTiles.Remove(tileSource); break; } else { tileSource = null; } } if (tileSource == null) { tileId = this.ReduceTileId(tileId); if (tileId == null) { tile.Source = null; break; } } } }
internal bool ValidateTile(TileId tileId) { if (!this.abort) { MultiscaleImageViewport viewport = this.parameters; if (viewport.ActualWidth > 0 && viewport.ViewportWidth > 0) { int tileZoomLevelShift = (int)Math.Round(Math.Log(viewport.TileWidth, 2)); int tileLevel = tileId.Level - tileZoomLevelShift; double zoom = viewport.ActualWidth / viewport.TileWidth / viewport.ViewportWidth; int tileEndLevel = (int)Math.Ceiling(Math.Log(zoom, 2d)); if (tileLevel <= tileEndLevel) { double scale = Math.Pow(2d, Math.Log(zoom, 2d) - tileLevel); MultiScaleTileSource tileSource = this.Source; if (tileSource != null) { double width = tileSource.TileWidth * scale; double height = tileSource.TileHeight * scale; double x = tileId.X * width; double y = tileId.Y * height; double imageLeft = -viewport.ActualWidth * viewport.ViewportOrigin.X / viewport.ViewportWidth; double imageTop = -viewport.ActualWidth * viewport.ViewportOrigin.Y / viewport.ViewportWidth; Rect tileBounds = new Rect(x + imageLeft, y + imageTop, width, height); Rect imageBounds = new Rect(0, 0, viewport.ActualWidth, viewport.ActualHeight); imageBounds.Intersect(tileBounds); if (!imageBounds.IsEmpty) { return(true); } } } } this.RemoveRequest(tileId); } return(false); }
private bool UpdateCachedTile(TileId key) { bool download = false; lock (this.requestsSyncronizer) { if (!this.requests.ContainsKey(key)) { this.requestList.Add(key); this.requests.Add(key, null); download = true; } else { this.requestList.Remove(key); this.requestList.Add(key); } } return(download); }
private void UpdateTileChache() { int tilesCounter; lock (this.requestsSyncronizer) { tilesCounter = this.requestList.Count; } for (int i = 0; i < MaxRemovedTiles && tilesCounter > this.MinTileNumber && this.tileCacheSize > this.MaxTileCacheSize; i++) { lock (this.requestsSyncronizer) { TileId removed = this.requestList[0]; TileSource tileSource = this.requests[removed]; if (tileSource != null && !tileSource.IsUsed) { this.tileCacheSize -= tileSource.Size; tileSource.Dispose(); this.requestList.RemoveAt(0); this.requests.Remove(removed); } else { this.requestList.Remove(removed); this.requestList.Add(removed); break; } tilesCounter = this.requestList.Count; } } }
private TileDownloader(TileId tileId, Action <TileDownloader> assyncImageReader) { this.imageId = tileId; this.readerCallback = assyncImageReader; }
public TileSource(TileId tileId, byte[] imageBody) { this.TileId = tileId; this.body = imageBody; }
internal TileDownloader(TileId tileId, Uri downloadingUri, Action <TileDownloader> assyncImageReader, ValidateDelegate validator) : this(tileId, assyncImageReader) { this.uri = downloadingUri; this.validator = validator; }