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():

  1. Open 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:
    How to do it…
  2. 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:
    How to do it…
  3. Click on the General tab and change the value in the Layer Name: textbox to the name, as shown in the following screenshot:
    How to do it…
  4. 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:
    How to do it…
  5. Change the alias of the OFFDESC field to Offense Description, as shown in the next screenshot.
  6. 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:
    How to do it…
  7. Click on OK to dismiss the Layer Properties dialog.
  8. In the data frame, right-click on Burglaries – No Forced Entry and select Save as Layer File.
  9. Save the file as c:ArcpyBookdataBurglariesNoForcedEntry.lyr.
  10. Right-click on the Burglaries – No Forced Entry layer and select Remove.
  11. Using the Add Data button in ArcMap, add the Crimes2009 feature class from the CityOfSanAntonio geodatabase. The feature class will be added to the data frame, as shown in the following screenshot:
    How to do it…
  12. Open the Python window in ArcMap.
  13. Import the arcpy.mapping module:
    import arcpy.mapping as mapping
  14. Reference the currently active document (Crime_Ch2.mxd) and assign the reference to a variable:
    mxd = mapping.MapDocument("CURRENT")
  15. Get a reference to the Crime data frame:
    df = mapping.ListDataFrames(mxd, "Crime")[0]
  16. Define the layer that will be updated:
    updateLayer = mapping.ListLayers(mxd,"Crimes2009",df)[0]
  17. Define the layer that will be used to update the properties:
    sourceLayer = mapping.Layer(r"C:ArcpyBookdataBurglariesNoForcedEntry.lyr")
  18. Call the UpdateLayer() function to update the symbology:
    mapping.UpdateLayer(df,updateLayer,sourceLayer,False)
  19. You can consult the solution file at c:ArcpyBookcodeCh2UpdateLayerProperties.py to verify the accuracy of your code.
  20. Run the script.
  21. 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:
    How to do it…
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset