Select to view content in your preferred language

Python code to find nearest features.

6379
3
08-02-2012 09:56 AM
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
Tags (3)
1 Solution

Accepted Solutions
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
3 Replies
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
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
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