private static Task LoadTile(Tile tile, TileSource tileSource, string cacheName)
        {
            if (string.IsNullOrEmpty(cacheName))
            {
                return(LoadTile(tile, tileSource));
            }

            var uri = tileSource.GetUri(tile.XIndex, tile.Y, tile.ZoomLevel);

            if (uri != null)
            {
                var extension = Path.GetExtension(uri.LocalPath);

                if (string.IsNullOrEmpty(extension) || extension == ".jpeg")
                {
                    extension = ".jpg";
                }

                var cacheKey = string.Format(CultureInfo.InvariantCulture,
                                             "{0}/{1}/{2}/{3}{4}", cacheName, tile.ZoomLevel, tile.XIndex, tile.Y, extension);

                return(LoadCachedTile(tile, uri, cacheKey));
            }

            return(Task.CompletedTask);
        }
Ejemplo n.º 2
0
        private async Task LoadPendingTilesAsync(TileSource tileSource, string sourceName)
        {
            Tile tile;

            while (pendingTiles.TryPop(out tile))
            {
                tile.Pending = false;

                try
                {
                    var uri = tileSource.GetUri(tile.XIndex, tile.Y, tile.ZoomLevel);

                    if (uri != null)
                    {
                        var extension = Path.GetExtension(uri.LocalPath);

                        if (string.IsNullOrEmpty(extension) || extension == ".jpeg")
                        {
                            extension = ".jpg";
                        }

                        var cacheKey = string.Format(CacheKeyFormat, sourceName, tile.ZoomLevel, tile.XIndex, tile.Y, extension);

                        await LoadTileImageAsync(tile, uri, cacheKey);
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("TileImageLoader: {0}/{1}/{2}: {3}", tile.ZoomLevel, tile.XIndex, tile.Y, ex.Message);
                }
            }
        }
        private async Task LoadTileImageAsync(Tile tile, TileSource tileSource, string sourceName)
        {
            if (tileSource != null)
            {
                if (Cache != null &&
                    tileSource.UriFormat != null &&
                    tileSource.UriFormat.StartsWith("http") &&
                    !string.IsNullOrEmpty(sourceName))
                {
                    var uri = tileSource.GetUri(tile.XIndex, tile.Y, tile.ZoomLevel);

                    if (uri != null)
                    {
                        var extension = Path.GetExtension(uri.LocalPath);

                        if (string.IsNullOrEmpty(extension) || extension == ".jpeg")
                        {
                            extension = ".jpg";
                        }

                        var cacheKey = string.Format(CacheKeyFormat, sourceName, tile.ZoomLevel, tile.XIndex, tile.Y, extension);

                        await LoadCachedTileImageAsync(tile, uri, cacheKey).ConfigureAwait(false);
                    }
                }
                else
                {
                    await LoadTileImageAsync(tile, tileSource).ConfigureAwait(false);
                }
            }
        }
Ejemplo n.º 4
0
        private static ImageSource CreateImage(TileSource tileSource, Tile tile)
        {
            ImageSource image = null;

            try
            {
                image = BitmapFrame.Create(tileSource.GetUri(tile.XIndex, tile.Y, tile.ZoomLevel));
            }
            catch (Exception ex)
            {
                Trace.TraceWarning("Creating tile image failed: {0}", ex.Message);
            }

            return(image);
        }
Ejemplo n.º 5
0
        private void LoadPendingTiles(Dispatcher dispatcher, TileSource tileSource, string sourceName, bool animateOpacity)
        {
            var  setImageAction  = new Action <Tile, ImageSource>((t, i) => t.SetImageSource(i, animateOpacity));
            var  imageTileSource = tileSource as ImageTileSource;
            Tile tile;

            while (pendingTiles.TryDequeue(out tile))
            {
                byte[]      buffer = null;
                ImageSource image  = null;

                if (imageTileSource != null)
                {
                    image = LoadImage(imageTileSource, tile);
                }
                else
                {
                    var uri = tileSource.GetUri(tile.XIndex, tile.Y, tile.ZoomLevel);

                    if (uri != null)
                    {
                        if (uri.Scheme == "file") // create from FileStream because creating from URI leaves the file open
                        {
                            image = CreateImage(uri.LocalPath);
                        }
                        else
                        {
                            buffer = DownloadImage(uri);
                            image  = CreateImage(buffer);
                        }
                    }
                }

                if (image != null || !tile.HasImageSource) // set null image if tile does not yet have an ImageSource
                {
                    dispatcher.BeginInvoke(setImageAction, tile, image);
                }

                if (image != null && buffer != null && Cache != null && !string.IsNullOrWhiteSpace(sourceName))
                {
                    Cache.Set(TileCache.Key(sourceName, tile), buffer, new CacheItemPolicy {
                        SlidingExpiration = CacheExpiration
                    });
                }
            }

            Interlocked.Decrement(ref threadCount);
        }
Ejemplo n.º 6
0
        private void LoadPendingTiles(Dispatcher dispatcher, TileSource tileSource, string sourceName)
        {
            var         imageTileSource = tileSource as ImageTileSource;
            PendingTile pendingTile;

            while (pendingTiles.TryDequeue(out pendingTile))
            {
                var         tile  = pendingTile.Tile;
                ImageSource image = null;

                if (imageTileSource != null)
                {
                    image = LoadImage(imageTileSource, tile);
                }
                else
                {
                    var uri = tileSource.GetUri(tile.XIndex, tile.Y, tile.ZoomLevel);

                    if (uri != null)
                    {
                        if (!uri.IsAbsoluteUri)
                        {
                            image = LoadImage(uri.OriginalString);
                        }
                        else if (uri.Scheme == "file")
                        {
                            image = LoadImage(uri.LocalPath);
                        }
                        else
                        {
                            image = DownloadImage(uri, CacheKey(sourceName, tile))
                                    ?? pendingTile.CachedImage; // use possibly cached image if download failed
                        }
                    }
                }

                if (image != null)
                {
                    dispatcher.BeginInvoke(new Action <Tile, ImageSource>((t, i) => t.SetImage(i)), tile, image);
                }
                else
                {
                    tile.SetImage(null);
                }
            }

            Interlocked.Decrement(ref taskCount);
        }
Ejemplo n.º 7
0
        private async Task LoadCachedTileImageAsync(Tile tile, TileSource tileSource, string sourceName)
        {
            var uri = tileSource.GetUri(tile.XIndex, tile.Y, tile.ZoomLevel);

            if (uri != null)
            {
                var extension = Path.GetExtension(uri.LocalPath);

                if (string.IsNullOrEmpty(extension) || extension == ".jpeg")
                {
                    extension = ".jpg";
                }

                var cacheKey = string.Format(CacheKeyFormat, sourceName, tile.ZoomLevel, tile.XIndex, tile.Y, extension);

                await LoadCachedTileImageAsync(tile, uri, cacheKey);
            }
        }
        private static async Task LoadCachedTileAsync(Tile tile, TileSource tileSource, string cacheName)
        {
            var uri = tileSource.GetUri(tile.XIndex, tile.Y, tile.ZoomLevel);

            if (uri != null)
            {
                var extension = Path.GetExtension(uri.LocalPath);

                if (string.IsNullOrEmpty(extension) || extension == ".jpeg")
                {
                    extension = ".jpg";
                }

                var cacheKey = string.Format("{0}/{1}/{2}/{3}{4}", cacheName, tile.ZoomLevel, tile.XIndex, tile.Y, extension);

                await LoadCachedTileAsync(tile, uri, cacheKey).ConfigureAwait(false);
            }
        }
Ejemplo n.º 9
0
        private void LoadPendingTiles(Dispatcher dispatcher, TileSource tileSource, string sourceName, bool animateOpacity)
        {
            var setImageAction = new Action<Tile, ImageSource>((t, i) => t.SetImageSource(i, animateOpacity));
            var imageTileSource = tileSource as ImageTileSource;
            Tile tile;

            while (pendingTiles.TryDequeue(out tile))
            {
                byte[] buffer = null;
                ImageSource image = null;

                if (imageTileSource != null)
                {
                    image = LoadImage(imageTileSource, tile);
                }
                else
                {
                    var uri = tileSource.GetUri(tile.XIndex, tile.Y, tile.ZoomLevel, tile.Rotation);

                    if (uri != null)
                    {
                        if (uri.Scheme == "file") // create from FileStream because creating from URI leaves the file open
                        {
                            image = CreateImage(uri.LocalPath);
                        }
                        else
                        {
                            buffer = DownloadImage(uri);
                            image = CreateImage(buffer);
                        }
                    }
                }

                if (image != null || !tile.HasImageSource) // set null image if tile does not yet have an ImageSource
                {
                    dispatcher.BeginInvoke(setImageAction, tile, image);
                }

                if (image != null && buffer != null && Cache != null && !string.IsNullOrWhiteSpace(sourceName))
                {
                    Cache.Set(TileCache.Key(sourceName, tile), buffer, new CacheItemPolicy { SlidingExpiration = CacheExpiration });
                }
            }

            Interlocked.Decrement(ref threadCount);
        }
        private void LoadPendingTiles(Dispatcher dispatcher, TileSource tileSource, string sourceName)
        {
            var imageTileSource = tileSource as ImageTileSource;
            PendingTile pendingTile;

            while (pendingTiles.TryDequeue(out pendingTile))
            {
                var tile = pendingTile.Tile;
                ImageSource image = null;

                if (imageTileSource != null)
                {
                    image = LoadImage(imageTileSource, tile);
                }
                else
                {
                    var uri = tileSource.GetUri(tile.XIndex, tile.Y, tile.ZoomLevel);

                    if (uri != null)
                    {
                        if (!uri.IsAbsoluteUri)
                        {
                            image = LoadImage(uri.OriginalString);
                        }
                        else if (uri.Scheme == "file")
                        {
                            image = LoadImage(uri.LocalPath);
                        }
                        else
                        {
                            image = DownloadImage(uri, CacheKey(sourceName, tile))
                                ?? pendingTile.CachedImage; // use possibly cached image if download failed
                        }
                    }
                }

                if (image != null)
                {
                    dispatcher.BeginInvoke(new Action<Tile, ImageSource>((t, i) => t.SetImage(i)), tile, image);
                }
                else
                {
                    tile.SetImage(null);
                }
            }

            Interlocked.Decrement(ref taskCount);
        }