AE開發(fā)實例代碼總結.doc_第1頁
AE開發(fā)實例代碼總結.doc_第2頁
AE開發(fā)實例代碼總結.doc_第3頁
AE開發(fā)實例代碼總結.doc_第4頁
AE開發(fā)實例代碼總結.doc_第5頁
已閱讀5頁,還剩28頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

精品文檔1、AE開發(fā)技術文檔一、數(shù)據(jù)加載問題1、加載個人數(shù)據(jù)庫個人數(shù)據(jù)庫是保存在Access中的數(shù)據(jù)庫。加載方式有兩種:通過名字和通過屬性加載(也許不只是這兩種,AE中實現(xiàn)同一功能可以有多種方式)A、通過設置屬性加載個人數(shù)據(jù)庫首先通過IPropertySet接口 定義要連接數(shù)據(jù)庫的一些相關屬性,在個人數(shù)據(jù)庫中為數(shù)據(jù)庫的路徑,例如:IPropertySet Propset= new PropertySetClass(); Propset.SetProperty(“DATABASE”,”D:testAodatashMapdata.mdb”);當定義完屬性并設置屬性后就可以進行打開數(shù)據(jù)庫的操作了,在ArcEngine開發(fā)中存在IWorkspaceFactory 、IFeatureWorkspace 、IFeatureClass 、IFeatureLayer等幾個常用的用于打開和操作數(shù)據(jù)空間地物的接口。IWorkspaceFactory 是一個用于創(chuàng)建和打開工作空間的接口,它是一個抽象的接口,我們在具體應用時要用對應的工作空間實例化它,如下: IWorkspaceFactory Fact = new AccessWorkspaceFactoryClass (); 如果我們打開的是SDE 數(shù)據(jù)庫就要用 SdeWorkspaceFactoryClass 實例化Fact。當我們完成了工作空間的實例化后就可以根據(jù)上邊設置的屬性打開對應的Access 數(shù)據(jù)庫了。打開方式如下: 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 的對象。上邊的通過屬性設置加載數(shù)據(jù)空間的方式還可以用于SDE 數(shù)據(jù)庫,在SDE 數(shù)據(jù)庫加載時會介紹。以下為通過設置屬性加載Access 數(shù)據(jù)庫的完整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、通過數(shù)據(jù)庫名字加載個人數(shù)據(jù)庫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(); 首先創(chuàng)建一個個人數(shù)據(jù)庫工作空間名,再指定工作空間名的ProgID,以確定打開的是什么類型的工作空間,例如在打開Access個人數(shù)據(jù)庫時,使用下面代碼;IworkspaceName pWorkspaceName = new WorkspaceNameClass();pWorkspaceName.WorkspaceFactoryProgID= “esriDataSourcesGDB.AccessWorkspaceFactory”;pWorkspaceName.PathName =“D:testAodatashMapData.mdb”;屬性WorkspaceFactoryProgID可以確保工作空間是AccessWorkspaceFactory,即個人數(shù)據(jù)庫,同時要指定打開數(shù)據(jù)庫路徑。為了打開數(shù)據(jù)庫,打開工作空間必須使用IName接口(思考有沒有其他辦法),所以接著定義IName、對象,并把工作空間名轉換成IName類型,并賦值給IName對象,然后通過IName對象的open方法打開相應的工作空間,代碼如下:IName n=pWorkspaceName as IName;IFeatureWorkspace Workspace= n.Open() as IFeatureWorkspace;2、AE開發(fā)編輯功能數(shù)據(jù)編輯問題1. 需要了解的概念長事務 短事務 編輯空間 抽象類,類,和組件對象類2、最基本的添加點線面功能添加點(方法有多種,基本思路一樣,只是少量的接口有變化)通過FeatureClass的CreatFeature函數(shù)來添加地物。public void AddPointByStore()/得到要添加地物的圖層IFeatureLayer l=MapCtr.Map.get_Layer(0) as IFeatureLayer;/定義一個地物類,把要編輯的圖層轉化為定義的地物類IFeatureClass fc= l.FeatureClass;/先定義一個編輯的工作空間,然后把它轉化為數(shù)據(jù)集,最后轉化為編輯工作空間,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控件幾乎沒有提供,那么這些都是需要自己開發(fā)的,在這里我做一個顯示屬性表的功能。分析:要顯示某一個圖層的屬性表,首先要將這個圖層選中,然后在另外一個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控件中的函數(shù),如下: 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(); 數(shù)據(jù)庫知識:DB2、Informix、PostgreSQL數(shù)據(jù)庫打開方式以及方法:1、 打開個人數(shù)據(jù)庫:public IWorkspace GetMDBWorkspace(String _pGDBName) IWorkspaceFactory pWsFac = new AccessWorkspaceFactoryClass(); IWorkspace pWs = pWsFac.OpenFromFile(_pGDBName,0);return pWs; 2、 打開文件數(shù)據(jù)庫:public IWorkspace GetFGDBWorkspace(String _pGDBName) IWorkspaceFactory pWsFac = new FileGDBWorkspaceFactoryClass(); IWorkspace pWs = pWsFac.OpenFromFile(_pGDBName, 0); return pWs; 3、 打開SDE數(shù)據(jù)庫:打開SDE數(shù)據(jù)庫我們使用的是Open方法,要用這個方法,我們就要對IPropertySet對象設置,要打開SDE數(shù)據(jù)庫,我們要獲取SDE數(shù)據(jù)庫的服務器地址,數(shù)據(jù)庫實例,數(shù)據(jù)庫,用戶,密碼等參數(shù)。而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數(shù)據(jù)庫需要用到ArcGIS Engine運行時的企業(yè)級許可。(即此時的License需要特別設置ArcEngine的Lisence應使用企業(yè)數(shù)據(jù)庫類型,在From1_Load事件中初始化lisence,不能使用lisence控件設置,否則提示沒有許可lisence。)具體的解決代碼如下:private void From1_Load(object sender,EventArg e)IAoInitialize pao=new AoInitializeClass();pao.Initialize(esriLisenceProductCode.esriLisenceProductCodeEngineGeoDB);4.4.2 獲取數(shù)據(jù)庫中的要素類 在ArcGIS Engine中,要得到某一個類,首要要獲取工作空間,然后進入工作空間再得到相應的東西,也就是以下兩個步驟: l 獲取工作空間; l 獲取相應的要素類。 我們定義一個函數(shù)用來獲取個人數(shù)據(jù)庫的路徑public string WsPath() string WsFileName=; OpenFileDialog OpenFile = new OpenFileDialog(); OpenFile.Filter = 個人數(shù)據(jù)庫(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); /如果是數(shù)據(jù)集 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的接口用來判斷我們的數(shù)據(jù)是否處于編輯狀態(tài),該接口只有一個方法,如下:示例代碼如下:public bool ISEdit (IFeatureClass pFeatureClass) IDatasetEdit pDataEdit = pFeatureClass as IDatasetEdit; return pDataEdit.IsBeingEdited(); 如何刪除要素類:IFeatureWorkspace這個接口主要是用于管理基于矢量數(shù)據(jù)的,如表,要素類,要素數(shù)據(jù)集等。要想刪除一個要素類,那么必須先得到這個。,如要打開一個名稱為PointTest的要素類,只需要在OpenFeatureClass中傳入這個要素類的名稱,代碼如下: IWorkspaceFactory pWsFt = new AccessWorkspaceFactoryClass(); IWorkspace pWs = pWsFt.OpenFromFile(WsName, 0); IFeatureWorkspace pFWs = pWs as IFeatureWorkspace; IFeatureClass pFClass = pFWs.OpenFeatureClass(PointTest);如果是在ArcMap中,我們會切換到Catalog中然后進入相應的數(shù)據(jù)庫,然后刪除相應的要素類,這種操作會讓我們想到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(); 刪除前:刪除后:創(chuàng)建要素類創(chuàng)建要素類用到了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接口那一節(jié),我們用IMap的IMap.SelectFeature方法實現(xiàn)了對查詢的要素高亮顯示,現(xiàn)在我們用IFeatureSelection接口實現(xiàn)查詢高亮顯示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函數(shù)是我們寫的一個根據(jù)圖層的名稱獲取圖層的方法,代碼如下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; 提問:以下三種方式的區(qū)別在哪里?axMapControl1.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null);axMapControl1.ActiveView.Refresh();axMapControl1.Refresh();創(chuàng)建符合要求的表: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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論