If you were interested in the farthest instead of nearest, then you would have to use Point Distance or Generate Near Table in your workflow, unless you wanted to role your own NumPy-based function. When working with data sets in the tens of thousands, which much from a practical stand point, I have found the Point Distance tool to be very slow and sometimes Generate Near Table fails with unspecified errors.
For situations where I want farthest points or quicker outputs similar to Point Distance, I have roled my own NumPy-based functions. For example:
def stat_point(in_features, other_features, stat='MINIMUM'):
import arcpy
import numpy
stats = {
'MINIMUM': {'FID': 'MIN_FID',
'DIST': 'MIN_DIST',
'INDEX': lambda x: 0},
'MAXIMUM': {'FID': 'MAX_FID',
'DIST': 'MAX_DIST',
'INDEX': lambda x: x - 1},
'MEDIAN_HIGH': {'FID': 'MEDH_FID',
'DIST': 'MEDH_DIST',
'INDEX': lambda x: x / 2},
'MEDIAN_LOW': {'FID': 'MEDL_FID',
'DIST': 'MEDL_DIST',
'INDEX': lambda x: x / 2 - 1 if x % 2 == 0 else x / 2 }
}
desc = arcpy.Describe(in_features)
SR = desc.spatialReference
desc = arcpy.Describe(other_features)
OID_name_other = desc.OIDFieldName
shape_name_other = desc.ShapeFieldName
narr_other = arcpy.da.FeatureClassToNumPyArray(
other_features,
[OID_name_other, desc.ShapeFieldName],
spatial_reference = SR
)
xy_other = narr_other[shape_name_other]
idx = stats[stat]['INDEX'](numpy.shape(xy_other)[0])
arcpy.AddField_management(in_features, stats[stat]['FID'], 'LONG')
arcpy.AddField_management(in_features, stats[stat]['DIST'], 'DOUBLE')
with arcpy.da.UpdateCursor(
in_features,
["SHAPE@XY", stats[stat]['FID'], stats[stat]['DIST']]
) as cur:
for xy, FID, DIST in cur:
xy_in = numpy.array(xy)
d0 = numpy.subtract.outer(xy_in[0], xy_other[:,0])
d1 = numpy.subtract.outer(xy_in[1], xy_other[:,1])
dist = numpy.hypot(d0, d1)
i = dist.argsort()[idx]
FID = narr_other[OID_name_other]
DIST = dist
cur.updateRow([xy, FID, DIST])
When used with "MINIMUM", the stat_point function mimics the Near tool. The Near tool is more performant than using stat_point to find the minimum distance point, but stat_point is orders of magnitude faster at finding non-nearest points compared to Point Distance or Generate Near Table. Of course, stat_point is doing planar measurements so that is something to be aware of. I threw MEDIAN parameters in as an example of how simple it is to extend this methodology beyond minimum or maxiumum points.
The NumPy heavy lifting, lines 43-45, comes from Alex Martelli in response to a Stack Overflow post: Euclidean distance between points in two different Numpy arrays, not within...