Finding the nearest feature to reference point in a target layer

399
3
10-28-2013 07:19 AM
FilipeEstacio
New Contributor
Hi all,

I've been searching the forums for the past few days and trying out different solutions but somehow can't work this one out. I'm in the middle of coding up a macro that automates a number of functions we have and need to find a way of doing the following:

I have a point feature selected and want to find the nearest feature of a different layer.

Essentially it's writing the code of a private function such as "GetNearestPoint(RefFeature as IFeature, TargetLayer as ILayer, pMap as IMap).

However the TargetLayer that applies in my case has literally millions of entries and there is no way I can write code that goes through all of them. Also I'm running ArcMap through Citrix so performance is an issue. Any suggestions?
0 Kudos
3 Replies
NeilClemmons
Regular Contributor III
You can use IIndexQuery2.NearestFeature for this.  The method will return the ObjectId of the nearest feature as well as its distance from the input geometry.
0 Kudos
FilipeEstacio
New Contributor
Thanks for the quick reply, Neil.

I had found the IIndexQuery2.NearestFeature but wasn't able to work out how to implement it. Do you happen to have a snippet of code that illustrates this?
0 Kudos
NeilClemmons
Regular Contributor III
IIndexQuery is implemented by the FeatureIndex class.  This class also implements IFeatureIndex.  Use IFeatureIndex to set the feature class and build the index.  You can also use a feature cursor to build the index if you need to reduce the number of features being indexed.
0 Kudos