arcpy.mapping.ListDataFrames

4294
4
Jump to solution
08-23-2012 08:10 AM
JonPedder
Occasional Contributor II
Morning all, this is a followup post to the thread I have going here

http://forums.arcgis.com/threads/65282-confusion-with-ListDataFrames-and-Listlayers

I'm still having a tough time understanding the ListDataFrames functionality and results. In my mind what I think ListDataFrames would give me is exactly that, a listing of data frames with the mxd. However when I execute this command I get back a listing of layers. All the examples I see in the help use [0] such as

df = arcpy.mapping.ListDataFrames(mxd, "Transportation")[0]

When I execute a line such as the above I get back a list of layers within my first layergroup.

This is probably just me not understanding python or coding very well, it's driving me a little nuts.

The objective of my script below is to save a layer of base_data from the current mxd to disk. Then look at the target mxd and see if it has a layer names Base_Data, if so remove it, then import the previously saved layer and add to the bottom of the target mxd. Then of course save.


import arcpy   from arcpy import env   # Set overwrite option arcpy.env.overwriteOutput = True   # Gather user input parameters  TargetFile = arcpy.GetParameterAsText(0)  if TargetFile > "":         arcpy.AddMessage("Target File is True")                  # from current open mxd, save base data layer file to disk         LayerFile = "./Base_Layer"         arcpy.SaveToLayerFile_management("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)                 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()
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MikeMacRae
Occasional Contributor III
I've had the same issue before with the index on the dataframe. it confuses me. usually I just set the mxd, loop the dataframes and then loop the layers:

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


Something like that might work

View solution in original post

4 Replies
MikeMacRae
Occasional Contributor III
I've had the same issue before with the index on the dataframe. it confuses me. usually I just set the mxd, loop the dataframes and then loop the layers:

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


Something like that might work
JonPedder
Occasional Contributor II
Thanks Mike, this loop appears to work. Though I can't really tell as I can't figure out the simplest command "save"

save appears to have no paramaters
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00s30000000n000000

I'm not looking to saveacopy as i want to overwrite the original file. I have set arcpy.env.overwriteOutput = true

what would the syntax be for save?

save()
arcpy.save()
mxd.arcpy.save()
save(mcd)

I've tried them all to no avail. Silly to be hung up on something so simple

Thanks
0 Kudos
JeffMoulds
Esri Contributor
The syntax to save a mxd is MapDocument.save(). For example:

mxd = arcpy.mapping.MapDocument(TargetFile)
...
mxd.save()


In regards to your questions with ListDataFrames and ListLayers, try this:

# loop through all layers in a specific data frame:
mxd = arcpy.mapping.MapDocument(TargetFile)
df = arcpy.mapping.ListDataFrames(mxd, "MyDataFrameName")[0]
for lyr in arcpy.mapping.ListLayers(mxd, data_frame=df):
  print lyr.name


These topics might help as well.

http://resources.arcgis.com/en/help/main/10.1/#/ListDataFrames/00s30000001p000000/
http://resources.arcgis.com/en/help/main/10.1/#/ListLayers/00s30000002n000000/
http://resources.arcgis.com/en/help/main/10.1/#/MapDocument/00s30000000n000000/
http://resources.arcgis.com/en/help/main/10.1/#/Guidelines_for_arcpy_mapping/00s30000006s000000/
0 Kudos
MikeMacRae
Occasional Contributor III
Try this:

mxd.save()
0 Kudos