2

I am trying to loop through a list of features in a layer to:

  • select a feature
  • select all features adjacent to that feature
  • export the selection as a new table, then convert it to an excel file.

I have the code to do this for an individual selection:

# Select a zip 3 zone 
arcpy.SelectLayerByAttribute_management("test", "NEW_SELECTION", """ "zip3_str" = '432' """) 
# select all the adjacent features to that zip 3 zone
arcpy.SelectLayerByLocation_management("test", "BOUNDARY_TOUCHES")
# MakeTableView_management (in_table, out_view, {where_clause}, {workspace}, {field_info})
arcpy.MakeTableView_management("selection", "zip3")
# convert it to an excel file
arcpy.TableToExcel_conversion("zip3", r"J:\Strategic and Org. Dev. Department\PantryTrak\ARCGIS\Projects\20160921 - Zip 3 Relationship File\zip3.xls")

This is how I am attempting to write the loop but it is not working:

cursor = arcpy.SearchCursor("test")
count = 432

for row in cursor:
    arcpy.SelectLayerByAttribute_management("test", "NEW_SELECTION",""" "zip3" = count """) 
    arcpy.SelectLayerByLocation_management("test", "BOUNDARY_TOUCHES")
    arcpy.TableToExcel_conversion("zip3" , r"J:\Strategic and Org. Dev. Department\PantryTrak\ARCGIS\Projects\20160921 - Zip 3 Relationship File\\")
    count = count + 1 

The program gives me an error for the line:

arcpy.SelectLayerByAttribute_management("test", "NEW_SELECTION",""" "zip3" = count """). 

It says """ "zip3" = count """ is an invalid expression, although it works without the variable. I am not sure why it doesn't work when I try to use a variable.

  • Is your feature class in a personal, file, or SDE gdb? Also I think it is looking for zip3 = count the string, not the variable. – ed.hank Oct 5 '16 at 15:58
3

You could iterate through selected feature using a cursor and then do a new selection after obtaining unique ID field values of the selected features, but this is potentially slow.

The problem with the current approach you have is that after doing the spatial selection, you would have both adjacent and the feature "zip3_str" = '432' selected. I would suggest just removing this feature from selection before exporting out to Excel. Using MakeTableView tool is unnecessary as TableToExcel respects the selection on the layer.

import arcpy

fc = r'C:\GIS\test.gdb\_fishnet'
selection_fl = arcpy.MakeFeatureLayer_management(fc,'selection_fl')
export_fl = arcpy.MakeFeatureLayer_management(fc,'export_fl')

# Select a zip 3 zone
arcpy.SelectLayerByAttribute_management(selection_fl, "NEW_SELECTION", """ "zipcode" = 3 """)
# select all the adjacent features to that zip 3 zone
arcpy.SelectLayerByLocation_management(export_fl, "BOUNDARY_TOUCHES", selection_fl)
arcpy.SelectLayerByAttribute_management(export_fl,"REMOVE_FROM_SELECTION",""" "zipcode" = 3 """)
# convert it to an excel file
arcpy.TableToExcel_conversion(export_fl,r'C:\GIS\out.xls')
  • I want my export to include the adjacent features as well as the selection. The thing I am having trouble with is writing a script that will iterate through the features in the original layer. Could you illustrate what a script would look like for the first part of your response? – Rae Oct 5 '16 at 14:36
  • Removing the line arcpy.SelectLayerByAttribute_management(export_fl,"REMOVE_FROM_SELECTION",""" "zipcode" = 3 """) will do just that. – Alex Tereshenkov Oct 6 '16 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.