GEOG 490/590: GIS Programming

Lab5. ArcMap UITool, Commands, and Vector Features Operations

 

Due Mar 11 before class.

 

Introduction:

 

You will create a UITool that allows users to select polygons from a polygon layer in ArcMap and report the total area of the selected polygons. You should find a polygon shapefile to use as test data.

 

Polygon Area Tool (PAT):

 

Users can use PAT to draw a rectangular box and select polygons that fall within the rectangular area. ArcMap will zoom in to the selected features and a popup message box then shows the number of features selected and the total area of the selected features.

 

UITool Control:

Please refer to Lab 3 ESRI online course or Burke Chapter 12 for instructions of creating UITool controls in ArcMap. Please set the tooltip of the PAT UITool control and assign appropriate button image.

 

Setting Cursor Type:

You first use the CursorID event to set the cursor type of PAT to crosshair. If the name of your UITool control is UITool_Select, then the code below sets the cursor type of the UI control.

 

Private Function UITool_Select_CursorID() As Variant

    UITool_Select_CursorID = 3 'crosshair

End Function

 

MouseDown Event:

PAT requires a user to click the mouse cursor on a map and drag the cursor to select a rectangular area. That means all your procedures must be associated with the MouseDown event.

 

Private Sub UITool_Select_MouseDown(ByVal button As Long, _

ByVal shift As Long, ByVal x As Long, ByVal y As Long)

    'Put your VB code here.

End Sub

 

Drawing a Rectangle:

You use the RubberBand object to track the movement of mouse cursor. When done, assign the tracked rectangular geometry to a graphic element.

 

    Dim pEnv As IEnvelope

Dim pRubberEnv As IrubberBand

    Set pRubberEnv = New RubberEnvelope

Set pEnv = pRubberEnv.TrackNew( _

pMxDoc.ActivatedView.ScreenDisplay, Nothing)

   

    'selected elements

    Dim pElem As IElement

    Set pElem = New RectangleElement

pElem.Geometry = pEnv

 

You then display the rectangular area as a rectangular graphic element. This can be achieved using the procedures below.

 

Dim pFillShapeElem As IFillShapeElement

Set pFillShapeElem = pElem

 

Dim pFillSymbol As IFillSymbol

Set pFillSymbol = pFillShapeElem.Symbol

 

Dim pColor As IColor

Set pColor = pFillSymbol.Color

   

Dim pLineSymbol As ILineSymbol

Set pLineSymbol = pFillSymbol.Outline

 

pColor.Transparency = 0 'set background to transparent

pColor.RGB = RGB(255, 0, 0) 'color is red

pLineSymbol.Width = 0.1

pFillSymbol.Color = pColor

pFillSymbol.Outline = pLineSymbol

 

pFillShapeElem.Symbol = pFillSymbol

 

'graphics container

Dim pGContainer As IGraphicsContainer

Set pGContainer = pMxDoc.ActivatedView

pGContainer.AddElement pElem, 0

pMxDoc.ActivatedView.Refresh

 

Make sure you remember to clear your graphics container before PAT draws any rectangle. This can be easily done with the DeleteAllElements method of the IgraphicsContainer interface.

 

Select and Zoom to Features:

You use ArcMap CommandItem objects to accomplish these tasks. You first use the Edit_SelectAll command to select the rectangular graphic element, then use the Query_SelectByGraphics command to select polygon features, and then use the Query_ZoomToSelected command to zoom to the selected polygons. A sample code is attached to demonstrate how to use CommandItems in VB code.

 

    Dim pCmdItem As ICommandItem

   

    'select all graphics in map

    Set pCmdItem = CommandBars.Find(arcid.Edit_SelectAll)

pCmdItem.Execute

 

Spatial Query:

You perform a spatial query to select the polygon features that fall within the rectangle area specified by the UITool. A sample code for spatial query is attached. Please refer to Lab4 for more information on performing a query.

 

Dim pSFilter As ISpatialFilter

Dim pFLayer As IFeatureLayer

Dim pFCursor As IFeatureCursor

Dim pFeature As IFeature

 

'prepare a spatial filter

Set pSFilter = New SpatialFilter

Set pSFilter.Geometry = pEnv 'pEnv is the envelope geometry

'you got from the IrubberBand interface

pSFilter.SpatialRel = esriSpatialRelIntersects

 

'assign the reference to a feature layer

Set pFLayer = pMxDoc.FocusMap.Layer(0)

Set pFCursor = pFLayer.Search(pSFilter, False)

 

Set pFeature = pFCursor.NextFeature

'This is how you get an individual feature

'from the feature cursor. You need to use a loop

'to access all selected features

 

Calculating Total Area:

The next step is to sum the areas of all selected features (i.e., the features in the Feature Cursor.) You use the IArea interface to extract the area information of the shape geometry. This is done by the following code. You have to put the code in a loop to sum all area values.

 

       Dim pArea As IArea

       Dim Sumarea As Double

       Set pArea = pFeature.Shape

       Sumarea = Sumarea + pArea.Area

 

You might want to use the PanZoom_FullExtent command (see the ICommandItem interface above) to reset the map display extent after the message box shows the query result.

 

Lab Deliverables:

 

Don’t forget to test/debug your AP. Try different ways of using the AP and see if that generates any error or unpredictable behavior. Submit your zipped mxd file. The mxd file must have the following features:

1.          The procedures of PAT. Make sure your code is stored in the mxd, not the mxt!

2.          An ArcMap ToolBar containing the PAT UI Control.

3.          Make sure your code is properly commented.