ArcGIS Engine基础(2)之各类数据源管理(AGSServer)

实现根据字符串获取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

    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xizhjxust_GIS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值