Add Layer Assertion Error

5358
2
Jump to solution
04-26-2013 10:42 AM
ZackBartlett
New Contributor III
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!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MikeHunter
Occasional Contributor
The first argument for arcpy.mapping.AddLayer has to be a DataFrame instance, but you are passing it a string, the name of the dataframe.  You've already hooked to the DataFrame instance in your code, so just change this line:

arcpy.mapping.AddLayer(df_Target, new_shplyr, "TOP")


to this:
arcpy.mapping.AddLayer(dataFrame, new_shplyr, "TOP")


I think that will fix you up.

good luck
Mike

View solution in original post

2 Replies
MikeHunter
Occasional Contributor
The first argument for arcpy.mapping.AddLayer has to be a DataFrame instance, but you are passing it a string, the name of the dataframe.  You've already hooked to the DataFrame instance in your code, so just change this line:

arcpy.mapping.AddLayer(df_Target, new_shplyr, "TOP")


to this:
arcpy.mapping.AddLayer(dataFrame, new_shplyr, "TOP")


I think that will fix you up.

good luck
Mike
ZackBartlett
New Contributor III
Perfect! It worked. Thanks a lot!

I have another question, however. Whenever I start this script, I use GetParameterAsText(0) to specify a Workspace, but also the feature dataset where files are to be stored.

However, later in the script, when I re-add these files, the script automatically opens files from my Deafult Geodatabase (the one on my C drive that I never use). This isn't a problem when I go into the Environment Settings and specify my Current and Scratch Workspaces.

Is there anyway to set the feature dataset specified in GetParameterAsText(0) as my Current and Scratch Workspace? Not a huge deal, but it would save a step.

Thanks!
0 Kudos