⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 capabilities.cs

📁 用c#开发的基础gis库源代码
💻 CS
📖 第 1 页 / 共 2 页
字号:
		{
			//XmlNode ServiceNode = capabilities.CreateNode(XmlNodeType.Element, "Service", "");
			XmlElement ServiceNode = capabilities.CreateElement("Service", wmsNamespaceURI);
			ServiceNode.AppendChild(CreateElement("Name", "WMS", capabilities, false, wmsNamespaceURI));
			ServiceNode.AppendChild(CreateElement("Title", serviceDescription.Title, capabilities, false, wmsNamespaceURI)); //Add WMS Title
			if (!String.IsNullOrEmpty(serviceDescription.Abstract)) //Add WMS abstract
				ServiceNode.AppendChild(CreateElement("Abstract", serviceDescription.Abstract, capabilities, false, wmsNamespaceURI));
			if (serviceDescription.Keywords.Length > 0) //Add keywords
			{
				XmlElement KeywordListNode = capabilities.CreateElement("KeywordList", wmsNamespaceURI);
				foreach (string keyword in serviceDescription.Keywords)
					KeywordListNode.AppendChild(CreateElement("Keyword", keyword, capabilities, false, wmsNamespaceURI));
				ServiceNode.AppendChild(KeywordListNode);
			}
			//Add Online resource
			XmlElement OnlineResourceNode = GenerateOnlineResourceElement(capabilities, serviceDescription.OnlineResource);
			ServiceNode.AppendChild(OnlineResourceNode);

			//Add ContactInformation info
			XmlElement ContactInfoNode = GenerateContactInfoElement(capabilities, serviceDescription.ContactInformation);
			if(ContactInfoNode.HasChildNodes)
				ServiceNode.AppendChild(ContactInfoNode);

			if (serviceDescription.Fees != null && serviceDescription.Fees != string.Empty)
				ServiceNode.AppendChild(CreateElement("Fees", serviceDescription.Fees, capabilities, false, wmsNamespaceURI));
			if (serviceDescription.AccessConstraints != null && serviceDescription.AccessConstraints != string.Empty)
				ServiceNode.AppendChild(CreateElement("AccessConstraints", serviceDescription.AccessConstraints, capabilities, false, wmsNamespaceURI));
			if(serviceDescription.LayerLimit>0)
				ServiceNode.AppendChild(CreateElement("LayerLimit", serviceDescription.LayerLimit.ToString(), capabilities, false, wmsNamespaceURI));
			if (serviceDescription.MaxWidth > 0)
				ServiceNode.AppendChild(CreateElement("MaxWidth", serviceDescription.MaxWidth.ToString(), capabilities, false, wmsNamespaceURI));
			if (serviceDescription.MaxHeight > 0)
				ServiceNode.AppendChild(CreateElement("MaxHeight", serviceDescription.MaxHeight.ToString(), capabilities, false, wmsNamespaceURI));
			return ServiceNode;
		}

		private static XmlNode GenerateCapabilityNode(SharpMap.Map map, XmlDocument capabilities)
		{
			string OnlineResource = "";
			OnlineResource = System.Web.HttpContext.Current.Server.HtmlEncode("http://" + System.Web.HttpContext.Current.Request.Url.Host + System.Web.HttpContext.Current.Request.Url.AbsolutePath);
			XmlNode CapabilityNode = capabilities.CreateNode(XmlNodeType.Element, "Capability", wmsNamespaceURI);
			XmlNode RequestNode = capabilities.CreateNode(XmlNodeType.Element, "Request", wmsNamespaceURI);
			XmlNode GetCapabilitiesNode = capabilities.CreateNode(XmlNodeType.Element, "GetCapabilities", wmsNamespaceURI);
			//Set format of supported capabilities mime types (only text/xml supported)
			GetCapabilitiesNode.AppendChild(CreateElement("Format", "text/xml", capabilities, false, wmsNamespaceURI));
			GetCapabilitiesNode.AppendChild(GenerateDCPTypeNode(capabilities, OnlineResource));
			RequestNode.AppendChild(GetCapabilitiesNode);

			XmlNode GetMapNode = capabilities.CreateNode(XmlNodeType.Element, "GetMap", wmsNamespaceURI);
			//Add supported fileformats. Return the ones that GDI+ supports
			foreach (System.Drawing.Imaging.ImageCodecInfo encoder in System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders())
				GetMapNode.AppendChild(CreateElement("Format", encoder.MimeType, capabilities, false, wmsNamespaceURI));
			
			GetMapNode.AppendChild(GenerateDCPTypeNode(capabilities,OnlineResource));

			RequestNode.AppendChild(GetMapNode);
			CapabilityNode.AppendChild(RequestNode);
			XmlElement exceptionNode = capabilities.CreateElement("Exception",wmsNamespaceURI);
			exceptionNode.AppendChild(CreateElement("Format", "text/xml", capabilities, false, wmsNamespaceURI));
			CapabilityNode.AppendChild(exceptionNode); //Add supported exception types

			//Build layerlist
			
			XmlNode LayerRootNode = capabilities.CreateNode(XmlNodeType.Element, "Layer", wmsNamespaceURI);
			LayerRootNode.AppendChild(CreateElement("Title", "SharpMap", capabilities, false, wmsNamespaceURI));
			LayerRootNode.AppendChild(CreateElement("CRS", "EPSG:" + map.Layers[0].SRID.ToString(), capabilities, false, wmsNamespaceURI)); //TODO
			LayerRootNode.AppendChild(GenerateBoundingBoxElement(map.GetExtents(), map.Layers[0].SRID, capabilities));
			//This should be changed when Transformation library is complete
			XmlElement geoBox = capabilities.CreateElement("EX_GeographicBoundingBox", wmsNamespaceURI);
			geoBox.Attributes.Append(CreateAttribute("minx", "-180", capabilities));
			geoBox.Attributes.Append(CreateAttribute("miny", "-90", capabilities));
			geoBox.Attributes.Append(CreateAttribute("maxx", "180", capabilities));
			geoBox.Attributes.Append(CreateAttribute("maxy", "90", capabilities));
			LayerRootNode.AppendChild(geoBox);

            foreach (SharpMap.Layers.ILayer layer in map.Layers)
				LayerRootNode.AppendChild(GetWmsLayerNode(layer, capabilities));

			CapabilityNode.AppendChild(LayerRootNode);
			
			return CapabilityNode;
		}

		private static XmlNode GenerateDCPTypeNode(XmlDocument capabilities, string OnlineResource)
		{
			XmlNode DcpType = capabilities.CreateNode(XmlNodeType.Element, "DCPType", wmsNamespaceURI);
			XmlNode HttpType = capabilities.CreateNode(XmlNodeType.Element, "HTTP", wmsNamespaceURI);
			XmlElement resource = GenerateOnlineResourceElement(capabilities, OnlineResource);

			XmlNode GetNode = capabilities.CreateNode(XmlNodeType.Element, "Get", wmsNamespaceURI);
			XmlNode PostNode = capabilities.CreateNode(XmlNodeType.Element, "Post", wmsNamespaceURI);
			GetNode.AppendChild(resource.Clone());
			PostNode.AppendChild(resource);
			HttpType.AppendChild(GetNode);
			HttpType.AppendChild(PostNode);
			DcpType.AppendChild(HttpType);
			return DcpType;
		}

		private static XmlElement GenerateOnlineResourceElement(XmlDocument capabilities, string OnlineResource)
		{
			XmlElement resource = capabilities.CreateElement("OnlineResource", wmsNamespaceURI);
			XmlAttribute attrType = capabilities.CreateAttribute("xlink", "type", xlinkNamespaceURI);
			attrType.Value = "simple";
			resource.Attributes.Append(attrType);
			XmlAttribute href = capabilities.CreateAttribute("xlink","href", xlinkNamespaceURI);
			href.Value = OnlineResource;
			resource.Attributes.Append(href);
			XmlAttribute xmlns = capabilities.CreateAttribute("xmlns:xlink");
			xmlns.Value = xlinkNamespaceURI;
			resource.Attributes.Append(xmlns);
			return resource;
		}

		private static XmlElement GenerateContactInfoElement(XmlDocument capabilities, WmsContactInformation info)
		{
			XmlElement infoNode = capabilities.CreateElement("ContactInformation", wmsNamespaceURI);
			
			//Add primary person
			XmlElement cpp = capabilities.CreateElement("ContactPersonPrimary", wmsNamespaceURI);
			if (info.PersonPrimary.Person != null && info.PersonPrimary.Person != String.Empty)
				cpp.AppendChild(CreateElement("ContactPerson", info.PersonPrimary.Person, capabilities, false, wmsNamespaceURI));
			if (info.PersonPrimary.Organisation!=null && info.PersonPrimary.Organisation!=String.Empty)
				cpp.AppendChild(CreateElement("ContactOrganization", info.PersonPrimary.Organisation, capabilities, false, wmsNamespaceURI));
			if (cpp.HasChildNodes)
				infoNode.AppendChild(cpp);

			if (info.Position != null && info.Position != string.Empty)
				infoNode.AppendChild(CreateElement("ContactPosition", info.Position, capabilities, false, wmsNamespaceURI));

			//Add address
			XmlElement ca = capabilities.CreateElement("ContactAddress", wmsNamespaceURI);
			if (info.Address.AddressType != null && info.Address.AddressType != string.Empty)
				ca.AppendChild(CreateElement("AddressType", info.Address.AddressType, capabilities, false, wmsNamespaceURI));
			if (info.Address.Address!=null && info.Address.Address != string.Empty)
				ca.AppendChild(CreateElement("Address", info.Address.Address, capabilities, false, wmsNamespaceURI));
			if (info.Address.City!=null && info.Address.City != string.Empty)
				ca.AppendChild(CreateElement("City", info.Address.City, capabilities, false, wmsNamespaceURI));
			if (info.Address.StateOrProvince!=null && info.Address.StateOrProvince != string.Empty)
				ca.AppendChild(CreateElement("StateOrProvince", info.Address.StateOrProvince, capabilities, false, wmsNamespaceURI));
			if (info.Address.PostCode !=null && info.Address.PostCode != string.Empty)
				ca.AppendChild(CreateElement("PostCode", info.Address.PostCode, capabilities, false, wmsNamespaceURI));
			if (info.Address.Country!=null && info.Address.Country != string.Empty)
				ca.AppendChild(CreateElement("Country", info.Address.Country, capabilities, false, wmsNamespaceURI));
			if (ca.HasChildNodes)
				infoNode.AppendChild(ca);

			if (info.VoiceTelephone!=null && info.VoiceTelephone != string.Empty)
				infoNode.AppendChild(CreateElement("ContactVoiceTelephone", info.VoiceTelephone, capabilities, false, wmsNamespaceURI));
			if (info.FacsimileTelephone!=null && info.FacsimileTelephone != string.Empty)
				infoNode.AppendChild(CreateElement("ContactFacsimileTelephone", info.FacsimileTelephone, capabilities, false, wmsNamespaceURI));
			if (info.ElectronicMailAddress!=null && info.ElectronicMailAddress != string.Empty)
				infoNode.AppendChild(CreateElement("ContactElectronicMailAddress", info.ElectronicMailAddress, capabilities, false, wmsNamespaceURI));

			return infoNode;
		}

		private static XmlNode GetWmsLayerNode(SharpMap.Layers.ILayer layer, XmlDocument doc)
		{
			XmlNode LayerNode = doc.CreateNode(XmlNodeType.Element, "Layer", wmsNamespaceURI);
			LayerNode.AppendChild(CreateElement("Name", layer.LayerName, doc, false, wmsNamespaceURI));
			LayerNode.AppendChild(CreateElement("Title", layer.LayerName, doc, false, wmsNamespaceURI));
			//If this is a grouplayer, add childlayers recursively
			if (layer.GetType() == typeof(SharpMap.Layers.LayerGroup))
				foreach (SharpMap.Layers.Layer childlayer in ((SharpMap.Layers.LayerGroup)layer).Layers)
					LayerNode.AppendChild(GetWmsLayerNode(childlayer, doc));		
			
			LayerNode.AppendChild(GenerateBoundingBoxElement(layer.Envelope, layer.SRID, doc));

			return LayerNode;
		}

		private static XmlElement GenerateBoundingBoxElement(SharpMap.Geometries.BoundingBox bbox, int SRID, XmlDocument doc)
		{
			XmlElement xmlBbox = doc.CreateElement("BoundingBox", wmsNamespaceURI);
			xmlBbox.Attributes.Append(CreateAttribute("minx", bbox.Left.ToString(SharpMap.Map.numberFormat_EnUS), doc));
			xmlBbox.Attributes.Append(CreateAttribute("miny", bbox.Bottom.ToString(SharpMap.Map.numberFormat_EnUS), doc));
			xmlBbox.Attributes.Append(CreateAttribute("maxx", bbox.Right.ToString(SharpMap.Map.numberFormat_EnUS), doc));
			xmlBbox.Attributes.Append(CreateAttribute("maxy", bbox.Top.ToString(SharpMap.Map.numberFormat_EnUS), doc));
			xmlBbox.Attributes.Append(CreateAttribute("CRS", "EPSG:" + SRID.ToString(), doc));
			return xmlBbox;
		}

		private static XmlAttribute CreateAttribute(string name, string value, XmlDocument doc)
		{
			XmlAttribute attr = doc.CreateAttribute(name);
			attr.Value = value;
			return attr;
		}

		private static XmlNode CreateElement(string name, string value, XmlDocument doc, bool IsXml, string namespaceURI)
		{
			XmlNode node = doc.CreateNode(XmlNodeType.Element, name, namespaceURI);
			if (IsXml)
				node.InnerXml = value;
			else
				node.InnerText = value;
			return node;
		}

		internal static XmlDocument CreateXml()
		{
			XmlDocument capabilities = new XmlDocument();
			//Set XMLSchema
			capabilities.Schemas = new System.Xml.Schema.XmlSchemaSet();
			capabilities.Schemas.Add(GetCapabilitiesSchema());

			return capabilities;
		}

		private static System.Xml.Schema.XmlSchema GetCapabilitiesSchema()
		{
			//Get XML Schema
			System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("SharpMap.Web.Wms.Schemas._1._3._0.capabilities_1_3_0.xsd");
			System.Xml.Schema.XmlSchema schema = System.Xml.Schema.XmlSchema.Read(stream, new System.Xml.Schema.ValidationEventHandler(ValidationError));
			stream.Close();
			return schema;
		}

		private static void ValidationError(object sender, System.Xml.Schema.ValidationEventArgs arguments)
		{
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -