I had made reference before of using numpy and arcpy together to perform particular tasks. Sometimes the combination of the two enables the user to do things they wouldn't otherwise be able to do. This ditty presents a very simple way to sort a dataset and produce a permantly sorted version while maintaining the original feature identification numbers. So to begin with, I will remind/introduce the reader to the arcpy data access module's functions as well as comparable ones in numpy.
On the arcmap side, the user should become familiar with:
- arcpy.Describe
- arcpy.Sort
- arcpy.da.FeatureClassToNumPyArray and
- arcpy.da.NumPyArrayToFeatureClass.
From the numpy side:
- np.setprintoptions
- np.arange and
- np.sort
The latter performs the same work as arcpy.Sort, but it doesn't have any license restrictions as the Sort tool does. It also allows you to produce a sequential list of numbers like the oft used Autoincrement field calculator function.
The script will perform the following tasks:
- import the required modules,
- specify the input file,
- the spatial reference of the input,
- the fields used to sort on, in the order of priority,
- a field to place the rank values, and
- specify the output file.
One can add the field to place the ranks into ahead of time or add a line of code to do it within the script (see arcpy.AddField_management).
<SPAN class="comment token"># imports ......</SPAN>
<SPAN class="keyword token">import</SPAN> numpy <SPAN class="keyword token">as</SPAN> np
<SPAN class="keyword token">import</SPAN> arcpy
np<SPAN class="punctuation token">.</SPAN>set_printoptions<SPAN class="punctuation token">(</SPAN>edgeitems<SPAN class="operator token">=</SPAN><SPAN class="number token">4</SPAN><SPAN class="punctuation token">,</SPAN>linewidth<SPAN class="operator token">=</SPAN><SPAN class="number token">75</SPAN><SPAN class="punctuation token">,</SPAN>precision<SPAN class="operator token">=</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN>
suppress<SPAN class="operator token">=</SPAN><SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">,</SPAN>threshold<SPAN class="operator token">=</SPAN><SPAN class="number token">10</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
<SPAN class="comment token"># inputs ......</SPAN>
input_shp <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'c:\!Scripts\Shapefiles\RandomPnts.shp'</SPAN>
all_fields <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>fld<SPAN class="punctuation token">.</SPAN>name <SPAN class="keyword token">for</SPAN> fld <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListFields<SPAN class="punctuation token">(</SPAN>input_shp<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN>
SR <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Describe<SPAN class="punctuation token">(</SPAN>input_shp<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>spatialReference
sort_fields <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'X'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'Y'</SPAN><SPAN class="punctuation token">]</SPAN>
order_field <SPAN class="operator token">=</SPAN> <SPAN class="string token">'Sort_id'</SPAN>
output_shp <SPAN class="operator token">=</SPAN> r'c<SPAN class="punctuation token">:</SPAN>\!Scripts\Shapefiles\SortedPnts<SPAN class="punctuation token">.</SPAN>
<SPAN class="comment token"># outputs ......</SPAN>
arr <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>FeatureClassToNumPyArray<SPAN class="punctuation token">(</SPAN>input_shp<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"*"</SPAN><SPAN class="punctuation token">,</SPAN> spatial_reference<SPAN class="operator token">=</SPAN>SR<SPAN class="punctuation token">)</SPAN>
arr_sort <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>sort<SPAN class="punctuation token">(</SPAN>arr<SPAN class="punctuation token">,</SPAN> order<SPAN class="operator token">=</SPAN>sort_fields<SPAN class="punctuation token">)</SPAN>
arr_sort<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'Sort_id'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>arange<SPAN class="punctuation token">(</SPAN>arr_sort<SPAN class="punctuation token">.</SPAN>size<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>NumPyArrayToFeatureClass<SPAN class="punctuation token">(</SPAN>arr_sort<SPAN class="punctuation token">,</SPAN> output_shp<SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'Shape'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> SR<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># summary .....</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">'\nInput file: {}\nField list: {}'</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>input_shp<SPAN class="punctuation token">,</SPAN>all_fields<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Sort by: {}, ranks to: {}'</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>sort_fields<SPAN class="punctuation token">,</SPAN>orderfield<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></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>result
Input file<SPAN class="punctuation token">:</SPAN> c<SPAN class="punctuation token">:</SPAN>\!Scripts\Shapefiles\RandomPnts<SPAN class="punctuation token">.</SPAN>shp
Field list<SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">[</SPAN>u<SPAN class="string token">'FID'</SPAN><SPAN class="punctuation token">,</SPAN> u<SPAN class="string token">'Shape'</SPAN><SPAN class="punctuation token">,</SPAN> u<SPAN class="string token">'Id'</SPAN><SPAN class="punctuation token">,</SPAN> u<SPAN class="string token">'X'</SPAN><SPAN class="punctuation token">,</SPAN> u<SPAN class="string token">'Y'</SPAN><SPAN class="punctuation token">,</SPAN> u<SPAN class="string token">'Sort_id'</SPAN><SPAN class="punctuation token">]</SPAN>
Sort by<SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'X'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Y'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> ranks to<SPAN class="punctuation token">:</SPAN> Sort_id
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Give it a try with your own files.