Hello everyone,
I am not that experienced with python, but I'm trying to copy and paste and filter(by floor) each layer in a map in Pro into 4 "Floor" group layers (Basement,Floor1-3). I am able to get correctly named and filtered layers based off floor when I use the addLayer method. However, when I use the addLayerToGroup method, I get each layer off by 1 in the name and filter and group, with the last layer not in a group at all.
Both methods are in the snippet here. Could it be a limitation to addLayerToGroup?
Any help would be much appreciated, thank you!
aprx = arcpy.mp.ArcGISProject("Current")
m = aprx.listMaps("Group Layers")[0];
# Create list of layer names for copy layers based off floor
lyr_list = m.listLayers()
# Groups to add too
groups = ("Basement", "Floor 1","Floor 2", "Floor 3")
for copy_lyr in lyr_list:
# If layer is a group layer, continue
if copy_lyr.isGroupLayer:
continue
# If layer does not have floor field, continue
field_list = arcpy.ListFields(copy_lyr,"FLOOR*")
if len(field_list) == 0:
continue
# create list with each floor plus layer name, in order to iterate through and create a corresponding layer filtered based off floor
lyr_name = copy_lyr.name
new_layers = (lyr_name + " 0", lyr_name + " 1", lyr_name + " 2" , lyr_name + " 3")
i = 0
for new_name in new_layers:
# Designate layer to floor
group_name = str(groups[i])
targetGroupLayer = m.listLayers(group_name)[0]
print(targetGroupLayer)
# m.addLayer(copy_lyr, "BOTTOM")
m.addLayerToGroup(targetGroupLayer,copy_lyr, "BOTTOM")
copy_lyr.name = new_name
print(copy_lyr.name)
# add new definition query to conincide with floor and remove old one
def_q_list = copy_lyr.listDefinitionQueries()
sql_def_q = "FLOOR = " + "\'" + str(i) + "\'"
def_q_list.append({'name': 'Floor Query', 'sql': sql_def_q, 'isActive': True})
def_q_list.pop(0)
copy_lyr.updateDefinitionQueries(def_q_list)
print(sql_def_q)
# add to counter (new value will be used for next floor def query)
print(str(i))
i += 1