Help Requested...spatial queries within the same layer.. STUCK

Discussion created by johncarter.919 on Apr 5, 2011
Latest reply on Apr 5, 2011 by johncarter.919
Hi all,

After much fiddling and mental agony, I've decided to ask here for help. 

I have a single layer in ArcMap that contains a bunch of polylines.  I simply want to write a VBA script that highlights those polylines that are within a certain distance of eachother.  This is proving difficult due to the fact that querying within the same layer seems impossible in Arc.

My current approach involves creating a GeometryBag with buffered versions of the polylines, removing them one at a time and querying the rest with the buffered version.  This won't work because a geometry bag cannot be the target of a spatial filter(a feature class is required if I'm not mistaken).

If anyone can point me in the right direction as to how to accomplish this, it would be much appreciated.

This is my current(broken) code... that illustrates what I'm trying to do... the buffer distance is currently hardcoded to 50 units but it will be changed once I get it working:

Private Sub UIButtonControl1_Click()
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument

Dim pActiveView As IMap
Set pActiveView = pMxDoc.FocusMap

Dim pLayer As IFeatureLayer
Set pLayer = pActiveView.Layer(0)

Dim pFClass As IFeatureClass
Set pFClass = pLayer.FeatureClass

Dim pQFilter As IQueryFilter
Dim pFCursor As IFeatureCursor

Set pQFilter = New queryFilter
pQFilter.WhereClause = ""

Set pFCursor = pFClass.Search(pQFilter, True)
Dim pFeature As IFeature
Set pFeature = pFCursor.NextFeature

Dim pBag As IGeometryCollection
Set pBag = New GeometryBag

Do Until pFeature Is Nothing
    Dim pTopo As ITopologicalOperator
    Set pTopo = pFeature.Shape
    pBag.AddGeometry pTopo.Buffer(50)
    Set pFeature = pFCursor.NextFeature

Dim pSF As ISpatialFilter
Dim pFSel As IFeatureSelection

Dim i As Integer
    Set pGeom = pBag.Geometry(0)        'remove an item from the bag
    pBag.RemoveGeometries 0, 1
    Set pSF = New SpatialFilter
    Set pSF.Geometry = pGeom
    pSF.SpatialRel = esriSpatialRelIntersects   'see if the rest of the bag intersects it
    Set pFSel = pBag 'this should query the rest of the bag somehow
    pFSel.SelectFeatures pSF, esriSelectionResultAdd, False
Loop While pBag.GeometryCount > 0

MsgBox (pFSel.SelectionSet.Count)

End Sub

Thanks in advance.