Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I would like to get a total count of multiple layer features (defined) within the ArcMap view extent. The code works fine with one layer but when I add a second or third layer with an "OR" operator I only get the count of 0 because one layer may be visible within the extent but the others are not.

e.g.

If pLayer.Name = "Layer1" Or pLayer.Name = "Layer2"

Here is the code. Any suggestion would be greatly appreciated. thx!

Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument

Dim pAv As IActiveView
Set pAv = pMxDoc.FocusMap
Dim pMap As IMap

Dim pEnv As IEnvelope
Set pEnv = pAv.ScreenDisplay.displayTransformation.VisibleBounds

Dim pSF As ISpatialFilter
Set pSF = New SpatialFilter
Set pSF.Geometry = pEnv
pSF.GeometryField = "Shape"
pSF.SpatialRel = esriSpatialRelIntersects

Dim pEnumLayer As IEnumLayer
Dim pLayer As ILayer

Set pMap = pMxDoc.ActiveView.FocusMap

Set pEnumLayer = pMap.Layers

Dim pFLayer As IFeatureLayer
Dim pFClass As IFeatureClass
Dim pFCursor As IFeatureCursor
Dim pF As IFeature
Dim intCount As Integer

Set pLayer = pEnumLayer.Next

Do Until pLayer Is Nothing

    If pLayer.Name = "Layer1" Or pLayer.Name = "Layer2" Then '?MULITI CUR         

     Set pFLayer = pLayer
     Set pFClass = pFLayer.FeatureClass
     Set pFCursor = pFClass.Search(pSF, False)

    End If
    Set pLayer = pEnumLayer.Next
Loop

   ' Loop through the cursor and count number of selected features.
Set pF = pFCursor.NextFeature
Do Until pF Is Nothing
    intCount = intCount + 1
    Set pF = pFCursor.NextFeature
Loop

If intCount = 0 Then
MsgBox "Count = 0"
Else
MsgBox ("Count = " & intCount)
End If

pAv.PartialRefresh esriViewGeoSelection, Nothing, Nothing  
share|improve this question

1 Answer 1

You need to loop over your features inside the loop on your layers:

Do Until pLayer Is Nothing
  If pLayer.Name = "Layer1" Or pLayer.Name = "Layer2" Then '?MULITI CUR         
    Set pFLayer = pLayer
    Set pFClass = pFLayer.FeatureClass
    Set pFCursor = pFClass.Search(pSF, False)
    ' Loop through the cursor and count number of selected features.
    Set pF = pFCursor.NextFeature
    Do Until pF Is Nothing
      intCount = intCount + 1
      Set pF = pFCursor.NextFeature
    Loop
  End If
  Set pLayer = pEnumLayer.Next
Loop
share|improve this answer
    
Thanks Regan, I'll give it a try. –  artwork21 Feb 17 '11 at 2:03
    
Regan, I tried the code out, but it only gave me a count of layer1 when both layers were visible. So when layer2 is only visible the count is 0. –  artwork21 Feb 17 '11 at 17:26
    
@artwork21, Have you ensured it is a problem with multiple layers by testing with each layer independently. Also can you ensure it is not a problem with your specific layers by trying with other layers/datasources. –  Regan Sarwas Feb 17 '11 at 18:41
1  
Btw, it would be more elegant to use intCount += pFClass.FeatureCount(pSF) instead of really looping over those features. HTH –  AndOne Apr 11 '11 at 15:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.