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

📄 client.cs

📁 C# 的地图开发例子(sharp map)
💻 CS
📖 第 1 页 / 共 2 页
字号:
			if (doc.DocumentElement.Attributes["version"] != null)
			{
				_WmsVersion = doc.DocumentElement.Attributes["version"].Value;
				if (_WmsVersion != "1.0.0" && _WmsVersion != "1.1.0" && _WmsVersion != "1.1.1" && _WmsVersion != "1.3.0")
					throw new ApplicationException("WMS Version " + _WmsVersion + " not supported");

				nsmgr.AddNamespace(String.Empty, "http://www.opengis.net/wms");
				if (_WmsVersion == "1.3.0")
				{
					nsmgr.AddNamespace("sm", "http://www.opengis.net/wms");
				}
				else
					nsmgr.AddNamespace("sm", "");
				nsmgr.AddNamespace("xlink", "http://www.w3.org/1999/xlink");
				nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
			}
			else
				throw (new ApplicationException("No service version number found!"));

			XmlNode xnService = doc.DocumentElement.SelectSingleNode("sm:Service", nsmgr);
			XmlNode xnCapability = doc.DocumentElement.SelectSingleNode("sm:Capability", nsmgr);
			if (xnService != null)
				ParseServiceDescription(xnService);
			else
				throw (new ApplicationException("No service tag found!"));



			if (xnCapability != null)
				ParseCapability(xnCapability);
			else
				throw (new ApplicationException("No capability tag found!"));
		}

		/// <summary>
		/// Parses service description node
		/// </summary>
		/// <param name="xnlServiceDescription"></param>
		private void ParseServiceDescription(XmlNode xnlServiceDescription)
		{
			XmlNode node = xnlServiceDescription.SelectSingleNode("sm:Title", nsmgr);
			_ServiceDescription.Title = (node != null ? node.InnerText : null);
			node = xnlServiceDescription.SelectSingleNode("sm:OnlineResource/@xlink:href", nsmgr);
			_ServiceDescription.OnlineResource = (node != null ? node.InnerText : null);
			node = xnlServiceDescription.SelectSingleNode("sm:Abstract", nsmgr);
			_ServiceDescription.Abstract = (node != null ? node.InnerText : null);
			node = xnlServiceDescription.SelectSingleNode("sm:Fees", nsmgr);
			_ServiceDescription.Fees = (node != null ? node.InnerText : null);
			node = xnlServiceDescription.SelectSingleNode("sm:AccessConstraints", nsmgr);
			_ServiceDescription.AccessConstraints = (node != null ? node.InnerText : null);

			XmlNodeList xnlKeywords = xnlServiceDescription.SelectNodes("sm:KeywordList/sm:Keyword", nsmgr);
			if (xnlKeywords != null)
			{
				_ServiceDescription.Keywords = new string[xnlKeywords.Count];
				for (int i = 0; i < xnlKeywords.Count; i++)
					ServiceDescription.Keywords[i] = xnlKeywords[i].InnerText;
			}
			//Contact information
			_ServiceDescription.ContactInformation = new Capabilities.WmsContactInformation();
			node = xnlServiceDescription.SelectSingleNode("sm:ContactInformation/sm:ContactAddress/sm:Address", nsmgr);
			_ServiceDescription.ContactInformation.Address.Address = (node != null ? node.InnerText : null);
			node = xnlServiceDescription.SelectSingleNode("sm:ContactInformation/sm:ContactAddress/sm:AddressType", nsmgr);
			_ServiceDescription.ContactInformation.Address.AddressType = (node != null ? node.InnerText : null);
			node = xnlServiceDescription.SelectSingleNode("sm:ContactInformation/sm:ContactAddress/sm:City", nsmgr);
			_ServiceDescription.ContactInformation.Address.City = (node != null ? node.InnerText : null);
			node = xnlServiceDescription.SelectSingleNode("sm:ContactInformation/sm:ContactAddress/sm:Country", nsmgr);
			_ServiceDescription.ContactInformation.Address.Country = (node != null ? node.InnerText : null);
			node = xnlServiceDescription.SelectSingleNode("sm:ContactInformation/sm:ContactAddress/sm:PostCode", nsmgr);
			_ServiceDescription.ContactInformation.Address.PostCode = (node != null ? node.InnerText : null);
			node = xnlServiceDescription.SelectSingleNode("sm:ContactInformation/sm:ContactElectronicMailAddress", nsmgr);
			_ServiceDescription.ContactInformation.Address.StateOrProvince = (node != null ? node.InnerText : null);
			node = xnlServiceDescription.SelectSingleNode("sm:ContactInformation/sm:ContactElectronicMailAddress", nsmgr);
			_ServiceDescription.ContactInformation.ElectronicMailAddress = (node != null ? node.InnerText : null);
			node = xnlServiceDescription.SelectSingleNode("sm:ContactInformation/sm:ContactFacsimileTelephone", nsmgr);
			_ServiceDescription.ContactInformation.FacsimileTelephone = (node != null ? node.InnerText : null);
			node = xnlServiceDescription.SelectSingleNode("sm:ContactInformation/sm:ContactPersonPrimary/sm:ContactOrganisation", nsmgr);
			_ServiceDescription.ContactInformation.PersonPrimary.Organisation = (node != null ? node.InnerText : null);
			node = xnlServiceDescription.SelectSingleNode("sm:ContactInformation/sm:ContactPersonPrimary/sm:ContactPerson", nsmgr);
			_ServiceDescription.ContactInformation.PersonPrimary.Person = (node != null ? node.InnerText : null);
			node = xnlServiceDescription.SelectSingleNode("sm:ContactInformation/sm:ContactVoiceTelephone", nsmgr);
			_ServiceDescription.ContactInformation.VoiceTelephone = (node != null ? node.InnerText : null);
		}
			
		/// <summary>
		/// Parses capability node
		/// </summary>
		/// <param name="xnCapability"></param>
		private void ParseCapability(XmlNode xnCapability)
		{
			XmlNode xnRequest = xnCapability.SelectSingleNode("sm:Request", nsmgr);
			if (xnRequest == null)
				throw (new System.Exception("Request parameter not specified in Service Description"));
			ParseRequest(xnRequest);
			XmlNode xnLayer = xnCapability.SelectSingleNode("sm:Layer", nsmgr);
			if (xnLayer == null)
				throw (new System.Exception("No layer tag found in Service Description")); 
			_Layer = ParseLayer(xnLayer);
			
			XmlNode xnException = xnCapability.SelectSingleNode("sm:Exception", nsmgr);
			if (xnException != null)
				ParseExceptions(xnException);
		}

		/// <summary>
		/// Parses valid exceptions
		/// </summary>
		/// <param name="xnlExceptionNode"></param>
		private void ParseExceptions(XmlNode xnlExceptionNode)
		{
			XmlNodeList xnlFormats = xnlExceptionNode.SelectNodes("sm:Format", nsmgr);
			if (xnlFormats != null)
			{
				_ExceptionFormats = new string[xnlFormats.Count];
				for (int i = 0; i < xnlFormats.Count; i++)
				{
					_ExceptionFormats[i] = xnlFormats[i].InnerText;
				}
			}
		}

		/// <summary>
		/// Parses request node
		/// </summary>
		/// <param name="xmlRequestNode"></param>
		private void ParseRequest(XmlNode xmlRequestNode)
		{
			XmlNode xnGetMap = xmlRequestNode.SelectSingleNode("sm:GetMap",nsmgr);
			ParseGetMapRequest(xnGetMap);
			//TODO:
			//XmlNode xnGetFeatureInfo = xmlRequestNodes.SelectSingleNode("sm:GetFeatureInfo", nsmgr);
			//XmlNode xnCapa = xmlRequestNodes.SelectSingleNode("sm:GetCapabilities", nsmgr); <-- We don't really need this do we?			
		}

		/// <summary>
		/// Parses GetMap request nodes
		/// </summary>
		/// <param name="GetMapRequestNodes"></param>
		private void ParseGetMapRequest(XmlNode GetMapRequestNodes)
		{
			XmlNode xnlHttp = GetMapRequestNodes.SelectSingleNode("sm:DCPType/sm:HTTP", nsmgr);
			if (xnlHttp != null && xnlHttp.HasChildNodes)
			{
				_GetMapRequests = new WmsOnlineResource[xnlHttp.ChildNodes.Count];
				for (int i = 0; i < xnlHttp.ChildNodes.Count; i++)
				{

					WmsOnlineResource wor = new WmsOnlineResource();
					wor.Type = xnlHttp.ChildNodes[i].Name;
					wor.OnlineResource = xnlHttp.ChildNodes[i].SelectSingleNode("sm:OnlineResource", nsmgr).Attributes["xlink:href"].InnerText;
					_GetMapRequests[i] = wor;
				}
			}
			XmlNodeList xnlFormats = GetMapRequestNodes.SelectNodes("sm:Format", nsmgr);
            //_GetMapOutputFormats = new Collection<string>(xnlFormats.Count);
            _GetMapOutputFormats = new Collection<string>();
			for (int i = 0; i < xnlFormats.Count;i++ )
				_GetMapOutputFormats.Add(xnlFormats[i].InnerText);
		}

		/// <summary>
		/// Iterates through the layer nodes recursively
		/// </summary>
		/// <param name="xmlLayer"></param>
		/// <returns></returns>
		private WmsServerLayer ParseLayer(XmlNode xmlLayer)
		{
			WmsServerLayer layer = new WmsServerLayer();
			XmlNode node = xmlLayer.SelectSingleNode("sm:Name", nsmgr);
			layer.Name = (node != null ? node.InnerText : null);
			node = xmlLayer.SelectSingleNode("sm:Title", nsmgr);
			layer.Title = (node != null ? node.InnerText : null);
			node = xmlLayer.SelectSingleNode("sm:Abstract", nsmgr);
			layer.Abstract = (node != null ? node.InnerText : null);
			XmlAttribute attr = xmlLayer.Attributes["queryable"];
			layer.Queryable = (attr != null && attr.InnerText == "1");


			XmlNodeList xnlKeywords = xmlLayer.SelectNodes("sm:KeywordList/sm:Keyword", nsmgr);
			if (xnlKeywords != null)
			{
				layer.Keywords = new string[xnlKeywords.Count];
				for (int i = 0; i < xnlKeywords.Count; i++)
					layer.Keywords[i] = xnlKeywords[i].InnerText;
			}
			XmlNodeList xnlCrs = xmlLayer.SelectNodes("sm:CRS", nsmgr);
			if (xnlCrs != null)
			{
				layer.CRS = new string[xnlCrs.Count];
				for (int i = 0; i < xnlCrs.Count; i++)
					layer.CRS[i] = xnlCrs[i].InnerText;
			}
			XmlNodeList xnlStyle = xmlLayer.SelectNodes("sm:Style", nsmgr);
			if (xnlStyle != null)
			{
				layer.Style = new WmsLayerStyle[xnlStyle.Count];
				for (int i = 0; i < xnlStyle.Count; i++)
				{
					node = xnlStyle[i].SelectSingleNode("sm:Name", nsmgr);
					layer.Style[i].Name = (node != null ? node.InnerText : null);
					node = xnlStyle[i].SelectSingleNode("sm:Title", nsmgr);
					layer.Style[i].Title = (node != null ? node.InnerText : null);
					node = xnlStyle[i].SelectSingleNode("sm:Abstract", nsmgr);
					layer.Style[i].Abstract = (node != null ? node.InnerText : null);
					node = xnlStyle[i].SelectSingleNode("sm:LegendUrl", nsmgr);
					if (node != null)
					{
						layer.Style[i].LegendUrl = new WmsStyleLegend();
						layer.Style[i].LegendUrl.Size = new System.Drawing.Size(
							int.Parse(node.Attributes["width"].InnerText), int.Parse(node.Attributes["height"].InnerText));
						layer.Style[i].LegendUrl.OnlineResource.OnlineResource = node.SelectSingleNode("sm:OnlineResource",nsmgr).Attributes["xlink:href"].InnerText;
						layer.Style[i].LegendUrl.OnlineResource.Type = node.SelectSingleNode("sm:Format", nsmgr).InnerText;
					}
					node = xnlStyle[i].SelectSingleNode("sm:StyleSheetURL", nsmgr);
					if (node != null)
					{
						layer.Style[i].StyleSheetUrl  = new WmsOnlineResource();
						layer.Style[i].StyleSheetUrl.OnlineResource = node.SelectSingleNode("sm:OnlineResource", nsmgr).Attributes["xlink:href"].InnerText;
						//layer.Style[i].StyleSheetUrl.OnlineResource = node.SelectSingleNode("sm:Format", nsmgr).InnerText;
					}
				}
			}
			XmlNodeList xnlLayers = xmlLayer.SelectNodes("sm:Layer", nsmgr);
			if (xnlLayers != null)
			{
				layer.ChildLayers = new WmsServerLayer[xnlLayers.Count];
				for (int i = 0; i < xnlLayers.Count; i++)
					layer.ChildLayers[i] = ParseLayer(xnlLayers[i]);
			}
			node = xmlLayer.SelectSingleNode("sm:LatLonBoundingBox", nsmgr);
			if (node != null)
			{
				double minx = 0; double miny = 0; double maxx = 0; double maxy = 0;
				if (!double.TryParse(node.Attributes["minx"].Value, System.Globalization.NumberStyles.Any, SharpMap.Map.numberFormat_EnUS, out minx) &
					!double.TryParse(node.Attributes["miny"].Value, System.Globalization.NumberStyles.Any, SharpMap.Map.numberFormat_EnUS, out miny) &
					!double.TryParse(node.Attributes["maxx"].Value, System.Globalization.NumberStyles.Any, SharpMap.Map.numberFormat_EnUS, out maxx) &
					!double.TryParse(node.Attributes["maxy"].Value, System.Globalization.NumberStyles.Any, SharpMap.Map.numberFormat_EnUS, out maxy))
					throw new ArgumentException("Invalid LatLonBoundingBox on layer '" + layer.Name + "'");
				layer.LatLonBoundingBox = new SharpMap.Geometries.BoundingBox(minx, miny, maxx, maxy);
			}
			return layer;
		}
	}
}

⌨️ 快捷键说明

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