AE开发实例代码总结.doc_第1页
AE开发实例代码总结.doc_第2页
AE开发实例代码总结.doc_第3页
AE开发实例代码总结.doc_第4页
AE开发实例代码总结.doc_第5页
已阅读5页,还剩28页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

精品文档1、AE开发技术文档一、数据加载问题1、加载个人数据库个人数据库是保存在Access中的数据库。加载方式有两种:通过名字和通过属性加载(也许不只是这两种,AE中实现同一功能可以有多种方式)A、通过设置属性加载个人数据库首先通过IPropertySet接口 定义要连接数据库的一些相关属性,在个人数据库中为数据库的路径,例如:IPropertySet Propset= new PropertySetClass(); Propset.SetProperty(“DATABASE”,”D:testAodatashMapdata.mdb”);当定义完属性并设置属性后就可以进行打开数据库的操作了,在ArcEngine开发中存在IWorkspaceFactory 、IFeatureWorkspace 、IFeatureClass 、IFeatureLayer等几个常用的用于打开和操作数据空间地物的接口。IWorkspaceFactory 是一个用于创建和打开工作空间的接口,它是一个抽象的接口,我们在具体应用时要用对应的工作空间实例化它,如下: IWorkspaceFactory Fact = new AccessWorkspaceFactoryClass (); 如果我们打开的是SDE 数据库就要用 SdeWorkspaceFactoryClass 实例化Fact。当我们完成了工作空间的实例化后就可以根据上边设置的属性打开对应的Access 数据库了。打开方式如下: IFeatureWorkspace Workspace = Fact.Open( Propset,0) as IFeatureWorkspace; 打开Access 工作空间后接下来的事情是做什么了,很简单,找到对应的地物类,赋给相应的层,通过MapControl 控件添加对应的层,然后刷新地图。以下为添加某一层的代码: IFeatureClass Fcls = Workspace.OpenFeatureClass(District);/找到对应的地物类 IFeatureLayer Fly = new FeatureLayerClass();/建立新图层 Fly.FeatureClass = Fcls; /将地物赋给相应的层MapCtr.Map.AddLayer (Fly);/添加层 MapCtr.ActiveView.Refresh();/刷新地图 其中District 为地物类的名字,MapCtr 为AE中MapControl 的对象。上边的通过属性设置加载数据空间的方式还可以用于SDE 数据库,在SDE 数据库加载时会介绍。以下为通过设置属性加载Access 数据库的完整C#代码:public void AddAccessDBByPro() IPropertySet Propset = new PropertySetClass(); Propset.SetProperty(DATABASE,D:testAodatashMapData.mdb ); IWorkspaceFactory Fact = new AccessWorkspaceFactoryClass (); IFeatureWorkspace Workspace = Fact.Open(Propset,0) as IFeatureWorkspace; IFeatureClass Fcls = Workspace.OpenFeatureClass (District); IFeatureLayer Fly = new FeatureLayerClass(); Fly.FeatureClass = Fcls; MapCtr.Map.AddLayer(Fly); MapCtr.ActiveView.Refresh(); B、通过数据库名字加载个人数据库public void AddAccessDBByName()IworkspaceName pWorkspaceName = new WorkspaceNameClass();pWorkspaceName.WorkspaceFactoryProgID= “esriDataSourcesGDB.AccessWorkspaceFactory”;pWorkspaceName.PathName =“D:testAodatashMapData.mdb”;IName n=pWorkspaceName as IName;IFeatureWorkspace Workspace= n.Open() as IFeatureWorkspace;IFeatureClass Fcls = Workspace.OpenFeatureClass (District); IFeatureLayer Fly = new FeatureLayerClass(); Fly.FeatureClass = Fcls; MapCtr.Map.AddLayer(Fly); MapCtr.ActiveView.Refresh(); 首先创建一个个人数据库工作空间名,再指定工作空间名的ProgID,以确定打开的是什么类型的工作空间,例如在打开Access个人数据库时,使用下面代码;IworkspaceName pWorkspaceName = new WorkspaceNameClass();pWorkspaceName.WorkspaceFactoryProgID= “esriDataSourcesGDB.AccessWorkspaceFactory”;pWorkspaceName.PathName =“D:testAodatashMapData.mdb”;属性WorkspaceFactoryProgID可以确保工作空间是AccessWorkspaceFactory,即个人数据库,同时要指定打开数据库路径。为了打开数据库,打开工作空间必须使用IName接口(思考有没有其他办法),所以接着定义IName、对象,并把工作空间名转换成IName类型,并赋值给IName对象,然后通过IName对象的open方法打开相应的工作空间,代码如下:IName n=pWorkspaceName as IName;IFeatureWorkspace Workspace= n.Open() as IFeatureWorkspace;2、AE开发编辑功能数据编辑问题1. 需要了解的概念长事务 短事务 编辑空间 抽象类,类,和组件对象类2、最基本的添加点线面功能添加点(方法有多种,基本思路一样,只是少量的接口有变化)通过FeatureClass的CreatFeature函数来添加地物。public void AddPointByStore()/得到要添加地物的图层IFeatureLayer l=MapCtr.Map.get_Layer(0) as IFeatureLayer;/定义一个地物类,把要编辑的图层转化为定义的地物类IFeatureClass fc= l.FeatureClass;/先定义一个编辑的工作空间,然后把它转化为数据集,最后转化为编辑工作空间,IWorkspaceEdit w=(fc as IDataset).Workspace as IWorkspaceEdit;IFeature f;IPoint p;/开始事务操作w.StartEditing(false); /?/开始编辑w.StartEditOperation();for(int i=0;i 0) axMapControl2.Map = new MapClass();for (int i = 0; i 0) if (e.button = 1) IPoint pPoint = new PointClass();pPoint.PutCoords(e.mapX, e.mapY); axMapControl1.CenterAt(pPoint); axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null); else if (e.button = 2) IEnvelope pEnv = axMapControl2.TrackRectangle(); axMapControl1.Extent = pEnv; axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null); 二、 显示属性表的信息我们知道ArcMap中的Table of Contents有很多功能,如下图:而ArcGIS Engine提供的TOCControl控件几乎没有提供,那么这些都是需要自己开发的,在这里我做一个显示属性表的功能。分析:要显示某一个图层的属性表,首先要将这个图层选中,然后在另外一个Form中将选中的这个图层的属性信息进行显示。方法:添加一个上下文菜单,添加一个新的Form窗体,在这个新的窗体上添加GridView控件,并在TOCControl控件的OnMouseDown事件下添加如下代码(pGlobalFeatureLayer是我定义的一个全局变量):private void axTOCControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.ITOCControlEvents_OnMouseDownEvent e) if (axMapControl1.LayerCount 0) esriTOCControlItem pItem = new esriTOCControlItem(); pGlobalFeatureLayer = new FeatureLayerClass(); IBasicMap pBasicMap = new MapClass(); object pOther = new object();object pIndex = new object(); axTOCControl1.HitTest(e.x, e.y, ref pItem, ref pBasicMap, ref pGlobalFeatureLayer, ref pOther, ref pIndex); if (e.button = 2) context.Show(axTOCControl1, e.x, e.y); 在上下文菜单的打开属性表的Click事件中添加如下代码: private void 打开属性表ToolStripMenuItem_Click(object sender, EventArgs e) FormTable Ft = new FormTable(pGlobalFeatureLayer as IFeatureLayer); Ft.Show(); 在新的窗体中添加一个将属性表显示到GridView控件中的函数,如下: public void Itable2Dtable() IFields pFields; pFields = pFeatureLayer.FeatureClass.Fields; dtGridView.ColumnCount = pFields.FieldCount; for (int i = 0; i pFields.FieldCount;i+ ) string fldName = pFields.get_Field(i).Name; dtGridView.Columnsi.Name = fldName; dtGridView.Columnsi.ValueType = System.Type.GetType(ParseFieldType(pFields.get_Field(i).Type); IFeatureCursor pFeatureCursor;pFeatureCursor = pFeatureLayer.FeatureClass.Search(null, false); IFeature pFeature; pFeature = pFeatureCursor.NextFeature(); while (pFeature != null) string fldValue = new stringpFields.FieldCount; for (int i = 0; i pFields.FieldCount; i+) string fldName; fldName = pFields.get_Field(i).Name; if (fldName=pFeatureLayer .FeatureClass .ShapeFieldName) fldValuei = Convert.ToString(pFeature.Shape.GeometryType); else fldValuei = Convert.ToString(pFeature.get_Value(i); dtGridView.Rows.Add(fldValue); pFeature = pFeatureCursor.NextFeature(); 数据库知识:DB2、Informix、PostgreSQL数据库打开方式以及方法:1、 打开个人数据库:public IWorkspace GetMDBWorkspace(String _pGDBName) IWorkspaceFactory pWsFac = new AccessWorkspaceFactoryClass(); IWorkspace pWs = pWsFac.OpenFromFile(_pGDBName,0);return pWs; 2、 打开文件数据库:public IWorkspace GetFGDBWorkspace(String _pGDBName) IWorkspaceFactory pWsFac = new FileGDBWorkspaceFactoryClass(); IWorkspace pWs = pWsFac.OpenFromFile(_pGDBName, 0); return pWs; 3、 打开SDE数据库:打开SDE数据库我们使用的是Open方法,要用这个方法,我们就要对IPropertySet对象设置,要打开SDE数据库,我们要获取SDE数据库的服务器地址,数据库实例,数据库,用户,密码等参数。而IPropertySet就好比一个Key-Value的对象,用来帮组我们设置这些,然后传到Open方法中。public IWorkspace GetSDEWorkspace(String _pServerIP, String _pInstance, String _pUser, String _pPassword, String _pDatabase, String _pVersion) ESRI.ArcGIS.esriSystem.IPropertySet pPropertySet = new ESRI.ArcGIS.esriSystem.PropertySetClass(); pPropertySet.SetProperty(SERVER, _pServerIP); pPropertySet.SetProperty(INSTANCE, _pInstance); pPropertySet.SetProperty(DATABASE, _pDatabase); pPropertySet.SetProperty(USER, _pUser); pPropertySet.SetProperty(PASSWORD, _pPassword); pPropertySet.SetProperty(VERSION, _pVersion); ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2 workspaceFactory; workspaceFactory = (ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2)new ESRI.ArcGIS.DataSourcesGDB.SdeWorkspaceFactoryClass(); return workspaceFactory.Open(pPropertySet, 0); 注意,打开SDE数据库需要用到ArcGIS Engine运行时的企业级许可。(即此时的License需要特别设置ArcEngine的Lisence应使用企业数据库类型,在From1_Load事件中初始化lisence,不能使用lisence控件设置,否则提示没有许可lisence。)具体的解决代码如下:private void From1_Load(object sender,EventArg e)IAoInitialize pao=new AoInitializeClass();pao.Initialize(esriLisenceProductCode.esriLisenceProductCodeEngineGeoDB);4.4.2 获取数据库中的要素类 在ArcGIS Engine中,要得到某一个类,首要要获取工作空间,然后进入工作空间再得到相应的东西,也就是以下两个步骤: l 获取工作空间; l 获取相应的要素类。 我们定义一个函数用来获取个人数据库的路径public string WsPath() string WsFileName=; OpenFileDialog OpenFile = new OpenFileDialog(); OpenFile.Filter = 个人数据库(MDB)|*.mdb; DialogResult DialogR = OpenFile.ShowDialog(); if (DialogR = DialogResult.Cancel) else WsFileName = OpenFile.FileName; return WsFileName; 要获取要素类,首先获取工作空间,然后对工作空间中的要素类进行遍历,代码如下: private void button2_Click(object sender, EventArgs e) string WsName = WsPath(); if (WsName != ) IWorkspaceFactory pWsFt = new AccessWorkspaceFactoryClass(); IWorkspace pWs = pWsFt.OpenFromFile(WsName, 0); IEnumDataset pEDataset =pWs.get_Datasets(esriDatasetType.esriDTAny);IDataset pDataset = pEDataset.Next(); while (pDataset != null) if (pDataset.Type =esriDatasetType.esriDTFeatureClass) FeatureClassBox.Items.Add(pDataset.Name); /如果是数据集 else if (pDataset.Type = esriDatasetType.esriDTFeatureDataset) IEnumDataset pESubDataset = pDataset.Subsets; IDataset pSubDataset = pESubDataset.Next(); while (pSubDataset != null) FeatureClassBox.Items.Add(pSubDataset.Name);/ -这是从哪里来的?-pSubDataset = pESubDataset.Next(); pDataset = pEDataset.Next(); FeatureClassBox.Text = FeatureClassBox.Items0.ToString(); 判断要素是否被编辑:ArcGIS Engine 提供了一个IDatasetEdit的接口用来判断我们的数据是否处于编辑状态,该接口只有一个方法,如下:示例代码如下:public bool ISEdit (IFeatureClass pFeatureClass) IDatasetEdit pDataEdit = pFeatureClass as IDatasetEdit; return pDataEdit.IsBeingEdited(); 如何删除要素类:IFeatureWorkspace这个接口主要是用于管理基于矢量数据的,如表,要素类,要素数据集等。要想删除一个要素类,那么必须先得到这个。,如要打开一个名称为PointTest的要素类,只需要在OpenFeatureClass中传入这个要素类的名称,代码如下: IWorkspaceFactory pWsFt = new AccessWorkspaceFactoryClass(); IWorkspace pWs = pWsFt.OpenFromFile(WsName, 0); IFeatureWorkspace pFWs = pWs as IFeatureWorkspace; IFeatureClass pFClass = pFWs.OpenFeatureClass(PointTest);如果是在ArcMap中,我们会切换到Catalog中然后进入相应的数据库,然后删除相应的要素类,这种操作会让我们想到FeatureClas这个对象会提供删除的方法,其实不然,这个删除的方法是定义在Dataset这个对象中。 private void button1_Click(object sender, EventArgs e) string WsName = WsPath(); if( WsName !=) IWorkspaceFactory pWsFt = new AccessWorkspaceFactoryClass(); IWorkspace pWs = pWsFt.OpenFromFile(WsName, 0); IFeatureWorkspace pFWs = pWs as IFeatureWorkspace; IFeatureClass pFClass = pFWs.OpenFeatureClass(PointTest);IDataset pDatset = pFClass as IDataset; pDatset.Delete(); 删除前:删除后:创建要素类创建要素类用到了IFeatureWorkspace.CreateFeatureClass方法。所需接口:IField,IFieldEdit,IFields,IFieldsEdit,IGeometryDef,IGeometryDefEdit接口(注意 在NET中,会遇到以“_2”结尾的属性,这些属性是可写的。)/定义一个几何字段,类型为点类型 ISpatialReference pSpatialReference = axMapControl1.ActiveView.FocusMap.SpatialReference; IGeometryDefEdit pGeoDef = new GeometryDefClass(); IGeometryDefEdit pGeoDefEdit = pGeoDef as IGeometryDefEdit; pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; pGeoDefEdit.SpatialReference_2 = pSpatialReference;/定义一个字段集合对象 IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields; /定义单个的字段 IField pField = new FieldClass(); IFieldEdit pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = SHAPE; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; pFieldsEdit.AddField(pField); pFieldEdit.GeometryDef_2 = pGeoDef; /定义单个的字段,并添加到字段集合中 pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = STCD; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldsEdit.AddField(pField); /定义单个的字段,并添加到字段集合中pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = SLM10; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldsEdit.AddField(pField); /定义单个的字段,并添加到字段集合中 pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = SLM20; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldsEdit.AddField(pField); /定义单个的字段,并添加到字段集合中 pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = SLM40; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldsEdit.AddField(pField); IWorkspaceFactory pFtWsFct = new AccessWorkspaceFactory();IFeatureWorkspace pWs = pFtWsFct.OpenFromFile(E:arcgisEngines.mdb, 0) as IFeatureWorkspace;IFeatureClass pFtClass = pWs.CreateFeatureClass(Test, pFields, null, null, esriFeatureType.esriFTSimple, SHAPE, null)/?如何改变字段的别名?public void ChangeFieldAliasName(ITable pTable, string pOriFieldName, string pDesFieldName) IClassSchemaEdit pClassSchemaEdit = (IClassSchemaEdit)pTable; /给对象加上锁 ISchemaLock pSchemaLock = (ISchemaLock)pTable; pSchemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); if (pTable.FindField(pOriFieldName) != -1) pClassSchemaEdit.AlterFieldAliasName(pOriFieldName, pDesFieldName); pSchemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);使用IFeatureSelection接口高亮显示在介绍IMap接口那一节,我们用IMap的IMap.SelectFeature方法实现了对查询的要素高亮显示,现在我们用IFeatureSelection接口实现查询高亮显示IMap pMap = axMapControl1.Map; IFeatureLayer pFeaturelayer = GetLayer(pMap, Roads) as IFeatureLayer; IFeatureSelection pFeatureSelection = pFeaturelayer as IFeatureSelection; IQueryFilter pQuery = new QueryFilterClass(); pQuery.WhereClause = TYPE= +paved; pFeatureSelection.SelectFeatures(pQuery,esriSelectionResultEnum.esriSelectionResultNew,false); axMapControl1.ActiveView.Refresh();其中GetLayer函数是我们写的一个根据图层的名称获取图层的方法,代码如下private ILayer GetLayer(IMap pMap, string LayerName) IEnumLayer pEnunLayer; pEnunLayer = pMap.get_Layers(null, false); pEnunLayer.Reset(); ILayer pRetureLayer; pRetureLayer = pEnunLayer.Next(); while (pRetureLayer != null) if (pRetureLayer.Name = LayerName) break; pRetureLayer = pEnunLayer.Next(); return pRetureLayer; 提问:以下三种方式的区别在哪里?axMapControl1.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null);axMapControl1.ActiveView.Refresh();axMapControl1.Refresh();创建符合要求的表:public ITable CreateTable(string _TablePath, string _TableName) IWorkspaceFactory pWks = new ShapefileWorkspaceFactoryClass(); IFeatureWorkspace pFwk = pWks.OpenFromFile(_TablePath, 0) as IFeatureWorkspace; /用于记

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论