ArcObjects iSpatialFilter - How To

04-25-2011 10:58 AM
New Contributor

I am having trouble using the iSpatialFilter interface.  I have selected a polygon feature using a query filter, and now I would like to select features from 'points' that are within the selected features of 'polygons'.  I cannot figure out how to tell the spatial filter to only use the selected features in the polygon layer.  Does anyone have advice or a code sample to get me started?

ArcGIS Desktop 10 VBA

0 Kudos
4 Replies
Regular Contributor II

Put the selected polygon (IFeature.Shape) into the ISpatialFilter.Geometry and open a new cursor with the ISpatialFilter. You must set the ISpatialFilter.SpatialRel too.

Have Fun
0 Kudos
MVP Frequent Contributor

If your attribute query is selecting more than 1 polygon then you need to bind your selection into a "single" geometry which you pass to an object of type ISpatialFilter. Below is some code that I have used to achieve this, alternatively you could call the SelectByLocation Geo-Processing tool to achieve the same thing...

Dim pSelectionSet_Nodes As ISelectionSet
    Set pSelectionSet_Nodes = pFeatureSelection_Nodes.SelectionSet
    Dim pEnumGeometry As IEnumGeometry
    Dim pEnumGeometryBind As IEnumGeometryBind
    Set pEnumGeometry = New EnumFeatureGeometry
    Set pEnumGeometryBind = pEnumGeometry
    pEnumGeometryBind.BindGeometrySource Nothing, pSelectionSet_Nodes
    Dim pGeometryFactory As IGeometryFactory
    Set pGeometryFactory = New GeometryEnvironment
    Dim pGeometry As IGeometry
    Set pGeometry = pGeometryFactory.CreateGeometryFromEnumerator(pEnumGeometry)
    Dim pSpatialFilter As ISpatialFilter
    Set pSpatialFilter = New SpatialFilter
    With pSpatialFilter
        Set .Geometry = pGeometry
        .GeometryField = "SHAPE"
        .SpatialRel = esriSpatialRelIntersects
    End With
    pFeatureSelection_Rivers.SelectFeatures pSpatialFilter, esriSelectionResultNew, False

0 Kudos
New Contributor
Thank you both. 

My attribute query only selects one polygon, but it is stored in a SelectionSet rather than feature.  I saw the need to get it as a "single" geometry but could not QI it withought getting a type mismatch. 

I will give your code a try Duncan, Thanks
0 Kudos
Regular Contributor II

Use ISelectionSet.Search to get a cursor on your selection set. From the cursor you can get the feature.

Have Fun
0 Kudos