官术网_书友最值得收藏!

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:\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:
  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:
  3. Click on the General tab and change the value in the Layer Name: textbox to the name, as shown in the following screenshot:
  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:
  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:
  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:\ArcpyBook\data\BurglariesNoForcedEntry.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:
  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:\ArcpyBook\data\BurglariesNoForcedEntry.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:\ArcpyBook\code\Ch2\UpdateLayerProperties.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:
主站蜘蛛池模板: 龙陵县| 克什克腾旗| 潜山县| 合山市| 齐河县| 辉南县| 昭觉县| 缙云县| 犍为县| 涞水县| 咸宁市| 上林县| 洞头县| 孟州市| 彭水| 类乌齐县| 黑龙江省| 饶平县| 嘉黎县| 房产| 昌平区| 牡丹江市| 扶沟县| 武冈市| 丽江市| 庆城县| 富蕴县| 青铜峡市| 二连浩特市| 东丰县| 新民市| 衢州市| 定襄县| 和平区| 西宁市| 堆龙德庆县| 黎平县| 洱源县| 虞城县| 宜兴市| 伊宁市|