Trouble with selecting features with both ISelectionSet and IFeatureSelection

795
3
08-11-2010 12:47 PM
JenniferHird
New Contributor
Hello,

I am a novice with ArcObjects programming (VBA/VB6). I am trying to select features from a point feature class that intersect with a particular polygon in a polygon feature class. I have tried using both the ISelectionSet interface (with the pFeatureClass.Select method), and the IFeatureSelection interface (the SelectFeatures method). I do not run into any compile or run-time errors, but in both cases the pSelectionSet.Count property returns a '0' at the end of the code. I know that this number should not be '0' (it should be upwards of 100 for some of my File Geodatabses). Why does it return a SelectionSet with 0 features in it?

Here is the code in which I try to use ISelectionSet:
***************************************

'pPtFClass is a point feature class
'pPplotFClass is a polygon feature class
'The above are both within the same FileGeodatabase

'Set up a cursor to move through the features in the polygon feature class
'-------------------------------------------------------------------------

Dim pPPlotFields As IFields
Set pPPlotFields = pPplotFClass.Fields

Dim intPosSUBTYPEField As Integer
intPosSUBTYPEField = pPPlotFields.FindField("SUBTYPE")

Dim intPosOBJECTIDField As Integer
intPosOBJECTIDField = pPPlotFields.FindField("OBJECTID")

Dim pPPlotFeatCursor As IFeatureCursor
Set pPPlotFeatCursor = pPplotFClass.Update(Nothing, True)

If pPPlotFeat Is Nothing Then
MsgBox "No features in abmi_pplot feature class." & vbCrLf & "Exiting subroutine."
Exit Function
End If

Do Until pPPlotFeat Is Nothing

'Select all point features that intersect the polygon whose SUBTYPE = 2
'----------------------------------------------------------------------

If pPPlotFeat.Value(intPosSUBTYPEField) = 2 Then

Dim pPplotIndivFeat As IFeature
Set pPplotIndivFeat = pPplotFClass.GetFeature(pPPlotFeat.Value(intPosOBJECTIDField))

Dim pQueryGeometry As IGeometry
Set pQueryGeometry = pPplotIndivFeat.Shape

Dim pSpatialFilter As ISpatialFilter
Set pSpatialFilter = New SpatialFilter

With pSpatialFilter
Set .Geometry = pQueryGeometry
.GeometryField = pPtFClass.ShapeFieldName
.SpatialRel = esriSpatialRelIntersects
End With

Dim pScratchWSFactory As IScratchWorkspaceFactory
Set pScratchWSFactory = New ScratchWorkspaceFactory
Dim pScratchWS As IWorkspace
Set pScratchWS = pScratchWSFactory.DefaultScratchWorkspace

Dim pInCoreSelSet As ISelectionSet
Set pInCoreSelSet = pPtFClass.Select(pSpatialFilter, _
esriSelectionTypeIDSet, esriSelectionOptionNormal, pScratchWS)

MsgBox "Number of point features in pInCoreSelSet: " & pInCoreSelSet.Count '*<<<<<<<<<<*

End if

Set pPPlotFeat = pPPlotFeatCursor.NextFeature

Loop


Alternatively, here is the code in which I try to use IFeatureSelection:
****************************************************

'pPtFClass is a point feature class
'pPplotFClass is a polygon feature class
'The above are both within the same FileGeodatabase

'Set up a cursor to move through the features in the polygon feature class
'-------------------------------------------------------------------------

Dim pPPlotFields As IFields
Set pPPlotFields = pPplotFClass.Fields

Dim intPosSUBTYPEField As Integer
intPosSUBTYPEField = pPPlotFields.FindField("SUBTYPE")

Dim intPosOBJECTIDField As Integer
intPosOBJECTIDField = pPPlotFields.FindField("OBJECTID")

Dim pPPlotFeatCursor As IFeatureCursor
Set pPPlotFeatCursor = pPplotFClass.Update(Nothing, True)

If pPPlotFeat Is Nothing Then
MsgBox "No features in abmi_pplot feature class." & vbCrLf & _
"Exiting subroutine."
Exit Function
End If

Do Until pPPlotFeat Is Nothing

'Select all point features that intersect the polygon whose SUBTYPE = 2
'----------------------------------------------------------------------

If pPPlotFeat.Value(intPosSUBTYPEField) = 2 Then

Dim pCorePtFLayer As IFeatureLayer
Set pCorePtFLayer = New FeatureLayer

Set pCorePtFLayer.FeatureClass = pPtFClass

