ArcEngine获取ArcGIS Server上的地图服务

利用ArcMap我们可以获取Arcgis Server上的地图服务,而ArcMap和ArcEngine都是基于ArcObjects的,基本上ArcMap能实现的功能ArcEngine都可以实现,因此我们也能利用ArcEngine在程序中加载Arcgis Server上的地图服务。

利用ArcEngine获取本地空间数据库内容 ,步骤是设置PropertySet 连接属性,创建工作空间工厂,根据连接信息打开工作空间,更加数据对象名称获取名称对象(IName),从名称对象中获取目标数据。Arcgis Server作为一个异地的数据库,那么获取其地图服务,步骤与上面相似,不过工作空间要改为服务器连接,具体实现过程如下:

1、设置连接属性

  IPropertySet pPropertySet = new PropertySet();

     if (pIsLAN)
       pPropertySet.SetProperty(“machine”, pHostOrUrl);//通过Lan(局域网)连接
   else
    pPropertySet.SetProperty(“url”, pHostOrUrl);//通过Internet连接

    pHostOrUrl表示服务器地址

2、获取服务器连接

   IAGSServerConnectionFactory pFactory = new AGSServerConnectionFactory();
  //创建服务器连接工作空间

   IAGSServerConnection pConnection = pFactory.Open(pPropertySet, 0);//第二个参数帮助文档里是hWnd表示句柄,不知道什么作用,直接赋值0即可
    //打开获取服务器,获取服务器连接

3、获取名称对象

  IAGSEnumServerObjectName pServerObjectNames = pConnection.ServerObjectNames;

  //获取服务器上所有服务的标识属性,即服务标识集合

  pServerObjectNames.Reset();
  //使指针指向服务开头
  IAGSServerObjectName ServerObjectName = pServerObjectNames.Next();
  //获取服务标识
  while (ServerObjectName != null)
  {
    if ((ServerObjectName.Name.ToLower() == pServiceName.ToLower()) &&(ServerObjectName.Type
== “MapServer”)) //获取的是地图服务,用MapServer表示

    {//判断获取所需服务
      break;
    }
    ServerObjectName = pServerObjectNames.Next(); //获取的名称对象,IAGSServerObjectName继承了IName
  }

4.获取地图服务

  ILayer lyr = null;
  try
  {
    IName pName = (IName)ServerObjectName;//获得服务对象名称
    IAGSServerObject pServerObject = (IAGSServerObject)pName.Open();//访问地图服务
    IMapServer pMapServer = (IMapServer)pServerObject;
    mapserver = pMapServer;//获取地图服务对象
    IMapServerLayer pMapServerLayer = new MapServerLayer() as IMapServerLayer;

    pMapServerLayer.ServerConnect(pServerObjectName, pMapServer.DefaultMapName);//连接地图服务,第一个参数为地图服务名称,第二个参数为数据框架名称(the
name of a data frame)

    lyr
= pMapServerLayer as ILayer;//将地图服务图层转化为ILayer这样Map控件可以添加进去

  }

 
 catch (Exception ex)

  {
    MessageBox.Show(“服务器配置信息错误,请到“系统管理-系统配置”设置”, “连接失败”);

  }

完整代码如下

  

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;


using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.GISClient;//导入服务器包
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;



namespace MapDocOperate
{
    /// <summary>
    /// ArcGisServer地图服务的添加
    /// </summary>
    public class MapServer
    {
        private IMapServer mapserver;
        /// <summary>
        /// 地图服务
        /// </summary>
        public IMapServer MpServer
        {
            get
            {
                return this.mapserver;
            }

        }
        /// <summary>
        /// 获取ArcGISServer服务图层
        /// </summary>
        /// <param name="serverUrl"></param>服务器地址
        /// <param name="mapservername"></param>地图服务名称
        /// <param name="isLan"></param>地图服务是局域网内还是互联网上的
        /// <returns></returns>
        public ILayer GetServerLyr(String serverUrl, String mapservername, bool isLan)
        {
            ILayer lyr = null;
            //获得服务对象名称
            try
            {
                IAGSServerObjectName pServerObjectName = GetMapServer(serverUrl, mapservername, isLan);//获取地图
                IName pName = (IName)pServerObjectName;
                //访问地图服务
                IAGSServerObject pServerObject = (IAGSServerObject)pName.Open();
                IMapServer pMapServer = (IMapServer)pServerObject;
                mapserver = pMapServer;//获取地图服务对象
                IMapServerLayer pMapServerLayer = new MapServerLayer() as IMapServerLayer;
                //连接地图服务

                pMapServerLayer.ServerConnect(pServerObjectName, pMapServer.DefaultMapName);
                //添加数据图层
                lyr = pMapServerLayer as ILayer;
            }
            catch (Exception ex)
            {
                MessageBox.Show("服务器配置信息错误,请到“系统管理-系统配置”设置", "连接失败");

            }
          
            return lyr;
        }
       

        /// <summary>
        /// 获取ArcGisServer地图服务标识,连接服务器
        /// </summary>
        /// <param name="pHostOrUrl"></param>服务器主机URL
        /// <param name="pServiceName"></param>服务名称
        /// <param name="pIsLAN"></param>主机是否是在局域网或者是互联网
        /// <returns></returns>
        private IAGSServerObjectName GetMapServer(string pHostOrUrl, string pServiceName, bool pIsLAN)
        {

            //设置连接属性
            IPropertySet pPropertySet = new PropertySet();
            if (pIsLAN)
                pPropertySet.SetProperty("machine", pHostOrUrl);
            else
                pPropertySet.SetProperty("url", pHostOrUrl);

            //打开连接

            IAGSServerConnectionFactory pFactory = new AGSServerConnectionFactory();
            //创建服务器连接工作空间

            IAGSServerConnection pConnection = pFactory.Open(pPropertySet, 0);
            //打开获取服务器

            IAGSEnumServerObjectName pServerObjectNames = pConnection.ServerObjectNames;
            //获取服务器上所有服务的标识属性,即服务标识集合
            pServerObjectNames.Reset();
            //使指针指向服务开头
            IAGSServerObjectName ServerObjectName = pServerObjectNames.Next();
            //获取服务标识
            while (ServerObjectName != null)
            {
                if ((ServerObjectName.Name.ToLower() == pServiceName.ToLower()) &&
                    (ServerObjectName.Type == "MapServer"))
                {//判断获取所需服务
                    break;
                }
                ServerObjectName = pServerObjectNames.Next();
            }
            //返回对象
            return ServerObjectName;//返回服务标识
        }
    }
}
复制代码

转载自:https://blog.csdn.net/u011193665/article/details/9262283

You may also like...