How to implement "select by location" by VBA and ArcObject ?

1072
6
02-06-2011 11:42 PM
GarySham
New Contributor
Dear all,

I have two polygon feature classes, polygon A and polygon B.

I would like to write a VBA to perform a select by location function to find out which polygon(s) in polygon B is/are intersect with a selected polygon in polygon A which is selected by a user.

I know, I can use "ISpatialFilter" to do this, but don't know how to implement it in VBA. Would you mind provide some sample code/hints for me ?

Regards
Gary
0 Kudos
6 Replies
Venkata_RaoTammineni
Occasional Contributor
Dear all,

I have two polygon feature classes, polygon A and polygon B.

I would like to write a VBA to perform a select by location function to find out which polygon(s) in polygon B is/are intersect with a selected polygon in polygon A which is selected by a user.

I know, I can use "ISpatialFilter" to do this, but don't know how to implement it in VBA. Would you mind provide some sample code/hints for me ?

Regards
Gary


public IFeatureCursor GetStreetsInEnvelope(IFeatureClass featureClass)

// Create the envelope and define its position.
  IEnvelope envelope = new EnvelopeClass();  envelope.PutCoords(-84.4078, 33.7787, -84.3856, 33.7997);
  // Create the spatial filter and set its spatial constraints.
  ISpatialFilter spatialFilter = new SpatialFilterClass();
  spatialFilter.Geometry = envelope;
  spatialFilter.GeometryField = featureClass.ShapeFieldName; 
spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
  // Set the attribute constraints and subfields.
// We want to exclude ramps, highways and interstates.
spatialFilter.WhereClause = "NAME <> 'Ramp' AND PRE_TYPE NOT IN ('Hwy', 'I')";    spatialFilter.SubFields = "NAME, TYPE";  
// Execute the query.
  IFeatureCursor featureCursor = featureClass.Search(spatialFilter, true);
  return featureCursor;
}
0 Kudos
GarySham
New Contributor
Actually, I have two polygon feature classes (RoadPolygon and AsstPolygon). The user selects a RoadPolygon and my code can find out all AsstPolygon which intersect with the selected RoadPolygon. I already write some code in VBA but seem does not work, anyone can help ?

' Find out the correct layer of RoadPolygon and AsstPolygon
For i = 0 To (pMap.LayerCount - 1)

    If pMap.Layer(i).Name = "RoadPolygon" Then

        Set RoadLayer = pMap.Layer(i)

    End If

    If pMap.Layer(i).Name = "RoadAssetPolygon" Then

        Set AssetLayer = pMap.Layer(i)

    End If

Next i

' Build a spatial filter

Dim spatialFilter As ISpatialFilter
Set spatialFilter = New spatialFilter

With spatialFilter
'      Set .Geometry = pMap.FeatureSelection ? ,pPointCollection ?, pFeature  'I don't know
      .GeometryField = AsstLayer
      .SpatialRel = esriSpatialRelIntersects
   
    End With

Dim featureSelection As IFeatureSelection
Set featureSelection = AsstLayer
featureSelection.SelectFeatures spatialFilter, esriSelectionResultNew, False
   
pDoc.ActiveView.Refresh
   

'Dim featureSelection As IFeatureSelection
'Set featureSelection = AsstLayer
'featureSelection.SelectFeatures spatialFilter, esriSelectionResultNew, False
   
'pDoc.ActiveView.Refresh
0 Kudos
SteveFang
New Contributor III
Your spatial filter is not set up properly.  You need three pieces of information.

Geometry - the shape of the selected RoadPolygon feature
GeometryField - the name of the shape field in your RoadPolygon, usually "SHAPE"
SpatialRel - whatever you have in your code

Try that and see if it works...

Steve
0 Kudos
GarySham
New Contributor
Thank You so much

Now I can select the AssetPolygon. However I am fail to refreah the map in order to show which AssetPolygon(s) is/are selected by the spatialFilter, any can help ?

My code is as follew :

