Dan:
I was trying to use your code, since I want to find the minimum distance to the actual vertice points in a polyline with the onMouseDownMap function of an addin tool. Here is the code I tried:
class SplitLineToolClass(object):
"""Implementation for SplitLine_addin.SplitLinetool (Tool)"""
def __init__(self):
self.enabled = False
self.cursor = 3 # Crosshairs
def onMouseDownMap(self, x, y, button, shift):
# Pass if not a Left Mouse Button Click
if button != 1:
pass
return
addressLines = pythonaddins.GetSelectedTOCLayerOrDataFrame()
## Code that verifies a polyline layer is selected in the TOC
desc=arcpy.Describe(addressLines)
sr = arcpy.SpatialReference(desc.spatialReference.factoryCode)
origin = np.array([x,y],dtype="float64")
dests = arcpy.da.FeatureClassToNumPyArray(addressLines, ["SHAPE@X","SHAPE@Y"], "", sr, explode_to_points=True)
deltas = dests - origin
distances = np.hypot(deltas[:,0], deltas[:,1])
min_dist = np.min(distances)
wh = np.where(distances == min_dist)
closest = dests[wh[0]]
print("Closest Vertice = {0}".format(closest))
However, my adaptation of your code is throwing an error on the line that subtracts the two arrays (deltas = dests - origin on line 18 of the code above). Here is the error:
Traceback (most recent call last):
File ..., line ..., in onMouseDownMap
deltas = dests - origin
TypeError: unsupported operand type(s) for -: 'numpy.ndarray' and 'numpy.ndarray'
When I added print lines for the origin and dest arrays, it showed that the Origin array created by line 16 is a list with a space a number a double space and a number, while the Dest array was a list of tuples containing two numbers separated by a comma and a space. What is the best way to get the two arrays compatible for line 18?
Here is how the arrays printed:
Origin Point = [ 6305489.12520918 2169986.76545799]
Destination Points = [(6305087.957613736, 2169963.2243613005)
(6305515.50958015, 2169962.629874304)
...
(6305742.797839478, 2169860.239002958)]
When I tried the "SHAPE@XY" field instead the result seemed worse:
Destination Point = [([6305087.957613736, 2169963.2243613005],)
([6305515.50958015, 2169962.629874304],)
...
([6305742.797839478, 2169860.239002958],)]