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
basedata = arcpy.mapping.ListLayers(mxd)[0] for i in basedata: arcpy.AddMessage(i)
df = arcpy.mapping.ListDataFrames(mxd)[0]
df = arcpy.mapping.ListDataFrames(mxd)
map = arcpy.mapping.MapDocument('some map name') lyr_list = arcpy.mapping.ListLayers(map) #leaving out the dataframe argument returns a list of all layers in all dataframes for lyr in lyr_list: if lyr.isGroupLayer: if 'Base_Data' in lyr.name: continue else: ... your code to copy to new mxd...
If your base data are always in a group layer that has Base_Data in the name somehow then you can query a layer to determine if its a group layer, if it is, then you can inspect its name to see if it has the string 'Base_Data' in it. Unless you need to process this dataframe by data frame I think you can skip the work with ListDataFrames.
something like this might work (concept only! hasn't been tested):map = arcpy.mapping.MapDocument('some map name') lyr_list = arcpy.mapping.ListLayers(map) #leaving out the dataframe argument returns a list of all layers in all dataframes for lyr in lyr_list: if lyr.isGroupLayer: if 'Base_Data' in lyr.name: continue else: ... your code to copy to new mxd...
you can also examine other attributes of a Layer object such as the name and longname. The name is the name of a layer as it appears in arcmap, the long name will have a reference to a group layer (like base_data\roads where roads is the layer and base_data is the group). You can always check to see if the name and long name are equal, if so, move them, if not, then check to see if the long name contains Base_Data or other string you are interested in filtering with.
mxd = arcpy.mapping.MapDocument("C:\tester.mxd") lyr = arcpy.mapping.ListLayers(mxd,"14*")[0] for i in lyr: arcpy.Delete_management(lyr) mxd.save()
>>> map = arcpy.mapping.MapDocument('C:\test.mxd') >>> lyr_list = arcpy.mapping.ListLayers(map) >>> for lyr in lyr_list: print lyr # result of the above displays a list of layers correctly # next to delete the layers in question >>> for lyr in lyr_list: if 'Base_Data' in lyr.name: arcpy.Delete_management(lyr) <Result 'true'> <Result 'true'> <Result 'true'> <Result 'true'>
>>> mxd = arcpy.mapping.MapDocument('C:\test.mxd') >>> df = arcpy.mapping.ListDataFrames(mxd,"")[0] >>> lyr_list = arcpy.mapping.ListLayers(mxd) >>> for lyr in lyr_list: if 'Base_Data' in lyr.name: arcpy.mapping.RemoveLayer(df,lyr) mxd.save()
Traceback (most recent call last): File "<pyshell#60>", line 4, in <module> mxd.save() File "C:\Program Files\ArcGIS\Desktop10.0\ArcPy\arcpy\utils.py", line 181, in fn_ return fn(*args, **kw) File "C:\Program Files\ArcGIS\Desktop10.0\ArcPy\arcpy\_mapping.py", line 643, in save return convertArcObjectToPythonObject(self._arc_object.save(*gp_fixargs((), True))) IOError: MapDocObject: Unable to save. Check to make sure you have write access to the specified file and that there is enough space on the storage device to hold your document.
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) arcpy.AddMessage(TargetDir) arcpy.AddMessage(TargetFile) 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) arcpy.AddMessage(mxd) df = arcpy.mapping.ListDataFrames(mxd,"") lyr_list = arcpy.mapping.ListLayers(mxd) for lyr in lyr_list: if 'Base_Data' in lyr.name: arcpy.mapping.RemoveLayer(df,lyr) path = LayerFile + '.lyr' arcpy.AddMessage("1" +path) addLayer = arcpy.mapping.Layer(path) arcpy.AddMessage("2") 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