示例#1
0
        /// <summary>
        /// OGC WMS GetCapabilities
        /// </summary>
        /// <param name="context"></param>
        private void ProcessGetCapabilities(HttpResponse response, StringDictionary parameters)
        {
            //Service parameter is mandatory for GetCapabilities request
            if (String.IsNullOrEmpty(parameters[OgcParameters.Service]))
            {
                SetResponseToServiceException(response, OgcErrorMessages.RequiredParameter(OgcParameters.Service));
                return;
            }

            if (String.Compare(parameters[OgcParameters.Service], ServiceNames.WMS.ToString()) != 0)
            {
                SetResponseToServiceException(response, "Invalid service for GetCapabilities Request. Service parameter must be 'WMS'");
                return;
            }

            // deserialise template GetCapabilities document
            XmlSerializer    deSerializer    = new XmlSerializer(typeof(WMS_Capabilities), Declarations.WmsNameSpace);
            FileStream       readStream      = new FileStream(Path.Combine(HttpContext.Current.Server.MapPath("~/App_Data"), "WMSCapabilities.xml"), FileMode.Open, FileAccess.Read);
            WMS_Capabilities wmsCapabilities = (WMS_Capabilities)deSerializer.Deserialize(readStream);

            readStream.Close();

            // get the map service view context
            ViewContext webMapContext = OgcUtilities.GetViewContext();

            // add layers
            List <GeospatialServices.Ogc.Wms.Layer> wmsLayers = new List <GeospatialServices.Ogc.Wms.Layer>();

            foreach (var layer in webMapContext.Layers)
            {
                GeospatialServices.Ogc.Wms.Layer ogcLayer = new GeospatialServices.Ogc.Wms.Layer( );
                ogcLayer.Queryable = 1;
                ogcLayer.Name      = layer.Name;
                ogcLayer.Title     = layer.Title;
                ogcLayer.CRSList.Add(layer.SRS);
                wmsLayers.Add(ogcLayer);
            }
            wmsCapabilities.Capability.Layer.LayerList = wmsLayers;

            // Namespaces
            XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();

            namespaces.Add(Declarations.WmsPrefix, Declarations.WmsNameSpace);
            namespaces.Add(Declarations.OgcPrefix, Declarations.OgcNameSpace);
            namespaces.Add(Declarations.XlinkPrefix, Declarations.XlinkNameSpace);

            // Serialize
            XmlSerializer capSerializer = new XmlSerializer(typeof(WMS_Capabilities), Declarations.WmsNameSpace);
            MemoryStream  memoryStream  = new MemoryStream();

            capSerializer.Serialize(memoryStream, wmsCapabilities, namespaces);

            byte[] buffer = memoryStream.ToArray();
            response.Clear();
            response.ContentType = "text/xml";
            response.OutputStream.Write(buffer, 0, buffer.Length);
        }
