1. 程式人生 > 其它 >C# soe 連線服務,並獲取Featureclass

C# soe 連線服務,並獲取Featureclass

直接上程式碼:(GetMapServer是做了一個返回多個服務的一個list封裝,主要根據pServiceName判斷)

//獲取服務
        public List<IAGSServerObjectName> GetMapServer(string pHostOrUrl, string pServiceName, bool pIsLAN)
        {

            List<IAGSServerObjectName> ListServerObjectName = new List<IAGSServerObjectName>();
            
string[] pServiceNameArr = pServiceName.Split(';'); //設定連線屬性 IPropertySet pPropertySet = new PropertySetClass(); if (pIsLAN) pPropertySet.SetProperty("machine", pHostOrUrl); else pPropertySet.SetProperty("url", pHostOrUrl);
//開啟連線 IAGSServerConnectionFactory pFactory = new AGSServerConnectionFactory(); //Type factoryType = Type.GetTypeFromProgID( // "esriGISClient.AGSServerConnectionFactory"); //IAGSServerConnectionFactory agsFactory = (IAGSServerConnectionFactory)
// Activator.CreateInstance(factoryType); IAGSServerConnection pConnection = pFactory.Open(pPropertySet, 0); //Get the image server. IAGSEnumServerObjectName pServerObjectNames = pConnection.ServerObjectNames; pServerObjectNames.Reset(); IAGSServerObjectName ServerObjectName = pServerObjectNames.Next(); while (ServerObjectName != null) { if (pServiceNameArr.Contains(ServerObjectName.Name) && (ServerObjectName.Type == "MapServer")) { ListServerObjectName.Add(ServerObjectName); break; } //if ((ServerObjectName.Name.ToLower() == pServiceName.ToLower()) && // (ServerObjectName.Type == "MapServer")) //{ // ListServerObjectName.Add(ServerObjectName); // //break; //} ServerObjectName = pServerObjectNames.Next(); } Marshal.ReleaseComObject(pFactory); //返回物件 return ListServerObjectName; }

呼叫GetMapServer

string serverurl="http://127.0.0.1:6080/arcgis/rest/services/test/Testxztd/MapServer";
//servername可多個用“;”分開;eg:"test/Testxztd;test/sss"
string servername="test/Testxztd"; 
var ids = [0,1];
List<IAGSServerObjectName> tempServerObjectName = GetMapServer("http://127.0.0.1:6080/arcgis/rest/services", servername, false); if (tempServerObjectName.Count > 0) {        //這裡是獲取為第一個為例子 var pServerObjectName = tempServerObjectName[0]; //query.WhereClause = "1=1";//設定SQL語句 foreach (var i in ids) { IName pName = (IName)pServerObjectName; //訪問地圖服務 IAGSServerObject pServerObject = (IAGSServerObject)pName.Open(); IMapServer pMapServer = (IMapServer)pServerObject; int id = Convert.ToInt32(i); IRecordSet set = new RecordSet(); try { //查詢 IQueryFilter qy = new QueryFilter(); set = pMapServer.QueryFeatureData(MapName, id, qy); ITable table = set.Table; IFeatureClass fc = table as IFeatureClass; if (found2) { qy.WhereClause = where; } IFeatureCursor cursor = fc.Search(qy, false); IFeature feature = null; ISpatialFilter sf = new SpatialFilterClass(); while ((feature = cursor.NextFeature()) != null) { //listFeature.Add(feature); //與本圖層進行疊加 sf.Geometry = feature.ShapeCopy as IGeometry; sf.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects; int temp = this.m_FeatureClass.FeatureCount(sf); if (temp > 0) { pList.Add(feature); } } Marshal.ReleaseComObject(cursor); } catch { // listFeature = null;//當查詢結果為空,即查詢運算式出錯時 } } }