📄 wmslayer.cs
字号:
/// Gets the list of available formats
/// </summary>
public List<string> OutputFormats
{
get { return wmsClient.GetMapOutputFormats; }
}
private string _SpatialReferenceSystem;
/// <summary>
/// Gets or sets the spatial reference used for the WMS server request
/// </summary>
public string SpatialReferenceSystem
{
get { return _SpatialReferenceSystem; }
set { _SpatialReferenceSystem = value; }
}
/// <summary>
/// Gets the service description from this server
/// </summary>
public SharpMap.Web.Wms.Capabilities.WmsServiceDescription ServiceDescription
{
get { return wmsClient.ServiceDescription; }
}
/// <summary>
/// Gets the WMS Server version of this service
/// </summary>
public string Version
{
get { return wmsClient.WmsVersion; }
}
private ImageAttributes _ImageAttributes;
/// <summary>
/// When specified, applies image attributes at image (fx. make WMS layer semi-transparent)
/// </summary>
/// <remarks>
/// <para>You can make the WMS layer semi-transparent by settings a up a ColorMatrix,
/// or scale/translate the colors in any other way you like.</para>
/// <example>
/// Setting the WMS layer to be semi-transparent.
/// <code lang="C#">
/// float[][] colorMatrixElements = {
/// new float[] {1, 0, 0, 0, 0},
/// new float[] {0, 1, 0, 0, 0},
/// new float[] {0, 0, 1, 0, 0},
/// new float[] {0, 0, 0, 0.5, 0},
/// new float[] {0, 0, 0, 0, 1}};
/// ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
/// ImageAttributes imageAttributes = new ImageAttributes();
/// imageAttributes.SetColorMatrix(
/// colorMatrix,
/// ColorMatrixFlag.Default,
/// ColorAdjustType.Bitmap);
/// myWmsLayer.ImageAttributes = imageAttributes;
/// </code>
/// </example>
/// </remarks>
public ImageAttributes ImageAttributes
{
get { return _ImageAttributes; }
set { _ImageAttributes = value; }
}
#region ILayer Members
/// <summary>
/// Renders the layer
/// </summary>
/// <param name="g">Graphics object reference</param>
/// <param name="map">Map which is rendered</param>
public override void Render(System.Drawing.Graphics g, Map map)
{
SharpMap.Web.Wms.Client.WmsOnlineResource resource = GetPreferredMethod();
Uri myUri = new Uri(GetRequestUrl(map.Envelope,map.Size));
System.Net.WebRequest myWebRequest = System.Net.WebRequest.Create(myUri);
myWebRequest.Method = resource.Type;
myWebRequest.Timeout = _TimeOut;
if (_Credentials != null)
myWebRequest.Credentials = _Credentials;
else
myWebRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
if (_Proxy != null)
myWebRequest.Proxy = _Proxy;
try
{
System.Net.HttpWebResponse myWebResponse = (System.Net.HttpWebResponse)myWebRequest.GetResponse();
System.IO.Stream dataStream = myWebResponse.GetResponseStream();
if (myWebResponse.ContentType.StartsWith("image"))
{
System.Drawing.Image img = System.Drawing.Image.FromStream(myWebResponse.GetResponseStream());
if (_ImageAttributes != null)
g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0,
img.Width, img.Height, GraphicsUnit.Pixel, this.ImageAttributes);
else
g.DrawImageUnscaled(img, 0, 0,map.Size.Width,map.Size.Height);
}
dataStream.Close();
myWebResponse.Close();
}
catch (System.Net.WebException webEx)
{
if (!_ContinueOnError)
throw (new SharpMap.Rendering.Exceptions.RenderException("There was a problem connecting to the WMS server when rendering layer '" + this.LayerName + "'", webEx));
else
//Write out a trace warning instead of throwing an error to help debugging WMS problems
System.Diagnostics.Trace.Write("There was a problem connecting to the WMS server when rendering layer '" + this.LayerName + "': " + webEx.Message);
}
catch (System.Exception ex)
{
if (!_ContinueOnError)
throw (new SharpMap.Rendering.Exceptions.RenderException("There was a problem rendering layer '" + this.LayerName + "'", ex));
else
//Write out a trace warning instead of throwing an error to help debugging WMS problems
System.Diagnostics.Trace.Write("There was a problem connecting to the WMS server when rendering layer '" + this.LayerName + "': " + ex.Message);
}
base.Render(g, map);
}
/// <summary>
/// Gets the URL for a map request base on current settings, the image size and boundingbox
/// </summary>
/// <param name="box">Area the WMS request should cover</param>
/// <param name="size">Size of image</param>
/// <returns>URL for WMS request</returns>
public string GetRequestUrl(SharpMap.Geometries.BoundingBox box, System.Drawing.Size size)
{
SharpMap.Web.Wms.Client.WmsOnlineResource resource = GetPreferredMethod();
System.Text.StringBuilder strReq = new StringBuilder(resource.OnlineResource);
if(!resource.OnlineResource.Contains("?"))
strReq.Append("?");
if (!strReq.ToString().EndsWith("&") && !strReq.ToString().EndsWith("?"))
strReq.Append("&");
strReq.AppendFormat(SharpMap.Map.numberFormat_EnUS, "REQUEST=GetMap&BBOX={0},{1},{2},{3}",
box.Min.X, box.Min.Y, box.Max.X, box.Max.Y);
strReq.AppendFormat("&WIDTH={0}&Height={1}", size.Width, size.Height);
strReq.Append("&Layers=");
if (_LayerList != null && _LayerList.Count > 0)
{
foreach (string layer in _LayerList)
strReq.AppendFormat("{0},", layer);
strReq.Remove(strReq.Length - 1, 1);
}
strReq.AppendFormat("&FORMAT={0}", _MimeType);
if (_SpatialReferenceSystem == string.Empty)
throw new ApplicationException("Spatial reference system not set");
if(wmsClient.WmsVersion=="1.3.0")
strReq.AppendFormat("&CRS={0}", _SpatialReferenceSystem);
else
strReq.AppendFormat("&SRS={0}", _SpatialReferenceSystem);
strReq.AppendFormat("&VERSION={0}", wmsClient.WmsVersion);
strReq.Append("&Styles=");
if (_StylesList != null && _StylesList.Count > 0)
{
foreach (string style in _StylesList)
strReq.AppendFormat("{0},", style);
strReq.Remove(strReq.Length - 1, 1);
}
return strReq.ToString();
}
/// <summary>
/// Returns the extent of the layer
/// </summary>
/// <returns>Bounding box corresponding to the extent of the features in the layer</returns>
public override SharpMap.Geometries.BoundingBox Envelope
{
get
{
return RootLayer.LatLonBoundingBox;
}
}
private Boolean _ContinueOnError;
/// <summary>
/// Specifies whether to throw an exception if the Wms request failed, or to just skip rendering the layer
/// </summary>
public Boolean ContinueOnError
{
get { return _ContinueOnError; }
set { _ContinueOnError = value; }
}
/// <summary>
/// Returns the type of the layer
/// </summary>
//public override SharpMap.Layers.Layertype LayerType
//{
// get { return SharpMap.Layers.Layertype.Wms; }
//}
#endregion
private SharpMap.Web.Wms.Client.WmsOnlineResource GetPreferredMethod()
{
//We prefer posting. Seek for supported post method
for (int i = 0; i < wmsClient.GetMapRequests.Length; i++)
if (wmsClient.GetMapRequests[i].Type.ToLower() == "post")
return wmsClient.GetMapRequests[i];
//Next we prefer the 'get' method
for (int i = 0; i < wmsClient.GetMapRequests.Length; i++)
if (wmsClient.GetMapRequests[i].Type.ToLower() == "get")
return wmsClient.GetMapRequests[i];
return wmsClient.GetMapRequests[0];
}
private System.Net.ICredentials _Credentials;
/// <summary>
/// Provides the base authentication interface for retrieving credentials for Web client authentication.
/// </summary>
public System.Net.ICredentials Credentials
{
get { return _Credentials; }
set { _Credentials = value; }
}
private System.Net.WebProxy _Proxy;
/// <summary>
/// Gets or sets the proxy used for requesting a webresource
/// </summary>
public System.Net.WebProxy Proxy
{
get { return _Proxy; }
set { _Proxy = value; }
}
private int _TimeOut;
/// <summary>
/// Timeout of webrequest in milliseconds. Defaults to 10 seconds
/// </summary>
public int TimeOut
{
get { return _TimeOut; }
set { _TimeOut = value; }
}
#region ICloneable Members
/// <summary>
/// Clones the object
/// </summary>
/// <returns></returns>
public override object Clone()
{
throw new NotImplementedException();
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -