I'm new to ArcPy.
I'm trying to write a script that will loop through the unique values in a field in a FC and create dynamic layers, with definition queries, for each unique value.
- Each of the dynamic layers would all point to the same master feature class.
- I do not want to output static feature classes. For example, I can't use the split by attributes tool for this.
I've attempted to adapt an ArcPy script from a related answer to do it:
from arcpy import *
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
fc = "WO_VW_FGDB"
field = "CLASSIFICATIONID"
env.overwriteOutput = True
#Create cursor to iterate rows
cursor = da.SearchCursor (fc, field)
for row in cursor:
#sql statement for a single feature
sql = '"{0}" = \'{1}\''.format (field, row[0])
#Make layer with sql for one feature only. Use the field value as the layer name.
MakeFeatureLayer_management (fc, row[0], sql)
#Make mapping layer object
lyr = mapping.Layer(row[0])
#Add mapping layer object to map
mapping.AddLayer (df, lyr)
del cursor
mxd.save ()
When I run the script in ArcGIS Desktop 10.7.1, it successfully creates the first layer. In fact, it actually creates duplicate layers.
But then it produces an error:
Runtime error Traceback (most recent call last): File "<string>", line 17, in <module>
File "c:\program files (x86)\arcgis\desktop10.7\arcpy\arcpy\management.py", line 6986, in MakeFeatureLayer
raise e ExecuteError: ERROR 000622: Failed to execute (Make Feature Layer). Parameters are not valid.
ERROR 000628: Cannot set input into parameter in_features.
What am I doing wrong?
set
. i.euniquevals = set((row[0] for row in cursor))
which you can then loop through i.efor val in uniquevals"