I'm trying to draw the nearest line between the nearest polygons in a Polygon dataset (ESRI Shapefile). I know that for this purpose there is the Near Tool (ArcGIS), but it just return the point TO which to connect but not the point FROM the first polygon and I don't want the centroid.
So I tried to do my own implementation and surprisingly it is running faster than Near Tool (at least for my huge data), and it is performing this way:
- Get a cursor and loop through all polygons
- For each polygon in cursor get another cursor2 from polygons which are in certain range from the extents
- Then it just calculate distances using built-in functions and get the closest one
- I can get the two closest points looping through all point in the polygons and use distance functions
Am I doing it the best way?
I tried using QgsSpatialIndex but it is returning the wrong pairs for some reason. Is there any built-in function to return the two closest points directly?
BTW I'm using arcpy, but previously I was using pyqgis.
Best Answer
Ok I could get it done with numpy instead of looping. I've done this way:
And the nearestPoint function: