Select to view content in your preferred language

UpdateLayer not copying the labels

2763
4
Jump to solution
09-09-2013 10:53 AM
ionarawilson1
Deactivated User
Using UpdateLayer or ApplySymbologyFromLayer_management copies the symbology but not the labels.  When I tried to use UpdateLayer with a third argument of False I get an insertion error, so I am not sure why doesn't work. Do you guys have any idea why? Thanks

 outputlyr = arcpy.mapping.Layer("D:/ArcGISData/SARS/Temp/ClpSoilsSort.shp")         arcpy.mapping.AddLayer(df2, outputlyr, "AUTO_ARRANGE")         arcpy.mapping.MapDocument('CURRENT').activeView = "Overview"               symbologylayer = arcpy.mapping.Layer("D:/ArcGISData/SARS/Temp/SoilsUpdateLayer2.lyr")         arcpy.ApplySymbologyFromLayer_management (outputlyr, symbologylayer)         #arcpy.mapping.UpdateLayer(df, outputlyr, symbologylayer)          arcpy.mapping.AddLayer(df, outputlyr, "AUTO_ARRANGE")         arcpy.mapping.AddLayer(df2, outputlyr, "AUTO_ARRANGE")         arcpy.RefreshActiveView()         
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JeffBarrette
Esri Regular Contributor
UpdateLayer with symbology_only=False needs to be used with caution.  Make sure you read the help topic. 

The most basic requirement is to only update layer symbology (and that has limits too).  If you want to update any other property that is available via layer properties, and is not a specific arcpy.mapping layer property (e.g., update selection symbology or field aliases, label properties, etc) you need to use UpdateLayer but realize that all layer properties will be changed.

Scenario - I have a layer in dozens of MXDs.  I want to change the label properties for the layer but I don't want to have to physically open up dozens of MXDs.

In this scenario, open one of the MXDs, modify the layer appropriately, save it to a LYR file and then iterate through all MXDs, find the layer of interest and update it using the layer file properties.

If you are trying to update layers with layer files that are referencing very different data, things won't work as expected.

I hope this helps,
Jeff

View solution in original post

0 Kudos
4 Replies
JeffBarrette
Esri Regular Contributor
The default value for UpdateLayer's {symbology_only} parameter is True.  Set this to False for ALL properties to be updated.  Note - it updates all properties, even data source.

Try:

arcpy.mapping.UpdateLayer(df, outputlyr, symbologylayer, False)


Jeff
0 Kudos
ionarawilson1
Deactivated User
Thank you Jeff. If I use False directly I get an insertion error. I had to create a variable for either True or False, but still nothing shows ups for me.  So if even source name changes, that is probably the reason it is not working for me.

What I did was to copy the symbology (at least I get the symbology), and then write code to add label and color in python:

        symbologyonly = "False"
        outputlyr = arcpy.mapping.Layer("D:/ArcGISData/SARS/Temp/ClpSoilsSort.shp")
        arcpy.mapping.AddLayer(df2, outputlyr, "AUTO_ARRANGE")
        arcpy.mapping.MapDocument('CURRENT').activeView = "Overview"
     
        symbologylayer = arcpy.mapping.Layer("D:/ArcGISData/SARS/Temp/SoilsUpdateLayer2.lyr")
       
        arcpy.mapping.UpdateLayer(df, outputlyr, symbologylayer, symbologyonly)

        arcpy.mapping.AddLayer(df, outputlyr, "AUTO_ARRANGE")
        arcpy.mapping.AddLayer(df2, outputlyr, "AUTO_ARRANGE")
        layer = arcpy.mapping.ListLayers(mxd, "")[0] #Indexing list for 1st layer
        if layer.supports("LABELCLASSES"):
            for lblclass in layer.labelClasses:
          
                #blclass.expression = "[MUSYM]"
                lblclass.expression = '"%s" & [MUSYM] & "%s"' % ("<CLR red='255' green='255' blue='255'>", "</CLR>")

0 Kudos
JeffBarrette
Esri Regular Contributor
UpdateLayer with symbology_only=False needs to be used with caution.  Make sure you read the help topic. 

The most basic requirement is to only update layer symbology (and that has limits too).  If you want to update any other property that is available via layer properties, and is not a specific arcpy.mapping layer property (e.g., update selection symbology or field aliases, label properties, etc) you need to use UpdateLayer but realize that all layer properties will be changed.

Scenario - I have a layer in dozens of MXDs.  I want to change the label properties for the layer but I don't want to have to physically open up dozens of MXDs.

In this scenario, open one of the MXDs, modify the layer appropriately, save it to a LYR file and then iterate through all MXDs, find the layer of interest and update it using the layer file properties.

If you are trying to update layers with layer files that are referencing very different data, things won't work as expected.

I hope this helps,
Jeff
0 Kudos
ionarawilson1
Deactivated User
I see, thank you for the explanation Jeff! Cheers
0 Kudos