
Count features within an area
DescriptionThis sample uses a spatial query filter to count the number of features within an area. First, click the tool button (you create) on the toolbar, then click and drag a rectangle around the features you want to have counted. The program retrieves the cursor's locations based on a filter, then loops through a counting procedure of all the features. The number of points, lines, and areas are totaled and reported to the user on a message box. In this sample, you will be adding a control tool and writing the code for it.
Start ArcMap.
Open an existing map document (.mxd) or add layers to the empty (Untitled) map document.
Click Tools and click Customize.
Click the Commands tab.
Click the drop-down arrow on the Save in combo box and click the map document in which the new command will be saved.
Scroll through the Categories list and click [UIControls].
Click New UIControl.
Click to select the UIToolControl as the UIControl Type.
Click Create.
Click and drag the new Project.UIToolControl1 in the Commands list and drop it on any toolbar.
Click Close.
Right-click the newly placed control and click View Source. This opens the Visual Basic Editor.
In the ThisDocument (Code) window, click the Procedure Box drop-down arrow (the one on the right of the window) and click MouseDown. This adds the wrapper code for the procedure you are creating.
Copy and paste the following code between the two wrapper code lines (between
---------------------------------------------------------------------------------------------
Private Sub UIToolControl1_MouseDown(ByVal button As Long, ByVal shift As Long, ByVal x As Long, ByVal y As Long) and End Sub): Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument
Dim pEnv As IEnvelope
Dim pRubber As IRubberBand
Set pRubber = New RubberEnvelope
Dim pActiveView As IActiveView
Set pActiveView = pMxDoc.FocusMap
Set pEnv = pRubber.TrackNew(pActiveView.ScreenDisplay, Nothing)
Dim pSpatialFilter As ISpatialFilter
Set pSpatialFilter = New SpatialFilter
Set pSpatialFilter.Geometry = pEnv
pSpatialFilter.SpatialRel = esriSpatialRelIntersects
Dim lPoints As Long, lPolylines As Long, lPolygons As Long
Dim pLayer As IFeatureLayer
Dim pFeatureCursor As IFeatureCursor
Dim pFeature As IFeature
Dim i As Long
For i = 0 To pMxDoc.FocusMap.LayerCount - 1
If (TypeOf pMxDoc.FocusMap.Layer(i) Is IGeoFeatureLayer) Then
Set pLayer = pMxDoc.FocusMap.Layer(i)
pSpatialFilter.GeometryField = pLayer.FeatureClass.ShapeFieldName
Set pFeatureCursor = pLayer.Search(pSpatialFilter, True)
Set pFeature = pFeatureCursor.NextFeature
Do Until (pFeature Is Nothing)
Select Case pFeature.Shape.GeometryType
Case esriGeometryPoint
lPoints = lPoints + 1
Case esriGeometryPolyline
lPolylines = lPolylines + 1
Case esriGeometryPolygon
lPolygons = lPolygons + 1
End Select
Set pFeature = pFeatureCursor.NextFeature
Loop
End If
Next i
MsgBox "Features Found:" & vbCrLf & lPoints & " Points " & vbCrLf & _
lPolylines & " Polylines " & vbCrLf & lPolygons & " Polygons "
---------------------------------------------------------------------------------------------
Close the Visual Basic Editor.
Click the Tool button in ArcMap, then click and drag to select an area in which to count the features.
No comments:
Post a Comment