Example #1
0
        Task ITileSource.InitAsync()
        {
            // Configuration values priority:
            // 1. Default values for MBTiles.
            // 2. Actual values (MBTiles metadata).
            // 3. Values from configuration file - overrides given above, if provided.

            this.repository = new MBTiles.Repository(configuration.Location, false);
            var metadata = new MBTiles.Metadata(this.repository.ReadMetadata());

            var title = String.IsNullOrEmpty(this.configuration.Title) ?
                        (!String.IsNullOrEmpty(metadata.Name) ? metadata.Name : this.configuration.Id) :
                        this.configuration.Title;

            var format = String.IsNullOrEmpty(this.configuration.Format) ?
                         (!String.IsNullOrEmpty(metadata.Format) ? metadata.Format : "png") :
                         this.configuration.Format;

            // Re-create configuration
            this.configuration = new TileSourceConfiguration
            {
                Id          = this.configuration.Id,
                Type        = this.configuration.Type,
                Format      = format,
                Title       = title,
                Tms         = this.configuration.Tms ?? true, // Default true for the MBTiles, following the Tile Map Service Specification.
                Location    = this.configuration.Location,
                ContentType = Utils.TileFormatToContentType(format),
                MinZoom     = this.configuration.MinZoom ?? metadata.MinZoom ?? 0, // TODO: ? Check actual SELECT MIN/MAX(zoom_level) ?
                MaxZoom     = this.configuration.MaxZoom ?? metadata.MaxZoom ?? 24,
            };

            return(Task.CompletedTask);
        }
        Task ITileSource.InitAsync()
        {
            // Configuration values priority:
            // 1. Default values for MBTiles source type.
            // 2. Actual values (MBTiles metadata table values).
            // 3. Values from configuration file - overrides given above, if provided.

            if (String.IsNullOrEmpty(this.configuration.Location))
            {
                throw new InvalidOperationException("configuration.Location is null or empty");
            }

            this.repository = new MBTiles.Repository(configuration.Location, false);
            var metadata = new MBTiles.Metadata(this.repository.ReadMetadata());

            var title = String.IsNullOrEmpty(this.configuration.Title) ?
                        (!String.IsNullOrEmpty(metadata.Name) ? metadata.Name : this.configuration.Id) :
                        this.configuration.Title;

            var format = String.IsNullOrEmpty(this.configuration.Format) ?
                         (!String.IsNullOrEmpty(metadata.Format) ? metadata.Format : ImageFormats.Png) :
                         this.configuration.Format;

            // Get tile width and height from first tile, for raster formats
            var tileWidth  = Utils.WebMercator.DefaultTileWidth;
            var tileHeight = Utils.WebMercator.DefaultTileHeight;
            var firstTile  = this.repository.ReadFirstTile();

            if (firstTile != null && (format == ImageFormats.Png || format == ImageFormats.Jpeg))
            {
                var size = Utils.ImageHelper.GetImageSize(firstTile);
                if (size.HasValue)
                {
                    tileWidth  = size.Value.Width;
                    tileHeight = size.Value.Height;
                }
            }

            var minZoom = this.configuration.MinZoom ?? metadata.MinZoom ?? null;
            var maxZoom = this.configuration.MaxZoom ?? metadata.MaxZoom ?? null;

            if (minZoom == null || maxZoom == null)
            {
                var zoomRange = this.repository.GetZoomLevelRange();
                if (zoomRange.HasValue)
                {
                    minZoom = zoomRange.Value.Min;
                    maxZoom = zoomRange.Value.Max;
                }
                else
                {
                    minZoom = 0;
                    maxZoom = 20;
                }
            }

            // Re-create configuration
            this.configuration = new SourceConfiguration
            {
                Id                 = this.configuration.Id,
                Type               = this.configuration.Type,
                Format             = format,
                Title              = title,
                Abstract           = this.configuration.Abstract,
                Tms                = this.configuration.Tms ?? true, // Default true for the MBTiles, following the Tile Map Service Specification.
                Srs                = Utils.SrsCodes.EPSG3857,        // MBTiles supports only Spherical Mercator tile grid
                Location           = this.configuration.Location,
                ContentType        = Utils.EntitiesConverter.TileFormatToContentType(format),
                MinZoom            = minZoom,
                MaxZoom            = maxZoom,
                GeographicalBounds = metadata.Bounds, // Can be null, if no corresponding record in 'metadata' table
                TileWidth          = tileWidth,
                TileHeight         = tileHeight,
                Cache              = null, // Not used for MBTiles source
            };

            return(Task.CompletedTask);
        }