private static Extent ToExtent(TileMatrixSet tileMatrixSet, WmtsTileSchema tileSchema, int[] ordinateOrder) { var boundingBox = tileMatrixSet.Item; if (boundingBox != null) { // the BoundingBox element should always be in the same CRS as the SupportedCRS, we make a check anyway if (string.IsNullOrEmpty(boundingBox.crs) || boundingBox.crs == tileMatrixSet.SupportedCRS) { var lowerCorner = boundingBox.LowerCorner; var upperCorner = boundingBox.UpperCorner; var lowerCornerDimensions = GetDimensions(lowerCorner); var upperCornerDimensions = GetDimensions(upperCorner); var xi = ordinateOrder[0]; var yi = ordinateOrder[1]; return(new Extent( lowerCornerDimensions[xi], lowerCornerDimensions[yi], upperCornerDimensions[xi], upperCornerDimensions[yi])); } } // Compute the extent of the tile schema var resolution = tileSchema.Resolutions.Last(); return(ToExtent(resolution.Value)); }
public static Coordinate GetTopLeftCorner(this TileMatrixSet tileMatrixSet, bool useLatLon) { Coordinate topLeftCorner = null; if (tileMatrixSet.TileMatrix?.Length > 0) { var coordStr = tileMatrixSet.TileMatrix[0].TopLeftCorner; string[] array = coordStr.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); bool ret = false; double x, y; if (useLatLon) { ret = GetCoordinate(coordStr, out y, out x); } else { ret = GetCoordinate(coordStr, out x, out y); } if (ret) { topLeftCorner = new Coordinate(x, y); } } return(topLeftCorner); }
public override byte[] GetTile(Capabilities capabilities, string path, GetTile getTile) { byte[] buffer = null; if (capabilities == null || string.IsNullOrWhiteSpace(path) || getTile == null) { return(buffer); } IMapLayer mapLayer = DataHelper.GetMapLayer(path); if (mapLayer != null) { LayerType layerType = capabilities.Contents?.DatasetDescriptionSummary?.FirstOrDefault(x => x.Identifier?.Value == getTile.Layer && x is LayerType) as LayerType; if (layerType != null) { BoundingBoxType boundingBoxType = layerType.BoundingBox?.Length > 0 ? layerType.BoundingBox[0] : layerType.WGS84BoundingBox?.Length > 0 ? layerType.WGS84BoundingBox[0] : null; string layerTileMatrixSetStr = layerType.TileMatrixSetLink[0].TileMatrixSet; TileMatrixSet layerTileMatrixSet = capabilities.Contents.TileMatrixSet?.FirstOrDefault(x => x.Identifier?.Value == layerTileMatrixSetStr); TileMatrix layerTileMatrix = layerTileMatrixSet.TileMatrix?.FirstOrDefault(x => x.Identifier.Value == getTile.TileMatrix); if (layerTileMatrix != null) { bool ret = WmtsHelper.ComputeTileBoundary(layerType, layerTileMatrix, getTile.TileCol, getTile.TileRow, out double tileXMin, out double tileYMin, out double tileXMax, out double tileYMax); if (ret) { Extent extent = new Extent(tileXMin, tileYMin, tileXMax, tileYMax); int tileWidth = Convert.ToInt32(layerTileMatrix.TileWidth); int tileHeight = Convert.ToInt32(layerTileMatrix.TileHeight); Rectangle rectangle = new Rectangle(0, 0, tileWidth, tileHeight); using (Bitmap bmp = GetBitmap(mapLayer, extent, rectangle)) { ImageFormat imageFormat = null; if (string.IsNullOrWhiteSpace(getTile.Format) || !getTile.Format.Contains('/')) { imageFormat = ImageFormat.Png; } else { string[] formatMimeArray = getTile.Format.Split('/'); switch (formatMimeArray[1]) { case "jpg": imageFormat = ImageFormat.Jpeg; break; default: imageFormat = ImageFormat.Png; break; } } buffer = GetBitmapBuffer(bmp, imageFormat); } } } } mapLayer.Dispose(); } return(buffer); }
private static IEnumerable <TileMatrix> SetLevelOnTileTileMatrix(TileMatrixSet tileMatrixSet) { var tileMatrices = tileMatrixSet.TileMatrix.OrderByDescending(m => m.ScaleDenominator); var count = 0; foreach (var tileMatrix in tileMatrices) { tileMatrix.Level = count; count++; } return(tileMatrices.ToList()); }
public static LayerType AddContent(Capabilities capabilities, IDataSet dataSet) { LayerType layerType = null; if (capabilities == null || dataSet == null) { return(layerType); } if (capabilities.Contents == null) { capabilities.Contents = new ContentsType(); } DatasetDescriptionSummaryBaseType[] datasets = capabilities.Contents.DatasetDescriptionSummary; if (datasets?.Any(x => x.Identifier.Value == dataSet.Name) == true) { return(layerType); } int layerCount = datasets == null ? 1 : datasets.Length + 1; capabilities.Contents.DatasetDescriptionSummary = new DatasetDescriptionSummaryBaseType[layerCount]; datasets?.CopyTo(capabilities.Contents.DatasetDescriptionSummary, 0); datasets = capabilities.Contents.DatasetDescriptionSummary; layerType = GetLayerType(dataSet); datasets[datasets.Length - 1] = layerType; TileMatrixSet[] tileMatrixSets = capabilities.Contents.TileMatrixSet; if (tileMatrixSets?.Any(x => x.Identifier.Value == dataSet.Projection.Name) != true) { int tileMatrixSetCount = tileMatrixSets == null ? 1 : tileMatrixSets.Length + 1; capabilities.Contents.TileMatrixSet = new TileMatrixSet[tileMatrixSetCount]; tileMatrixSets?.CopyTo(capabilities.Contents.TileMatrixSet, 0); tileMatrixSets = capabilities.Contents.TileMatrixSet; int minLevel = 0; int maxLevel = 20; TileMatrix[] tileMatrices = GetTileMatrices(dataSet, minLevel, maxLevel); TileMatrixSet tileMatrixSet = new TileMatrixSet() { Identifier = new CodeType() { Value = dataSet.Projection.Name }, SupportedCRS = "urn:ogc:def:crs:OGC:1.3:CRS84",//TODO 待修改 TileMatrix = tileMatrices }; tileMatrixSets[tileMatrixSets.Length - 1] = tileMatrixSet; } return(layerType); }
public static int GetEpsg(this TileMatrixSet tileMatrixSet) { int epsg = -1; string supportedCRS = tileMatrixSet.SupportedCRS; if (!string.IsNullOrEmpty(supportedCRS)) { supportedCRS = supportedCRS.Replace("urn:ogc:def:crs:", ""); string[] array = supportedCRS.Split(new string[] { "::" }, StringSplitOptions.RemoveEmptyEntries); if (array.Length == 2 && array[0] == "EPSG") { int.TryParse(array[1], out epsg); } } return(epsg); }
public static List <double> GetResolutions(this TileMatrixSet tileMatrixSet, IEnumerable <string> tileMatrixIdentifiers) { List <double> resolutions = new List <double>(); bool isDegree = tileMatrixSet.GetIsDegreeByLocalDb(); foreach (var tileMatrixIdentifier in tileMatrixIdentifiers) { TileMatrix tileMatrix = tileMatrixSet.TileMatrix.FirstOrDefault(x => x.Identifier.Value == tileMatrixIdentifier); if (tileMatrix != null) { double res = tileMatrix.GetResolution(isDegree); resolutions.Add(res); } } return(resolutions); }
public static bool GetIsDegreeByLocalDb(this TileMatrixSet tileMatrixSet) { bool ret = false; var SupportedCRS = tileMatrixSet.SupportedCRS; if (string.IsNullOrEmpty(SupportedCRS) || !SupportedCRS.Contains(":EPSG:")) { return(ret); } string[] array = SupportedCRS.Split(':'); bool convertResult = int.TryParse(array[array.Length - 1], out int epsg); String projcs = SpatialReferenceHelper.GetProjcs(epsg); String geogcs = SpatialReferenceHelper.GetGeogcs(epsg); ret = string.IsNullOrEmpty(projcs) && !string.IsNullOrEmpty(geogcs); return(ret); }
public static TileMatrixSet FromBaseObject(BaseObject baseObj) { if (baseObj == null || baseObj.NativeObject == IntPtr.Zero) { return(null); } TileMatrixSet obj = baseObj as TileMatrixSet; if (object.Equals(obj, null)) { obj = new TileMatrixSet(CreatedWhenConstruct.CWC_NotToCreate); obj.BindNativeObject(baseObj.NativeObject, "CTileMatrixSet"); obj.IncreaseCast(); } return(obj); }
private async Task <ContentResult> GetFeatureInfo(string serviceName, GetFeatureInfo getFeatureInfo) { GetTile getTile = getFeatureInfo.GetTile; string content = 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 != "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(); 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; } if (getFeatureInfo.I < 0 || getFeatureInfo.I >= tileWidth || getFeatureInfo.J < 0 || getFeatureInfo.J >= tileHeight) { exception = ExceptionReportHelper.GetExceptionReport("PointIJOutOfRange", exceptionText: "Bad request"); statusCode = 400; goto Exception; } } } } FeatureInfoResponse featureInfoResponse = wmts1Service.GetFeatureInfo(serviceRecord.Path, getFeatureInfo); content = XmlHelper.XmlSerialize(featureInfoResponse, Encoding, null); goto Success; Exception: content = XmlHelper.XmlSerialize(exception, Encoding, null); Success: ContentResult result = new ContentResult() { StatusCode = statusCode, Content = content }; return(result); }
public BaseObject Create() { TileMatrixSet emptyInstance = new TileMatrixSet(CreatedWhenConstruct.CWC_NotToCreate); return(emptyInstance); }
public async Task <IActionResult> Preview(int?id, int width, int height) { if (id == null) { return(NotFound()); } var layerRecord = await ConfigContext.Layers .Include(l => l.Service) .FirstOrDefaultAsync(m => m.Id == id); if (layerRecord == null) { return(NotFound()); } ServiceRecord serviceRecord = layerRecord.Service; View view = new View(); OpenLayers.layer.BaseLayer baseLayer = null; switch (serviceRecord.Type) { case OgcServiceType.Wmts: GdalWmtsService gdalWmtsService = new GdalWmtsService(); string capabilitiesPath = ServicePathManager.GetCapabilitiesPath(serviceRecord.Type, serviceRecord.Version, serviceRecord.Name); Capabilities capabilities = null; using (StreamReader sr = new StreamReader(capabilitiesPath)) { capabilities = gdalWmtsService.XmlDeSerialize(sr); } LayerType layerType = capabilities.GetLayerType(layerRecord.Name); string format = layerType.Format.FirstOrDefault(); string destTileMatrixSetName = layerType.TileMatrixSetLink.FirstOrDefault()?.TileMatrixSet; TileMatrixSet tileMatrixSet = capabilities.GetTileMatrixSet(destTileMatrixSetName); double[] origin = tileMatrixSet.TileMatrix.FirstOrDefault()?.TopLeftCorner?.ToDoubleValues(); string[] matrixIds = tileMatrixSet.TileMatrix.Select(x => x.Identifier.Value).ToArray(); double[] resolutions = tileMatrixSet.GetResolutions(matrixIds).ToArray(); var bound = layerType.BoundingBox.FirstOrDefault(); double[] lowerCorner = bound.LowerCorner.ToDoubleValues(); double[] upperCorner = bound.UpperCorner.ToDoubleValues(); double xmin = lowerCorner[0]; double ymin = lowerCorner[1]; double xmax = upperCorner[0]; double ymax = upperCorner[1]; double[] extent = new double[] { xmin, ymin, xmax, ymax }; double centerX = (xmin + xmax) / 2; double centerY = (ymin + ymax) / 2; view.center = new double[] { centerX, centerY }; view.extent = extent; view.resolution = TileMatrixSet.GetSuitableResolution(resolutions.ToList(), xmin, ymin, xmax, ymax, width, height); view.minResolution = resolutions[resolutions.Length - 1]; view.maxResolution = resolutions[0]; if (!string.IsNullOrEmpty(tileMatrixSet.SupportedCRS)) { string[] array = tileMatrixSet.SupportedCRS.Split("EPSG"); if (array.Length > 0) { string epsg = array[array.Length - 1].Replace(":", ""); view.projection = $"EPSG:{epsg}"; } } WmtsTileGrid tileGrid = new WmtsTileGrid() { origin = origin, matrixIds = matrixIds, resolutions = resolutions, extent = extent }; OpenLayers.source.Wmts source = new OpenLayers.source.Wmts() { url = capabilities.GetHref(), layer = layerRecord.Name, tileGrid = tileGrid, matrixSet = destTileMatrixSetName, format = format }; baseLayer = new OpenLayers.layer.TileLayer() { source = source }; break; default: return(NotFound()); } Map map = new Map() { layers = new OpenLayers.layer.BaseLayer[] { baseLayer }, view = view }; return(new JsonResult(map)); //PreviewModel previewModel = new PreviewModel() //{ // Layers = new OpenLayers.layer.BaseLayer[] { baseLayer }, // View = view //}; //return View(previewModel); }
public static LayerType AddToCapabilities(Capabilities capabilities, string name, string projectionStr, double xMin, double yMin, double xMax, double yMax) { LayerType layerType = null; if (capabilities == null || capabilities == null) { return(layerType); } if (capabilities.Contents == null) { capabilities.Contents = new ContentsType(); } DatasetDescriptionSummaryBaseType[] datasets = capabilities.Contents.DatasetDescriptionSummary; if (datasets?.Any(x => x.Identifier.Value == name) == true) { return(layerType); } int layerCount = datasets == null ? 1 : datasets.Length + 1; capabilities.Contents.DatasetDescriptionSummary = new DatasetDescriptionSummaryBaseType[layerCount]; datasets?.CopyTo(capabilities.Contents.DatasetDescriptionSummary, 0); datasets = capabilities.Contents.DatasetDescriptionSummary; #region 获取layerType layerType = GetLayerType(name); string projectName = null; double semimajor; BoundingBoxType[] boundingBoxs = GetBoundingBoxTypes(xMin, yMin, xMax, yMax); WGS84BoundingBoxType[] WGS84BoundingBoxes = null; using (OSGeo.OSR.SpatialReference srcSR = new OSGeo.OSR.SpatialReference(projectionStr)) { semimajor = srcSR.GetSemiMajor(); projectName = srcSR.GetAttrValue("PROJCS", 0); using (OSGeo.OSR.SpatialReference destSR = new OSGeo.OSR.SpatialReference("")) { destSR.SetWellKnownGeogCS("EPSG:4326"); if (srcSR.IsSame(destSR) != 1) { double[] leftBottom = { xMin, yMin }; double[] topRight = { xMax, yMax }; srcSR.CoordTransform(destSR, leftBottom, topRight); xMin = leftBottom[0]; yMin = leftBottom[1]; xMax = topRight[0]; yMax = topRight[1]; } WGS84BoundingBoxes = GetWGS84BoundingBoxTypes(xMin, yMin, xMax, yMax); } } TileMatrixSetLink[] tileMatrixSetLinks = GetTileMatrixSetLinks(projectName); layerType.BoundingBox = boundingBoxs; layerType.WGS84BoundingBox = WGS84BoundingBoxes; layerType.TileMatrixSetLink = tileMatrixSetLinks; #endregion datasets[datasets.Length - 1] = layerType; #region 设置tileMatrixSet TileMatrixSet[] tileMatrixSets = capabilities.Contents.TileMatrixSet; if (tileMatrixSets?.Any(x => x.Identifier.Value == projectName) != true) { int tileMatrixSetCount = tileMatrixSets == null ? 1 : tileMatrixSets.Length + 1; capabilities.Contents.TileMatrixSet = new TileMatrixSet[tileMatrixSetCount]; tileMatrixSets?.CopyTo(capabilities.Contents.TileMatrixSet, 0); tileMatrixSets = capabilities.Contents.TileMatrixSet; int minLevel = 0; int maxLevel = 20; TileMatrix[] tileMatrices = GetTileMatrices(semimajor, xMin, yMin, xMax, yMax, minLevel, maxLevel);; TileMatrixSet tileMatrixSet = new TileMatrixSet() { Identifier = new CodeType() { Value = projectName }, SupportedCRS = "urn:ogc:def:crs:OGC:1.3:CRS84",//TODO 待修改 TileMatrix = tileMatrices }; tileMatrixSets[tileMatrixSets.Length - 1] = tileMatrixSet; } #endregion return(layerType); }
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); }
public static TileMatrixSet GetTileMatrixSet(this Capabilities capabilities, string tileMatrixSetName) { TileMatrixSet tileMatrixSet = capabilities.Contents.TileMatrixSet.FirstOrDefault(x => x is TileMatrixSet && x.Identifier.Value == tileMatrixSetName) as TileMatrixSet; return(tileMatrixSet); }
public static LayerType AddToCapabilities(Capabilities capabilities, string name, string projectionStr, double xMin, double yMin, double xMax, double yMax, int minLevel, int maxLevel) { LayerType layerType = null; if (capabilities == null || capabilities == null || minLevel < 0 || minLevel > maxLevel) { return(layerType); } if (capabilities.Contents == null) { capabilities.Contents = new ContentsType(); } DatasetDescriptionSummaryBaseType[] datasets = capabilities.Contents.DatasetDescriptionSummary; if (datasets?.Any(x => x.Identifier.Value == name) == true) { return(layerType); } int layerCount = datasets == null ? 1 : datasets.Length + 1; capabilities.Contents.DatasetDescriptionSummary = new DatasetDescriptionSummaryBaseType[layerCount]; datasets?.CopyTo(capabilities.Contents.DatasetDescriptionSummary, 0); datasets = capabilities.Contents.DatasetDescriptionSummary; #region 获取layerType layerType = CreateLayerType(name); string projcs = null; string geogcs = null; double semimajor; BoundingBoxType[] boundingBoxs = CreateBoundingBoxTypes(xMin, yMin, xMax, yMax); WGS84BoundingBoxType[] WGS84BoundingBoxes = null; using (OSGeo.OSR.SpatialReference srcSR = new OSGeo.OSR.SpatialReference(projectionStr)) { semimajor = srcSR.GetSemiMajor(); projcs = srcSR.GetAttrValue("PROJCS", 0); if (string.IsNullOrEmpty(projcs)) { geogcs = srcSR.GetAttrValue("GEOGCS", 0); } using (OSGeo.OSR.SpatialReference destSR = new OSGeo.OSR.SpatialReference("")) { destSR.SetWellKnownGeogCS("EPSG:4326"); double WGS84XMin = xMin; double WGS84YMin = yMin; double WGS84XMax = xMax; double WGS84YMax = yMax; if (srcSR.IsSame(destSR) != 1) { double[] leftBottom = { xMin, yMin }; double[] topRight = { xMax, yMax }; srcSR.CoordTransform(destSR, leftBottom, topRight); WGS84XMin = leftBottom[0]; WGS84YMin = leftBottom[1]; WGS84XMax = topRight[0]; WGS84YMax = topRight[1]; } WGS84BoundingBoxes = CreateWGS84BoundingBoxTypes(WGS84XMin, WGS84YMin, WGS84XMax, WGS84YMax); } } string tileMatrixSetName = !string.IsNullOrEmpty(projcs) ? projcs : geogcs; TileMatrixSetLink[] tileMatrixSetLinks = CreateTileMatrixSetLinks(tileMatrixSetName); layerType.BoundingBox = boundingBoxs; layerType.WGS84BoundingBox = WGS84BoundingBoxes; layerType.TileMatrixSetLink = tileMatrixSetLinks; #endregion datasets[datasets.Length - 1] = layerType; #region 设置tileMatrixSet TileMatrixSet[] tileMatrixSets = capabilities.Contents.TileMatrixSet; if (tileMatrixSets?.Any(x => x.Identifier.Value == tileMatrixSetName) != true) { int tileMatrixSetCount = tileMatrixSets == null ? 1 : tileMatrixSets.Length + 1; capabilities.Contents.TileMatrixSet = new TileMatrixSet[tileMatrixSetCount]; tileMatrixSets?.CopyTo(capabilities.Contents.TileMatrixSet, 0); tileMatrixSets = capabilities.Contents.TileMatrixSet; bool isDegree = projcs == null; TileMatrix[] tileMatrices = CreateTileMatrices(semimajor, isDegree, xMin, yMin, xMax, yMax, minLevel, maxLevel); int?wkid = null; if (!string.IsNullOrEmpty(projcs)) { wkid = SpatialReferenceHelper.GetWellKnownWkidFromProjecs(projcs); } else if (!string.IsNullOrEmpty(geogcs)) { wkid = SpatialReferenceHelper.GetWellKnownWkidFromGeogcs(geogcs); } if (wkid.HasValue) { TileMatrixSet tileMatrixSet = new TileMatrixSet() { Identifier = new CodeType() { Value = tileMatrixSetName }, SupportedCRS = $"urn:ogc:def:crs:EPSG::{wkid.Value}",//TODO 待修改 根据名称获取EPSG,网上下载arcgis的 TileMatrix = tileMatrices }; tileMatrixSets[tileMatrixSets.Length - 1] = tileMatrixSet; } } #endregion return(layerType); }
public override FeatureInfoResponse GetFeatureInfo(Capabilities capabilities, string path, GetFeatureInfo getFeatureInfo) { FeatureInfoResponse featureInfoResponse = null; if (capabilities == null || string.IsNullOrEmpty(path) || getFeatureInfo == null) { return(featureInfoResponse); } LayerFactory layerFactory = new LayerFactory(); #region 验证getTile参数 GetTile getTile = getFeatureInfo.GetTile; LayerType layerType = capabilities.GetLayerType(getTile.Layer); if (layerType == null) { return(featureInfoResponse); } TileMatrixSet tileMatrixSet = capabilities.GetTileMatrixSet(getTile.TileMatrixSet); if (tileMatrixSet == null) { return(featureInfoResponse); } TileMatrix tileMatrix = tileMatrixSet.GetTileMatrix(getTile.TileMatrix); if (tileMatrix == null) { return(featureInfoResponse); } BoundingBoxType boundingBoxType = layerType.BoundingBox.FirstOrDefault(); if (boundingBoxType == null) { return(featureInfoResponse); } bool ret = boundingBoxType.LowerCorner.ToPosition(out double xMin, out double yMin); if (!ret) { return(featureInfoResponse); } ret = boundingBoxType.UpperCorner.ToPosition(out double xMax, out double yMax); if (!ret) { return(featureInfoResponse); } bool isDegree = tileMatrixSet.GetIsDegreeByLocalDb(); tileMatrix.GetTileIndex(isDegree, xMin, yMax, out int startCol, out int startRow); int matrixWidth = Convert.ToInt32(tileMatrix.MatrixWidth); int matrixHeight = Convert.ToInt32(tileMatrix.MatrixHeight); if (getTile.TileCol < startCol || getTile.TileCol >= startCol + matrixWidth || getTile.TileRow < startRow || getTile.TileRow >= startRow + matrixHeight) { return(featureInfoResponse); } if (!layerType.Style.Any(x => x.Identifier.Value == getTile.Style)) { return(featureInfoResponse); } #endregion #region 验证getFeatureInfo参数 int tileWidth = Convert.ToInt32(tileMatrix.TileWidth); int tileHeight = Convert.ToInt32(tileMatrix.TileHeight); if (getFeatureInfo.J < 0 || getFeatureInfo.J >= tileHeight || getFeatureInfo.I < 0 || getFeatureInfo.I >= tileWidth) { return(featureInfoResponse); } #endregion using (var dataSource = LayerFactory.OpenDataSource(path)) { if (dataSource == null) { return(featureInfoResponse); } using (var layer = dataSource.GetLayerByName(getTile.Layer)) { if (layer == null) { return(featureInfoResponse); } tileMatrix.GetFeatureInfoBoundary(isDegree, getTile.TileRow, getTile.TileCol, getFeatureInfo.J, getFeatureInfo.I, out double ftInfoXMin, out double ftInfoYMin, out double ftInfoXMax, out double ftInfoYMax); layer.SetSpatialFilterRect(ftInfoXMin, ftInfoYMin, ftInfoXMax, ftInfoYMax); FeatureCollectionType featureCollectionType = new FeatureCollectionType(); featureInfoResponse = new FeatureInfoResponse() { Item = featureCollectionType }; var ft = layer.GetNextFeature(); while (ft != null) { ft = layer.GetNextFeature(); //todo添加查询的feature } } } return(featureInfoResponse); }
public override byte[] GetTile(Capabilities capabilities, string path, GetTile getTile) { byte[] buffer = null; if (capabilities == null || string.IsNullOrEmpty(path) || getTile == null) { return(buffer); } LayerFactory layerFactory = new LayerFactory(); #region 验证getTile参数 LayerType layerType = capabilities.GetLayerType(getTile.Layer); if (layerType == null) { return(buffer); } TileMatrixSet tileMatrixSet = capabilities.GetTileMatrixSet(getTile.TileMatrixSet); if (tileMatrixSet == null) { return(buffer); } TileMatrix tileMatrix = tileMatrixSet.GetTileMatrix(getTile.TileMatrix); if (tileMatrix == null) { return(buffer); } BoundingBoxType boundingBoxType = layerType.BoundingBox.FirstOrDefault(); if (boundingBoxType == null) { return(buffer); } bool ret = boundingBoxType.LowerCorner.ToPosition(out double xMin, out double yMin); if (!ret) { return(buffer); } ret = boundingBoxType.UpperCorner.ToPosition(out double xMax, out double yMax); if (!ret) { return(buffer); } bool isDegree = tileMatrixSet.GetIsDegreeByLocalDb(); tileMatrix.GetTileIndex(isDegree, xMin, yMax, out int startCol, out int startRow); int matrixWidth = Convert.ToInt32(tileMatrix.MatrixWidth); int matrixHeight = Convert.ToInt32(tileMatrix.MatrixHeight); if (getTile.TileCol < startCol || getTile.TileCol >= startCol + matrixWidth || getTile.TileRow < startRow || getTile.TileRow >= startRow + matrixHeight) { return(buffer); } if (!layerType.Style.Any(x => x.Identifier.Value == getTile.Style)) { return(buffer); } if (!layerType.Format.Contains(getTile.Format)) { return(buffer); } #endregion using (var layer = layerFactory.OpenLayer(path)) { if (layer == null) { return(buffer); } tileMatrix.GetTileBoundary(isDegree, getTile.TileRow, getTile.TileCol, out double tileXMin, out double tileYMin, out double tileXMax, out double tileYMax); using (Envelope envelope = new Envelope()) { envelope.MinX = tileXMin; envelope.MinY = tileYMin; envelope.MaxX = tileXMax; envelope.MaxY = tileYMax; int tileWidth = Convert.ToInt32(tileMatrix.TileWidth); int tileHeight = Convert.ToInt32(tileMatrix.TileHeight); Rectangle rectangle = new Rectangle(0, 0, tileWidth, tileHeight); using (var image = new Image <Rgba32>(rectangle.Width, rectangle.Height)) { layer.DrawReagion(image, rectangle, envelope, false, null, null); using (MemoryStream ms = new MemoryStream()) { string formatName = getTile.Format.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries)[1]; switch (formatName) { case "png": image.SaveAsPng(ms); break; case "jpeg": image.SaveAsJpeg(ms); break; default: return(buffer); } buffer = ms.ToArray(); } } } } return(buffer); }
public virtual void RemoveContent(Capabilities capabilities, string contentIdentifier) { //remove content DatasetDescriptionSummaryBaseType[] srcContents = capabilities?.Contents?.DatasetDescriptionSummary; if (srcContents == null || string.IsNullOrWhiteSpace(contentIdentifier)) { return; } LayerType content = null; for (int i = 0; i < srcContents.Length; i++) { if (srcContents[i].Identifier.Value == contentIdentifier) { content = srcContents[i] as LayerType; break; } } if (content == null) { return; } DatasetDescriptionSummaryBaseType[] destContents = srcContents.Remove(content); capabilities.Contents.DatasetDescriptionSummary = destContents; if (content.TileMatrixSetLink == null || content.TileMatrixSetLink.Length == 0) { return; } //remove tileMatrixSet TileMatrixSetLink tileMatrixSetLink = content.TileMatrixSetLink[0]; string tileMatrixSetName = tileMatrixSetLink.TileMatrixSet; TileMatrixSet[] tileMatrixSets = capabilities.Contents.TileMatrixSet; TileMatrixSet tileMatrixSet = null; for (int i = 0; i < tileMatrixSets.Length; i++) { if (tileMatrixSets[i].Identifier.Value == tileMatrixSetName) { tileMatrixSet = tileMatrixSets[i]; break; } } if (tileMatrixSet == null) { return; } bool isReferenced = false; for (int i = 0; i < destContents.Length; i++) { if (destContents[i] is LayerType layerType) { if (layerType.TileMatrixSetLink == null || layerType.TileMatrixSetLink.Length == 0) { continue; } if (layerType.TileMatrixSetLink[0].TileMatrixSet == tileMatrixSetName) { isReferenced = true; break; } } } if (!isReferenced) { capabilities.Contents.TileMatrixSet = capabilities.Contents.TileMatrixSet.Remove(tileMatrixSet); } }