码迷,mamicode.com
首页 > 其他好文 > 详细

ArcGis SOE(server object extensions)之REST Template初体验

时间:2014-07-25 02:40:56      阅读:556      评论:0      收藏:0      [点我收藏+]

标签:des   cWeb   style   blog   http   java   color   os   

一、安装vs和arcgis server for .net(本例是vs2010、as 10),然后打开vs新建一个项目

bubuko.com,布布扣

二、项目创建成功后(本例项目名称是GetArea),打开.cs文件。

bubuko.com,布布扣

三、接口方法说明

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

            RestOperation sampleOper = new RestOperation("getGeometryArea",
                                                      new string[] { "sqlString" },
                                                      new string[] { "json" },
                                                      SampleOperHandler);

            rootRes.operations.Add(sampleOper);

            return rootRes;
        }

CreateRESTSchema函数中定义了在你的SOE中可以获取的Resource和Operation,每一个Resource和Operation都与一个处理函数(handle function)相关联。该处理函数用来在Resource或Operation被触发时,描述所要进行的处理操作。这里面就是你需要编写大量AO代码的地方。
  在REST SOE模板中,有一个处理resource逻辑的RootRESTHandler函数,他返回一个简单的JSON格式的字符串
  一个resource的handler function实际上是一个在SOESupport类库中的代理(delegate),这意味着所有的resource的处理函数都有一个类似的函数签名。  

SOE中接口和方法具体可以参考esri官网:http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/What_is_a_server_object_extension/0001000000zv000000/

四、附上我的项目中的GetArea.cs文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Collections.Specialized;

using System.Runtime.InteropServices;
using System.EnterpriseServices;

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 GetArea
{
    [ComVisible(true)]
    [Guid("8ea49511-1303-4ddc-aef9-3056741d7478")]
    [ClassInterface(ClassInterfaceType.None)]
    public class GetArea : ServicedComponent, IServerObjectExtension, IObjectConstruct, IRESTRequestHandler
    {
        private string soe_name;

        private IPropertySet configProps;
        private IServerObjectHelper serverObjectHelper;
        private ServerLogger logger;
        private IRESTRequestHandler reqHandler;
    
        public GetArea()
        {
            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()
        {
        }

        #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("getGeometryArea",
                                                      new string[] { "sqlString" },
                                                      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("hello", "world");

            return Encoding.UTF8.GetBytes(result.ToJson());
        }

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

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

            double geometryArea = GetGeometryArea(parm1Value);
            string strArea = geometryArea.ToString();
            JsonObject result = new JsonObject();
            result.AddString("GeometryArea", strArea);
            return Encoding.UTF8.GetBytes(result.ToJson());
        }


        public double GetGeometryArea(string sql)
        {
            IMapServer mapServer = serverObjectHelper.ServerObject as IMapServer;
            IMapServerObjects mapServerObjs = mapServer as IMapServerObjects;
            IMap map = mapServerObjs.get_Map(mapServer.DefaultMapName);
            IFeatureLayer pflayer = map.get_Layer(0) as IFeatureLayer;
            IFeatureClass pfc = pflayer.FeatureClass;

            ISpatialFilter psf = new SpatialFilterClass();
            psf.WhereClause = sql;
            psf.SpatialRel = ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum.esriSpatialRelIntersects;

            IFeatureCursor pfCursor = pfc.Search(psf as IQueryFilter, false);
            IFeature pfeature = pfCursor.NextFeature();

            if (pfeature == null)
                return -2;
            IPolygon polygon = pfeature.Shape as IPolygon;

            IArea pArea = polygon as IArea;

            return pArea.Area;
        }
    }
}

五、注册

1、注册com:在开始菜单-》visual studio 20110-》visual studio tools-》命令提示,打开命令提示工具。输入regasm  "文件路径"  /codebase  例如:regasm  “C:\Users\LZZ\Documents\Visual Studio 2010\Projects\Saturation\Saturation\bin\Debug\GetArea.dll”  /codebase

