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")