Hi. I am trying to add 30+ layers from same feature class to the Table of Contents (TOC) into groups, each with it's own definition query. My attribute table has this basic design (note that I simplified it and reduced size):
site | spp1 | spp2 | spp3 | spp4 | spp5 |
site1 | 3 | 3 | 0 | 1 | 5 |
site2 | 0 | 3 | 12 | 0 | 6 |
site3 | 4 | 0 | 5 | 0 | 8 |
site4 | 6 | 0 | 0 | 8 | 0 |
site5 | 0 | 0 | 4 | 12 | 3 |
Each Site includes an attribute for each species (spp1,spp2,etc.) with the value = count - i.e. at Site 1 there were 3 spp2.
I ultimately want one layer group per site in my TOC. Each layer group receives a layer per species. In this example, each site will get 5 layers - one for each spp.
I am able to create the Groups
# 1) CREATE GROUPS
p = arcpy.mp.ArcGISProject('current')
m = p.listMaps('site_map')[0]
# Add each group to Table of Contents
# Each group will get multiple layers
sites = ['site1','site2','site3','site4']
group_layers = []
for s in sites:
group_layers.append(m.createGroupLayer(s))
I am able to then add layers to each group BUT I cannot add a defQuery per layer. I don't know how to access the defQuery Property from the groupLayer attribute in this loop via (line 9).
lyrx = r'path/to/data.lyrx'
lyr_file = arcpy.mp.LayerFile(lyrx)
sites = ['site1','site2','site3','site4','site5']
target_cols = ['spp1','spp2','spp3','spp4','spp5']
for s in sites:
gl = [gl for gl in group_layers if gl.name == s]
gl = gl[0]
for tc in target_cols:
lyr = m.addLayerToGroup(gl, lyr_file)
def_q = r'site=="{}"'.format(s)
lyr.definitionQuery(def_q)
Is there a way to set a definitionQuery via the defintionQuery property on each layer, preferably within this for loop? Note that I receive this error for line 11:
AttributeError: The attribute 'definitionQuery' is not supported on this instance of Layer.
Note that this obviously works
m.listLayers()[0].definitionQuery='def query whatever'
but I would love to do it within the for loop via the groupLayer property.