Beispiel #1
0
        private bool UpdateChildLayers(WmtsTileMatrixSet tileMatrixSet)
        {
            var layersChanged = false;
            var maxScale      = 1.001 * ParentMap.ViewTransform.Scale; // avoid rounding issues

            // show all TileMatrix layers with Scale <= maxScale, at least the first layer
            //
            var currentMatrixes = tileMatrixSet.TileMatrixes
                                  .Where((matrix, i) => i == 0 || matrix.Scale <= maxScale)
                                  .ToList();

            if (this != ParentMap.MapLayer)                                                 // do not load background tiles
            {
                currentMatrixes = currentMatrixes.Skip(currentMatrixes.Count - 1).ToList(); // last element only
            }
            else if (currentMatrixes.Count > MaxBackgroundLevels + 1)
            {
                currentMatrixes = currentMatrixes.Skip(currentMatrixes.Count - MaxBackgroundLevels - 1).ToList();
            }

            var currentLayers = ChildLayers.Where(layer => currentMatrixes.Contains(layer.TileMatrix)).ToList();

            Children.Clear();

            foreach (var tileMatrix in currentMatrixes)
            {
                var layer = currentLayers.FirstOrDefault(l => l.TileMatrix == tileMatrix);

                if (layer == null)
                {
                    layer         = new WmtsTileMatrixLayer(tileMatrix, tileMatrixSet.TileMatrixes.IndexOf(tileMatrix));
                    layersChanged = true;
                }

                if (layer.SetBounds(ParentMap.ViewTransform, ParentMap.RenderSize))
                {
                    layersChanged = true;
                }

                Children.Add(layer);
            }

            return(layersChanged);
        }
        private void ReadCapabilities(XElement capabilitiesElement)
        {
            TileMatrixSets.Clear();

            XNamespace ns  = capabilitiesElement.Name.Namespace;
            XNamespace ows = "http://www.opengis.net/ows/1.1";

            var contentsElement = capabilitiesElement.Element(ns + "Contents");

            if (contentsElement == null)
            {
                throw new ArgumentException("Contents element not found.");
            }

            XElement layerElement;

            if (!string.IsNullOrEmpty(LayerIdentifier))
            {
                layerElement = contentsElement.Descendants(ns + "Layer")
                               .FirstOrDefault(e => e.Element(ows + "Identifier")?.Value == LayerIdentifier);

                if (layerElement == null)
                {
                    throw new ArgumentException("Layer element \"" + LayerIdentifier + "\" not found.");
                }
            }
            else
            {
                layerElement = capabilitiesElement.Descendants(ns + "Layer").FirstOrDefault();

                if (layerElement == null)
                {
                    throw new ArgumentException("No Layer element found.");
                }

                LayerIdentifier = layerElement.Element(ows + "Identifier")?.Value ?? "";
            }

            var urlTemplate = layerElement.Element(ns + "ResourceURL")?.Attribute("template")?.Value;

            if (string.IsNullOrEmpty(urlTemplate))
            {
                throw new ArgumentException("No valid ResourceURL element found in Layer \"" + LayerIdentifier + "\".");
            }

            var styleElement = layerElement.Descendants(ns + "Style")
                               .FirstOrDefault(e => e.Attribute("isDefault")?.Value == "true");

            if (styleElement == null)
            {
                styleElement = layerElement.Descendants(ns + "Style").FirstOrDefault();
            }

            var style = styleElement?.Element(ows + "Identifier")?.Value;

            if (string.IsNullOrEmpty(style))
            {
                throw new ArgumentException("No valid Style element found in Layer \"" + LayerIdentifier + "\".");
            }

            var tileMatrixSetIds = layerElement
                                   .Descendants(ns + "TileMatrixSetLink")
                                   .Select(e => e.Element(ns + "TileMatrixSet")?.Value)
                                   .Where(id => !string.IsNullOrEmpty(id))
                                   .ToList();

            foreach (var tileMatrixSetId in tileMatrixSetIds)
            {
                var tileMatrixSetElement = capabilitiesElement.Descendants(ns + "TileMatrixSet")
                                           .FirstOrDefault(e => e.Element(ows + "Identifier")?.Value == tileMatrixSetId);

                if (tileMatrixSetElement == null)
                {
                    throw new ArgumentException("Linked TileMatrixSet element not found in Layer \"" + LayerIdentifier + "\".");
                }

                var tileMatrixSet = WmtsTileMatrixSet.Create(tileMatrixSetElement);

                TileMatrixSets.Add(tileMatrixSet.SupportedCrs, tileMatrixSet);
            }

            TileSource = new WmtsTileSource(urlTemplate.Replace("{Style}", style));

            UpdateTileLayer();
        }