Exemplo n.º 1
0
        private CascadedWMSLayer getLayerInfomation(XElement layer, Dictionary <XElement, BBox> layer_bbox_map, HierachicalWMSLayers wms)
        {
            CascadedWMSLayer layerObject = new CascadedWMSLayer();
            string           name        = null;
            string           title       = null;
            XElement         nameElement = layer.Element(XName.Get("Name"));
            //XElement LatLonBoundingBoxElement = layer.Element(XName.Get("LatLonBoundingBox"));
            XElement Extent = layer.Element(XName.Get("Extent"));

            IEnumerable <XElement> cascadedLayers = layer.Elements(XName.Get("Layer"));

            //get BBOX information
            BBox bbox = getBBoxInformation(layer);

            if (bbox != null)
            {
                layerObject.latLonBBox = bbox;
                layer_bbox_map.Add(layer, bbox);
            }

            //if current layer don't contain LatLonBoundingBox and Parent Layer is not exist or do not contain  LatLonBoundingBox either, current layer is not accessible
            else
            {
                XElement parent = layer.Parent;
                while (parent.Name.Equals(XName.Get("Layer")))
                {
                    if (layer_bbox_map.ContainsKey(layer.Parent))
                    {
                        bbox = BBox.CreateBBox(layer_bbox_map[layer.Parent]);
                        layerObject.latLonBBox = bbox;
                        layer_bbox_map.Add(layer, bbox);
                        break;
                    }
                    else
                    {
                        parent = parent.Parent;
                    }
                }
            }

            //if there is not Layer name or LatLonBoundingBox element exist, the layer is not accessible
            if (nameElement != null && layerObject.latLonBBox != null)
            {
                layerObject.canGetMap = true;
                wms.allGetMapEnabledLayers_Number++;
            }
            else
            {
                layerObject.canGetMap = false;
            }


            if (nameElement == null)
            {
                layerObject.name = string_unknown;
            }
            else
            {
                name = nameElement.Value;
                if (name != null && !(name.Trim().Equals("")))
                {
                    layerObject.name = name;
                }
            }

            XElement titleElement = layer.Element(XName.Get("Title"));

            if (titleElement != null)
            {
                title = titleElement.Value;
                if (title != null && !(title.Trim().Equals("")))
                {
                    layerObject.title = title;
                }
                else
                {
                    layerObject.title = string_unknown;
                }
            }

            //we want to show title, which provide us more inforamtion than layer name, but when there is not layer title, we still show layer name as title
            //if (layerObject.title == null && layerObject.name != null)
            //    layerObject.title = layerObject.name;

            //add time extent
            if (Extent != null && Extent.Attribute(XName.Get("name")).Value.Equals("time"))
            {
                layerObject.extent_time_default = Extent.Attribute(XName.Get("default")).Value;
                layerObject.extent_time         = Extent.Value;
                layerObject.timeEnabled         = true;
            }
            else
            {
                layerObject.timeEnabled = false;
            }

            //get the legend for the layer
            XElement style = layer.Element(XName.Get("Style"));

            if (style != null)
            {
                XElement legendURL = style.Element(XName.Get("LegendURL"));
                if (legendURL != null)
                {
                    XElement OnlineResource = legendURL.Element(XName.Get("OnlineResource"));
                    if (OnlineResource != null)
                    {
                        XAttribute url = OnlineResource.Attribute(XName.Get("href", "http://www.w3.org/1999/xlink"));
                        if (url != null)
                        {
                            layerObject.legendURL = url.Value;
                        }
                    }
                }
            }

            //add children layers
            if (cascadedLayers != null)
            {
                layerObject.Children = new System.Collections.ObjectModel.ObservableCollection <CascadedWMSLayer>();
                foreach (XElement childLayer in cascadedLayers)
                {
                    layerObject.Children.Add(getLayerInfomation(childLayer, layer_bbox_map, wms));
                }
            }

            //get queryable information
            XAttribute queryable = layer.Attribute(XName.Get("queryable"));

            if (queryable != null && queryable.Value.Equals("1"))
            {
                layerObject.queryable = true;
            }
            else
            {
                layerObject.queryable = false;
            }

            return(layerObject);
        }
