How To: Spatially select multiple features in MapObjects LT 1


A procedure to spatially select and return the attributes for more than one feature shape.


The Identify method is the only spatial search method available in MapObjects LT 1.0. This method returns the attributes for the first feature found within a specified search tolerance. In some cases, it may be desirable to allow the user to spatially select several features and return their attributes simultaneously, such as when a shapefile contains points that exactly overlap each other.

One way to do this is as follows:

a) Position a rectangle object around the user-clicked Point.
b) Loop through all the features and compare coordinate
values to see which features fall within the rectangle.
c) If a feature is found to fall within the rectangle, store
a unique identifier value for that feature in a Strings
d) Open the shapefile's DBF using a data access
technology (such as DAO). Use the values stored
in the Strings collection to select the desired set
of records and then return the desired field values
from this recordset.

The following Visual Basic MouseDown subroutine demonstrates how this may be done.

Private Sub Map1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)

Dim ptClick As MapObjectsLT.Point
Dim rectSelect As MapObjectsLT.Rectangle
Dim dblOffset As Double
Dim strsSelected As New MapObjectsLT.Strings
Dim tmpShape As MapObjectsLT.Shape
Dim lyr As MapObjectsLT.MapLayer

' Get the user-clicked point
Set ptClick = Map1.ToMapPoint(x, y)

' "Make" a "selection" rectangle
Set rectSelect = Map1.Extent
dblOffset = Map1.Extent.Height / 15
rectSelect.Top = ptClick.y + dblOffset
rectSelect.Bottom = ptClick.y - dblOffset
rectSelect.Left = ptClick.x - dblOffset
rectSelect.Right = ptClick.x + dblOffset

' Return the MapLayer to search
Set lyr = Map1.Layers(0)

' Loop through all features in the MapLayer.
Dim i As Integer
i = 1
Do While True
' Find each feature by unique, consecutive integer ID
Set tmpShape = lyr.Find("ID = " & i)
If tmpShape Is Nothing Then
' We've gone through all the features
Exit Do
End If
' Check to see if this Shape falls within rectSelect
If tmpShape.Extent.Top < rectSelect.Top And _
tmpShape.Extent.Top > rectSelect.Bottom And _
tmpShape.Extent.Left > rectSelect.Left And _
tmpShape.Extent.Left < rectSelect.Right Then
' Add the FeatureId to strsSelected
strsSelected.Add i
End If
' Increment i
i = i + 1

' We now have a list of all the FeatureID's that fall
' within rectSelect. Now we can open the shapefile's
' DBF using DAO, ADO, etc., and select the desired
' records with strsSelected.

' For debug purposes...
Dim str
For Each str In strsSelected
List1.AddItem str
Next str

End Sub

MapObjects LT 2 is now available; see link below. At version 2, the spatial and attribute query functions are more robust, comparable to the functionality found in the full version of MapObjects 2.0.

Related Information