基于ArcGIS Server的地址解析服务的实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Web;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.DataSourcesFile;
using System.IO;
using ESRI.ArcGIS.Server;

// 注意: 如果更改此处的类名“Service”,也必须更新 Web.config 和关联的 .svc 文件中对“Service”的引用。
public class AddressReverse: IAddressReverse
{   

  IPoint pPoint;
    public string AddressRevServer(double iLongtitude, double iLatitude)
    {

        //ArcGis Server的身份验证,服务是需要验证的
        ESRI.ArcGIS.ADF.Identity identity = new ESRI.ArcGIS.ADF.Identity("ArcGISWebServices", "1", "sse12a2db84c65");
        ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection gisServerConnection;
        gisServerConnection = new ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection("sse12a2db84c65", identity);
        gisServerConnection.Connect();
        IServerObjectManager4 serverObjectManager = (IServerObjectManager4)gisServerConnection.ServerObjectManager;
        IServerContext serverContext = serverObjectManager.CreateServerContext("", "");//创建一个空的服务器上下文
        string result = string.Empty;//保存最终的地址

                IFeatureWorkspace pFeatureWorkspace;
                IFeatureLayer pFeatureLayer;
                IFeatureClass cityfeatureClass;
                IFeatureClass xiangfeatureClass;
                IFeatureClass cunfeatureClass;
  
                string cityfilePath = @"E:\fjWGS1984"; //数据目录
                string cityfileName = @"city_Project.shp"; //市图层
                string xiangfilename = @"xiang_region_town_Project.shp";//乡镇图层
                string cunfilename = @"xiang_region_county_Project.shp";//村图层

                IWorkspaceFactory factory = (IWorkspaceFactory)serverContext.CreateObject("esriDataSourcesFile.ShapefileWorkspaceFactory");

                //创建point,服务里面涉及到com组件的不能用new,只能用CreateObject
                pPoint = serverContext.CreateObject("esriGeometry.Point") as IPoint;
                //给point赋经纬度值
                pPoint.PutCoords(iLongtitude, iLatitude);
      //打开工作空间
                pFeatureWorkspace = (IFeatureWorkspace)factory.OpenFromFile(cityfilePath, 0);
                pFeatureLayer = new FeatureLayerClass();
                pFeatureLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass(cityfileName);
                pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;
                cityfeatureClass = pFeatureWorkspace.OpenFeatureClass(cityfileName);

      //创建spatialFilter,用于空间查询
                ISpatialFilter spatialFilter = serverContext.CreateObject("esriGeoDatabase.SpatialFilter") as ESRI.ArcGIS.Geodatabase.SpatialFilter;
                spatialFilter.Geometry = pPoint;
                spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;// 空间关系是相交
                spatialFilter.GeometryField = cityfeatureClass.ShapeFieldName;
               
                //获取与点相交的市图层中的feature
                IFeatureCursor featureCursor = cityfeatureClass.Search(spatialFilter, false);
                IFeature resultFeature = featureCursor.NextFeature();
                if (resultFeature != null)
                {
                    result+=resultFeature.get_Value(resultFeature.Fields.FindField("SNAME")).ToString();//获取城市名称
                }


           //获取县名称
                pFeatureWorkspace = (IFeatureWorkspace)factory.OpenFromFile(cityfilePath, 0);
                pFeatureLayer = new FeatureLayerClass();
                pFeatureLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass(cunfilename);
                pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;
                cunfeatureClass = pFeatureWorkspace.OpenFeatureClass(cunfilename);

                IFeatureCursor featureCursorcun = cunfeatureClass.Search(spatialFilter, false);
                IFeature resultFeaturecun = featureCursorcun.NextFeature();
                if (resultFeaturecun != null)
                {
                    result += resultFeaturecun.get_Value(resultFeaturecun.Fields.FindField("XNAME")).ToString();
                }

                //获取乡镇名称
                pFeatureWorkspace = (IFeatureWorkspace)factory.OpenFromFile(cityfilePath, 0);
                pFeatureLayer = new FeatureLayerClass();
                pFeatureLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass(xiangfilename);
                pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;
                xiangfeatureClass = pFeatureWorkspace.OpenFeatureClass(xiangfilename);

                IFeatureCursor featureCursortown = xiangfeatureClass.Search(spatialFilter, false);
                IFeature resultFeaturetown = featureCursortown.NextFeature();
                if (resultFeaturetown != null)
                {
                    result += resultFeaturetown.get_Value(resultFeaturetown.Fields.FindField("ZNAME")).ToString();
                }

                return result;
    }

}

posted on 2011-04-28 20:09  bin糖葫芦  阅读(1275)  评论(2编辑  收藏  举报

导航