1

I would like to take an empty group layer and add it an existing group layer within an MXD. The empty group layer does not currently exist , i am going to use an empty group layer i have saved within a folder.

i am using a list to build the layers i would like to create and then add.

The whole process consists of having order 1 groups , that contain sub groups.

i was able to do the first step , by creating the intial layers using the code below

Group_1 = ['Scotland','England','Wales','Spain']

for first_Group in Group_1:

        mxd = arcpy.mapping.MapDocument(r"C:\GIS\PROJECT\PYTHON\GROUP_LAYER_TEMPLATE_copy.mxd")
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        groupLayer = arcpy.mapping.Layer(r"C:\GIS\PROJECT\PYTHON\\An_empty_Group_Layer.lyr")
        arcpy.mapping.AddLayer(df, groupLayer, "BOTTOM")
        layers = arcpy.mapping.ListLayers(mxd)
        for layer in layers:
            if layer.isGroupLayer and layer.name == "An_empty_Group_Layer":
                layer.name = first_Group
        mxd.save()

Using the above Code , i was able to add the follwing group layers to the MXD

Scotland

England

Wales

Spain

I Like to use second list i have to create the second level of groups

 Group_2 = [['Scotland','Aberdeen'],['Scotland','Glasgow'],['Scotland','Inverness'],['England','london']['England','bristol']['England','NewCastle'],['Wales',Cardiff],['Wales','Newport']

,['Wales','Swansea'],['Spain','Mardid],['Spain','Valencia'],['Spain','Barcelonia'],

i can use a loop to go through my Second group

That would allow me to add groups , i would need to find a way using the group name to tie into adult group

# An example item in the list ['Scotland','Aberdeen']
# The first element can be used track by to the group above. In this example it would return 'Scotland'. I would like to take this value and then use it to reference the group above.

for item in group_2:
    # x equals the first element of the group 
    x = item[0]
#Can use the Second Value from the Item to be used to make the group name 
for item in group_2:
    # x Equals Aberdeen 
    x = item[1]

Another question related to group layers

1 Answer 1

1

The layers you add to your group layer has to be physically on disk or in an MXD.

To add a layer to a group layer you need to get a list of layer in your MXD. Once you have this list, you can loop through it and see if the layer is a group layer. If it is get a reference to your layer in your directory and add it to the group layer.

Sample code below assumes you already have a MXD created with your group layers and your layers are physically in a folder. I'm using a dictionary instead of a list of lists.

import arcpy
import os.path

# dict of layer to go under group layer
layers_dict = {'Scotland': ['Aberdeen', 'Glasgow', 'Inverness'],
                'England': ['London', 'Bristol', 'New Castle']}

# directory of where lyr files are located
lyr_file_dir = r'c:\gis\project\python'

mxd = arcpy.mapping.MapDocument(r'c:\gis\project\python\map.mxd')
df = arcpy.mapping.ListDataFrames(mxd)[0]
layers_list = arcpy.mapping.ListLayers(mxd)
for layer in layers_list:
    if layer.isGroupLayer and layers_dict.get(layer.name) is not None:
        for lyr in layers_dict[layer.name]:
            lyr_file = arcpy.mapping.Layer(os.path.join(lyr_file_dir, lyr) + '.lyr')
            lyr_file.name = lyr
            arcpy.mapping.AddLayerToGroup(df, layer, lyr_file, 'BOTTOM')
mxd.save()
2
  • Hi Ken , thanks for the answer i d actually re written , while you anwered. For the first stage i was able to use an empty layer fle that was say located here C:\GIS\PROJECT\PYTHON\\An_empty_Group_Layer.lyr. I was then able to reference this layer and take it onto the map. Is it not possible to utlise a similar process. I would like to build the groups first . Using the list. Without referencing an Physical Actual layer . Jul 30, 2015 at 18:29
  • I think you need a reference an empty layer on disk to add to your table of contents. No way to get around it.
    – Ken
    Jul 30, 2015 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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