Beispiel #1
0
        private async Task <IActionResult> GetTileAsync(string tileset, int tileCol, int tileRow, int tileMatrix, string mediaType)
        {
            var tileSource = this.tileSourceFabric.Get(tileset);

            if (!WebMercator.IsInsideBBox(tileCol, tileRow, tileMatrix, tileSource.Configuration.Srs))
            {
                return(ResponseWithNotFoundError(Identifiers.NotFound, "The requested tile is outside the bounding box of the tile map."));
            }

            var data = await tileSource.GetTileAsync(tileCol, WebMercator.FlipYCoordinate(tileRow, tileMatrix), tileMatrix); // In WMTS Y axis goes down from the top

            if (data != null && data.Length > 0)
            {
                if (String.Compare(mediaType, tileSource.Configuration.ContentType, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return(File(data, mediaType)); // Return original source image
                }
                else
                {
                    var isFormatSupported = Utils.EntitiesConverter.IsFormatInList(
                        new[]
                    {
                        MediaTypeNames.Image.Png,
                        MediaTypeNames.Image.Jpeg,
                        MediaTypeNames.Image.Webp,
                    },
                        mediaType);

                    // Convert source image to requested output format, if possible
                    if (isFormatSupported)
                    {
                        var outputImage = ImageHelper.ConvertImageToFormat(data, mediaType, 90); // TODO: quality parameter
                        if (outputImage != null)
                        {
                            return(File(outputImage, mediaType));
                        }
                        else
                        {
                            return(ResponseWithNotFoundError(Identifiers.NotFound, "Specified tile was not found"));
                        }
                    }
                    else
                    {
                        return(File(data, mediaType)); // Conversion not possible
                    }
                }
            }
            else
            {
                return(ResponseWithNotFoundError(Identifiers.NotFound, "Specified tile was not found"));
            }
        }
        public async Task <IActionResult> GetTileAsync(string tileset, int x, int y, int z, string extension)
        {
            // TODO: z can be a string, not integer number
            if (String.IsNullOrEmpty(tileset) || String.IsNullOrEmpty(extension))
            {
                return(BadRequest());
            }

            if (this.tileSourceFabric.Contains(tileset))
            {
                // TODO: ? convert source format to requested output format
                var tileSource = this.tileSourceFabric.Get(tileset);

                if (!WebMercator.IsInsideBBox(x, y, z, tileSource.Configuration.Srs))
                {
                    return(ResponseWithNotFoundError("The requested tile is outside the bounding box of the tile map."));
                }

                var data = await tileSource.GetTileAsync(x, y, z);

                if (data != null && data.Length > 0)
                {
                    var mediaType = U.EntitiesConverter.ExtensionToMediaType(extension);

                    if (String.Compare(mediaType, tileSource.Configuration.ContentType, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        return(File(data, mediaType)); // Return original source image
                    }
                    else
                    {
                        var isFormatSupported = U.EntitiesConverter.IsFormatInList(
                            new[]
                        {
                            MediaTypeNames.Image.Png,
                            MediaTypeNames.Image.Jpeg,
                            MediaTypeNames.Image.Webp,
                        },
                            mediaType);

                        // Convert source image to requested output format, if possible
                        if (isFormatSupported)
                        {
                            var outputImage = U.ImageHelper.ConvertImageToFormat(data, mediaType, 90); // TODO: quality parameter
                            if (outputImage != null)
                            {
                                return(File(outputImage, mediaType));
                            }
                            else
                            {
                                return(NotFound());
                            }
                        }
                        else
                        {
                            return(File(data, mediaType)); // Conversion not possible
                        }
                    }
                }
                else
                {
                    return(NotFound());
                }
            }
            else
            {
                return(NotFound($"Specified tileset '{tileset}' not found"));
            }
        }