Help w/ MakeFeatureLayer and Assertion Error

2828
1
04-01-2014 10:56 AM
kg76
by
Occasional Contributor
I'm trying to write a script that compiles all of the stuff I do to a geodatabase on a weekly basis. This includes:

1. Backing up the geodatabase
2. Renaming the backed up geodatabase to include a time/date stamp
3. Compacting the original geodatabase
4. Remove legacy accuracy data if data has been updated
5. Add/update XY coordinates for all point feature classes
6. Performing an attribute selection and then running the frequency tool on the selection to detect duplicate entries
7. Add the outputs from the frequency tool to an existing map document and join the tables to the appropriate feature classes

I'm good through step 5 but I keep getting stuck on step 6 because of the need to make feature layers in order to do an attribute selection. I'm getting an AssertionError.

Anyway, here's what I have:

#TASK 4 - Access map document, make feature layers from all feature
#classes, perform selection, copy features to new fcs, run frequency
#tool on new fcs, add feature layers and frequency tables to map
#document, join frequency tables to feature layers

#Set variables

mxd = arcpy.mapping.MapDocument("S:\\GIS\\gis_work\\2014\\FLAG\\ArchGDB_Maint_Model\\GIS\\Script_Test\\Admin_Map2.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyrList = arcpy.mapping.ListLayers(mxd, "*", df)

try:
    for lyr in lyrList:
        #Execute make feature layer for each feature class in map document
        flyrList = arcpy.MakeFeatureLayer_management(lyr, "z"+str(lyr))

except arcpy.ExecuteError:
    print arcpy.GetMessages()

try:
    for flyr in flyrList:
        #Execute select sites only
        arcpy.SelectLayerByAttribute_management(flyr, "NEW_SELECTION", ' "SUBTYPE" = 1 ')

except arcpy.ExecuteError:
    print arcpy.GetMessages()

#Set variable
addLayer = arcpy.mapping.ListLayers(mxd, "*z*", df)
arcpy.env.workspace = r'S:\GIS\gis_work\2014\FLAG\ArchGDB_Maint_Model\GIS\Script_Test'

try:
    for flyr in flyrList:
        #Execute create new layer files from site selections
        arcpy.SaveToLayerFile_management(flyr, flyr+"SITES")

except arcpy.ExecuteError:
    print arcpy.GetMessages()

try:
    for flyr in flyrList:
        #Execute frequency tool on new site only fcs based on ASMIS ID field
        freqtabList = arcpy.Frequency_analysis(flyr, str(flyr)+"_freq", "ASMIS_ID")

except arcpy.ExecuteError:
    print arcpy.GetMessages()

try:
    for flyr in flyrList:
        #Add feature layers to map document
        arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")

except arcpy.ExecuteError:
    print arcpy.GetMessages()

try:
    for freqtab in freqtabList:
        #Add frequency tables to map document
        arcpy.mapping.AddTableView(df, freq)

except arcpy.ExecuteError:
    print arcpy.GetMessages()

#TASK 5 - Save map document and remove lock

try:
    mxd.save()
    del mxd

except arcpy.ExecuteError:
    print arcpy.GetMessages()


And here's the error I get:

Executing: SaveToLayerFile zWUPA_Polygon S:\GIS\gis_work\2014\FLAG\ArchGDB_Maint_Model\GIS\Script_Test\zWUPA_PolygonSITES.lyr # CURRENT
Start Time: Tue Apr 01 11:52:53 2014
Failed to execute. Parameters are not valid.
ERROR 000732: Input Layer: Dataset zWUPA_Polygon does not exist or is not supported
Failed to execute (SaveToLayerFile).
Failed at Tue Apr 01 11:52:53 2014 (Elapsed Time: 0.01 seconds)
Executing: Frequency zWUPA_Polygon S:\GIS\gis_work\2014\FLAG\ArchGDB_Maint_Model\GIS\Script_Test\zWUPA_Polygon_freq ASMIS_ID #
Start Time: Tue Apr 01 11:52:54 2014
Failed to execute. Parameters are not valid.
ERROR 000732: Input Table: Dataset zWUPA_Polygon does not exist or is not supported
Failed to execute (Frequency).
Failed at Tue Apr 01 11:52:54 2014 (Elapsed Time: 0.10 seconds)

Traceback (most recent call last):
  File "S:\GIS\gis_work\2014\FLAG\ArchGDB_Maint_Model\Documents\ArchGDB_maintenance_tasks.py", line 173, in <module>
    arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")
  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\utils.py", line 181, in fn_
    return fn(*args, **kw)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\mapping.py", line 49, in AddLayer
    assert isinstance(add_layer, Layer)
AssertionError


Any help would be greatly appreciated.

Kerry
Tags (2)
0 Kudos
1 Reply
MathewCoyle
Frequent Contributor
You are attempting to add the addLayer variable which is a list of layers. And you are iterating over a single layer of your flyrList variable. I think you are getting your variable names confused.
0 Kudos