Note that there are some explanatory texts on larger screens.

plurals
  1. POLoading a feature class in a list using arcpy : strange behaviour of SearchCursor
    text
    copied!<p>Using <code>arcpy</code>, my purpose is to stock a feature class in a list for further processing. Each row will be a dict of <code>{'field name': value}</code>, including geometry.</p> <p>The most pythonic way to achieve this task should be to use list comprehension :</p> <pre><code>fc = '/path/to/fc' fields = [f.name for f in arcpy.ListFields(fc)] # get field list features = [[row.getValue(f) for f in fields] for row in arcpy.SearchCursor(fc)] </code></pre> <p>This method works for data but geometries in the list are all the same (the last geometry retrieved in fc). <a href="https://stackoverflow.com/questions/10815149/appending-to-a-tuple-overwriting-previous-values">This behaviour of SearchCursor has already been commented on StackOverflow</a>.</p> <p>I tried another approach:</p> <pre><code>fc = '/path/to/fc' shape_field = arcpy.Describe(fc).shapeFieldName # load geometry in a list geom = arcpy.Geometry() feat = [{shape_field: f} for f in arcpy.CopyFeatures_management(fc, geom)] # slow # load data in a list fields = [f.name for f in arcpy.ListFields(fc)] data = [dict([(f, row.getValue(f)) for f in fields if f != shape_field]) for row in arcpy.SearchCursor(fc)] # slow # merge merge = zip(feat, data) merge = [dict([(k, v) for adict in line for (k, v) in adict.items()]) for line in merge] # sorry for that... </code></pre> <p>It works with my dataset but:</p> <ul> <li>It is slow.</li> <li>I'm not sure it's safe to assert that data and feat come in the same order.</li> </ul> <p>Any opinion on that ?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload