Generate closest features by distance
From near.py See references
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 near.py.
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 2x2 unit square, with a centre point. The points are arranged in clockwise order.
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> a <SPAN class="comment token"># a.shape => (5, 2)</SPAN>
array<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> dtype<SPAN class="operator token">=</SPAN>int32<SPAN class="punctuation token">)</SPAN>
The array reshaping <SPAN class="keyword token">is</SPAN> needed <SPAN class="keyword token">in</SPAN> order subtract the arrays<SPAN class="punctuation token">.</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> b <SPAN class="operator token">=</SPAN> a<SPAN class="punctuation token">.</SPAN>reshape<SPAN class="punctuation token">(</SPAN>np<SPAN class="punctuation token">.</SPAN>prod<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">.</SPAN>shape<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> a<SPAN class="punctuation token">.</SPAN>shape<SPAN class="punctuation token">[</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> b <SPAN class="comment token"># b.shape => (5, 1, 2)</SPAN>
array<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> dtype<SPAN class="operator token">=</SPAN>int32<SPAN class="punctuation token">)</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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 5x2 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.
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> diff <SPAN class="operator token">=</SPAN> b <SPAN class="operator token">-</SPAN> a <SPAN class="comment token"># diff.shape => (5, 5, 2)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
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 ( https://github.com/Dan-Patterson/numpy_samples/blob/master/formatting/arr_frmts.py )
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">import</SPAN> arr_tools <SPAN class="keyword token">as</SPAN> art
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> art<SPAN class="punctuation token">.</SPAN>frmt_<SPAN class="punctuation token">(</SPAN>diff<SPAN class="punctuation token">)</SPAN>
Array<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN>
<SPAN class="operator token">-</SPAN>shape <SPAN class="punctuation token">(</SPAN><SPAN class="number token">5</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">5</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> ndim <SPAN class="number token">3</SPAN>
<SPAN class="punctuation token">.</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">2</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">2</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">2</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">2</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">1</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">1</SPAN>
<SPAN class="punctuation token">.</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="number token">2</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">2</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">2</SPAN> <SPAN class="number token">2</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">1</SPAN> <SPAN class="number token">1</SPAN>
<SPAN class="punctuation token">.</SPAN> <SPAN class="number token">2</SPAN> <SPAN class="number token">2</SPAN> <SPAN class="number token">2</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="number token">2</SPAN> <SPAN class="number token">1</SPAN> <SPAN class="number token">1</SPAN>
<SPAN class="punctuation token">.</SPAN> <SPAN class="number token">2</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="number token">2</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">2</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">2</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="number token">1</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">1</SPAN>
<SPAN class="punctuation token">.</SPAN> <SPAN class="number token">1</SPAN> <SPAN class="number token">1</SPAN> <SPAN class="number token">1</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">1</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">1</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">1</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">1</SPAN> <SPAN class="number token">1</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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...
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> dist <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>einsum<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'ijk,ijk->ij'</SPAN><SPAN class="punctuation token">,</SPAN> diff<SPAN class="punctuation token">,</SPAN> diff<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># the magic happens...</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> d <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>sqrt<SPAN class="punctuation token">(</SPAN>dist<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>squeeze<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># get rid of extra 'stuff'</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> d <SPAN class="comment token"># the distance array...</SPAN>
array<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN> <SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.8</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1.4</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.8</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1.4</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN> <SPAN class="number token">2.8</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1.4</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.8</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1.4</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN> <SPAN class="number token">1.4</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1.4</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1.4</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1.4</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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.
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> kv <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>argsort<SPAN class="punctuation token">(</SPAN>d<SPAN class="punctuation token">,</SPAN> axis<SPAN class="operator token">=</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># sort 'd' on last axis to get keys</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> kv
array<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">4</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">3</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">4</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">3</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">4</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">3</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">4</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">4</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">3</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
>>> coords = a[kv] # for each point, pull out the points in closest order
>>> a[kv].shape # the shape is still not ready for use...
(5, 5, 2)
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.
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> s0<SPAN class="punctuation token">,</SPAN> s1<SPAN class="punctuation token">,</SPAN> s2 <SPAN class="operator token">=</SPAN> coords<SPAN class="punctuation token">.</SPAN>shape
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> coords <SPAN class="operator token">=</SPAN> coords<SPAN class="punctuation token">.</SPAN>reshape<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN>s0<SPAN class="punctuation token">,</SPAN> s1<SPAN class="operator token">*</SPAN>s2<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># the result will be a 2D array...</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> coords
array<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> dtype<SPAN class="operator token">=</SPAN>int32<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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.
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> dist <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>sort<SPAN class="punctuation token">(</SPAN>d<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># slice sorted distances, skip 1st</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> dist
array<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN> <SPAN class="number token">1.4</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.8</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN> <SPAN class="number token">1.4</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.8</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN> <SPAN class="number token">1.4</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.8</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN> <SPAN class="number token">1.4</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.8</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN> <SPAN class="number token">1.4</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1.4</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1.4</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1.4</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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...
<SPAN class="punctuation token">:</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN>
<SPAN class="punctuation token">:</SPAN>Closest <SPAN class="number token">2</SPAN> points <SPAN class="keyword token">for</SPAN> points <SPAN class="keyword token">in</SPAN> an array<SPAN class="punctuation token">.</SPAN> Results returned <SPAN class="keyword token">as</SPAN>
<SPAN class="punctuation token">:</SPAN> a structured array <SPAN class="keyword token">with</SPAN> coordinates <SPAN class="operator token">and</SPAN> distance values<SPAN class="punctuation token">.</SPAN>
Demonstrate n_near function <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN>
<SPAN class="punctuation token">:</SPAN>Input points<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> array <SPAN class="string token">'a'</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">:</SPAN>output array
ID Xo Yo C0_X C0_Y C1_X C1_Y Dist0 Dist1
<SPAN class="number token">0.00</SPAN> <SPAN class="number token">0.00</SPAN> <SPAN class="number token">0.00</SPAN> <SPAN class="number token">1.00</SPAN> <SPAN class="number token">1.00</SPAN> <SPAN class="number token">0.00</SPAN> <SPAN class="number token">2.00</SPAN> <SPAN class="number token">1.41</SPAN> <SPAN class="number token">2.00</SPAN>
<SPAN class="number token">1.00</SPAN> <SPAN class="number token">0.00</SPAN> <SPAN class="number token">2.00</SPAN> <SPAN class="number token">1.00</SPAN> <SPAN class="number token">1.00</SPAN> <SPAN class="number token">0.00</SPAN> <SPAN class="number token">0.00</SPAN> <SPAN class="number token">1.41</SPAN> <SPAN class="number token">2.00</SPAN>
<SPAN class="number token">2.00</SPAN> <SPAN class="number token">1.00</SPAN> <SPAN class="number token">1.00</SPAN> <SPAN class="number token">0.00</SPAN> <SPAN class="number token">0.00</SPAN> <SPAN class="number token">0.00</SPAN> <SPAN class="number token">2.00</SPAN> <SPAN class="number token">1.41</SPAN> <SPAN class="number token">1.41</SPAN>
<SPAN class="number token">3.00</SPAN> <SPAN class="number token">2.00</SPAN> <SPAN class="number token">2.00</SPAN> <SPAN class="number token">1.00</SPAN> <SPAN class="number token">1.00</SPAN> <SPAN class="number token">0.00</SPAN> <SPAN class="number token">2.00</SPAN> <SPAN class="number token">1.41</SPAN> <SPAN class="number token">2.00</SPAN>
<SPAN class="number token">4.00</SPAN> <SPAN class="number token">2.00</SPAN> <SPAN class="number token">0.00</SPAN> <SPAN class="number token">1.00</SPAN> <SPAN class="number token">1.00</SPAN> <SPAN class="number token">0.00</SPAN> <SPAN class="number token">0.00</SPAN> <SPAN class="number token">1.41</SPAN> <SPAN class="number token">2.00</SPAN>
<SPAN class="punctuation token">:</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN>
This <SPAN class="keyword token">is</SPAN> the final form of the array<SPAN class="punctuation token">.</SPAN>
array<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1.4142135</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1.4142135</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1.4142135</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1.4142135</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1.4142135</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">4</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1.4142135</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
dtype<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">'ID'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'<i4'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Xo'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'<f8'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Yo'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'<f8'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'C0_X'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'<f8'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">'C0_Y'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'<f8'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'C1_X'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'<f8'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">'C1_Y'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'<f8'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Dist0'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'<f8'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Dist1'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'<f8'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
I took the liberty of doing some fiddling with the format to make it easier to read. 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.
References:
----------
http://desktop.arcgis.com/en/arcmap/latest/tools/analysis-toolbox/generate-near-table.htm
https://github.com/Dan-Patterson/numpy_samples/blob/master/geometry/scripts/near.py
----------------------------------------------------------------------------