1

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?

User1974
  • 1,132
  • 15
  • 57
  • 1
    Post you referred deals with city names that are supposedly unique. You need to create unique list of values and iterate through it. – FelixIP Aug 23 '20 at 20:31
  • 2
    Unique vals can be generated using a python set. i.e uniquevals = set((row[0] for row in cursor)) which you can then loop through i.e for val in uniquevals" – user2856 Aug 23 '20 at 21:59

2 Answers2

1

First you need to create a list of unique field values. Use a function like the one below:

def unique_values(table , field, criteria = None):
with arcpy.da.SearchCursor(table, [field], criteria) as cursor:
vals = set({row[0] for row in cursor})
if (None in vals):
    vals.remove(None)
if ('' in vals):
    vals.remove('')    

return sorted(vals)

Then as a seperate step loop through those values creating your layers. E.g.

vals = unique_values(fc)
for v in vals :
sql = '&quot;{0}&quot; = \'{1}\''.format (field, v)
MakeFeatureLayer_management (fc, v, sql)   
.... 

Anna Forrest
  • 568
  • 2
  • 8
0

This script works in ArcGIS Pro 2.4 (but not ArcGIS Desktop 10.7.1.):

def DuplicateLayer(CopyLayer, field, uniquevalue):
    p = arcpy.mp.ArcGISProject("Current")
    m = p.listMaps("Map")[0]
    LayerToDup = m.listLayers(CopyLayer)[0]
    print(LayerToDup.name)
    m.addLayer(LayerToDup, "AUTO_ARRANGE")
    LayerToDup.name = str(CopyLayer)
    lyr = m.listLayers(CopyLayer)[0]
    lyr.definitionQuery = field + " = '" + uniquevalue + "'"
    lyr.name = uniquevalue
    print (lyr.definitionQuery)

def unique_values(layer, field): with arcpy.da.SearchCursor(layer, [field]) as cursor: return sorted({row[0] for row in cursor})

def ExplodeFC(layername,field): fieldvals = unique_values(layername,field) for value in fieldvals: DuplicateLayer(layername,field,value)

ExplodeFC("WO_VW_FGDB","CLASSIFICATIONID")

User1974
  • 1,132
  • 15
  • 57