---- The SearchCursor Approach ----The functions getSR and _view_ are described at the end. They are helper functions used to derive the spatial reference and to reshape the coordinates. The key operative in this approach is on line 8, _as_narray, which does the conversion behind the scenes. <SPAN class="keyword token">def</SPAN> <SPAN class="token function">cur_xy</SPAN><SPAN class="punctuation token">(</SPAN>in_fc<SPAN class="punctuation token">,</SPAN> to_pnts<SPAN class="operator token">=</SPAN><SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""Convert featureclass geometry (in_fc) to a simple 2D structured array
with ID, X, Y values. Optionally convert to points, otherwise centroid.
"""</SPAN>
SR <SPAN class="operator token">=</SPAN> getSR<SPAN class="punctuation token">(</SPAN>in_fc<SPAN class="punctuation token">)</SPAN>
flds <SPAN class="operator token">=</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>
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> flds<SPAN class="punctuation token">,</SPAN> spatial_reference<SPAN class="operator token">=</SPAN>SR<SPAN class="punctuation token">,</SPAN>
explode_to_points<SPAN class="operator token">=</SPAN>to_pnts<SPAN class="punctuation token">)</SPAN>
a <SPAN class="operator token">=</SPAN> cur<SPAN class="punctuation token">.</SPAN>_as_narray<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
a <SPAN class="operator token">=</SPAN> _view_<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">return</SPAN> a<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>---- Results .... Essentially you have a simplified list of X, Y coordinates since the 'shp' was defined as 'SHAPE@X' and 'SHAPE@Y' with 'explode_to_points' set to True (False, returns centroids). Sadly you can't reconstruct the polygons unless you deal with the which points belong to what polygon. array([[ 300020., 5000000.],
[ 300010., 5000000.],
[ 300010., 5000010.],
...,
[ 300002., 5000002.],
[ 300008., 5000002.],
[ 300005., 5000008.]])<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN> |
---- Retrieving shape objects ---- If you want to use arcpy directly because of the builtin methods, you need the contents of the 'shape' fields using SHAPE@' rather than just extracting the X and Y coordinates as in the previous example <SPAN class="keyword token">def</SPAN> <SPAN class="token function">fc_shapes</SPAN><SPAN class="punctuation token">(</SPAN>in_fc<SPAN class="punctuation token">,</SPAN> as_array<SPAN class="operator token">=</SPAN><SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""Derive, arcpy geometry objects from a featureClass searchcursor.
Parameters
----------
in_fc : text
Path to the input featureclass
as_array: boolean
True, return an object array of arcpy polygon objects. False, returns
a list.
"""</SPAN>
SR <SPAN class="operator token">=</SPAN> getSR<SPAN class="punctuation token">(</SPAN>in_fc<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">with</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> <SPAN class="string token">'SHAPE@'</SPAN><SPAN class="punctuation token">,</SPAN> None<SPAN class="punctuation token">,</SPAN> SR<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> cursor<SPAN class="punctuation token">:</SPAN>
a <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> cursor<SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">if</SPAN> as_array<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>
<SPAN class="keyword token">return</SPAN> a
<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>---- Put it to work ---- polys = fc_shapes(in_fc, as_array=True)
polys
array([<Polygon object at 0x197f1bcebe0[0x197f0199968]>,
<Polygon object at 0x197f1bcec18[0x197e9b48da0]>,
<Polygon object at 0x197f1bceb70[0x197f005b738]>,
<Polygon object at 0x197f1bceb38[0x197f005b5a8]>,
<Polygon object at 0x197f1bceac8[0x197f005b760]>], dtype=object)<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN> |
---- Geometry as a structured array ----Nothing fancy, but there is an integer ID field indicating which feature a point belongs to and the coordinates. A simpler version of the above... just an ID field and coordinates. <SPAN class="keyword token">def</SPAN> <SPAN class="token function">fc_xyID</SPAN><SPAN class="punctuation token">(</SPAN>in_fc<SPAN class="punctuation token">,</SPAN> to_pnts<SPAN class="operator token">=</SPAN><SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""Convert featureclass geometry (in_fc) to a simple 2D structured array
with ID, X, Y values. Optionally convert to points, otherwise centroid.
"""</SPAN>
SR <SPAN class="operator token">=</SPAN> getSR<SPAN class="punctuation token">(</SPAN>in_fc<SPAN class="punctuation token">)</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>
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> flds<SPAN class="punctuation token">,</SPAN> spatial_reference<SPAN class="operator token">=</SPAN>SR<SPAN class="punctuation token">,</SPAN>
explode_to_points<SPAN class="operator token">=</SPAN>to_pnts<SPAN class="punctuation token">)</SPAN>
a <SPAN class="operator token">=</SPAN> cur<SPAN class="punctuation token">.</SPAN>_as_narray<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
a<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">'IDs'</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">'X_s'</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">'Y_s'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'<f8'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">return</SPAN> a<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>---- The results ---- a = fc_xyID(in_fc, to_pnts=True)
a
array([(1, 300010., 5000000.), (1, 300000., 5000000.), (1, 300000., 5000010.),
(1, 300010., 5000010.),(1, 300010., 5000000.),
... snip
(5, 300020., 5000010.),(5, 300022., 5000010.), (5, 300025., 5000002.),
(5, 300028., 5000010.), (5, 300030., 5000010.),(5, 300026., 5000000.),
(5, 300024., 5000000.), (5, 300020., 5000010.)],
dtype=[('IDs', '<i4'), ('X_s', '<f8'), ('Y_s', '<f8')])<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>The polygon ID that each point belongs to is retained, however, it is replicated many times and the null points separating polygon parts is removed. |