1

I'm trying to iterate through a user-specified field, extracting the unique values of that field into a list. I then want to loop through that unique list, doing a select by attributes and then extracting that group into separate feature classes.

My current script reads as follows:

import arcpy, os
from arcpy import env

env.overwriteOutput = True

#set input parameters
inputFeatures = arcpy.GetParameterAsText(0) # input feature layer
fieldName = arcpy.GetParameterAsText(1) # selecting features field name
outputWorkspace = arcpy.GetParameterAsText(2) #output .gdb workspace
outputFilePrefix = arcpy.GetParameterAsText(3) # string prefix for outputs

env.workspace = outputWorkspace

values= []

with arcpy.da.SearchCursor(inputFeatures, fieldName) as cursor:
    for row in cursor:
        values.append(row[0])
        uniqueValues = set(values)
        valueList = list(uniqueValues)

inputLayer = arcpy.MakeFeatureLayer_management(inputFeatures, "inputLayer")

arcpy.AddMessage("...Extracting unique values: ")
with arcpy.da.SearchCursor(inputLayer, "*") as cursor:
    for selection in valueList:
        arcpy.AddMessage(selection)
        arcpy.SelectLayerByAttribute_management(inputLayer, "NEW_SELECTION", "\'fieldName\' = \'%s\'" %(selection))
        arcpy.CopyFeatures_management(inputLayer, outputFilePrefix + str(selection))

The script executes without any traceback errors, however nothing is actually in the output feature classes, they are empty. Could anyone suggest why this might be the case?

3

Instead of:

inputLayer = arcpy.MakeFeatureLayer_management(inputFeatures, "inputLayer")

arcpy.AddMessage("...Extracting unique values: ")
with arcpy.da.SearchCursor(inputLayer, "*") as cursor:
    for selection in valueList:
        arcpy.AddMessage(selection)
        arcpy.SelectLayerByAttribute_management(inputLayer, "NEW_SELECTION", "\'fieldName\' = \'%s\'" %(selection))
        arcpy.CopyFeatures_management(inputLayer, outputFilePrefix + str(selection))

you could try this:

arcpy.MakeFeatureLayer_management(inputFeatures, "inputLayer")

arcpy.AddMessage("...Extracting unique values: ")
with arcpy.da.SearchCursor("inputLayer", "*") as cursor:
    for selection in valueList:
        arcpy.AddMessage(selection)
        arcpy.SelectLayerByAttribute_management("inputLayer", "NEW_SELECTION", "{0} = '{1}'".format(fieldName,selection))
        arcpy.CopyFeatures_management("inputLayer", outputFilePrefix + str(selection))

but to be honest I would just use the Split By Attributes tool which:

Splits an input dataset by unique attributes.

  • yah, no need to re-invent the wheel – NULL.Dude Apr 17 '18 at 13:34
  • Thanks, I've tried your suggested code previously but ended up with the same result. The Split By Attributes tool kind of works as an alternative however, my preference is to get my original script to work because it gives me more flexibility to add the new layers to the map as the tool runs as well as add a prefix name to organise the new files within the geodatabase. I am having lots of difficulty lately working with temporary "layers" in scripts run within arc toolbox and would like to isolate and fix this issue for other models/scripts I also need to update. – smh Apr 17 '18 at 22:57
  • 1
    I've revised my code w.r.t. the where_clause which should work if you are using a text field, and needs the single quotes taken out for numeric fields. – PolyGeo Apr 17 '18 at 23:04
  • The revised code you provided works and the features are correctly being saved in the output feature classes. I just need to account for null values now. – smh Apr 17 '18 at 23:51

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.