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

📄 client.cs

📁 Sharp Map 用于制作GIS系统S harp Map 用于制作GIS系统S harp Map 用于制作GIS系统
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace SharpMap.Web.Wms
{
	/// <summary>
	/// Class for requesting and parsing a WMS servers capabilities
	/// </summary>
	[Serializable]
	public class Client
	{
		private XmlNamespaceManager nsmgr;

		#region WMS Data structures

		/// <summary>
		/// Structure for holding information about a WMS Layer 
		/// </summary>
		public struct WmsServerLayer
		{
			/// <summary>
			/// Layer title
			/// </summary>
			public string Title;
			/// <summary>
			/// Unique name of this layer used for requesting layer
			/// </summary>
			public string Name;
			/// <summary>
			/// Abstract
			/// </summary>
			public string Abstract;
			/// <summary>
			/// Specifies whether this layer is queryable using GetFeatureInfo requests
			/// </summary>
			public bool Queryable;
			/// <summary>
			/// Keywords
			/// </summary>
			public string[] Keywords;
			/// <summary>
			/// List of styles supported by layer
			/// </summary>
			public WmsLayerStyle[] Style;
			/// <summary>
			/// Coordinate Reference Systems supported by layer
			/// </summary>
			public string[] CRS;
			/// <summary>
			/// Collection of child layers
			/// </summary>
			public WmsServerLayer[] ChildLayers;
			/// <summary>
			/// Latitudal/longitudal extent of this layer
			/// </summary>
			public SharpMap.Geometries.BoundingBox LatLonBoundingBox;
		}

		/// <summary>
		/// Structure for storing information about a WMS Layer Style
		/// </summary>
		public struct WmsLayerStyle
		{
			/// <summary>
			/// Name
			/// </summary>
			public string Name;
			/// <summary>
			/// Title
			/// </summary>
			public string Title;
			/// <summary>
			/// Abstract
			/// </summary>
			public string Abstract;
			/// <summary>
			/// Legend
			/// </summary>
			public WmsStyleLegend LegendUrl;
			/// <summary>
			/// Style Sheet Url
			/// </summary>
			public WmsOnlineResource StyleSheetUrl;
		}

		/// <summary>
		/// Structure for storing WMS Legend information
		/// </summary>
		public struct WmsStyleLegend
		{
			/// <summary>
			/// Online resource for legend style 
			/// </summary>
			public WmsOnlineResource OnlineResource;
			/// <summary>
			/// Size of legend
			/// </summary>
			public System.Drawing.Size Size;
		}

		/// <summary>
		/// Structure for storing info on an Online Resource
		/// </summary>
		public struct WmsOnlineResource
		{
			/// <summary>
			/// Type of online resource (Ex. request method 'Get' or 'Post')
			/// </summary>
			public string Type;
			/// <summary>
			/// URI of online resource
			/// </summary>
			public string OnlineResource;
		}

		#endregion

		#region Properties

		private Capabilities.WmsServiceDescription _ServiceDescription;

		/// <summary>
		/// Gets the service description
		/// </summary>
		public Capabilities.WmsServiceDescription ServiceDescription
		{
			get { return _ServiceDescription; }
		}
		private string _WmsVersion;

		/// <summary>
		/// Gets the version of the WMS server (ex. "1.3.0")
		/// </summary>
		public string WmsVersion
		{
			get { return _WmsVersion; }
		}

		private List<string> _GetMapOutputFormats;

		/// <summary>
		/// Gets a list of available image mime type formats
		/// </summary>
		public List<string> GetMapOutputFormats
		{
			get { return _GetMapOutputFormats; }
		}

		private string[] _ExceptionFormats;

		/// <summary>
		/// Gets a list of available exception mime type formats
		/// </summary>
		public string[] ExceptionFormats
		{
			get { return _ExceptionFormats; }
		}

		private WmsOnlineResource[] _GetMapRequests;

		/// <summary>
		/// Gets the available GetMap request methods and Online Resource URI
		/// </summary>
		public WmsOnlineResource[] GetMapRequests
		{
			get { return _GetMapRequests; }
		}

		private WmsServerLayer _Layer;
		/// <summary>
		/// Gets the hiarchial layer structure
		/// </summary>
		public WmsServerLayer Layer
		{
			get { return _Layer; }
		}
		#endregion

		/// <summary>
		/// Initalizes WMS server and parses the Capabilities request
		/// </summary>
		/// <param name="url">URL of wms server</param>
		public Client(string url)
			: this(url, null)
		{
		}

		/// <summary>
		/// Initalizes WMS server and parses the Capabilities request
		/// </summary>
		/// <param name="url">URL of wms server</param>
		/// <param name="proxy">Proxy to use</param>
		public Client(string url, System.Net.WebProxy proxy)
		{
			System.Text.StringBuilder strReq = new StringBuilder(url);
			if (!url.Contains("?"))
				strReq.Append("?");
			if (!strReq.ToString().EndsWith("&") && !strReq.ToString().EndsWith("?"))
				strReq.Append("&");
			if(!url.ToLower().Contains("service=wms"))
				strReq.AppendFormat("SERVICE=WMS&");
			if (!url.ToLower().Contains("request=getcapabilities"))
				strReq.AppendFormat("REQUEST=GetCapabilities&");		

			XmlDocument xml = GetRemoteXml(strReq.ToString(), proxy);
			ParseCapabilities(xml);
		}


		/// <summary>
		/// Downloads servicedescription from WMS service
		/// </summary>
		/// <returns>XmlDocument from Url. Null if Url is empty or inproper XmlDocument</returns>
		private XmlDocument GetRemoteXml(string Url, System.Net.WebProxy proxy)
		{
			try
			{
				System.Net.WebRequest myRequest = System.Net.WebRequest.Create(Url);
				if (proxy != null) myRequest.Proxy = proxy;
				System.Net.WebResponse myResponse = myRequest.GetResponse();
				System.IO.Stream stream = myResponse.GetResponseStream();
				XmlTextReader r = new XmlTextReader(Url, stream);
				XmlDocument doc = new XmlDocument();
				doc.Load(r);
				stream.Close();
				nsmgr = new XmlNamespaceManager(doc.NameTable);
				return doc;
			}
			catch (System.Exception ex)
			{
				throw new ApplicationException("Could now download capabilities", ex);
			}
		}


		/// <summary>
		/// Parses a servicedescription and stores the data in the ServiceDescription property
		/// </summary>
		/// <param name="doc">XmlDocument containing a valid Service Description</param>
		private void ParseCapabilities(XmlDocument doc)
		{

⌨️ 快捷键说明

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