AE學習筆記之地圖編輯(上)
地圖編輯這一部分還是挺複雜的,先看一下整個編輯過程的流程圖(圖1),再繼續介紹每一步的細節資訊和程式碼。
圖1 流程圖
具體步驟如下:
一、新建一個編輯工具欄
工具欄的樣式如下,包括一下幾個內容:編輯、選擇、取消、重複、節點、移動、目標圖層(Combox列表)、屬性、儲存、退出。各個控制元件的功能介紹如下:
編輯:該按鈕是開始編輯按鈕。
選擇:單擊該按鈕可以選擇地圖上一定區域內的要素。
取消:取消本次編輯。
重複:回覆上次編輯。
節點:對要素的節點資訊進行編輯。
移動:移動選擇的要素。
目標圖層:顯示當前地圖中包含的向量圖層名。
屬性:顯示選擇要素的屬性資訊。
儲存:儲存當前編輯內容。
退出:退出本次編輯。
在對地圖編輯之前需要將當前MapControl中包含的地圖傳入該類中。因此需要在類的Load函式中獲取需要編輯的地圖。
二、地圖編輯
不同的圖層所具有的要素型別也不相同,整體可分為:單點、多點、線、面。在單擊編輯時,開啟編輯空間的開始編輯。
//開始編輯 public void StartEditing() { try { if (InEdit() == -1) return; if (!(m_pCurrentLayer is ESRI.ArcGIS.Carto.IGeoFeatureLayer)) return; ESRI.ArcGIS.Carto.IFeatureLayer pFeatureLayer = this.m_pCurrentLayer as ESRI.ArcGIS.Carto.IFeatureLayer; ESRI.ArcGIS.Geodatabase.IDataset pDataset = pFeatureLayer.FeatureClass as ESRI.ArcGIS.Geodatabase.IDataset; ESRI.ArcGIS.Geodatabase.IWorkspaceEdit pWorkspaceEdit = pDataset.Workspace as ESRI.ArcGIS.Geodatabase.IWorkspaceEdit; if (!pWorkspaceEdit.IsBeingEdited()) { pWorkspaceEdit.StartEditing(true); pWorkspaceEdit.EnableUndoRedo(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
以上包含了三步:
1.得到向量圖層中的要素類(FeatureClass);
2.將要素類轉化為資料集(IDataset);
3.把要素集的工作空間轉化為編輯工作空間,開始編輯(StartEditing(true))。其中true表示是否啟用UndoRedo,真表示啟用。下面就接著:EnableUndoRedo();
由於編輯時需要選擇圖層,就以目前圖層中選擇的圖層作為當前編輯圖層。然後根據圖層的要素型別進行編輯:
//編輯 public void NewFeatureMouseDown(int x, int y) { ESRI.ArcGIS.Display.INewLineFeedback pNewLineFeedback; ESRI.ArcGIS.Display.INewMultiPointFeedback pMultiPointFeedback; ESRI.ArcGIS.Display.INewPolygonFeedback pPolygonFeedback; try { if (this.InEdit() == -1) return; ESRI.ArcGIS.Carto.IFeatureLayer pFeatureLayer = m_pCurrentLayer as ESRI.ArcGIS.Carto.IFeatureLayer; ESRI.ArcGIS.Carto.IActiveView pActiveView = m_pMap as ESRI.ArcGIS.Carto.IActiveView; ESRI.ArcGIS.Geometry.IPoint pPoint = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y); if (!m_bInUse) { this.m_pMap.ClearSelection(); switch (pFeatureLayer.FeatureClass.ShapeType) { case (ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint): CreateFeature(pPoint); break; case (ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryMultipoint): m_bInUse = true; this.m_pFeedback = new ESRI.ArcGIS.Display.NewMultiPointFeedbackClass(); pMultiPointFeedback = m_pFeedback as ESRI.ArcGIS.Display.INewMultiPointFeedback; m_pPointCollection = new ESRI.ArcGIS.Geometry.Multipoint(); pMultiPointFeedback.Start(m_pPointCollection, pPoint); break; case (ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline): m_bInUse = true; this.m_pFeedback = new ESRI.ArcGIS.Display.NewLineFeedbackClass(); pNewLineFeedback = m_pFeedback as ESRI.ArcGIS.Display.INewLineFeedback; pNewLineFeedback.Start(pPoint); break; case (ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon): m_bInUse = true; this.m_pFeedback = new ESRI.ArcGIS.Display.NewPolygonFeedbackClass(); pPolygonFeedback = m_pFeedback as ESRI.ArcGIS.Display.INewPolygonFeedback; pPolygonFeedback.Start(pPoint); break; } if (m_pFeedback != null) m_pFeedback.Display = pActiveView.ScreenDisplay; } else { if (this.m_pFeedback is ESRI.ArcGIS.Display.INewMultiPointFeedback) { object obj = System.Reflection.Missing.Value; m_pPointCollection.AddPoint(pPoint, ref obj, ref obj); } else if (this.m_pFeedback is ESRI.ArcGIS.Display.INewLineFeedback) { pNewLineFeedback = this.m_pFeedback as ESRI.ArcGIS.Display.INewLineFeedback; pNewLineFeedback.AddPoint(pPoint); } else if (this.m_pFeedback is ESRI.ArcGIS.Display.INewPolygonFeedback) { pPolygonFeedback = this.m_pFeedback as ESRI.ArcGIS.Display.INewPolygonFeedback; pPolygonFeedback.AddPoint(pPoint); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
結束編輯:
public void NewFeatureEnd()
{
ESRI.ArcGIS.Geometry.IGeometry pGeometry = null;
ESRI.ArcGIS.Geometry.IPointCollection pPointCol = null;
try
{
if (this.m_pFeedback is ESRI.ArcGIS.Display.INewMultiPointFeedback)
{
ESRI.ArcGIS.Display.INewMultiPointFeedback pNewMultiPointFeedback = m_pFeedback as ESRI.ArcGIS.Display.INewMultiPointFeedback;
pNewMultiPointFeedback.Stop(); //返回值為Void
pGeometry = this.m_pPointCollection as ESRI.ArcGIS.Geometry.IGeometry;
}
else if (this.m_pFeedback is ESRI.ArcGIS.Display.INewLineFeedback)
{
ESRI.ArcGIS.Display.INewLineFeedback pNewLineFeedback = this.m_pFeedback as ESRI.ArcGIS.Display.INewLineFeedback;
ESRI.ArcGIS.Geometry.IPolyline pPolyline = pNewLineFeedback.Stop();
pPointCol = pPolyline as ESRI.ArcGIS.Geometry.IPointCollection;
if (pPointCol.PointCount < 2)
{
MessageBox.Show("至少輸入兩個節點");
return;
}
else
pGeometry = pPointCol as ESRI.ArcGIS.Geometry.IGeometry;
}
else if (this.m_pFeedback is ESRI.ArcGIS.Display.INewPolygonFeedback)
{
ESRI.ArcGIS.Display.INewPolygonFeedback pNewPolygonFeedback = this.m_pFeedback as ESRI.ArcGIS.Display.INewPolygonFeedback;
ESRI.ArcGIS.Geometry.IPolygon pPolygon = pNewPolygonFeedback.Stop();
pPointCol = pPolygon as ESRI.ArcGIS.Geometry.IPointCollection;
if (pPointCol.PointCount < 3)
{
MessageBox.Show("至少輸入三個節點");
return;
}
else
pGeometry = pPointCol as ESRI.ArcGIS.Geometry.IGeometry;
}
CreateFeature(pGeometry);
m_pFeedback = null;
m_bInUse = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
結束編輯中的CreatFeature()函式:
public void CreateFeature(ESRI.ArcGIS.Geometry.IGeometry pGeometry)
{
try
{
if (pGeometry == null) return;
if (this.m_pCurrentLayer == null) return;
ESRI.ArcGIS.Geodatabase.IWorkspaceEdit pWorkspaceEdit = GetWorkspaceEdit();
ESRI.ArcGIS.Carto.IFeatureLayer pFeatureLayer = this.m_pCurrentLayer as ESRI.ArcGIS.Carto.IFeatureLayer;
ESRI.ArcGIS.Geodatabase.IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
pWorkspaceEdit.StartEditOperation();
ESRI.ArcGIS.Geodatabase.IFeature pFeature = pFeatureClass.CreateFeature();
pFeature.Shape = pGeometry;
pFeature.Store();
pWorkspaceEdit.StopEditOperation();
this.m_pMap.SelectFeature(this.m_pCurrentLayer, pFeature);
ESRI.ArcGIS.Carto.IActiveView pActiveView = this.m_pMap as ESRI.ArcGIS.Carto.IActiveView;
pActiveView.Refresh();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
整個大體的框架就是這樣,其中含包含的具體細節資訊我會在下一節中在做詳細的介紹。
--------------------------------------------------------------------------------------end----------------------------------------------------------------------------------