How to iteratively select by unique field values and create new feature

992
4
Jump to solution
01-22-2021 01:26 PM
PenelopeMitchell2
New Contributor II

Hello,

I am trying to iteratively select by attribute>create feature layer> create new feature class(es) from the selections. But I am getting errors in the Make Feature Layer process.  Any suggestions?

Thanks, Penelope 

Update: Edited for code formatting--thanks for the tip.

 

 

#obtain unique values

values = [row[0] for row in arcpy.da.SearchCursor(SortiesLnFP, 'RS_Name')]
uniqueValues = set(values)

print(uniqueValues)

 

 

#create where clause for .makefeaturelayer using unique Values
#use where query to make a feature layer from selection then convert to feature class

OutPath = wkspc
for value in uniqueValues:
    OutFile = wkspc + os.sep + str(value)
    query = "\"RS_Name = '{}'\"".format(value)
    #query = "RS_Name = " + str(value)
    sortieLayer = arcpy.management.MakeFeatureLayer(SortiesLnFP, OutFile, query)
    arcpy.conversion.FeatureClassToFeatureClass(sortieLayer, wkspc, str(value))
    arcpy.managementDelete(sortieLayer)

 

 

 

#Output of query
"RS_Name = 'Pink_PM'"
"RS_Name = 'Pink2'"
#i've tried w/ the "" and without, get the same error either way-- I saw another post including those for the same purpose so thought I would try it (https://community.esri.com/t5/arcgis-api-for-python-questions/select-unique-values-from-field-using-selectbyattribute/td-p/825616/page/2)

 

---------------------------------------------------------------------------
ExecuteError                              Traceback (most recent call last)
In  [67]:
Line 6:     sortieLayer = arcpy.management.MakeFeatureLayer(SortiesLnFP, OutFile, query)

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py, in MakeFeatureLayer:
Line 8668:  raise e

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py, in MakeFeatureLayer:
Line 8665:  retval = convertArcObjectToPythonObject(gp.MakeFeatureLayer_management(*gp_fixargs((in_features, out_layer, where_clause, workspace, field_info), True)))

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py, in <lambda>:
Line 511:   return lambda *args: val(*gp_fixargs(args, True))

ExecuteError: ERROR 000358: Invalid expression
Failed to execute (MakeFeatureLayer).

 

0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor
4 Replies
DanPatterson
MVP Esteemed Contributor

Code formatting ... the Community Version - GeoNet, The Esri Community

will help make your code more readable 

BTW

sortieLayer = arcpy.management.MakeFeatureLayer(SortiesLnFP, OutFile, query)
arcpy.conversion.FeatureClassToFeatureClass(SortieLayer

case difference !


... sort of retired...
DavidPike
MVP Frequent Contributor

Agreed, the code is very hard to read and unformatted.

No idea if the rest is any good, but it seems that your query is enclosed in another set of quotes, remove those and it should be valid (if value is a string, if its a number - remove the single quotes also).

 

query = "RS_Name = '{}'".format(value)
0 Kudos
DanPatterson
MVP Esteemed Contributor

of course there is always

Split By Attributes (Analysis)—ArcGIS Pro | Documentation


... sort of retired...
PenelopeMitchell2
New Contributor II

Thank you very much @DanPatterson for pointing me in the right direction. I cannot believe I've never used that tool, very useful! 

Best,

Penelope

0 Kudos