In this article by Hussein Nasser, author of the book ArcGIS By Example we will discuss the following topics:
(For more resources related to this topic, see here.)
YharanamCo is a construction contractor experienced in executing efficient and economical excavations for utility and telecom companies. When YharanamCo’s board of directors heard of ArcGIS technology, they wanted to use their expertise with the power of ArcGIS to come up with a solution that helps them cut costs even more. Soil type is not the only factor in the excavation, there are many factors including the green factor where you need to preserve the trees and green area while excavating for visual appeal. Using ArcGIS, YharanamCo can determine the soil type and green factor and calculate the cost of an excavation.
The excavation planning manager is the application you will be writing on top of ArcGIS. This application will help YharanamCo to create multiple designs and scenarios for a given excavation. This way they can compare the cost for each one and consider how many trees they could save by going through another excavation route. YharanamCo has provided us with the geodatabase of a soil and trees data for one of their new projects for our development.
So far we learned how to view and query the geodatabase and we were able to achieve that by opening what we called a workspace. However, changing the underlying data requires establishing an editing session. All edits that are performed during an edit sessions are queued, and the moment the session is saved, these edits are committed to the geodatabase. Geodatabase editing supports atomic transactions, which are referred to as operations in the geodatabase.
Atomic transaction is a list of database operations that either all occur together or none. This is to ensure consistency and integrity.
After this short introduction to geodatabase editing, we will prepare our data and project.
Before we dive into the coding part, we need to do some preparation for our new project and our data.
The YharnamCo team has provided us with the geodatabase and map document, so we will simply copy the necessary files to your drive. Follow these steps to start your preparation of the data and map:
We will now start our project. First we need to create our Yharnam Visual Studio extending ArcObjects project. To do so, follow these steps:
You have finished preparing your Visual Studio with extending ArcObjects support. Move to the next section to write some code.
The new excavation tool will be used to draw a polygon on the map, which represents the geometry of the excavation. Then, this will create a corresponding excavation feature using this geometry:
Public Sub New()
MyBase.New()
' TODO: Define values for the public properties
' TODO: Define values for the public properties
MyBase.m_category = "Yharnam" 'localizable text
MyBase.m_caption = "New Excavation" 'localizable text
MyBase.m_message = "New Excavation" 'localizable text
MyBase.m_toolTip = "New Excavation" 'localizable text
MyBase.m_name = "Yharnam_NewExcavation"
Try
'TODO: change resource name if necessary
Dim bitmapResourceName As String = Me.GetType().Name + ".bmp"
MyBase.m_bitmap = New Bitmap(Me.GetType(), bitmapResourceName)
MyBase.m_cursor = New System.Windows.Forms.Cursor(Me.GetType(), Me.GetType().Name + ".cur")
Catch ex As Exception
System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap")
End Try
End Sub
The excavation editor is a tool that will let us click an excavation feature on the map and display the excavation information such as depth, area, and so on. It will also allow us to edit some of the information.
We will now add a tool to our project. To do that, follow these steps:
MyBase.m_category = "Yharnam" 'localizable text
MyBase.m_caption = "Excavation Editor" 'localizable text
MyBase.m_message = "Excavation Editor" 'localizable text
MyBase.m_toolTip = "Excavation Editor" 'localizable text
MyBase.m_name = "Yharnam_ExcavationEditor"
Control | Name | Properties |
Label | lblDesignID | Text: Design ID |
Label | lblExcavationOID | Text: Excavation ObjectID |
Label | lblExcavationArea | Text: Excavation Area |
Label | lblExcavationDepth | Text: Excavation Depth |
Label | lblTreeCount | Text: Number of Trees |
Label | lblTotalCost | Text: Total Excavation Cost |
Text | txtDesignID | Read-Only: True |
Text | txtExcavationOID | Read-Only: True |
Text | txtExcavationArea | Read-Only: True |
Text | txtExcavationDepth | Read-Only: False |
Text | txtTreeCount | Read-Only: True |
Text | txtTotalCost | Read-Only: True |
Button | btnSave | Text: Save |
Now that we have our two tools, we will add a toolbar to group them together. Follow these steps to add the Yharnam Excavation Planning Manager Toolbar to your project:
Public Overrides ReadOnly Property Caption() As String
Get
'TODO: Replace bar caption
Return "YharnamExcavation Planning Manager"
End Get
End Property
Public Sub New()
AddItem("Yharnam.tlNewExcavation")
AddItem("Yharnam.tlExcavationEditor")
End Sub
For a list of all ArcMap commands, you can refer to http://bit.ly/b04748_arcmapids. Check the commands with namespace esriArcMapUI.
In the next section, we will do the real work of editing.
Features are nothing but records in a table. However, these special records cannot be created without a geometry shape attribute. To create a feature, we need first to learn how to draw and create geometries. We will be using the rubber band ArcObjects interface to create a polygon geometry. We can use it to create other types of geometries as well, but since our excavations are polygons, we will use the polygon rubber band.
In this exercise, we will use the rubber band object to create a polygon geometry by clicking on multiple points on the map. We will import libraries as we need them. Follow these steps:
Public Overrides Sub OnMouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer)
Dim pRubberBand As IRubberBand = New RubberPolygonClass
End Sub
Public Overrides Sub OnMouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer)
Dim pDocument As IMxDocument = m_application.Document
Dim pRubberBand As IRubberBand = New RubberPolygonClass
Dim pFillSymbol As ISimpleFillSymbol = New SimpleFillSymbol
Dim pPolygon as IGeometry=pRubberBand.TrackNew(pDocument.ActiveView.ScreenDisplay, pFillSymbol)
End Sub
Public Overrides Sub OnMouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer)
Dim pDocument As IMxDocument = m_application.Document
Dim pRubberBand As IRubberBand = New RubberPolygonClass
Dim pFillSymbol As ISimpleFillSymbol = New SimpleFillSymbol
Dim pColor As IColor = New RgbColor
pColor.RGB = RGB(255, 0, 0)
pFillSymbol.Color = pColor
Dim pPolygon As IGeometry = pRubberBand.TrackNew(pDocument.ActiveView.ScreenDisplay, pFillSymbol)
Dim pDisplay As IScreenDisplay = pDocument.ActiveView.ScreenDisplay
pDisplay.StartDrawing(pDisplay.hDC, ESRI.ArcGIS.Display.esriScreenCache.esriNoScreenCache)
pDisplay.SetSymbol(pFillSymbol)
pDisplay.DrawPolygon(pPolygon)
pDisplay.FinishDrawing()
End Sub
Now that we have learned how to draw a polygon, we will convert that polygon into an excavation feature. Follow these steps to do so:
Public Overrides Sub OnMouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer)
Dim pDocument As IMxDocument = m_application.Document
Dim pRubberBand As IRubberBand = New RubberPolygonClass
Dim pFillSymbol As ISimpleFillSymbol = New SimpleFillSymbol
Dim pPolygon As IGeometry = pRubberBand.TrackNew(pDocument.ActiveView.ScreenDisplay, pFillSymbol)
End Sub
Public Function getYharnamWorkspace() As IWorkspace
Dim pWorkspaceFactory As IWorkspaceFactory = New FileGDBWorkspaceFactory
Return pWorkspaceFactory.OpenFromFile("C:\ArcGISByExample\yharnam\Data\Yharnam.gdb", m_application.hWnd)
End Function
Public Function getExcavationFeatureClass(pWorkspace As IWorkspace) As IFeatureClass
Dim pFWorkspace As IFeatureWorkspace = pWorkspace
Return pFWorkspace.OpenFeatureClass("Excavation")
End Function
Public Overrides Sub OnMouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer)
Dim pDocument As IMxDocument = m_application.Document
Dim pRubberBand As IRubberBand = New RubberPolygonClass
Dim pFillSymbol As ISimpleFillSymbol = New SimpleFillSymbol
Dim pPolygon As IGeometry = pRubberBand.TrackNew(pDocument.ActiveView.ScreenDisplay, pFillSymbol)
Dim pWorkspaceEdit As IWorkspaceEdit = getYharnamWorkspace()
pWorkspaceEdit.StartEditing(True)
pWorkspaceEdit.StartEditOperation()
pWorkspaceEdit.StopEditOperation()
pWorkspaceEdit.StopEditing(True)
End Sub
Dim pWorkspaceEdit As IWorkspaceEdit = getYharnamWorkspace()
pWorkspaceEdit.StartEditing(True)
pWorkspaceEdit.StartEditOperation()
Dim pExcavationFeatureClass As IFeatureClass = getExcavationFeatureClass(pWorkspaceEdit)
Dim pFeature As IFeature = pExcavationFeatureClass.CreateFeature()
pFeature.Shape = pPolygon
pFeature.Store()
pWorkspaceEdit.StopEditOperation()
pWorkspaceEdit.StopEditing(True)
We have learned how to create features. Now we will learn how to edit excavations as well.
We have created some excavation features on the map; however, these are merely polygons and we need to extract useful information from them, and display and edit these excavations. For that, we will use the Excavation Editor tool to click on an excavation and display the Excavation Editor form with the excavation information. Then we will give the ability to edit this information. Follow these steps:
Private _application As IApplication
Public Property ArcMapApplication() As IApplication
Get
Return _application
End Get
Set(ByVal value As IApplication)
_application = value
End Set
End Property
Public Sub PopulateExcavation(pFeature As IFeature)
End Sub
Public Sub PopulateExcavation(pFeature As IFeature)
Dim designID As Long = 0
Dim dDepth As Double = 0
If IsDBNull(pFeature.Value(pFeature.Fields.FindField("DESIGNID"))) = False Then
designID = pFeature.Value(pFeature.Fields.FindField("DESIGNID"))
End If
If IsDBNull(pFeature.Value(pFeature.Fields.FindField("DEPTH"))) = False Then
dDepth = pFeature.Value(pFeature.Fields.FindField("DEPTH"))
End If
txtDesignID.Text = designID
txtExcavationDepth.Text = dDepth
txtExcavationOID.Text= pFeature.OID
End Sub
…..
txtExcavationOID.Text = pFeature.OID
Dim pArea As IArea = pFeature.Shape
txtExcavationArea.Text = pArea.Area
End Sub
Public Overrides Sub OnMouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer)
'TODO: Add tlExcavationEditor.OnMouseDown implementation
Dim pMxdoc As IMxDocument = m_application.Document
Dim pPoint As IPoint = pMxdoc.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y)
Dim pSFilter As ISpatialFilter = New SpatialFilter
pSFilter.Geometry = pPoint
Dim pFeatureClass As IFeatureClass = getExcavationFeatureClass(getYharnamWorkspace())
Dim pFCursor As IFeatureCursor = pFeatureClass.Search(pSFilter, False)
Dim pFeature As IFeature = pFCursor.NextFeature
If pFeatureIs Nothing Then Return
Dim pExcavationEditor As New frmExcavationEditor
pExcavationEditor.ArcMapApplication = m_application
pExcavationEditor.PopulateExcavation(pFeature)
pExcavationEditor.Show()
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim pWorkspaceEdit As IWorkspaceEdit = getYharnamWorkspace()
Dim pFeatureClass As IFeatureClass = getExcavationFeatureClass(pWorkspaceEdit)
Dim pFeature As IFeature = pFeatureClass.GetFeature(txtExcavationOID.Text)
pWorkspaceEdit.StartEditing(True)
pWorkspaceEdit.StartEditOperation()
pFeature.Value(pFeature.Fields.FindField("DEPTH")) = txtExcavationDepth.Text
pFeature.Store()
pWorkspaceEdit.StopEditOperation()
pWorkspaceEdit.StopEditing(True)
Me.Close
End Sub
In this article, you started writing the excavation planning manager, code named Yharnam. In the first part of the article, you spent time learning to use the geodatabase editing and preparing the project. You then learned how to use the rubber band tool, which allows you to draw geometries on the map. Using this drawn geometry, you edited the workspace and created a new excavation feature with that geometry. You then learned how to view and edit the excavation feature with attributes.
Further resources on this subject:
I remember deciding to pursue my first IT certification, the CompTIA A+. I had signed…
Key takeaways The transformer architecture has proved to be revolutionary in outperforming the classical RNN…
Once we learn how to deploy an Ubuntu server, how to manage users, and how…
Key-takeaways: Clean code isn’t just a nice thing to have or a luxury in software projects; it's a necessity. If we…
While developing a web application, or setting dynamic pages and meta tags we need to deal with…
Software architecture is one of the most discussed topics in the software industry today, and…