Exemplo n.º 1
0
        private async Task <ActionResult> GetTile(string serviceName, GetTile getTile)
        {
            ActionResult    result     = null;
            ExceptionReport exception  = null;
            int             statusCode = 200;

            #region Validate parameters
            if (string.IsNullOrWhiteSpace(serviceName))
            {
                exception  = ExceptionReportHelper.GetExceptionReport("MissingParameterValue", "serviceName", exceptionText: "Bad request");
                statusCode = 400;
                goto Exception;
            }
            string serviceType = getTile.service;
            if (string.IsNullOrWhiteSpace(serviceType))
            {
                exception  = ExceptionReportHelper.GetExceptionReport("MissingParameterValue", "service", exceptionText: "Bad request");
                statusCode = 400;
                goto Exception;
            }
            if (serviceType.ToLower() != "wmts")
            {
                exception  = ExceptionReportHelper.GetExceptionReport("InvalidParameterValue", "version", exceptionText: "Bad request");
                statusCode = 400;
                goto Exception;
            }
            string version = getTile.version;
            if (string.IsNullOrWhiteSpace(version))
            {
                exception  = ExceptionReportHelper.GetExceptionReport("MissingParameterValue", "version", exceptionText: "Bad request");
                statusCode = 400;
                goto Exception;
            }
            if (version != "1.0.0")
            {
                exception  = ExceptionReportHelper.GetExceptionReport("InvalidParameterValue", "version", exceptionText: "Bad request");
                statusCode = 400;
                goto Exception;
            }
            string layerName = getTile.Layer;
            if (string.IsNullOrWhiteSpace(layerName))
            {
                exception  = ExceptionReportHelper.GetExceptionReport("MissingParameterValue", "layer", exceptionText: "Bad request");
                statusCode = 400;
                goto Exception;
            }
            string style = getTile.Style;
            if (string.IsNullOrWhiteSpace(style))
            {
                exception  = ExceptionReportHelper.GetExceptionReport("MissingParameterValue", "style", exceptionText: "Bad request");
                statusCode = 400;
                goto Exception;
            }
            string format = getTile.Format;
            if (string.IsNullOrWhiteSpace(format))
            {
                exception  = ExceptionReportHelper.GetExceptionReport("MissingParameterValue", "format", exceptionText: "Bad request");
                statusCode = 400;
                goto Exception;
            }
            string tileMatrixSet = getTile.TileMatrixSet;
            if (string.IsNullOrWhiteSpace(tileMatrixSet))
            {
                exception  = ExceptionReportHelper.GetExceptionReport("MissingParameterValue", "tileMatrixSet", exceptionText: "Bad request");
                statusCode = 400;
                goto Exception;
            }
            string tileMatrix = getTile.TileMatrix;
            if (string.IsNullOrWhiteSpace(tileMatrix))
            {
                exception  = ExceptionReportHelper.GetExceptionReport("MissingParameterValue", "tileMatrix", exceptionText: "Bad request");
                statusCode = 400;
                goto Exception;
            }
            ServiceRecord serviceRecord = await GetServiceRecord(serviceName, version);

            if (serviceRecord == null)
            {
                exception  = ExceptionReportHelper.GetExceptionReport("InvalidParameterValue", exceptionText: "Bad request");
                statusCode = 400;
                goto Exception;
            }
            if (!System.IO.File.Exists(serviceRecord.Path))
            {
                exception  = ExceptionReportHelper.GetExceptionReport("NoApplicableCode", exceptionText: "Internal server error");
                statusCode = 500;
                goto Exception;
            }
            LayerRecord layerRecord = await GetLayerRecord(serviceRecord, layerName);

            if (layerRecord == null)
            {
                exception  = ExceptionReportHelper.GetExceptionReport("InvalidParameterValue", exceptionText: "Bad request");
                statusCode = 400;
                goto Exception;
            }
            if (!System.IO.File.Exists(layerRecord.Path))
            {
                exception  = ExceptionReportHelper.GetExceptionReport("NoApplicableCode", exceptionText: "Internal server error");
                statusCode = 500;
                goto Exception;
            }
            #endregion
            GetCapabilities getCapabilities = new GetCapabilities()
            {
                AcceptVersions = new string[] { version }
            };
            IWmtsService wmts1Service = GetWmts1Service(version);
            Capabilities capabilities = wmts1Service.GetCapabilities(serviceRecord.Path, getCapabilities);
            if (capabilities == null)
            {
                exception  = ExceptionReportHelper.GetExceptionReport("NoApplicableCode", exceptionText: "Internal server error");
                statusCode = 500;
                goto Exception;
            }
            LayerType layerType = capabilities.Contents?.DatasetDescriptionSummary?.FirstOrDefault(x => x.Identifier?.Value == layerName && x is LayerType) as LayerType;
            if (layerType == null)
            {
                exception  = ExceptionReportHelper.GetExceptionReport("NoApplicableCode", exceptionText: "Internal server error");
                statusCode = 500;
                goto Exception;
            }
            if (!layerType.Format.Contains(format))
            {
                exception  = ExceptionReportHelper.GetExceptionReport("InvalidParameterValue", "format", exceptionText: "Bad request");
                statusCode = 400;
                goto Exception;
            }
            if (layerType.TileMatrixSetLink == null || layerType.TileMatrixSetLink.Length == 0)
            {
                exception  = ExceptionReportHelper.GetExceptionReport("NoApplicableCode", exceptionText: "Internal server error");
                statusCode = 500;
                goto Exception;
            }
            string layerTileMatrixSetStr = layerType.TileMatrixSetLink[0].TileMatrixSet;
            if (layerTileMatrixSetStr != null)
            {
                TileMatrixSet layerTileMatrixSet = capabilities.Contents.TileMatrixSet?.FirstOrDefault(x => x.Identifier?.Value == layerTileMatrixSetStr);
                if (layerTileMatrixSet != null)
                {
                    TileMatrix layerTileMatrix = layerTileMatrixSet.TileMatrix?.FirstOrDefault(x => x.Identifier.Value == getTile.TileMatrix);
                    if (layerTileMatrix != null)
                    {
                        bool ret = layerTileMatrix.TopLeftCorner.ToPosition(out double left, out double top);
                        if (!ret)
                        {
                            exception  = ExceptionReportHelper.GetExceptionReport("NoApplicableCode", exceptionText: "Internal server error");
                            statusCode = 500;
                            goto Exception;
                        }
                        BoundingBoxType boundingBoxType = layerType.BoundingBox?.Length > 0 ? layerType.BoundingBox[0] : layerType.WGS84BoundingBox?.Length > 0 ? layerType.WGS84BoundingBox[0] : null;
                        if (boundingBoxType == null)
                        {
                            exception  = ExceptionReportHelper.GetExceptionReport("NoApplicableCode", exceptionText: "Internal server error");
                            statusCode = 500;
                            goto Exception;
                        }
                        bool ret1 = boundingBoxType.LowerCorner.ToPosition(out double xMin, out double yMin);
                        bool ret2 = boundingBoxType.UpperCorner.ToPosition(out double xMax, out double yMax);
                        if (!ret1 || !ret2)
                        {
                            exception  = ExceptionReportHelper.GetExceptionReport("NoApplicableCode", exceptionText: "Internal server error");
                            statusCode = 500;
                            goto Exception;
                        }
                        double tileXMin = 0, tileYMin = 0, tileXMax = 0, tileYMax = 0;
                        int    tileWidth  = Convert.ToInt32(layerTileMatrix.TileWidth);
                        int    tileHeight = Convert.ToInt32(layerTileMatrix.TileHeight);
                        tileXMin = left + getTile.TileCol * tileWidth * layerTileMatrix.ScaleDenominator;
                        tileXMax = left + (getTile.TileCol + 1) * tileWidth * layerTileMatrix.ScaleDenominator;
                        tileYMax = top - getTile.TileRow * tileHeight * layerTileMatrix.ScaleDenominator;
                        tileYMin = top - (getTile.TileRow + 1) * tileHeight * layerTileMatrix.ScaleDenominator;
                        if (tileXMax <= xMin || tileXMin >= xMax || tileYMax <= yMin || tileYMin >= yMax)
                        {
                            exception  = ExceptionReportHelper.GetExceptionReport("TileOutOfRange", exceptionText: "Bad request");
                            statusCode = 400;
                            goto Exception;
                        }
                    }
                }
            }
            byte[] tileBuffer = wmts1Service.GetTile(capabilities, layerRecord.Path, getTile);
            result = new FileContentResult(tileBuffer, getTile.Format);
            goto Success;
Exception:
            string content = XmlHelper.XmlSerialize(exception, Encoding, null);
            result         = new ContentResult()
            {
                StatusCode = statusCode,
                Content    = content
            };
Success:
            return(result);
        }
        private async Task <ActionResult> GetTile(string serviceName, GetTile getTile)
        {
            ActionResult    result     = null;
            ExceptionReport exception  = null;
            int             statusCode = 200;

            #region Validate parameters
            if (string.IsNullOrWhiteSpace(serviceName))
            {
                exception  = ExceptionReportHelper.GetExceptionReport("MissingParameterValue", "serviceName", exceptionText: "Bad request");
                statusCode = 400;
                goto Exception;
            }
            string serviceType = getTile.service;
            if (string.IsNullOrWhiteSpace(serviceType))
            {
                exception  = ExceptionReportHelper.GetExceptionReport("MissingParameterValue", "service", exceptionText: "Bad request");
                statusCode = 400;
                goto Exception;
            }
            if (serviceType.ToLower() != "wmts")
            {
                exception  = ExceptionReportHelper.GetExceptionReport("InvalidParameterValue", "version", exceptionText: "Bad request");
                statusCode = 400;
                goto Exception;
            }
            string version = getTile.version;
            if (string.IsNullOrWhiteSpace(version))
            {
                exception  = ExceptionReportHelper.GetExceptionReport("MissingParameterValue", "version", exceptionText: "Bad request");
                statusCode = 400;
                goto Exception;
            }
            if (version != "1.0.0")
            {
                exception  = ExceptionReportHelper.GetExceptionReport("InvalidParameterValue", "version", exceptionText: "Bad request");
                statusCode = 400;
                goto Exception;
            }
            string layerName = getTile.Layer;
            if (string.IsNullOrWhiteSpace(layerName))
            {
                exception  = ExceptionReportHelper.GetExceptionReport("MissingParameterValue", "layer", exceptionText: "Bad request");
                statusCode = 400;
                goto Exception;
            }
            string style = getTile.Style;
            if (string.IsNullOrWhiteSpace(style))
            {
                exception  = ExceptionReportHelper.GetExceptionReport("MissingParameterValue", "style", exceptionText: "Bad request");
                statusCode = 400;
                goto Exception;
            }
            string format = getTile.Format;
            if (string.IsNullOrWhiteSpace(format))
            {
                exception  = ExceptionReportHelper.GetExceptionReport("MissingParameterValue", "format", exceptionText: "Bad request");
                statusCode = 400;
                goto Exception;
            }
            string tileMatrixSet = getTile.TileMatrixSet;
            if (string.IsNullOrWhiteSpace(tileMatrixSet))
            {
                exception  = ExceptionReportHelper.GetExceptionReport("MissingParameterValue", "tileMatrixSet", exceptionText: "Bad request");
                statusCode = 400;
                goto Exception;
            }
            string tileMatrix = getTile.TileMatrix;
            if (string.IsNullOrWhiteSpace(tileMatrix))
            {
                exception  = ExceptionReportHelper.GetExceptionReport("MissingParameterValue", "tileMatrix", exceptionText: "Bad request");
                statusCode = 400;
                goto Exception;
            }
            ServiceRecord serviceRecord = await GetServiceRecord(serviceName, version);

            if (serviceRecord == null)
            {
                exception  = ExceptionReportHelper.GetExceptionReport("InvalidParameterValue", exceptionText: "Bad request");
                statusCode = 400;
                goto Exception;
            }
            if (!System.IO.File.Exists(serviceRecord.Path))
            {
                exception  = ExceptionReportHelper.GetExceptionReport("NoApplicableCode", exceptionText: "Internal server error");
                statusCode = 500;
                goto Exception;
            }
            LayerRecord layerRecord = await GetLayerRecord(serviceRecord, layerName);

            if (layerRecord == null)
            {
                exception  = ExceptionReportHelper.GetExceptionReport("InvalidParameterValue", exceptionText: "Bad request");
                statusCode = 400;
                goto Exception;
            }
            #endregion
            GetCapabilities getCapabilities = new GetCapabilities(version);
            IWmtsService    wmts1Service    = GetWmts1Service(version);
            Capabilities    capabilities    = wmts1Service.GetCapabilities(serviceRecord.Path, getCapabilities);
            if (capabilities == null)
            {
                exception  = ExceptionReportHelper.GetExceptionReport("NoApplicableCode", exceptionText: "Internal server error");
                statusCode = 500;
                goto Exception;
            }
            LayerType layerType = capabilities.GetLayerType(layerRecord.Name);
            if (layerType == null)
            {
                exception  = ExceptionReportHelper.GetExceptionReport("NoApplicableCode", exceptionText: "Internal server error");
                statusCode = 500;
                goto Exception;
            }
            if (!layerType.Format.Contains(format))
            {
                exception  = ExceptionReportHelper.GetExceptionReport("InvalidParameterValue", "format", exceptionText: "Bad request");
                statusCode = 400;
                goto Exception;
            }
            if (layerType.TileMatrixSetLink == null || layerType.TileMatrixSetLink.Length == 0)
            {
                exception  = ExceptionReportHelper.GetExceptionReport("NoApplicableCode", exceptionText: "Internal server error");
                statusCode = 500;
                goto Exception;
            }
            string layerTileMatrixSetStr = layerType.TileMatrixSetLink[0].TileMatrixSet;
            if (layerTileMatrixSetStr != null)
            {
                TileMatrixSet layerTileMatrixSet = capabilities.Contents.TileMatrixSet?.FirstOrDefault(x => x.Identifier?.Value == layerTileMatrixSetStr);
                if (layerTileMatrixSet != null)
                {
                    TileMatrix layerTileMatrix = layerTileMatrixSet.TileMatrix?.FirstOrDefault(x => x.Identifier.Value == getTile.TileMatrix);
                    if (layerTileMatrix != null)
                    {
                        bool ret = layerTileMatrix.TopLeftCorner.ToPosition(out double left, out double top);
                        if (!ret)
                        {
                            exception  = ExceptionReportHelper.GetExceptionReport("NoApplicableCode", exceptionText: "Internal server error");
                            statusCode = 500;
                            goto Exception;
                        }
                        BoundingBoxType boundingBoxType = layerType.BoundingBox?.Length > 0 ? layerType.BoundingBox[0] : layerType.WGS84BoundingBox?.Length > 0 ? layerType.WGS84BoundingBox[0] : null;
                        if (boundingBoxType == null)
                        {
                            exception  = ExceptionReportHelper.GetExceptionReport("NoApplicableCode", exceptionText: "Internal server error");
                            statusCode = 500;
                            goto Exception;
                        }
                        bool ret0 = boundingBoxType.LowerCorner.ToPosition(out double xMin, out double yMin);
                        bool ret1 = boundingBoxType.UpperCorner.ToPosition(out double xMax, out double yMax);
                        if (!ret0 || !ret1)
                        {
                            exception  = ExceptionReportHelper.GetExceptionReport("NoApplicableCode", exceptionText: "Internal server error");
                            statusCode = 500;
                            goto Exception;
                        }
                        bool isDegree = layerTileMatrixSet.GetIsDegreeByLocalDb();
                        layerTileMatrix.GetTileIndex(isDegree, xMin, yMax, out int startCol, out int startRow);
                        int matrixWidth  = Convert.ToInt32(layerTileMatrix.MatrixWidth);
                        int matrixHeight = Convert.ToInt32(layerTileMatrix.MatrixHeight);
                        if (getTile.TileCol < startCol || getTile.TileCol >= startCol + matrixWidth || getTile.TileRow < startRow || getTile.TileRow >= startRow + matrixHeight)
                        {
                            exception  = ExceptionReportHelper.GetExceptionReport("TileOutOfRange", exceptionText: "Bad request");
                            statusCode = 400;
                            goto Exception;
                        }
                    }
                }
            }
            try
            {
                byte[] tileBuffer = wmts1Service.GetTile(capabilities, layerRecord.Path, getTile);
                result = new FileContentResult(tileBuffer, getTile.Format);
            }
            catch (Exception e)
            {
                Debug.WriteLine($"获取瓦片{getTile.TileMatrix}_{getTile.TileCol}_{getTile.TileRow}失败:{e.Message}");
            }
            goto Success;
Exception:
            string content = XmlHelper.XmlSerialize(exception, Encoding, null);
            result         = new ContentResult()
            {
                StatusCode = statusCode,
                Content    = content
            };
Success:
            return(result);
        }