1

I have a script that allows me apply a single SQL Definition Query to all feature classes within a particular Group Layer. For ease of input for others in the office, I changed this into a model tool, and so rather than typing these values into the python script itself, a dialog pops up with two text boxes/drop down menus.

One of the input boxes is just a SQL Expression data type, and the other uses the "Group Layer" data type, so it populates a drop down list of all group layers within the map. Using this method, the drop down does populate with all the map's group layers and I am able to select a group layer. However, the script doesn't work (though it doesn't throw any errors). If I instead type the Group Layer name directly into the script with quotes around it, it works properly. What am I doing wrong? I'm sure it's something simple, but I'm new to scripting. Script code as follows:

 groupLayer = arcpy.GetParameterAsText(0)
 sql = arcpy.GetParameterAsText(1)
 mxd = arcpy.mapping.MapDocument ("CURRENT")

 for lyr in arcpy.mapping.ListLayers(mxd):
     if not lyr.isGroupLayer:
         continue
     if not lyr.name == groupLayer:
         continue
     for subLyr in lyr:
         subLyr.definitionQuery = sql
  • Play with type of parameter – FelixIP Oct 31 '15 at 0:28
  • Alternatively use groupLayer.name in Lislayers as wildcard. It will also make 1st if redundant – FelixIP Oct 31 '15 at 0:50
  • Add an arcpy.AddMessage (groupLayer) before the start of the for loop. In ArcMap this will print the variable's value in the progress window. Does the variable contain the name of the group layer? – Emil Brundage Oct 31 '15 at 2:54
  • @Emil Brundage I tested with the arcpy.AddMessage, and it looks like my problem may have something to do with the fact that we have nested Group Layers. It my test, it printed "Noxious Weeds\Points" because the Group Layer I tested is the "Points" group layer, which is within the "Noxious Weeds" group layer. In some cases we have group layers nested even a couple of layers deeper. Any suggestions on how to work around this? – Tim Miller Nov 2 '15 at 17:08
1

Since it appears to be a subgroup layer problem, if not lyr.name == groupLayer: should be changed to if not lyr.longName == groupLayer:.

groupLayer = arcpy.GetParameterAsText(0)
sql = arcpy.GetParameterAsText(1)
mxd = arcpy.mapping.MapDocument ("CURRENT")

for lyr in arcpy.mapping.ListLayers(mxd):
    if not lyr.isGroupLayer:
        continue
    if not lyr.longName == groupLayer:
        continue
    for subLyr in lyr:
        subLyr.definitionQuery = sql
  • It works perfectly now. Thank you very very much! – Tim Miller Nov 2 '15 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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