In the previous recipe, you learned how to update the symbology of a layer. As I mentioned, UpdateLayer()
can also be used to update various properties of a layer, such as field aliases, query definitions, and others. In this recipe, you will use UpdateLayer()
to alter various properties of a layer.
You can also use the UpdateLayer()
function to update a limited number of layer properties. Specific layer properties, such as field aliases, selection symbology, query definitions, label fields, and others, can be updated using UpdateLayer()
. A common scenario is to have a layer in many map documents that needs to have a specific property changed across all the instances of the layer in all map documents. To accomplish this, you will have to use ArcMap to modify the layer with the appropriate properties and save it to a layer file. This layer file then becomes the source layer, which will be used to update the properties of another layer called update_layer
. In this recipe, you'll use ArcMap to alter the properties of a layer, save to a layer file (.lyr
) and then use Python to write a script that uses UpdateLayer()
to apply the properties to another layer.
Follow these steps to learn how to update layer properties with UpdateLayer()
:
c:ArcpyBookCh2Crime_Ch2.mxd
with ArcMap. For this recipe, you will be working with the Burglaries in 2009 feature class, as shown in the following screenshot:OFFDESC
field to Offense Description
, as shown in the next screenshot.c:ArcpyBookdataBurglariesNoForcedEntry.lyr
.Crimes2009
feature class from the CityOfSanAntonio
geodatabase. The feature class will be added to the data frame, as shown in the following screenshot:arcpy.mapping
module:import arcpy.mapping as mapping
Crime_Ch2.mxd
) and assign the reference to a variable:mxd = mapping.MapDocument("CURRENT")
Crime
data frame:df = mapping.ListDataFrames(mxd, "Crime")[0]
updateLayer = mapping.ListLayers(mxd,"Crimes2009",df)[0]
sourceLayer = mapping.Layer(r"C:ArcpyBookdataBurglariesNoForcedEntry.lyr")
UpdateLayer()
function to update the symbology:mapping.UpdateLayer(df,updateLayer,sourceLayer,False)
c:ArcpyBookcodeCh2UpdateLayerProperties.py
to verify the accuracy of your code.BurglariesNoForcedEntry.lyr
file. This is illustrated in the following screenshot. Turn on the layer to view the definition query that has been applied. You can also open the Layer Properties dialog to view the property changes that have been applied to the Crimes2009 feature class: