C#+Arcengine+ArcGIS编程总结,完善中…

C# Arcengine编程总结

1、、环境配置:

安装arcengine10.1、arcgis10.1、VS2010

2、、链接SDE工作空间两种方式

        public static IWorkspace WorkgroupArcSdeWorkspaceFromString(string connectionString)
        {
            try
            {
                Type factoryType = Type.GetTypeFromProgID(
                    “esriDataSourcesGDB.SdeWorkspaceFactory”);
                IWorkspaceFactory2 workspaceFactory2 = (IWorkspaceFactory2)
                    Activator.CreateInstance(factoryType);
                return workspaceFactory2.OpenFromString(connectionString, 0);
            }
            catch { return null; }
        }

   public static IWorkspace ConnectToTransactionalVersion(String server, String
           instance, String user, String password, String database, String version)
        {
            IPropertySet propertySet = new PropertySetClass();
            propertySet.SetProperty(“SERVER”, server);
            propertySet.SetProperty(“INSTANCE”, instance);
            propertySet.SetProperty(“DATABASE”, database);
            propertySet.SetProperty(“USER”, user);
            propertySet.SetProperty(“PASSWORD”, password);
            propertySet.SetProperty(“VERSION”, version);

            Type factoryType = Type.GetTypeFromProgID(
                “esriDataSourcesGDB.SdeWorkspaceFactory”);
            IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance
                (factoryType);
            try
            {
                return workspaceFactory.Open(propertySet, 0);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

3、坐标系统自定义

        public static Dictionary<string, ISpatialReference> SpatialReference()
        {

           //OpenXmlDocument(坐标系统文件Pro目录,”path”)
            string xmlpath = FilePathHelper.OpenXmlDocument(“System//SR_Xian1980_3_Degree_GK_Zone_35”, “path”);
            Dictionary<string, ISpatialReference> diction = new Dictionary<string, ISpatialReference>();
            ISpatialReferenceFactory pSRF = new SpatialReferenceEnvironmentClass();
            ISpatialReference spatial = pSRF.CreateESRISpatialReferenceFromPRJFile(xmlpath);
            diction.Add(“XI’AN_1980”, spatial);
            spatial = pSRF.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);
            diction.Add(“WGS_1984”, spatial);
            return diction;
        }

4、坐标系统转换列子,如果将WGS_1984转换XI’AN_1980

public override string getWGS1984ToXian1980(double x, double y, double z)
        {
            SDEHelper.getEngineLicense();
            IPoint ppoint = new PointClass();
            ppoint.PutCoords(x, y);
            ppoint.Z = z;
            IGeometry pgeometry = ppoint as IGeometry;
            pgeometry.Project(GISHelper.SpatialReference()[“WGS_1984”]);
            pgeometry.Project(GISHelper.SpatialReference()[“XI’AN_1980”]);
            string[] fileds={“X”,”Y”,”Z”};//转换后的值
            string[] values={ppoint.X+””,ppoint.Y+””,ppoint.Z+””};
            return Json.stringToJson(fileds,values);
        }

5、如果C#+arcengine发布为webservice时,一定要使用功能时先注册arcengine的license,否则arcengine功能服务使用

6、注册arcengine代码

       private static esriLicenseStatus pLicesestatus;//AE licese注册
        public static void getEngineLicense()
        {
            if (m_AoInitialize == null)
            {
                //ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);
                ////初始化License,保证可以访问SDE
                // m_AoInitialize = new AoInitializeClass();
                //pLicesestatus = (esriLicenseStatus)m_AoInitialize.IsProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB);
                //if (pLicesestatus == esriLicenseStatus.esriLicenseAvailable)
                //{
                //    if (pLicesestatus != esriLicenseStatus.esriLicenseCheckedOut)
                //        pLicesestatus = (esriLicenseStatus)m_AoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB);
                //}
                //绑定License
                //ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);
                ESRI.ArcGIS.RuntimeManager.BindLicense(ESRI.ArcGIS.ProductCode.EngineOrDesktop, ESRI.ArcGIS.LicenseLevel.Standard);
                //初始化License,保证可以访问SDE
                m_AoInitialize = new AoInitializeClass();
                esriLicenseStatus pLicesestatus = (esriLicenseStatus)m_AoInitialize.IsProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB);

                if (pLicesestatus == esriLicenseStatus.esriLicenseAvailable)
                {
                    if (pLicesestatus != esriLicenseStatus.esriLicenseCheckedOut)
                        pLicesestatus = (esriLicenseStatus)m_AoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB);
                }
                AoInitialize aoi = new AoInitialize();
                esriLicenseExtensionCode extensionCodes = esriLicenseExtensionCode.esriLicenseExtensionCodeSpatialAnalyst;
                esriLicenseExtensionCode extensionCode = esriLicenseExtensionCode.esriLicenseExtensionCode3DAnalyst;
                esriLicenseExtensionCode extensionCode3 = esriLicenseExtensionCode.esriLicenseExtensionCodeAeronautical;
                esriLicenseExtensionCode extensionCode4 = esriLicenseExtensionCode.esriLicenseExtensionCodeAGINSPIRE;
                esriLicenseExtensionCode extensionCode5 = esriLicenseExtensionCode.esriLicenseExtensionCodeAirports;
                esriLicenseExtensionCode extensionCode6 = esriLicenseExtensionCode.esriLicenseExtensionCodeGeoStats;
                esriLicenseExtensionCode extensionCode7 = esriLicenseExtensionCode.esriLicenseExtensionCodeRuntimeBasic;
                esriLicenseExtensionCode extensionCode8 = esriLicenseExtensionCode.esriLicenseExtensionCodeVector;
                esriLicenseExtensionCode extensionCode9 = esriLicenseExtensionCode.esriLicenseExtensionCodeServerStandardEdition;
                esriLicenseExtensionCode extensionCode10 = esriLicenseExtensionCode.esriLicenseExtensionCodeServerEnterprise;
                esriLicenseExtensionCode extensionCode11 = esriLicenseExtensionCode.esriLicenseExtensionCodeServerAdvancedEdition;
                esriLicenseExtensionCode extensionCode12 = esriLicenseExtensionCode.esriLicenseExtensionCodeSchematicsSDK;
                esriLicenseExtensionCode extensionCode13 = esriLicenseExtensionCode.esriLicenseExtensionCodeSchematics;
                esriLicenseExtensionCode extensionCode14 = esriLicenseExtensionCode.esriLicenseExtensionCodeRuntimeStandard;
                esriLicenseExtensionCode extensionCode15 = esriLicenseExtensionCode.esriLicenseExtensionCodeRuntimeAdvanced;
                esriLicenseExtensionCode extensionCode16 = esriLicenseExtensionCode.esriLicenseExtensionCodeDataReViewer;
                esriLicenseExtensionCode extensionCode17 = esriLicenseExtensionCode.esriLicenseExtensionCodeBusinessStandard;
                esriLicenseExtensionCode extensionCode18 = esriLicenseExtensionCode.esriLicenseExtensionCodeBusiness;
                esriLicenseExtensionCode extensionCode19 = esriLicenseExtensionCode.esriLicenseExtensionCodeBusinessStandard;
                esriLicenseExtensionCode[] obj = { extensionCodes,extensionCode,extensionCode3,extensionCode4,extensionCode5
                                                 ,extensionCode6,extensionCode7,extensionCode8,extensionCode9
                                                 ,extensionCode10,extensionCode11,extensionCode12,extensionCode13,extensionCode14
                                                 ,extensionCode15,extensionCode16,extensionCode17,extensionCode18,extensionCode19, };
                esriLicenseProductCode pro = esriLicenseProductCode.esriLicenseProductCodeEngine;
                if (aoi.IsProductCodeAvailable(pro) == esriLicenseStatus.esriLicenseAvailable &&
                    aoi.IsExtensionCodeAvailable(pro, extensionCode) == esriLicenseStatus.esriLicenseAvailable &&
                    aoi.IsExtensionCodeAvailable(pro, extensionCodes) == esriLicenseStatus.esriLicenseAvailable
                     )
                {
                    aoi.Initialize(pro);
                    for (int i = 0; obj.Length > i; i++)
                    {
                        aoi.CheckOutExtension(obj[i]);
                    }
                }
            }
        }

