ArcEngine 加載地圖服務數據

加載MapServer

  1. 可以通過遍歷的方式,根據地圖服務的名稱進行判斷,然後加載

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using ESRI.ArcGIS.Carto;
    using ESRI.ArcGIS.Controls;
    using ESRI.ArcGIS.DataSourcesFile;
    using ESRI.ArcGIS.DataSourcesGDB;
    using ESRI.ArcGIS.Display;
    using ESRI.ArcGIS.esriSystem;
    using ESRI.ArcGIS.Geodatabase;
    using ESRI.ArcGIS.Geometry;
    using ESRI.ArcGIS.GISClient;
    using ESRI.ArcGIS.Output;
    using ESRI.ArcGIS.SystemUI;
    
    namespace DXApplication1
    {
        public partial class Form1 : DevExpress.XtraBars.Ribbon.RibbonForm
        {
            public Form1()
            {
                InitializeComponent();
                axTOCControl1.SetBuddyControl(axMapControl1.Object);
            }
    
            /// <summary>
            /// 家在地圖服務
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
            {
                LoadMapServer("http://localhost:6080/arcgis/admin", "SampleWorldCities", false);
            }
    
            /// <summary>
            /// 加載ArcGIS地圖服務
            /// </summary>
            /// <param name="hostOrUrl">服務器地址</param>
            /// <param name="serviceName">服務名稱</param>
            /// <param name="isLAN">是否爲局域網連接</param>
            private void LoadMapServer(string hostOrUrl, string serviceName, bool isLAN)
            {
                IPropertySet pPropertySet = new PropertySet();
                if (isLAN)
                {
                    pPropertySet.SetProperty("machine", hostOrUrl);
                }
                else
                {
                    pPropertySet.SetProperty("url", hostOrUrl);
                }
    
                // 連接服務器
                IAGSServerConnectionFactory pAGSServerConnectionFactory = new AGSServerConnectionFactory();
                IAGSServerConnection pAGSServerConnection = pAGSServerConnectionFactory.Open(pPropertySet, 0);
    
                // 獲取服務
                IAGSEnumServerObjectName pAGSEnumServerObjectName = pAGSServerConnection.ServerObjectNames;
                pAGSEnumServerObjectName.Reset();
                IAGSServerObjectName pAGSServerObjectName = pAGSEnumServerObjectName.Next();
                while (pAGSServerObjectName != null)
                {
                    if (pAGSServerObjectName.Name.ToLower() == serviceName.ToLower() && pAGSServerObjectName.Type == "MapServer")
                    {
                        break;
                    }
                    pAGSServerObjectName = pAGSEnumServerObjectName.Next();
                }
    
                // 獲取服務圖層
                IAGSServerObject pAGSServerObject = (pAGSServerObjectName as IName).Open() as IAGSServerObject;
                IMapServer pMapServer = pAGSServerObject as IMapServer;
                IMapServerLayer pMapServerLayer = new MapServerLayer() as IMapServerLayer;
                pMapServerLayer.ServerConnect(pAGSServerObjectName, pMapServer.DefaultMapName);
    
                // 添加數據圖層
                axMapControl1.AddLayer(pMapServerLayer as ILayer);
                axMapControl1.Refresh();
            }
        }
    }
    
  2. Engine 10.1及之後版本的話可以使用IMapServerRESTLayer加載

    IMapServerRESTLayer mapServerRESTLayer = new MapServerRESTLayerClass();
    mapServerRESTLayer.Connect(http://localhost:6080/arcgis/rest/services/XXXX/MapServer);
    
    axMapControl1.AddLayer(mapServerRESTLayer as ILayer);
    axMapControl1.Refresh();
    

加載帶Token的地圖服務

public ILayer AddServiceLayer(string url, string serviceName) 
{ 
	//create a new ArcGIS Server connection factory 
	ESRI.ArcGIS.GISClient.IAGSServerConnectionFactory2 connectionFactory; 
	connectionFactory = (ESRI.ArcGIS.GISClient.IAGSServerConnectionFactory2)new ESRI.ArcGIS.GISClient.AGSServerConnectionFactory(); 
	//create a property set to hold connection properties 
	IPropertySet connectionProps; 
	connectionProps = new PropertySet(); 
	//specify the URL for the server 
	connectionProps.SetProperty(URL, url); 
	//define username and password for the connection 
	connectionProps.SetProperty(USER, <USER>); 
	connectionProps.SetProperty(PASSWORD, <PASS>); 
	//open the server connection, pass in the property set, get a connection object back 
	ESRI.ArcGIS.GISClient.IAGSServerConnection gisServer; 
	gisServer = connectionFactory.Open(connectionProps, 0); 
	
	//get an enum of all server object names from the server (GIS services, i.e.) 
	ESRI.ArcGIS.GISClient.IAGSEnumServerObjectName soNames = gisServer.ServerObjectNames; 
	ESRI.ArcGIS.GISClient.IAGSServerObjectName3 soName; 
	//loop thru all services, find a map service called I3_Imagery_Prime_World_2D (high res imagery for the world) 
	soName = (ESRI.ArcGIS.GISClient.IAGSServerObjectName3)soNames.Next(); 
	do 
	{ 
		if ((soName.Type == MapServer) && (soName.Name == serviceName)) 
		{ 
			break; //found it 
		} 
		//keep searching the services ... 
		soName = (ESRI.ArcGIS.GISClient.IAGSServerObjectName3)soNames.Next(); 
	} while (soName != null); 
	//if the desired service was found ... 
	ILayer serviceLayer = null; 
	if (soName != null) 
	{ 
		//create a layer factory to make a new MapServerLayer from the server object name 
		ILayerFactory msLayerFactory; 
		msLayerFactory = new MapServerLayerFactory(); 
		//create an enum of layers using the name object (will contain only a single layer) 
		IEnumLayer enumLyrs = msLayerFactory.Create(soName); 
		//get the layer from the enum, store it in a MapServerLayer variable 
		IMapServerLayer mapServerLayer; 
		mapServerLayer = (IMapServerLayer)enumLyrs.Next(); 
		//make sure the layer is not empty (Nothing), then add it to the map 
		if (mapServerLayer != null) 
		{ 
			serviceLayer = ((ILayer)mapServerLayer); 
		} 
	} 
	return serviceLayer; 
} 

public IMapServer GetMapServer(string agsUrl, string serviceName) 
{ 
	IMapServer mapserver = null; 
	
	//Set connection propertyset. sample URL: http://host:port/arcgis/services. 
	IPropertySet propertySet = new PropertySetClass(); 
	propertySet.SetProperty(url, agsUrl); 
	
	//Open an AGS connection. 
	Type factoryType = Type.GetTypeFromProgID(esriGISClient.AGSServerConnectionFactory); 
	IAGSServerConnectionFactory agsFactory = (IAGSServerConnectionFactory)Activator.CreateInstance(factoryType); 
	IAGSServerConnection agsConnection = agsFactory.Open(propertySet, 0); 
	
	//Get the image server. 
	IAGSEnumServerObjectName agsServerObjectNames = agsConnection.ServerObjectNames; 
	agsServerObjectNames.Reset(); 
	IAGSServerObjectName agsServerObjectName = agsServerObjectNames.Next(); 
	while (agsServerObjectName != null) 
	{ 
	if ((agsServerObjectName.Name.ToLower() == serviceName.ToLower()) && (agsServerObjectName.Type == MapServer)) 
	{ 
		IName pName = (IName)agsServerObjectName; 
		IAGSServerObject agsServerObject = (IAGSServerObject)pName.Open(); 
		mapserver = (IMapServer)agsServerObject; 
		break; 
	} 
	agsServerObjectName = agsServerObjectNames.Next(); 
	} 
	
	//Return the image server object. 
	return mapserver; 
} 

public IServerObjectAdmin ConnectAGS(string host, string username, string password) 
{ 
	IServerObjectAdmin m_ServerObjectAdmin = null; 
	try 
	{ 
		IPropertySet propertySet = new PropertySetClass(); 
		propertySet.SetProperty(url, host); 
		propertySet.SetProperty(ConnectionMode, esriAGSConnectionMode.esriAGSConnectionModeAdmin); 
		propertySet.SetProperty(ServerType, esriAGSServerType.esriAGSServerTypeDiscovery); 
		propertySet.SetProperty(user, username); 
		propertySet.SetProperty(password, password); 
		propertySet.SetProperty(ALLOWINSECURETOKENURL, true); 
		IAGSServerConnectionName3 connectName = new AGSServerConnectionNameClass() as IAGSServerConnectionName3; 
		connectName.ConnectionProperties = propertySet; 
		IAGSServerConnectionAdmin agsAdmin = ((IName)connectName).Open() as IAGSServerConnectionAdmin; 
		
		m_ServerObjectAdmin = agsAdmin.ServerObjectAdmin; 
	} 

	catch (Exception exc) 
	{ 
		Console.WriteLine(連接失敗:{0}.Message:{1}, host, exc.Message); 
		return null; 
	} 
	return m_ServerObjectAdmin; 
}

加載本地的切片服務

直接調用IRasterLayer的CreateFromFilePath方法即可

參考資料

  1. http://zhihu.esrichina.com.cn/question/19418
  2. http://zhihu.esrichina.com.cn/question/12518
  3. https://blog.csdn.net/HerryDong/article/details/89632896
發佈了93 篇原創文章 · 獲贊 88 · 訪問量 37萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章