Dim pPtCoreFeatSel As IFeatureSelection
Set pPtCoreFeatSel = pCorePtFLayer

Dim pPplotIndivFeat As IFeature
Set pPplotIndivFeat = pPplotFClass.GetFeature(pPPlotFeat.Value(intPosOBJECTIDField))

Dim pQueryGeometry As IGeometry
Set pQueryGeometry = pPplotIndivFeat.Shape

Dim pSpatialFilter As ISpatialFilter
Set pSpatialFilter = New SpatialFilter

With pSpatialFilter
Set .Geometry = pQueryGeometry
.GeometryField = pPtFClass.ShapeFieldName
.SpatialRel = esriSpatialRelIntersects
End With

pPtCoreFeatSel.SelectFeatures pSpatialFilter, esriSelectionResultAdd, False

MsgBox "Number of point features in pInCoreSelSet: " & pInCoreSelSet.Count '*<<<<<<<<<<*

End if

Set pPPlotFeat = pPPlotFeatCursor.NextFeature

Loop


Both of these return a '0' on for the .Count method (see lines with '*<<<<<<<<<<*).
I very much appreciate any advice or suggestions! Thank you!

Regards,
Jen Hird
0 Kudos
3 Replies
JeffMatson
Occasional Contributor III
Maybe try using IFeatureSelection.Add instead? Here is a sample that might help (currently just uses the top map layers, layer 0 is the point layer and layer 1 is the polygon layer)


Public Sub SelectPoints()
    Dim pFeature As IFeature
    Dim pFLayer_point As IFeatureLayer
    Dim pFLayer_poly As IFeatureLayer
    Dim pFSelection_point As IFeatureSelection
    Dim pFSelection_poly As IFeatureSelection
    Dim pSelectionSet As ISelectionSet
    Dim pFClass_poly As IFeatureClass
    Dim pFCursor As IFeatureCursor
    Dim pResult As IFeatureCursor
    Dim pResFeat As IFeature
    Dim pSpatialFilter As ISpatialFilter
    Dim pGeometry As IGeometry
    Dim pMxDocument As IMxDocument
    Dim pActiveView As IActiveView
    Dim pmap As IMap
    Dim pTOC As IContentsView
 
    Set pMxDocument = Application.Document
    Set pActiveView = pMxDocument.ActiveView
    Set pmap = pMxDocument.FocusMap
    Set pFLayer_point = pmap.Layer(0)
    Set pFLayer_poly = pmap.Layer(1)
    Set pSpatialFilter = New SpatialFilter
    Set pFClass_poly = pFLayer_poly.FeatureClass
    Set pFSelection_poly = pFLayer_poly
    Set pFCursor = pFClass_poly.Search(Nothing, False)
    Set pFeature = pFCursor.NextFeature
    While Not pFeature Is Nothing
        Set pGeometry = pFeature.Shape
        With pSpatialFilter
            Set pSpatialFilter.Geometry = pGeometry
            .GeometryField = pFClass_poly.ShapeFieldName 'not necessary
            .SpatialRel = esriSpatialRelContains
        End With
        Set pResult = pFLayer_point.Search(pSpatialFilter, False)
        Set pFSelection_point = pFLayer_point
        Set pResFeat = pResult.NextFeature
        Do Until pResFeat Is Nothing
            pFSelection_point.Add pResFeat
            Set pResFeat = pResult.NextFeature
        Loop
        Set pFeature = pFCursor.NextFeature
    Wend
    Set pActiveView = pMxDocument.ActiveView
    pActiveView.Refresh
    'refresh the selection tab of the TOC (item #2)
    Set pTOC = pMxDocument.ContentsView(2)
    pTOC.Refresh 0 'not sure why this needs an argument, #s 0-4 provide same result?
End Sub
0 Kudos
JenniferHird
New Contributor
Hi Jeff,

Thank you so much for the quick response, and the suggestion!  I actually found my mistake - someone changed the meaning of the "SUBTYPE" attribute in the polygon layer in the newer version of the data sets I am working with, so it should actually have been a 1 instead of a 2.  In other words, I was using the wrong polygon for the spatial query (one that did not have any point features contained within it).  Once I changed this, the code worked.  So it was a logical error on my part.  It's funny how you find the answer yourself after you have decided to admit defeat and ask for help 🙂

Thanks again for the help!  I've found these forums to be very informative many, many times, and it is all due to people like you.

Regards,
Jen
0 Kudos
JeffMatson
Occasional Contributor III
Nice catch - the Subtype thing was going to be my second question...now you probably can't call yourself a novice anymore 🙂
0 Kudos