示例#2
0
        /// <summary>
        /// Gets the WMC Context associated with a WMS Request
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ViewContext(StringDictionary parameters)
        {
            // Parse map size
            int width  = 0;
            int height = 0;

            int.TryParse(parameters[WmsParameters.Width], out width);
            int.TryParse(parameters[WmsParameters.Height], out height);

            this.General.Window.Width  = width;
            this.General.Window.Height = height;

            // Attach the Bounding Box if passed in, otherwise, it will be set to the
            // envelope of all layers
            string crs  = parameters[WmsParameters.Crs];
            int    srid = OgcUtilities.GetSridFromCrs(crs);

            if (parameters[WmsParameters.Bbox] != null)
            {
                this.General.BoundingBox     = new GeospatialServices.Ogc.Wmc.BoundingBox(parameters[WmsParameters.Bbox], srid);
                this.General.BoundingBox.SRS = crs;
            }

            SLD sldRead = null;

            // Get the SLD_BODY if specified
            if (!String.IsNullOrEmpty(parameters[WmsParameters.SldBody]))
            {
                // Namespaces
                XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();

                namespaces.Add(Declarations.WmsPrefix, Declarations.WmsNameSpace);
                namespaces.Add(Declarations.SldPrefix, Declarations.SldNameSpace);
                namespaces.Add(Declarations.SePrefix, Declarations.SeNameSpace);
                namespaces.Add(Declarations.OgcPrefix, Declarations.OgcNameSpace);
                namespaces.Add(Declarations.XlinkPrefix, Declarations.XlinkNameSpace);

                using (StringReader stringReader = new StringReader(parameters[WmsParameters.SldBody]))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(SLD));
                    sldRead = (SLD)serializer.Deserialize(stringReader);
                }
            }

            // Extract all layers from request
            string layers = "";

            if (parameters[OgcParameters.Service] == GeospatialServices.Ogc.Common.ServiceNames.WFS.ToString())
            {
                layers = parameters[WfsParameters.TypeName];
            }
            else
            {
                layers = parameters[WmsParameters.Layers];
            }

            if (!String.IsNullOrEmpty(layers))
            {
                string[] requestedLayers = layers.Split(new char[] { ',' });
                string[] requestedStyles = null;

                string styles = parameters[WmsParameters.Styles];
                if (string.IsNullOrEmpty(styles))
                {
                    requestedStyles = new string[0];
                }
                else
                {
                    requestedStyles = styles.Split(new char[] { ',' });
                }

                // get all know layers from the confguration view context document
                ViewContext configurationContext = OgcUtilities.GetViewContext();

                // build a custom WMC document from the layers and styles in the request
                for (int i = 0; i < requestedLayers.Count(); i++)
                {
                    // find the requested layers configuration
                    string layerName  = requestedLayers[i].Trim();
                    int    layerIndex = configurationContext.Layers.FindIndex(delegateLayer => delegateLayer.Name == layerName);

                    if (layerIndex != -1)
                    {
                        // find the style
                        GeospatialServices.Ogc.Wmc.UserStyle sldStyle = null;

                        string requestedStyle = string.Empty;
                        if (requestedStyles.Count() > i)
                        {
                            requestedStyle = requestedStyles[i].Trim();
                        }

                        if (sldRead != null)
                        {
                            foreach (var sldlayer in sldRead.StyledLayerDescriptor.UserLayers)
                            {
                                foreach (UserStyle style in sldlayer.UserStyles)
                                {
                                    if (style.Name == requestedStyle)
                                    {
                                        sldStyle       = style;
                                        requestedStyle = string.Empty;
                                        break;
                                    }
                                }
                                if (requestedStyle == string.Empty)
                                {
                                    break;
                                }
                            }
                        }
                        else
                        {
                            if (string.IsNullOrEmpty(requestedStyle))
                            {
                                requestedStyle = "default";
                            }
                            Style style = configurationContext.Layers[layerIndex].StyleList.Find(delegateStyle => delegateStyle.Name == requestedStyle);
                            if (style == null)
                            {
                                throw new InvalidDataException("Style not found");
                            }
                            sldStyle = style.SLD.StyledLayerDescriptor.UserLayers[0].UserStyles[0];
                        }

                        // get the layer and remove configured styles
                        GeospatialServices.Ogc.Wmc.Layer wmcLayer = configurationContext.Layers[layerIndex].Clone();
                        wmcLayer.StyleList.Clear();

                        // now add the style to be rendered
                        if (sldStyle != null)
                        {
                            wmcLayer.StyleList.Add(new GeospatialServices.Ogc.Wmc.Style());
                            wmcLayer.StyleList[0].Current = 1;
                            UserLayer ul = new UserLayer();
                            ul.UserStyles = new List <UserStyle>();
                            wmcLayer.StyleList[0].SLD.StyledLayerDescriptor.UserLayers.Add(ul);
                            wmcLayer.StyleList[0].SLD.StyledLayerDescriptor.UserLayers[0].UserStyles.Add(sldStyle);
                        }

                        if (parameters[WmsParameters.Format] != null)
                        {
                            wmcLayer.FormatList.Add(new Format(parameters[WmsParameters.Format], 1));
                        }

                        this.Layers.Add(wmcLayer);
                    }
                }

                //TODO: do this properly....
                // compute envelope if required
                if (this.General.BoundingBox.ToSqlGeometry == null)
                {
                    this.General.BoundingBox = new BoundingBox("-180,-85,180,85", 4326);
                }
            }
        }