2、注册arcserver:新建一个控制台应该程序,加入以下代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Server;
using ESRI.ArcGIS;
using ESRI.ArcGIS.ADF.Connection.AGS;

namespace Registersoerest
{
    class Program
    {
        static void Main(string[] args)
        {
            // Must run as an user in the agsadmin group on the SOM.
            ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection agsServerConnection = new ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection();
            agsServerConnection.Host = "localhost";
            agsServerConnection.Connect();
            ESRI.ArcGIS.Server.IServerObjectAdmin2 serverObjectAdmin = agsServerConnection.ServerObjectAdmin as ESRI.ArcGIS.Server.IServerObjectAdmin2;
            // 扩展类名,和程序集同名 
            string extensionName = "GetArea"; // 注销时需要输入参数
            if (args.Length == 1 && args[0] == "/unregister")
            { // 检测是否已经注册SOE 
                if (ExtensionRegistered(serverObjectAdmin, extensionName))
                { // 删除SOE.
                    serverObjectAdmin.DeleteExtensionType("MapServer", extensionName); Console.WriteLine(extensionName + " successfully unregistered");
                }
                else
                    Console.WriteLine(extensionName + " is not registered with ArcGIS Server");
            }
            else
            {
                // 检查SOE是否已经注册. 
                if (!ExtensionRegistered(serverObjectAdmin, extensionName))
                { //获取属性页.
                    ESRI.ArcGIS.Server.IServerObjectExtensionType3 serverObjectExtensionType = serverObjectAdmin.CreateExtensionType() as ESRI.ArcGIS.Server.IServerObjectExtensionType3;
                    // 程序集的CLSID.
                    serverObjectExtensionType.CLSID = "GetArea.GetArea"; //serverObjectExtensionType.CLSID = "{C41E8674-F186-4a0c-8FC9-AAB7885EFD00}"; 
                    serverObjectExtensionType.Description = "Returns KML of a feature at the input location for the input layer";
                    serverObjectExtensionType.Name = extensionName;
                    // 显示的名称. 
                    serverObjectExtensionType.DisplayName = "GetArea";
                    // Required to enable exposure of SOE with ArcGIS Server REST endpoint.
                    serverObjectExtensionType.Info.SetProperty("SupportsREST", "true");
                    serverObjectExtensionType.Info.SetProperty("SupportsMSD", "false");
                    // 注册SOE.到Server
                    serverObjectAdmin.AddExtensionType("MapServer", serverObjectExtensionType); Console.WriteLine(extensionName + " successfully registered with ArcGIS Server");
                }
                else
                    Console.WriteLine(extensionName + " is already registered with ArcGIS Server");
            }
            Console.ReadLine();

        }

        static private bool ExtensionRegistered(ESRI.ArcGIS.Server.IServerObjectAdmin2 serverObjectAdmin, string extensionName)
        { // Get the extensions that extend MapServer server objects. 
            ESRI.ArcGIS.Server.IEnumServerObjectExtensionType extensionTypes = serverObjectAdmin.GetExtensionTypes("MapServer"); extensionTypes.Reset();
            // If an extension with a name matching that passed-in is found, return true.
            ESRI.ArcGIS.Server.IServerObjectExtensionType extensionType = extensionTypes.Next();
            while (extensionType != null)
            {
                if (extensionType.Name == extensionName) return true;
                extensionType = extensionTypes.Next();
            }
            // No matching extension was found, so return false. 
            return false;
        }


    }
}

注意:serverObjectExtensionType.CLSID = "GetArea.GetArea";必须与命名空间类名一致

运行该程序完成arcserver注册。

六、测试soe

1、发布地图服务,并勾选对应的服务

bubuko.com,布布扣

2、通过URL查看

bubuko.com,布布扣

3、根据条件查询并到得面积

bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣

ArcGis SOE(server object extensions)之REST Template初体验,布布扣,bubuko.com

ArcGis SOE(server object extensions)之REST Template初体验

标签:des   cWeb   style   blog   http   java   color   os   

原文地址:http://www.cnblogs.com/aegisada/p/3866790.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!