ArcObjects iSpatialFilter - How To

573
4
04-25-2011 10:58 AM
JustinConklin
New Contributor
Hello,

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


Thanks,
Justin
0 Kudos
4 Replies
ModyBuchbinder
Regular Contributor II
Hello

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
Mody
0 Kudos
DuncanHornby
MVP Frequent Contributor
Justin,

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


Duncan
0 Kudos
JustinConklin
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
ModyBuchbinder
Regular Contributor II
Hi

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

Have Fun
Mody
0 Kudos