
Updating layer symbology
There may be times when you will want to change the symbology of a layer in a map document. This can be accomplished through the use of the UpdateLayer()
function, which can be used to change the symbology of a layer as well as various properties of a layer. In this recipe, you will use the UpdateLayer()
function to update the symbology of a layer.
Getting ready
The arcpy.mapping
module also gives you the capability of updating layer symbology from your scripts by using the UpdateLayer()
function. For example, you might want your script to update a layer's symbology from a graduated color to a graduated symbol, as illustrated in the following screenshot. UpdateLayer()
can also be used to update various layer properties, but the default functionality is to update the symbology. Since UpdateLayer()
is a robust function that is capable of altering both symbology and properties, you do need to understand the various parameters that can be supplied as an input:

How to do it…
Follow these steps to learn how to update the symbology of a layer using UpdateLayer()
:
- Open
c:\ArcpyBook\Ch2\Crime_Ch2.mxd
with ArcMap. - Click on the Python window button from the main ArcMap toolbar.
- Import the
arcpy.mapping
module:import arcpy.mapping as mapping
- Reference the currently active document (
Crime_Ch2.mxd
) and assign the reference to a variable:mxd = mapping.MapDocument("CURRENT")
- Get a reference to the
Crime
data frame:df = mapping.ListDataFrames(mxd, "Crime")[0]
- Define the layer that will be updated:
updateLayer = mapping.ListLayers(mxd,"Crime Density by School District",df)[0]
- Define the layer that will be used to update the symbology:
sourceLayer = mapping.Layer(r"C:\ArcpyBook\data\CrimeDensityGradSym.lyr")
- Call the
UpdateLayer()
function to update the symbology:mapping.UpdateLayer(df,updateLayer,sourceLayer,True)
- You can consult the solution file at
c:\ArcpyBook\code\Ch2\UpdateLayerSymbology.py
to verify the accuracy of your code. - Run the script. The Crime Density by School District layer will now be symbolized with graduated symbols instead of graduated colors, as shown in the following screenshot:
How it works…
In this recipe, we used the UpdateLayer()
function to update the symbology of a layer. We didn't update any properties, but we'll do so in the next recipe. The UpdateLayer()
function requires that you pass several parameters including a data frame, layer to be updated, and a reference layer from which the symbology will be pulled and applied to update the layer. In our code, the updateLayer
variable holds a reference to the Crime Density by School District layer, which will have its symbology updated. The source layer from which the symbology will be pulled and applied to the updated layer is a layer file (CrimeDensityGradSym.lyr
), containing graduated symbols.
To update the symbology for a layer, you must first ensure that the update layer and the source layer have the same geometry (point, line, or polygon). You also need to check that the attribute definitions are the same in some cases, depending upon the renderer. For example, graduated color symbology and graduated symbols are based on a particular attribute. In this case, both the layers had polygon geometry and a CrimeDens
field containing crime density information.
Once we had references to both the layers, we called the UpdateLayer()
function, passing in the data frame and layers along with a fourth parameter that indicated that we're updating symbology only. We supplied a True
value as this fourth parameter, indicating that we were only updating the symbology and not properties:
mapping.UpdateLayer(df,updateLayer,sourceLayer,True)
There's more...
The UpdateLayer()
function also provides the ability to remove one layer and add another layer in its place. The layers can be completely unrelated, so there is no need to ensure that the geometry type and attribute field are the same as you would when redefining the symbology of a layer. This switching of layers essentially executes a call to RemoveLayer()
and then a call to AddLayer()
as one operation. To take advantage of this functionality, you must set the symbology_only
parameter to False
.