7、获取IfeatureClass的3种方式,通过加载shp路径和打开sde空间表、条件查询sde值

  //通过加载shp路径方式获取IFeatureClass  
public  IFeatureClass  getPathIFeatureClass (IGeometry polygon{      
            IWorkspaceFactory pworkspacefactory = new ShapefileWorkspaceFactoryClass();
            IWorkspace pworkspace = pworkspacefactory.OpenFromFile( “C:\\SHP”, 0);
            IFeatureWorkspace pfeatureworkspace = pworkspace as IFeatureWorkspace;
            IFeatureClass clipfeatureclass = pfeatureworkspace.OpenFeatureClass(“temp.shp”);
            IFeatureCursor pfeautrecursor = clipfeatureclass.Search(null, false);
            IFeature pfeature = pfeautrecursor.NextFeature();
            while (pfeature != null)
            {
                pfeature.Delete(); //删除元素
                pfeature = pfeautrecursor.NextFeature();
            }
            pfeature = clipfeatureclass.CreateFeature();
            pfeature.Shape = polygon;
            pfeature.Store();
            //关闭资源锁定  
            IWorkspaceFactoryLockControl ipWsFactoryLock;
            ipWsFactoryLock = (IWorkspaceFactoryLockControl)pworkspacefactory;
            if (ipWsFactoryLock.SchemaLockingEnabled)
            {
                ipWsFactoryLock.DisableSchemaLocking();
            }  
     }

   //打开sde方式获取IFeatureClass 

   public static IFeatureClass GetSDEIFeatureClass (IWorkspace work,string featureClassName)
        {
            try
            {
                IFeatureWorkspace pFeatureWorkSpace =
work
as IFeatureWorkspace;
                return pFeatureWorkSpace.OpenFeatureClass(featureClassName);//featureClassName是空间表名称
            }
            catch { return null; }
        }

  public IFeatureClass GetFeatureClassByFilter(IWrokSpace space,string filter, string featureClassName)
        {
            IFeatureWorkspace pFeatureWorkSpace = space as IFeatureWorkspace;
            IQueryDef pQueryDef = pFeatureWorkSpace.CreateQueryDef();
            pQueryDef.Tables = featureClassName;
            pQueryDef.SubFields = “*”;
            pQueryDef.WhereClause = filter;//filter查询条件相当于比如: fileter=” 1=1 and id=1″
            IFeatureClass pFeatureClass = null;
            try
            {
                IFeatureDataset fDataset = pFeatureWorkSpace.OpenFeatureQuery(“temp”, pQueryDef);
                IFeatureClassContainer featureClassContainer = fDataset as IFeatureClassContainer;
                pFeatureClass = featureClassContainer.get_ClassByName(“temp”);
            }
            catch { }
            return pFeatureClass;
        }

8、创建IGeometry几何对象(可以是IPoint、Polygon、polyline的等)

public IGeometry getGeometry(){
                IPointCollection ppointcollection = new PolygonClass();
                IPoint ppoint = new PointClass();
                ppoint.PutCoords(经度, 纬度);
ppoint.PutCoords(经度, 纬度);
                ppoint.PutCoords(经度, 纬度);
                object missing = Type.Missing;
                ppointcollection.AddPoint(ppoint, ref missing, ref missing);
                IPolygon polygon = ppointcollection as IPolygon;//可是是IPoint/IPolyline
                //通过坐标集合构建一个闭合面d
                IGeometry pgeometry = intersectPolygon as IGeometry;
                pgeometry.Project(坐标系统1980); //如果原坐标值是80坐标,那么先将坐标系统设置为1980
                 pgeometry.Project(坐标系统1984);//第二部才可设置为1984
                polygon .Close();
                return pgeometry;
}

9、实现增删改查

http://www.cnblogs.com/feilong3540717/archive/2011/03/11/1981842.html

10、栅格数据使用

http://blog.csdn.net/lysc_forever/article/details/7723354



转载自:https://blog.csdn.net/ml5271169588/article/details/44220055

You may also like...