Arcgisserver(SOE)开发之获取指定图层、指定属性和属性值的json对象

SOE开发源码(C#)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Server;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.SOESupport;


//TODO: sign the project (project properties > signing tab > sign the assembly)
//      this is strongly suggested if the dll will be registered using regasm.exe <your>.dll /codebase


namespace RestSOE
{
    [ComVisible(true)]
    [Guid("6af5df1f-56e5-458d-95d8-8e1c00f7a734")]
    [ClassInterface(ClassInterfaceType.None)]
    [ServerObjectExtension("MapServer",//use "MapServer" if SOE extends a Map service and "ImageServer" if it extends an Image service.
        AllCapabilities = "",
                           DefaultCapabilities = "",
                           Description = "查询某一图层的某一字段的某一属性的一条数据",
                           DisplayName = "RestSOE",
                           Properties = "",
                           SupportsREST = true,
                           SupportsSOAP = false)]
    public class RestSOE : IServerObjectExtension, IObjectConstruct, IRESTRequestHandler
    {
        private string soe_name;
        private IPropertySet configProps;
        private IServerObjectHelper serverObjectHelper;
        private ServerLogger logger;
        private IRESTRequestHandler reqHandler;
        private IFeatureClass pfeatureclass ;
        private IMapServer3 mapServer;
        private IMapServerDataAccess dataAccess;

        public RestSOE()
        {
            soe_name = this.GetType().Name;
            logger = new ServerLogger();
            reqHandler = new SoeRestImpl(soe_name, CreateRestSchema()) as IRESTRequestHandler;
        }

        #region IServerObjectExtension Members

        public void Init(IServerObjectHelper pSOH)
        {
            serverObjectHelper = pSOH;
        }

        public void Shutdown()
        {
            this.soe_name = null;
            this.configProps = null;
            this.serverObjectHelper = null;
            this.logger = null;
            this.reqHandler = null;
            this.pfeatureclass = null;
            this.mapServer = null;
            this.dataAccess = null;
        }

        #endregion

        #region IObjectConstruct Members

        public void Construct(IPropertySet props)
        {
            configProps = props;
        }

        #endregion

        #region IRESTRequestHandler Members

        public string GetSchema()
        {
            return reqHandler.GetSchema();
        }

        public byte[] HandleRESTRequest(string Capabilities, string resourceName, string operationName, string operationInput, string outputFormat, string requestProperties, out string responseProperties)
        {
            return reqHandler.HandleRESTRequest(Capabilities, resourceName, operationName, operationInput, outputFormat, requestProperties, out responseProperties);
        }

        #endregion

        private RestResource CreateRestSchema()
        {
            RestResource rootRes = new RestResource(soe_name, false, RootResHandler);

            RestOperation sampleOper = new RestOperation("Query",
                                                      new string[] { "LayerName", "FieldName", "WhereClause" },
                                                      new string[] { "json" },
                                                      SampleOperHandler);

            rootRes.operations.Add(sampleOper);

            return rootRes;
        }

        private byte[] RootResHandler(NameValueCollection boundVariables, string outputFormat, string requestProperties, out string responseProperties)
        {
            responseProperties = null;
            JsonObject result = new JsonObject();
            result.AddString("描述", "查找指定图层的指定的字段的值的一条记录");
            return Encoding.UTF8.GetBytes(result.ToJson());
        }


        private byte[] SampleOperHandler(NameValueCollection boundVariables,
                                                  JsonObject operationInput,
                                                      string outputFormat,
                                                      string requestProperties,
                                                  out string responseProperties)
        {
            responseProperties = null;

            string LayerName;
            bool found = operationInput.TryGetString("LayerName", out LayerName);
            if (!found || string.IsNullOrEmpty(LayerName))
                throw new ArgumentNullException("LayerName");

            string FieldName;
            found = operationInput.TryGetString("FieldName", out FieldName);
            if (!found || string.IsNullOrEmpty(FieldName))
                throw new ArgumentNullException("FieldName");

            string WhereClause;
            found = operationInput.TryGetString("WhereClause", out WhereClause);
            if (!found || string.IsNullOrEmpty(WhereClause))
                throw new ArgumentNullException("WhereClause");

            List<IFeature> FeatureList = getFeatureList(LayerName, FieldName, WhereClause);
            return GetResultJson(FeatureList);
        }

        private List<IFeature> getFeatureList(string LayerName, string FieldName, string WhereClause)
        {

            List<IFeature> pFeatureList = new List<IFeature>();
                this.mapServer = (IMapServer3)serverObjectHelper.ServerObject;
                IMapLayerInfo layerInfo;
                IMapLayerInfos layerInfos = mapServer.GetServerInfo(mapServer.DefaultMapName).MapLayerInfos;

                //获取当前图层
                int LayerCount = layerInfos.Count;
                int Layerindex = 0;
                for (int i = 0; i < layerInfos.Count; i++)
                {
                    layerInfo = layerInfos.get_Element(i);
                    if (layerInfo.Name == LayerName)
                    {
                        Layerindex = i;
                        continue;
                    }
                }
                IMapServerDataAccess dataAccess = (IMapServerDataAccess)mapServer;
                this.pfeatureclass = (IFeatureClass)dataAccess.GetDataSource(mapServer.DefaultMapName, Layerindex);

                IQueryFilter pQueryFilter= new QueryFilterClass();
                pQueryFilter.WhereClause = FieldName + "='" + WhereClause + "'";
                IFeatureCursor pCursor= this.pfeatureclass.Search(pQueryFilter, false);
                IFeature pFeaeature= pCursor.NextFeature();
                while (pFeaeature != null) {
                    pFeatureList.Add(pFeaeature);
                    pFeaeature = pCursor.NextFeature();
                }
                return pFeatureList;
            }


        //获取一个要素集合中的所有属性
        private byte[] GetResultJson(List<IFeature> pList)
        {
            List<JsonObject> pJoList = null;
            string fieldvalue;
            if (pList != null)
            {
                IFeature pfea = null;
                pJoList = new List<JsonObject>();
                for (int i = 0; i <= pList.Count - 1; i++)
                {
                    pfea = pList[i];
                    IFields pFields = pfea.Class.Fields;
                    for (int j = 0; j <= pFields.FieldCount - 1; j++)
                    {
                        JsonObject jsonobj = new JsonObject();
                        fieldvalue = pfea.get_Value(j).ToString();
                        jsonobj.AddString(pFields.get_Field(j).AliasName, fieldvalue);
                        pJoList.Add(jsonobj);
                    }
                }
            }
            JsonObject resultJson = new JsonObject();
            resultJson.AddArray("attr", pJoList.ToArray());
            byte[] result = Encoding.UTF8.GetBytes(resultJson.ToJson());
            return result;
        }
    }
}

在这里插入图片描述在这里插入图片描述在这里插入图片描述小结:

  1. C#和java很像
  2. ArcObjects不熟练,后期应努力学习!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值