Set pAssetFeatureClass = pAssetLayer.FeatureClass
   
    Set pSpatialFilter.Geometry = pFeature.Shape
   
    pSpatialFilter.SpatialRel = esriSpatialRelIntersects
   
    Set pFeature = pEnumFeat.Next


    Set pAssetFeatureCursor = pAssetFeatureClass.Search(pSpatialFilter, Flase)

    Set pAssetFeature = pAssetFeatureCursor.NextFeature


    Do Until pAssetFeature Is Nothing

     NumOfAsset = NumOfAsset + 1
      
     Set pAssetFeature = pAssetFeatureCursor.NextFeature

   Loop

Dim pDoc As IMxDocument
Set pDoc = ThisDocument

Dim pMap As IMap
Set pMap = pDoc.FocusMap

Dim pActiveView As IActiveView
Set pActiveView = pMap

pActiveView.Refresh ---------- It does not seem work !!!!!
0 Kudos
SteveFang
New Contributor III
Look at IFeatureSelection interface in the documentations.  There are sample code that your can use.  Basically, you want to make your selection in your featurelayer and trigger the SelectionChanged method in IFeatureSelection.
0 Kudos
KingsleyPayne
New Contributor
Sub SpatialFilter()

Dim pMxd As IMxDocument
Set pMxd = ThisDocument

Dim pEnumFeature As IEnumFeature
Set pEnumFeature = pMxd.FocusMap.FeatureSelection

pEnumFeature.Reset

Dim pSearchFeature As IFeature
Set pSearchFeature = pEnumFeature.Next

If (pSearchFeature Is Nothing) Then
    MsgBox "Please select a feature to search."
    Exit Sub
End If

pMxd.FocusMap.ClearSelection

Dim pLayer As ILayer
Dim pSearchLayer As IFeatureLayer
Dim pFeatureLayer As IFeatureLayer

Dim i As Integer

For i = 0 To pMxd.FocusMap.LayerCount - 1
    Set pLayer = pMxd.FocusMap.Layer(i)
    
    If pLayer.Name = "Roads" Then
        Set pSearchLayer = pLayer
        Exit For
    End If
Next

If (pSearchLayer Is Nothing) Then
    MsgBox "Roads layer not found!"
    Exit Sub
End If

For i = 0 To pMxd.FocusMap.LayerCount - 1
    Set pLayer = pMxd.FocusMap.Layer(i)
    
    If pLayer.Name = "Road Assets" Then
        Set pFeatureLayer = pLayer
        Exit For
    End If
Next

If (pSearchLayer Is Nothing) Then
    MsgBox "Road Assets layer not found!"
    Exit Sub
End If

Dim pSpatialFilter As ISpatialFilter
Set pSpatialFilter = New SpatialFilter

Dim pTopoOp As ITopologicalOperator
Set pTopoOp = pSearchFeature.Shape

' You might need to buffer the search geometry in certain cases.
' Use a buffer value of 0 to search using the original geometry.

Dim pSearchGeometry As IGeometry
Set pSearchGeometry = pTopoOp.Buffer(10)

Set pSpatialFilter.Geometry = pSearchGeometry
pSpatialFilter.GeometryField = pSearchLayer.FeatureClass.ShapeFieldName
pSpatialFilter.SpatialRel = esriSpatialRelIntersects

Dim pFeatureCursor As IFeatureCursor
Set pFeatureCursor = pFeatureLayer.Search(pSpatialFilter, False)

Dim pFeature As IFeature
Dim FeatureCount As Integer

Set pFeature = pFeatureCursor.NextFeature

Do While Not pFeature Is Nothing
    FeatureCount = FeatureCount + 1
    pMxd.FocusMap.SelectFeature pFeatureLayer, pFeature
    Set pFeature = pFeatureCursor.NextFeature
Loop

If (FeatureCount = 1) Then
    MsgBox FeatureCount & " feature found."
Else
    MsgBox FeatureCount & " features found."
End If

Dim pActiveView As IActiveView
Set pActiveView = pMxd.FocusMap

pActiveView.Refresh

End Sub
0 Kudos