Generate closest features by distance
Emulating Generate Near Table from ArcMap

Let us begin with finding the closest 3 points to every point in a point set.
Well, that is easy… we just use the ‘Generate Near Table tool’.

You have been spoiled. This tool is only available at the Advanced license level. You are now working in a job that uses a Standard license… what to do!?

Of course!…. roll out your own.
We will begin with a simple call to ‘n_near’ in

We can step through the process…

Begin with array ‘a’. Since we are going to use einsum to perform the distance calculations, we need to clone and reshape the array to facilitate the process.

The following array, ‘a’, represents the 4 corners of a 2×2 unit square, with a centre point. The points are arranged in clockwise order.

The array reshaping is needed in order subtract the arrays.

I have documented the details of the array construction and einsum notation elsewhere. Suffice to say, we can now subtract the two arrays, perform the einsum product summation and finish with the euclidean distance calculation.

The difference array produces 5 blocks of 5×2 values. The summation of the products of these arrays essentially yields the squared distance, from which, euclidean distance is derived. There are other ways of doing this, such as dot product calculations. I prefer einsum methods since it can be scaled up from 1D to n-D unlike most other approaches.

The ‘diff’ array looks like the following. I took the liberty of using a function in arr_tools (on github) to rearrange the array into a more readable form ( )

The distance calculation is pretty simple, just a bit of einsum notation, get rid of some extraneous dimensions if present and there you have it…

The result as you can see from the above is a row-column structure much like that derived from scipy’s cdist function. Each row and column represents a point, resulting in the diagonal having a distance of zero.

The next step is to get a sorted list of the distances. This is where np.argsort comes into play, since it returns a list of indices that represent the sorted distance values. The indices are used to pull out the coordinates in the appropriate order.

The coordinate array (coords) needs to be reshaped so that the X, Y pair values can be laid out in row format for final presentation. Each point calculates the distances to itself and the other points, so the array has 5 groupings of 5 pairs of coordinates. This can be reshaped, to produce 5 rows of x, y values using the following.

Each row represents an input point in the order they were input. Compare input array ‘a’ with the first two columns of the ‘coords’ array to confirm. The remaining columns are pairs of the x, y values arranged by their distance sorted order (more about this later).

The distance values are then sorted in ascending order. Obviously, the first value in each list will be the distance of each point to itself (0.0) so it is sliced off leaving the remaining distances.

If you examine the points that were used as input, they formed a rectangle with a point in the middle. It should come as no surprise that the first column represents the distance of each point to the center point (the last row). The next two columns are the distance of each point to its adjacent neighbour while the last column is the distance of each point to its diagonal. The exception is of course the center point (last row) which is equidistant to the other 4 points.

The rest of the code is nothing more that a fancy assemblage of the resultant data into a structure that can be used to output a structured array of coordinates and distances, which can be brought in to ArcMap to form various points or polyline assemblages.
Here are the results from the script…

This is the final form of the array.

It should be readily apparent that this array could be used as input to NumPyArrayToFeatureClass so that you can produce a featureclass or shapefile of the data.

That is about all now. There are a variety of ways to perform the same thing… hope this adds to your arsenal of tools.