Select to view content in your preferred language

confusion with ListDataFrames and Listlayers

2383
11
08-22-2012 08:41 AM
JonPedder
Deactivated User
I have a script that moves a group layer from one mxd to others, the layer is saved from the current mxd then written to a list of other mxd???s, the group layer is Base_Data.

My script is working well but I???d like to clean it up a bit by first removing any existing Base_Data layer groups in the target mxd before writing the layer source. As it is, if I run the script 3 time I get 3 copies of Base_Data in the target.

I???m trying to use the following but am seeing unexpected results.

Using the ListDataFrames function I assumed I???d get back a list of the DataFrames in the mxd, such as ???New Data Frame???
df = arcpy.mapping.ListDataFrames(mxd,"")[0]
instead when I iterate the list df I get a list of all the layers.
For i in df:
                Print i


So when I try to list the layers to select the Base_Data I just get a list of layers the first group layer 
basedata = arcpy.mapping.ListLayers(mxd)[0]
       for i in basedata:
                arcpy.AddMessage(i)



All I???m really trying to accomplish it to detete any layers in the target mxd that contain ???Base_Data???

Thanks
Tags (2)
0 Kudos
11 Replies
JonPedder
Deactivated User
Latest error, while running from within ArcMap

Running script Layers...
Target File is True
Base Data Saved as ./Base_Layer
1
<type 'exceptions.AssertionError'>: 
Failed to execute (Layers).





import glob
import arcpy
 
from arcpy import env
 
# Set overwrite option
arcpy.env.overwriteOutput = True
 
# Gather user input parameters
TargetDir = arcpy.GetParameterAsText(0)
TargetFile = arcpy.GetParameterAsText(1)

if TargetFile > "":
        arcpy.AddMessage("Target File is True")
        
        # Save base data layer file to disk
        LayerFile = "./Base_Layer"
        arcpy.SaveToLayerFile_management("14 Base_Data_Group",LayerFile,"RELATIVE")
        arcpy.AddMessage("Base Data Saved as "+ LayerFile)

        mxd = arcpy.mapping.MapDocument(TargetFile)
        df = arcpy.mapping.ListDataFrames(mxd,"14*")
        lyr_list = arcpy.mapping.ListLayers(mxd)
        arcpy.AddMessage("1")
        for lyr in lyr_list:
                if 'Base_Data' in lyr.name:
                        arcpy.mapping.RemoveLayer(df,lyr)
                        arcpy.AddMessage(lyr)

        path = LayerFile + '.lyr'
        arcpy.AddMessage("adding layer")
        addLayer = arcpy.mapping.Layer(path)
        arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")
        mxd.save()
        
elif TargetDir > "":
        arcpy.AddMessage("Target Directory is true")
        
        # Save base data layer file to disk
        LayerFile = "./Base_Layer"
        arcpy.SaveToLayerFile_management("14 Base_Data_Group",LayerFile,"RELATIVE")
        arcpy.AddMessage("Base Data Saved as "+ LayerFile)

        # Get list of files from directory
        path = TargetDir +"\*.mxd"
        mxds = glob.glob(path)
        arcpy.AddMessage(mxds)
        for row in mxds:
                note = '"'+row+'"'
                arcpy.AddMessage("Applying base data to mxd...")
                arcpy.AddMessage(row)
                
                mxd = arcpy.mapping.MapDocument(row)
                df = arcpy.mapping.ListDataFrames(mxd)[0]
                basedata = arcpy.mapping.ListLayers(mxd, "14 Base_Data", df)[0]
                for i in basedata:
                        arcpy.AddMessage(i)
                        
                path = LayerFile + '.lyr'
                              
                addLayer = arcpy.mapping.Layer(path)

                arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")
                mxd.save()

del mxd, addLayer

0 Kudos
JonPedder
Deactivated User
OK I've officially had enough for the day, at every turn I run into errors that I'm unable to resolve and it's driving me up the wall. Either I get an exceptions.Assertion error or error when trying to simply save.



Here's a code snippet below.

import glob
import arcpy
 
from arcpy import env
 
# Set overwrite option
arcpy.env.overwriteOutput = True
 
# Gather user input parameters
TargetDir = arcpy.GetParameterAsText(0)
TargetFile = arcpy.GetParameterAsText(1)

if TargetFile > "":
        arcpy.AddMessage("Target File is True")
        
        # Save base data layer file to disk
        LayerFile = "./Base_Layer"
        arcpy.SaveToLayerFile_management("14 Base_Data_Group",LayerFile,"RELATIVE")
        arcpy.AddMessage("Base Data Saved as "+ LayerFile)

        mxd = arcpy.mapping.MapDocument(TargetFile)
        df = arcpy.mapping.ListDataFrames(mxd)
        lyr_list = arcpy.mapping.ListLayers(mxd)
        arcpy.AddMessage("1")
        for lyr in lyr_list:
                if 'Base_Data' in lyr.name:
                        arcpy.mapping.RemoveLayer(df,lyr)
                        arcpy.AddMessage(lyr)

        path = LayerFile + '.lyr'
        arcpy.AddMessage("adding layer")
        addLayer = arcpy.mapping.Layer(path)
        arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")
        TargetFile.arcpy.save()
0 Kudos