My favorite way of getting just the attributesSuch nice functions, FeatureClassToNumPyArray, TableToNumPyArray, and back the other way. I am sure many of you have explored where it all comes from only to find it all buried in a *.pyd file <SPAN class="keyword token">import</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da <SPAN class="keyword token">as</SPAN> apd
apd<SPAN class="punctuation token">.</SPAN>__file__ <SPAN class="comment token"># ---- 'C:\\...install path...\\Resources\\ArcPy\\arcpy\\da.py'</SPAN>
<SPAN class="comment token"># ---- which is actually just imports arcgisscripting</SPAN>
<SPAN class="comment token"># ---- so import it directly</SPAN>
<SPAN class="keyword token">import</SPAN> arcgisscripting <SPAN class="keyword token">as</SPAN> ags
ags<SPAN class="punctuation token">.</SPAN>__file__
<SPAN class="comment token"># ---- 'C:\\...install path...\\bin\\Python\\envs\\arcgispro-py3\\lib\\</SPAN>
<SPAN class="comment token"># site-packages\\arcgisscripting.pyd'</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> No bother, since you can pull out data for the attributes nicely accounting for <null> records. <SPAN class="keyword token">def</SPAN> <SPAN class="token function">fc_data</SPAN><SPAN class="punctuation token">(</SPAN>in_fc<SPAN class="punctuation token">,</SPAN> verbose<SPAN class="operator token">=</SPAN><SPAN class="token boolean">False</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""Pull all editable attributes from a featureclass tables. During the
process, <null> values are changed to an appropriate type.
Parameters
----------
in_fc : text
Path to the input featureclass
verbose : boolean
Requires ``'prn_rec' in locals().keys()`` in order to set to ``True``.
``prn_rec`` is imported from arraytools.frmts
"""</SPAN>
flds <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'OID@'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'SHAPE@X'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'SHAPE@Y'</SPAN><SPAN class="punctuation token">]</SPAN>
null_dict<SPAN class="punctuation token">,</SPAN> fld_names <SPAN class="operator token">=</SPAN> _make_nulls_<SPAN class="punctuation token">(</SPAN>in_fc<SPAN class="punctuation token">,</SPAN> int_null<SPAN class="operator token">=</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">999</SPAN><SPAN class="punctuation token">)</SPAN>
fld_names <SPAN class="operator token">=</SPAN> flds <SPAN class="operator token">+</SPAN> fld_names
new_names <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'OID_orig'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'X_centroid'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Y_centroid'</SPAN><SPAN class="punctuation token">]</SPAN>
a <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>FeatureClassToNumPyArray<SPAN class="punctuation token">(</SPAN>in_fc<SPAN class="punctuation token">,</SPAN> fld_names<SPAN class="punctuation token">,</SPAN> skip_nulls<SPAN class="operator token">=</SPAN><SPAN class="token boolean">False</SPAN><SPAN class="punctuation token">,</SPAN>
null_value<SPAN class="operator token">=</SPAN>null_dict<SPAN class="punctuation token">)</SPAN>
a<SPAN class="punctuation token">.</SPAN>dtype<SPAN class="punctuation token">.</SPAN>names <SPAN class="operator token">=</SPAN> new_names <SPAN class="operator token">+</SPAN> fld_names<SPAN class="punctuation token">[</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">if</SPAN> verbose<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN> prn_rec<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># ** prn_rec imported from arraytools.frmts</SPAN>
<SPAN class="keyword token">except</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">return</SPAN> np<SPAN class="punctuation token">.</SPAN>asarray<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">)</SPAN>The explorers amongst us, may have discovered a few searchcursor shortcuts fld_names <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'OBJECTID'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Long_1'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Short_1'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Float_1'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Double_1'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Text_1'</SPAN><SPAN class="punctuation token">]</SPAN>
cur <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>in_fc<SPAN class="punctuation token">,</SPAN> fld_names<SPAN class="punctuation token">,</SPAN> explode_to_points<SPAN class="operator token">=</SPAN><SPAN class="token boolean">False</SPAN><SPAN class="punctuation token">)</SPAN>
z <SPAN class="operator token">=</SPAN> cur<SPAN class="punctuation token">.</SPAN>_as_narray<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
Traceback <SPAN class="punctuation token">(</SPAN>most recent call last<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
File <SPAN class="string token">"<ipython-input-220-b4e724f0982b>"</SPAN><SPAN class="punctuation token">,</SPAN> line <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">in</SPAN> <SPAN class="operator token"><</SPAN>module<SPAN class="operator token">></SPAN>
z <SPAN class="operator token">=</SPAN> cur<SPAN class="punctuation token">.</SPAN>_as_narray<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> Sadly, the integer fields with <nulls> bring the whole shortcut down. The searchcursor actually has enough information in it to create a structured/recarray. If you have a clean table with no nulls, the actual calls to _dtype and fields show that you can clearly link cursors and NumPy arrays. Too bad, the whole integer fix isn't incorporated, but _as_narray and FeatureClassToNumPyArray and TableToNumPyArray yield the same results on a 'clean' dataset. dir<SPAN class="punctuation token">(</SPAN>cur<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>snip<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="string token">'_as_narray'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'_dtype'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'fields'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'next'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'reset'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN> |