Exemplo n.º 2
0
        public HierachicalWMSLayers getHierachicalLayersOfWMS(string urlstring)
        {
            HierachicalWMSLayers wms_layers = new HierachicalWMSLayers();

            wms_layers.layersList = new List <CascadedWMSLayer>();
            wms_layers.WMSURL     = urlstring;

            if (!urlstring.ToLower().Contains("service="))
            {
                if (urlstring.Contains("?"))
                {
                    if (urlstring.EndsWith("&"))
                    {
                        urlstring += "SERVICE=WMS&REQUEST=GetCapabilities&VERSION=1.1.1";
                    }
                    else
                    {
                        urlstring += "&SERVICE=WMS&REQUEST=GetCapabilities&VERSION=1.1.1";
                    }
                }
                else
                {
                    urlstring += "?SERVICE=WMS&REQUEST=GetCapabilities&VERSION=1.1.1";
                }
            }

            XDocument doc = null;

            try
            {
                string response = BaseHttpFunctions.HttpGet(urlstring);
                doc = XDocument.Parse(response);
            }
            catch (Exception e)
            {
                e.GetType();
                return(null);
            }
            if (doc != null)
            {
                XElement rootElement = doc.Root;

                IEnumerable <XElement> Services = rootElement.Descendants(XName.Get("Service"));
                if (Services != null)
                {
                    foreach (XElement Service in Services)
                    {
                        string   name        = null;
                        string   title       = null;
                        XElement nameElement = Service.Element(XName.Get("Name"));
                        if (nameElement != null)
                        {
                            name = nameElement.Value;
                            if (name != null && !(name.Trim().Equals("")))
                            {
                                wms_layers.serviceName = name;
                            }
                        }

                        XElement titleElement = Service.Element(XName.Get("Title"));
                        if (titleElement != null)
                        {
                            title = titleElement.Value;
                            if (title != null && !(title.Trim().Equals("")))
                            {
                                wms_layers.serviceTitle = title;
                            }
                        }
                    }
                }

                Dictionary <XElement, BBox> layer_bbox_map = new Dictionary <XElement, BBox>();

                XElement Capability       = rootElement.Element(XName.Get("Capability"));
                XElement firstLevel_Layer = Capability.Element(XName.Get("Layer"));
                if (firstLevel_Layer != null)
                {
                    IEnumerable <XElement> secondLevel_Layers = firstLevel_Layer.Elements(XName.Get("Layer"));
                    if (wms_layers.serviceName == null || wms_layers.serviceName.Trim().Equals(""))
                    {
                        XElement nameElement = firstLevel_Layer.Element(XName.Get("Name"));
                        if (nameElement == null || nameElement.Value.Trim().Equals(""))
                        {
                            wms_layers.serviceName = string_unknown;
                        }
                        else
                        {
                            wms_layers.serviceName = nameElement.Value;
                        }
                    }

                    if (wms_layers.serviceTitle == null || wms_layers.serviceTitle.Trim().Equals(""))
                    {
                        XElement titleElement = firstLevel_Layer.Element(XName.Get("Title"));
                        if (titleElement == null || titleElement.Value.Trim().Equals(""))
                        {
                            wms_layers.serviceTitle = string_unknown;
                        }
                        else
                        {
                            wms_layers.serviceTitle = titleElement.Value;
                        }
                    }

                    //get BBOX information
                    BBox bbox = getBBoxInformation(firstLevel_Layer);
                    if (bbox != null)
                    {
                        wms_layers.latLonBBox = bbox;
                        layer_bbox_map.Add(firstLevel_Layer, bbox);
                    }

                    wms_layers.subLayers_Number = secondLevel_Layers.Count();
                    foreach (XElement layer in secondLevel_Layers)
                    {
                        wms_layers.layersList.Add(getLayerInfomation(layer, layer_bbox_map, wms_layers));
                    }
                }
            }
            return(wms_layers);
        }