I am trying to create a Python toolbox which will iterate through selected features and create a statistics table for each selected feature.
It works as follow:
- Define parameters, including the input layers with selected features
- Export selected features in new layer
- Use
searchCursor()to iterate through rows of the new layer - Get row value
- Use row value in an expression to apply
SelectLayerByAttribute() - Use selected feature to clip another input layer
- Apply some other tools before compute statistics on the other input layer
fc = newLayer
field = "ID_reserve_castor"
cursor = arcpy.SearchCursor(fc)
row = cursor.next()
while row:
value = row.getValue(field)
# Process: Select Layer By Attribute (Select Layer By Attribute) (management)
Reserve_castor_ExportFeature = arcpy.management.SelectLayerByAttribute(
in_layer_or_view=fc,
selection_type="NEW_SELECTION",
where_clause='"ID_reserve_castor" = ' + value,
invert_where_clause="")
row = cursor.next()
The iteration and row.getValue() work, but not the SelectLayerByAttribute().
As the new layer is saved in a geodatabase, I used the exemple mentioned in esri technical support web page.
#Identifies File Geodatabase
if WPtype == "esriDataSourcesGDB.FileGDBWorkspaceFactory.1":
#sample where clause for fgdb data
WhereClause = '"ObjectID" = ' + value
print WhereClause
What did I miss ?
arcpy.SearchCursorfor anything. Instead usearcpy.da.SearchCursor. If you must manage the selection environment of a layer inside a cursor, make sure it doesn't corrupt the layer on which the cursor is already operating. Best practice is to never nest cursors.