1

Is there a way of copying one group of layers with data (001_Group) from one MXD(group1.mxd) to another(group2.MXD) with arcpy. As far as I understand all the layers in ArcMap are referenced as standalone layers in arcpy, so arcpy wouldn't know if for example 010_Subgroup is beneath 001_Group.

I have hundreds of large groups which have the same structure as 001_Group but layer_with_data layers have different data sources. I already have a script to update all the layers within 001_Group (in group1.mxd) with 002_Group datasources and save as group2.MXD.

However, I want to keep all the groups within the same MXD, so 001_Group, 002_Group and so on are listed from top to bottom. There was one question posted how to move all layers with looping through with arcpy.mapping.ListLayers, but this does mess things up and layer structure is inverted or not in correct order, as you can see from the example TOC below that some layers need to be left empty or lots of subgroups.

Here is the code which produces Group_002.mxd, Group_003.mxd and Group_004.mxd separately, which I'm trying to make into a single MXD with Groups being organised from top to bottom:

import arcpy
########################################################################################################
mxd = arcpy.mapping.MapDocument(r"C:\temp\Proj\Group_001.mxd")
subGroupPath_010 = r"C:\temp\Proj\subGr_010"
subGroupPath_020 = r"C:\temp\Proj\subGr_020"
subSubGroupPath_021 = r"C:\temp\Proj\subGr_020\ssubGr_021"
subSubGroupPath_022 = r"C:\temp\Proj\subGr_020\ssubGr_022"

grp_noArr = range(2,5)
grp_nameArr = ["Group_002", "Group_003", "Group_004"]
########################################################################################################
for grp_no, grp_name in enumerate(grp_nameArr, grp_noArr[0]):
    fileName111 = str(grp_no) + "_" + grp_name
    srcOutName50d = str(grp_no) + "_" + grp_name

    for lyr in arcpy.mapping.ListLayers(mxd):
        if lyr.name == "001_Group":
            lyr.name = grp_name
        if lyr.supports("DATASOURCE"):
            for lay_i in range(1,5):
                if lyr.name == "10" + str(lay_i) + "_layer_with_data":
                    lyr.replaceDataSource(subGroupPath_010, "RASTER_WORKSPACE", fileName111 + "_subgrp010_" + str(lay_i))

                elif lyr.name == "21" + str(lay_i) + "_layer_with_data":
                    lyr.replaceDataSource(subGroupPath_020, "RASTER_WORKSPACE", fileName111 + "_subgrp020_" + str(lay_i))
                elif lyr.name == "22" + str(lay_i) + "_layer_with_data":
                    lyr.replaceDataSource(subGroupPath_020, "RASTER_WORKSPACE", fileName111 + "_subgrp020_" + str(lay_i))
                elif lyr.name == "23" + str(lay_i) + "_layer_with_data":
                    lyr.replaceDataSource(subGroupPath_020, "RASTER_WORKSPACE", fileName111 + "_subgrp020_" + str(lay_i))

    mxd.saveACopy(r"C:\\temp\Proj\\" + grp_name + ".mxd")

enter image description here

2
  • What does your code for this bit so far look like?
    – PolyGeo
    Commented Sep 25, 2017 at 11:37
  • Any suggestions anyone?
    – Curtis
    Commented Sep 25, 2017 at 16:30

1 Answer 1

2

There are many ways you can read the structure of the layers in the TOC. Work with the Layer object.

First, you can check to see if the layer supports the isGroupLayer property. Second, you can evaluate the longNameproperty. A layer's longName value will include the group name in addition to the layer name. For example, a layer named Layer1 in a group layer named Group1 will have a longNamevalue of Group1\Layer1. If the name value is equal to longName value, then the layer is not a group layer or the layer is not inside a group layer.

Using this properties, you will be able to work your way through all the layers.

1
  • Thanks for your answer, great to see an option like longName, unfortunately it's Read-only according to documentation. This means I can query to see whether a layer1 is within Group1 with longName. However, what if I want to see layer1in `Group5\SubGroup2` etc. How would I do that? I'm trying to understand how I can organize the hierarchy of TOC items in a way I want (or client wants). I will try to post my code in a bit, as it was an example only. My script is actually too long to post.
    – Curtis
    Commented Sep 25, 2017 at 12:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.