Select by location - SpatialQuery

Discussion created by erdhummel on May 31, 2010
Latest reply on May 31, 2010 by thaviti

I'm trying to use "select by location" using VBA. I found out that a spatial filter (Spatial Query) performs the same function as using the "select by location" command.
I have a polygon feature layer (buildings) and a point feature layer that contains informations about the addresses of the buildings (street name and house number). I selected some points with specific address-attributes by using a query filter. Now I would like to select the polygons that are under the selected points.
My problem is that only one polygon will be selected but the number of selected features should be three. I wrote the following code:

Private Sub GetBuildings_Click()

    Dim pMxDoc As IMxDocument
    Dim pMap As IMap
    Set pMxDoc = ThisDocument
    Set pMap = pMxDoc.FocusMap

    Dim pFeatureSelection As IFeatureSelection
    Dim pSelectionSet As ISelectionSet
    ' addresses...
    Dim adresse1 As String
    Dim adresse2 As String
    Dim adresse3 As String
    adresse1 = "Kreuzackerstra??e 8"
    adresse2 = "Grundstra??e 3"
    adresse3 = "Rathausstra??e 4"
    ' Query Filter...
    Dim pQueryFilter As IQueryFilter
    Set pQueryFilter = New QueryFilter
    pQueryFilter.WhereClause = "Adresse = '" & adresse1 & "' OR Adresse = '" & adresse2 & "' OR Adresse = '" & adresse3 & "'"
    Dim pPointFeatureLayer As IFeatureLayer
    Set pPointFeatureLayer = Module1.FindLayer("Addresses")
    ' select points...
    Set pFeatureSelection = pPointFeatureLayer
    pFeatureSelection.SelectFeatures pQueryFilter, esriSelectionResultNew, False
    Set pSelectionSet = pFeatureSelection.SelectionSet

    Dim pFeatureCursor As IFeatureCursor
    Dim pFeature As IFeature
    pSelectionSet.Search Nothing, True, pFeatureCursor
    Set pFeature = pFeatureCursor.NextFeature

    ' spatial filter...
    Dim pSpatialFilter As ISpatialFilter
    Set pSpatialFilter = New SpatialFilter
    Set pSpatialFilter.Geometry = pFeature.Shape
    pSpatialFilter.SpatialRel = esriSpatialRelWithin

    Dim pBuildingsFLayer As IFeatureLayer
    Dim pBuildingsFClass As IFeatureClass
    Dim pBuildingsFCursor As IFeatureCursor
    Dim pBuildingsFeature As IFeature
    Set pBuildingsFLayer = Module1.FindLayer("Buildings")
    Set pBuildingsFClass = pBuildingsFLayer.FeatureClass

    Dim pBuildingsFSelection As IFeatureSelection
    Set pBuildingsFSelection = pBuildingsFLayer
    pBuildingsFSelection.SelectFeatures pSpatialFilter, esriSelectionResultNew, False


End Sub