
Updating layer properties
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.
Getting ready
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.
How to do it…
Follow these steps to learn how to update layer properties with UpdateLayer()
:
- Open
c:\ArcpyBook\Ch2\Crime_Ch2.mxd
with ArcMap. For this recipe, you will be working with the Burglaries in 2009 feature class, as shown in the following screenshot: - Double-click on the Burglaries in 2009 feature class in the Crime data frame to display the Layer Properties window, as shown in the following screenshot. Each of the tabs represents properties that can be set for this layer:
- Click on the General tab and change the value in the Layer Name: textbox to the name, as shown in the following screenshot:
- Click on the Definition Query tab and define the query, as shown in the following screenshot. You can use the Query Builder… button to define the query or simply type in the query:
- Change the alias of the
OFFDESC
field toOffense Description
, as shown in the next screenshot. - Click on the Fields tab in Layer Properties and make visible only those fields that are selected with a checkmark in the following screenshot. This is done by unchecking the fields that you see in the following screenshot:
- Click on OK to dismiss the Layer Properties dialog.
- In the data frame, right-click on Burglaries – No Forced Entry and select Save as Layer File.
- Save the file as
c:\ArcpyBook\data\BurglariesNoForcedEntry.lyr
. - Right-click on the Burglaries – No Forced Entry layer and select Remove.
- Using the Add Data button in ArcMap, add the
Crimes2009
feature class from theCityOfSanAntonio
geodatabase. The feature class will be added to the data frame, as shown in the following screenshot: - Open the Python window in ArcMap.
- 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,"Crimes2009",df)[0]
- Define the layer that will be used to update the properties:
sourceLayer = mapping.Layer(r"C:\ArcpyBook\data\BurglariesNoForcedEntry.lyr")
- Call the
UpdateLayer()
function to update the symbology:mapping.UpdateLayer(df,updateLayer,sourceLayer,False)
- You can consult the solution file at
c:\ArcpyBook\code\Ch2\UpdateLayerProperties.py
to verify the accuracy of your code. - Run the script.
- The Crimes2009 layer will be updated with the properties associated with the
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: