實作根據字元串擷取AGSServer圖層
private static AGSServerDataSource _instance = null;
// 擷取執行個體互斥鎖
private static readonly object _getInstanceMutexLocker = new object();
public static readonly char DATASOURCE_SPLIT_CHAR = '|';
public static readonly char LAYER_PATH_SEPARATOR = '\';
private AGSServerDataSource() { }
public static AGSServerDataSource CreateSingleton() {
if (_instance != null)
return _instance;
lock (_getInstanceMutexLocker) {
if (_instance == null)
_instance = new AGSServerDataSource();
}
return _instance;
}
public AGSServerLayerSetting GetAGSServerLayerSetting(string layerDataSourceString) {
if (string.IsNullOrEmpty(layerDataSourceString)) return null;
string[] dataSourceStrings = layerDataSourceString.Split(DATASOURCE_SPLIT_CHAR);
string url = dataSourceStrings[0];
string mapServerName = GetDataSourceValue(dataSourceStrings, "mapserver=");
string layers = GetDataSourceValue(dataSourceStrings, "layers=");
return new AGSServerLayerSetting(url, mapServerName, layers.Split(','));
}
public ILayer OpenLayer(string layerDataSourceString) {
try {
AGSServerLayerSetting agsLayerSetting = GetAGSServerLayerSetting(layerDataSourceString);
IAGSServerConnection connection = GetServerConnection(agsLayerSetting.Url);
IAGSEnumServerObjectName serverObjectNames = connection.ServerObjectNames;
Dictionary<string, IAGSServerObjectName> mapServerObjects = GetMapServerObjects(serverObjectNames);
if (mapServerObjects.ContainsKey(agsLayerSetting.MapServerName)) {
IAGSServerObjectName serverObjectName = mapServerObjects[agsLayerSetting.MapServerName];
IMapServerLayer layer = new MapServerLayerClass();
(layer as IDataLayer).Connect(serverObjectName as IName);
Dictionary<string, ILayer> layers = new Dictionary<string, ILayer>();
LayerWrapper.GetCompositeSubLayers(layer as ILayer, ref layers);
foreach (KeyValuePair<string, ILayer> kvp in layers) {
kvp.Value.Visible = agsLayerSetting.LayerNames.Contains(
kvp.Key.Substring(kvp.Key.IndexOf('\\') + 1).Replace('\\', LAYER_PATH_SEPARATOR));
}
return layer as ILayer;
}
}
catch {
throw;
}
return null;
}
public IEnvelope GetExtent(string layerDataSourceString) {
ILayer layer = null;
try {
layer = OpenLayer(layerDataSourceString);
}
catch {
throw;
}
IGeoDataset geoDataset = layer as IGeoDataset;
if (geoDataset == null) return null;
return geoDataset.Extent;
}
#region 公開方法
public IAGSServerConnection GetServerConnection(string url) {
try {
return GetServerConnection(url, null, null);
}
catch (Exception) {
throw;
}
}
public IAGSServerConnection GetServerConnection(string url, string username, string password) {
try {
IAGSServerConnectionFactory connectionFactory = Activator.CreateInstance(Type.GetTypeFromProgID(
"esriGISClient.AGSServerConnectionFactory")) as IAGSServerConnectionFactory;
IPropertySet propertySet = new PropertySet();
propertySet.SetProperty("url", url);
if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password)) {
propertySet.SetProperty("user", username);
propertySet.SetProperty("password", password);
}
return connectionFactory.Open(propertySet, 0);
}
catch (Exception) {
throw;
}
}
public Dictionary<string, IAGSServerObjectName> GetMapServerObjects(IAGSEnumServerObjectName serverObjectNames) {
Dictionary<string, IAGSServerObjectName> mapServerObjects = new Dictionary<string, IAGSServerObjectName>();
IAGSServerObjectName serverObjectName = null;
while ((serverObjectName = serverObjectNames.Next()) != null) {
if (serverObjectName.Type.Equals("MapServer"))
mapServerObjects.Add(serverObjectName.Name, serverObjectName);
}
return mapServerObjects;
}
#endregion
#region 私有方法
private string GetDataSourceValue(string[] dataSourceStrings, string dataSourceKey) {
foreach (string dataSourceString in dataSourceStrings) {
if (dataSourceString.StartsWith(dataSourceKey, StringComparison.CurrentCultureIgnoreCase))
return dataSourceString.Substring(dataSourceKey.Length);
}
return string.Empty;
}
#endregion
/// <summary>
/// ArcGIS Server圖層設定類
/// </summary>
public class AGSServerLayerSetting {
#region ArcGIS Server位址屬性
private string _url;
public string Url {
get {
return _url;
}
}
#endregion
#region MapServer名稱
private string _mapServerName;
public string MapServerName {
get {
return _mapServerName;
}
}
#endregion
#region 圖層名數組屬性
private string[] _layerNames;
public string[] LayerNames {
get {
return _layerNames;
}
}
#endregion
#region 構造函數
private AGSServerLayerSetting() { }
public AGSServerLayerSetting(string url, string mapServerName, string[] layerNames) {
_url = url;
_mapServerName = mapServerName;
_layerNames = layerNames;
}
#endregion
}