📄 wmslayer.cs
字号:
// Copyright 2005, 2006 - Morten Nielsen (www.iter.dk)
//
// This file is part of SharpMap.
// SharpMap is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SharpMap is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public License
// along with SharpMap; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Imaging;
using System.Drawing;
namespace SharpMap.Layers
{
/// <summary>
/// Web Map Service layer
/// </summary>
/// <remarks>
/// The WmsLayer is currently very basic and doesn't support automatic fetching of the WMS Service Description.
/// Instead you would have to add the nessesary parameters to the URL,
/// and the WmsLayer will set the remaining BoundingBox property and proper requests that changes between the requests.
/// See the example below.
/// </remarks>
/// <example>
/// The following example creates a map with a WMS layer the Demis WMS Server
/// <code lang="C#">
/// myMap = new SharpMap.Map(new System.Drawing.Size(500,250);
/// string wmsUrl = "http://www2.demis.nl/mapserver/request.asp";
/// SharpMap.Layers.WmsLayer myLayer = new SharpMap.Layers.WmsLayer("Demis WMS", myLayer);
/// myLayer.AddLayer("Bathymetry");
/// myLayer.AddLayer("Countries");
/// myLayer.AddLayer("Topography");
/// myLayer.AddLayer("Hillshading");
/// myLayer.SetImageFormat(layWms.OutputFormats[0]);
/// myLayer.SpatialReferenceSystem = "EPSG:4326";
/// myMap.Layers.Add(myLayer);
/// myMap.Center = new SharpMap.Geometries.Point(0, 0);
/// myMap.Zoom = 360;
/// myMap.MaximumZoom = 360;
/// myMap.MinimumZoom = 0.1;
/// </code>
/// </example>
public class WmsLayer : SharpMap.Layers.Layer
{
private SharpMap.Web.Wms.Client wmsClient;
private string _MimeType = "";
/// <summary>
/// Initializes a new layer, and downloads and parses the service description
/// </summary>
/// <remarks>In and ASP.NET application the service description is automatically cached for 24 hours when not specified</remarks>
/// <param name="layername"></param>
/// <param name="url"></param>
public WmsLayer(string layername, string url) : this(layername,url,new TimeSpan(24,0,0))
{
}
private List<string> _LayerList;
/// <summary>
/// Gets the list of enabled layers
/// </summary>
public List<string> LayerList
{
get { return _LayerList; }
}
/// <summary>
/// Adds a layer to WMS request
/// </summary>
/// <remarks>Layer names are case sensitive.</remarks>
/// <param name="name">Name of layer</param>
/// <exception cref="System.ArgumentException">Throws an exception is an unknown layer is added</exception>
public void AddLayer(string name)
{
if(!LayerExists(wmsClient.Layer,name))
throw new ArgumentException("Cannot add WMS Layer - Unknown layername");
_LayerList.Add(name);
}
/// <summary>
/// Recursive method for checking whether a layername exists
/// </summary>
/// <param name="layer"></param>
/// <param name="name"></param>
/// <returns></returns>
private bool LayerExists(SharpMap.Web.Wms.Client.WmsServerLayer layer, string name)
{
if(name == layer.Name) return true;
foreach (SharpMap.Web.Wms.Client.WmsServerLayer childlayer in layer.ChildLayers)
if (LayerExists(childlayer,name)) return true;
return false;
}
/// <summary>
/// Removes a layer from the layer list
/// </summary>
/// <param name="name">Name of layer to remove</param>
public void RemoveLayer(string name)
{
_LayerList.Remove(name);
}
/// <summary>
/// Removes the layer at the specified index
/// </summary>
/// <param name="index"></param>
public void RemoveLayerAt(int index)
{
_LayerList.RemoveAt(index);
}
/// <summary>
/// Removes all layers
/// </summary>
public void RemoveAllLayers()
{
_LayerList.Clear();
}
private List<string> _StylesList;
/// <summary>
/// Gets the list of enabled styles
/// </summary>
public List<string> StylesList
{
get { return _StylesList; }
}
/// <summary>
/// Adds a style to the style collection
/// </summary>
/// <param name="name">Name of style</param>
/// <exception cref="System.ArgumentException">Throws an exception is an unknown layer is added</exception>
public void AddStyle(string name)
{
if (!StyleExists(wmsClient.Layer, name))
throw new ArgumentException("Cannot add WMS Layer - Unknown layername");
_StylesList.Add(name);
}
/// <summary>
/// Recursive method for checking whether a layername exists
/// </summary>
/// <param name="layer">layer</param>
/// <param name="name">name of style</param>
/// <returns>True of style exists</returns>
private bool StyleExists(SharpMap.Web.Wms.Client.WmsServerLayer layer, string name)
{
foreach(SharpMap.Web.Wms.Client.WmsLayerStyle style in layer.Style)
if (name == style.Name) return true;
foreach (SharpMap.Web.Wms.Client.WmsServerLayer childlayer in layer.ChildLayers)
if (StyleExists(childlayer, name)) return true;
return false;
}
/// <summary>
/// Removes a style from the collection
/// </summary>
/// <param name="name">Name of style</param>
public void RemoveStyle(string name)
{
_StylesList.Remove(name);
}
/// <summary>
/// Removes a style at specified index
/// </summary>
/// <param name="index">Index</param>
public void RemoveStyleAt(int index)
{
_StylesList.RemoveAt(index);
}
/// <summary>
/// Removes all styles from the list
/// </summary>
public void RemoveAllStyles()
{
_StylesList.Clear();
}
/// <summary>
/// Initializes a new layer, and downloads and parses the service description
/// </summary>
/// <param name="layername"></param>
/// <param name="url">Url of service description</param>
/// <param name="cachetime">Time for caching Service Description (ASP.NET only)</param>
public WmsLayer(string layername, string url, TimeSpan cachetime)
{
_TimeOut = 10000;
this.LayerName = layername;
_ContinueOnError = true;
if (System.Web.HttpContext.Current != null && System.Web.HttpContext.Current.Cache["SharpMap_WmsClient_" + url] != null)
{
wmsClient = (SharpMap.Web.Wms.Client)System.Web.HttpContext.Current.Cache["SharpMap_WmsClient_" + url];
}
else
{
wmsClient = new SharpMap.Web.Wms.Client(url, _Proxy);
if (System.Web.HttpContext.Current != null)
System.Web.HttpContext.Current.Cache.Insert("SharpMap_WmsClient_"+url, wmsClient, null,
System.Web.Caching.Cache.NoAbsoluteExpiration, cachetime);
}
//Set default mimetype - We prefer compressed formats
if (OutputFormats.Contains("image/jpeg")) _MimeType = "image/jpeg";
else if (OutputFormats.Contains("image/png")) _MimeType = "image/png";
else if (OutputFormats.Contains("image/gif")) _MimeType = "image/gif";
else //None of the default formats supported - Look for the first supported output format
{
bool formatSupported = false;
foreach (System.Drawing.Imaging.ImageCodecInfo encoder in System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders())
if (OutputFormats.Contains(encoder.MimeType.ToLower()))
{
formatSupported = true;
_MimeType = encoder.MimeType;
break;
}
if(!formatSupported)
throw new ArgumentException("GDI+ doesn't not support any of the mimetypes supported by this WMS service");
}
_LayerList = new List<string>();
_StylesList = new List<string>();
}
/// <summary>
/// Sets the image type to use when requesting images from the WMS server
/// </summary>
/// <remarks>
/// <para>See the <see cref="OutputFormats"/> property for a list of available mime types supported by the WMS server</para>
/// </remarks>
/// <exception cref="ArgumentException">Throws an exception if either the mime type isn't offered by the WMS
/// or GDI+ doesn't support this mime type.</exception>
/// <param name="mimeType">Mime type of image format</param>
public void SetImageFormat(string mimeType)
{
if (!OutputFormats.Contains(mimeType))
throw new ArgumentException("WMS service doesn't not offer mimetype '" + mimeType + "'");
//Check whether SharpMap supports the specified mimetype
bool formatSupported = false;
foreach (System.Drawing.Imaging.ImageCodecInfo encoder in System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders())
if (encoder.MimeType.ToLower() == mimeType.ToLower())
{
formatSupported = true;
break;
}
if (!formatSupported)
throw new ArgumentException("GDI+ doesn't not support mimetype '" + mimeType + "'");
_MimeType = mimeType;
}
/// <summary>
/// Gets the hiarchial list of available WMS layers from this service
/// </summary>
public SharpMap.Web.Wms.Client.WmsServerLayer RootLayer
{
get { return wmsClient.Layer; }
}
/// <summary>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -