Select to view content in your preferred language

Select by Location

773
3
04-07-2010 08:57 PM
PriyankaMehta
Deactivated User
Hi all,
  
   This is relatively a common query but somehow i m not being able to solve it. I have 2 point shapefiles and i need to select points from Layer1 that intersects with points of Layer 2.

I am trying this code :

   Dim IMxDoc As IMxDocument
    Dim pMap As IMap
    Dim Point_Layer1 As IFeatureLayer
    Dim Point_Layer2 As IFeatureLayer
    Dim pFeatureCursor As IFeatureCursor
    
    Dim pFeature As IFeature
    Dim pGeometryCollection As IGeometryCollection
    Dim pSpatialfilter As ISpatialFilter
    Dim pFeatureSelection As IFeatureSelection
     
Set IMxDoc = ThisDocument
Set pMap = IMxDoc.ActiveView

Set Point_Layer1 = pMap.Layer(0)
Set Point_Layer2 = pMap.Layer(1)

     
     Set pFeatureCursor = Point_Layer1.FeatureClass.Search(Nothing, False)
     Set pGeometryCollection = New GeometryBag
     
     Set pFeature = pFeatureCursor.NextFeature
     
     Do While Not pFeature Is Nothing
          pGeometryCollection.AddGeometry pFeature.Shape
          Set pFeature = pFeatureCursor.NextFeature
     Loop
     
     Set pSpatialfilter = New SpatialFilter
     
     With pSpatialfilter
          Set .Geometry = pGeometryCollection
          .GeometryField = Point_Layer2.FeatureClass.ShapeFieldName
          .SpatialRel = esriSpatialRelIntersects
     End With
     
     Set pFeatureSelection = Point_Layer1
     pFeatureSelection.SelectFeatures pSpatialfilter, esriSelectionResultNew, False
     
  IMxDoc.ActiveView.Refresh
     



but for some reason it selects all the points in layer(0).. where am i going wrong.. I need to select only the intersecting points.

Thanks in Advance,
Priyanka Mehta
0 Kudos
3 Replies
KirkKuykendall
Deactivated User
Are they exact intersections?  If not you'll need to buffer them into polygons first.

Otherwise,  maybe try using a multipoint instead of a geometrybag.

You probably should explicitly set the spatialreference of the multipoint to that of the points you are adding to it. 

Performance might be improved using ISpatialIndex.Invalidate on the multipoint before assigning it to the spatialfilter.
0 Kudos
PriyankaMehta
Deactivated User
Hey Thanks Kirk,
  Problem solved.  Although used another code of yours for that. I had three steps for my application

1) Select points from Point Layer1 that intersects/ overlaps with point layer 2
2) Delete them from Point Layer 1
3) Merge Point Layer 2

So, I thought of going through the other way

1) Merge point layer 2
2) Select overlapping points in that layer
3) Delete the overlapping points

For the 2nd step .. I tried this code of yours

http://forums.esri.com/Thread.asp?c=93&f=992&t=77245

and its done !!!


Thanks a lot !
Priyanka Mehta
0 Kudos
PriyankaMehta
Deactivated User
Hi,
Problem not solved 😞 
I only want features from the point_Layer1 to be deleted.. so i ll have to stick to the first approach only, where i have to select intersecting points from layer1 and then delete  them and then merge (to be sure that points from first layer are being deleted )I ll try with the way  you suggested. Might have to get back to you for help again..

Btw.. how do i go for this :
"You probably should explicitly set the spatialreference of the multipoint to that of the points you are adding to it. "

Thanks a lot !
Priyanka
0 Kudos