Python code to find nearest features.

5839
3
Jump to solution
08-02-2012 09:56 AM
RichardGordon1
New Contributor
I am looking at replicating some of our c# ArcObjects code into python to create a 10.0 ArcGIS Server geoprocessing tasks and wonder if the following is possible.

Without using the near_analysis tool as it requires ArcInfo/Advanced level licensing and adds fields onto the table being searched.

1)  From a specified point find all the features in a feature class that are within a specified distance of that point. I.e. use the buffer and clip analysis tools. The features being searched for can be point, line or polygon.
2) Iterate through each of the features that fall within the buffer created from the input point and work out the minimum distance of the feature from the point. Not too difficult if the feature class being searched is a point, but more difficult for lines and polygons, unless there is a handy arcpy function I have not come across?
3) I can then easily sort them in python into distance sequence.

The above is not too difficult using ArcObjects but with the move to python I cannot seem to find the same type of functionality in the arcpy module.

Thanks,
Richard
0 Kudos
1 Solution

Accepted Solutions
BruceHarold
Esri Regular Contributor
Richard

ArcPy geomerty objects have a distanceTo (other geometry) method that should get you what you need.
First use SelectByLocation to get your candidates, then loop through these with the distanceTo function to get the minimum.


Regards

View solution in original post

0 Kudos
3 Replies
BruceHarold
Esri Regular Contributor
Richard

ArcPy geomerty objects have a distanceTo (other geometry) method that should get you what you need.
First use SelectByLocation to get your candidates, then loop through these with the distanceTo function to get the minimum.


Regards
0 Kudos
RichardGordon1
New Contributor
Hi Bruce,

Thanks for the tips. I had looked through the Geometry object before posting and did not notice the distanceTo method as I am working with 10.0, looks like it was introduced in 10.1. Maybe time to upgrade.

Thanks,
Richard
0 Kudos
BruceHarold
Esri Regular Contributor
Richard

In the meantime you could build something with the math.hypot function but if you're not using point features it might be inefficient.

Regards
0 Kudos