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;//当查询结果为空,即查询运算式出错时 } } }

 

posted @ 2022-03-30 10:06  小鱼写代码的过往  阅读(86)  评论(0编辑  收藏  举报