AnsweredAssumed Answered

Add Layer Assertion Error

Question asked by zackb11 on Apr 26, 2013
Latest reply on Apr 29, 2013 by zackb11
I have a script that:
1. Reads the layers in my Table of Contents
2. exports them to a feature dataset
3. re-adds the exported features
4. applies a pre-determined symbology to them based on layer files.

I've managed to complete steps 1 and 2, however, I am getting hung up with an Assertion Error at step 3.

My code is below. The error is arising in Section 3: RE-ADD NEWLY EXPORTED BASE FEATURES.

# SET UP

import arcpy, glob, os

base_features_Output = arcpy.GetParameter(0)

arcpy.env.Workspace = base_features_Output
arcpy.env.OverwriteOutput = True

arcpy.AddMessage("Workspace: " + str(arcpy.env.Workspace))

df_Target = arcpy.GetParameterAsText(1)

mxd = arcpy.mapping.MapDocument("CURRENT")
dataFrame = arcpy.mapping.ListDataFrames(mxd, df_Target)[0]
layerfile_list = r"N:\BASE_DATA\CANVEC\1_to_50k\lyrfiles"

# 1. LIST FEATURE CLASSES IN TABLE OF CONTENTS

TOC_shp_list = arcpy.mapping.ListLayers(mxd, "", dataFrame)

# 2. EXPORT TOC FEATURES TO GEODATABASE

arcpy.AddMessage("Exporting base features to geodatabase...")

for shp in TOC_shp_list:
    shp_ext = shp.name[7:]
    arcpy.AddMessage(str(shp_ext))
    arcpy.FeatureClassToFeatureClass_conversion(shp,base_features_Output,str(shp_ext))
    arcpy.AddMessage(str(shp) + " ---- EXPORTED")
    del shp_ext
arcpy.AddMessage("All base features exported to geodatabase.")

del shp
del TOC_shp_list

# 3. RE-ADD NEWLY EXPORTED BASE FEATURES

arcpy.AddMessage("Re-adding newly exported layers...")

FC_shp_list = arcpy.ListFeatureClasses()

for FC_shp in FC_shp_list:
    arcpy.AddMessage(FC_shp)
    new_shplyr = arcpy.mapping.Layer(str(base_features_Output) + "\\" + str(FC_shp))
    arcpy.AddMessage(str(new_shplyr))
    arcpy.mapping.AddLayer(df_Target, new_shplyr, "TOP")
   
arcpy.RefreshActiveView()
arcpy.RefreshTOC()

# 4. APPLY SYMBOLOGY

TOC_shp_list = arcpy.mapping.ListLayers(mxd, "", dataFrame)

for shp in TOC_shp_list:
    for lyr in layerfile_list:
        if lyr == shp:
            arcpy.ApplySymbologyFromLayer_management(shp, lyr)
        del lyr
    del shp

arcpy.RefreshTOC()
arcpy.RefreshActiveView()


ArcMap is giving me the following error:

Executing: ExpSymCanvec N:\BASE_DATA\CANVEC\Test.gdb\Base Layers
Start Time: Fri Apr 26 14:38:09 2013
Running script ExpSymCanvec...
Workspace: N:\BASE_DATA\CANVEC\Test.gdb\Base
Exporting base features to geodatabase...
TR_1760009_1
031g05_TR_1760009_1 ---- EXPORTED
All base features exported to geodatabase.
Re-adding newly exported layers...
TR_1760009_1
TR_1760009_1

Traceback (most recent call last):
File "N:\DOCUMENTATION\PYTHON\Canvec_Symbolization\canvec_autosym_basedatatool.py", line 59, in <module>
arcpy.mapping.AddLayer(df_Target, new_shplyr, "TOP")
File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\utils.py", line 181, in fn_
return fn(*args, **kw)
File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\mapping.py", line 48, in AddLayer
assert isinstance(data_frame, DataFrame)
AssertionError

Failed to execute (ExpSymCanvec).
Failed at Fri Apr 26 14:38:25 2013 (Elapsed Time: 16.00 seconds)

Does anyone have any idea what's going on here? Any help would be appreciated.

Thanks!

Outcomes