Pulling Feature Layer from Group Layer and apply Symbology?

722
3
05-15-2014 09:12 AM
MikeMacRae
Occasional Contributor III
I'm trying to use the symbology from a layer file, to apply to a layer in my map via python. My layer file is a group layer file. The layer in the map is not a grouped layer file. When I do this:

for lyr in arcpy.mapping.ListLayers(mxd,"",df):
     Layer = arcpy.mapping.Layer(path_to_LayerFile)
     arcpy.ApplySymbologyFromLayer_management (lyr, Layer)


It will crash beause the 'Layer' is a group layer, where lyr is not.

I flipped through the arcpy.mapping.Layer helpfile here and I can see that I can test for isGroupLayer or isFeatureLayer, but I'm not sure how I would go about grabbing the underlying feature layer from the group layer and apply the symbology to lyr in my example about? More specifically, I don't suppose there is a directory path for feature layers inside of a group layer (i.e. G:\layers\Test_Grid_Group.lyr\Unit) <---That doesn't work, but I'm looking for something like that....
Tags (2)
0 Kudos
3 Replies
RichardFairhurst
MVP Honored Contributor
Since you can only apply symbology to a feature layer you want to test for isFeatureLayer:

for lyr in arcpy.mapping.ListLayers(mxd,"",df):
     Layer = arcpy.mapping.Layer(path_to_LayerFile)
     if Layer.isFeatureLayer:
          arcpy.ApplySymbologyFromLayer_management (lyr, Layer)
0 Kudos
MikeMacRae
Occasional Contributor III
Thanks Richard. That helps for sure. I updated my post while you were responding with an additional question


More specifically, I don't suppose there is a directory path for feature layers inside of a group layer (i.e. G:\layers\Test_Grid_Group.lyr\Unit) <---That doesn't work, but I'm looking for something like that....


Reason being is, I am setting up a spreadsheet that include a column for dataSources and one for its associated layer file. Each layer file is set up quite dynamically (some make use of group layers, otehrs are standalone feature layers) and there are a couple hundred of them. Instead of scripting to iterate over each individual layer file to test and find unique feature layers, It would make more sense to have a full path directory in my spreadhseet (configuration file) and read from there. I don't think there is a file path structure to a feature layer within a group layer, but I thought I'd ask.
0 Kudos
RichardFairhurst
MVP Honored Contributor
Thanks Richard. That helps for sure. I updated my post while you were responding with an additional question



Reason being is, I am setting up a spreadsheet that include a column for dataSources and one for its associated layer file. Each layer files are set up quite dynamically (some make use of group layers, otehrs are standalone feature layers) and there are a couple hundred of them. Instead of scripting to iterate over each individual layer file to test, I would rather just find the path, stick it into my spreadsheet and read the path from there. I don't think there is a file path structure to a feature layer within a group layer, but I thought I'd ask.


You don't query the GroupLayer for the Feature layer paths inside of it.  You only query feature layers for the path and determine the feature layer groups by doing the opposite and querying the longName of each feature layer to find out if it is in a group layer.  The helps says:

"There are two ways of determining if a layer is a group layer. First, you can check to see if the layer supports the isGroupLayer property. Second, you can evaluate the longName property. A layer's longName value will include the group name in addition to the layer name. For example, a layer named Layer1 in a group layer named Group1 will have a longName value of Group1\Layer1. If the name value is equal to longName value, then the layer is not a group layer or the layer is not inside a group layer."

So something like this should work:

for lyr in arcpy.mapping.ListLayers(mxd,"",df):
     Layer = arcpy.mapping.Layer(path_to_LayerFile)
     if Layer.isFeatureLayer:
          if Layer.longName <> Layer.name:
               groupLayerNames = layer.longName.split(r"\")
               for i = 0 to len(grougLayerNames) - 1:
                    print groupLayerNames
          arcpy.ApplySymbologyFromLayer_management (lyr, Layer)


I am not sure if multiple group layer levels are handled in the longName property or if only the immediate parent Group layer is listed.  If all group layers containing a feature layer are included in the longName, then the for loop can get all of the group containers in order.  If only the immediate parent group layer is listed at each level then some form or recursion method would have to work its way out to the outermost group and keep track of each inner group layer level.
0 Kudos