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

Changing the map extent

There will be many occasions when you will need to change the map extent. This is frequently the case when you are automating the map production process and need to create many maps of different areas or features. There are a number of ways that the map extent can be changed with arcpy. But, for this recipe, we'll concentrate on using a definition expression to change the extent.

Getting ready

The DataFrame class has an extent property that you can use to set the geographic extent. This is often used in conjunction with the Layer.definitionQuery property that is used to define a definition query for a layer. In this recipe, you will learn how to use these objects and properties to change the map extent.

How to do it...

Follow these steps to learn how to get a list of layers from a map document:

  1. Open c:\ArcpyBook\Ch3\Crime_Ch3.mxd with ArcMap.
  2. Click on the Python window button from the main ArcMap toolbar.
  3. Import the arcpy.mapping module:
    import arcpy.mapping as mapping
  4. Reference the currently active document (Crime_Ch3.mxd), and assign the reference to a variable:
    mxd = mapping.MapDocument("CURRENT")
  5. Create a for loop that will loop through all the data frames in the map document:
    for df in mapping.ListDataFrames(mxd):
  6. Find the data frame called Crime and a specific layer that we'll apply the definition query against:
    if (df.name == 'Crime'):
      layers = mapping.ListLayers(mxd,'Crime Density by 
      School District',df)
  7. Create a for loop that will loop through the layers. There will only be one, but we'll create the loop anyway. In the for loop, create a definition query and set the new extent of the data frame:
    for layer in layers:
      query = '"NAME" = \'Lackland ISD\''
      layer.definitionQuery = query
      df.extent = layer.getExtent()
  8. The entire script should appear as follows:
    import arcpy.mapping as mapping
    mxd = mapping.MapDocument("CURRENT")
    for df in mapping.ListDataFrames(mxd):
      if (df.name == 'Crime'):
        layers = mapping.ListLayers(mxd,'Crime Density by School District',df)
      for layer in layers:
          query = '"NAME" = \'Lackland ISD\''
          layer.definitionQuery = query
          df.extent = layer.getExtent()
  9. Save and run the script. The extent of the data view should update to visualize only the features matching the definition expression, as shown in the following screenshot:
    How to do it...

How it works...

This recipe used a definition query on a layer to update the map extent. Near the end of the script, you created a new variable called query that holds the definition expression. The definition expression is set up to find school districts with a name of Lackland ISD. This query string is then applied to the definitionQuery property. Finally, the df.extent property is set to the returned value of layer.getExtent().

主站蜘蛛池模板: 清徐县| 石泉县| 江山市| 淳化县| 曲靖市| 冕宁县| 石棉县| 城口县| 铜梁县| 九江县| 临海市| 苗栗县| 习水县| 理塘县| 沙坪坝区| 武陟县| 贡山| 新兴县| 明星| 忻城县| 屏边| 岑巩县| 宁强县| 淮安市| 贺州市| 财经| 湖北省| 阿勒泰市| 山东省| 蓬安县| 积石山| 尚志市| 丹江口市| 城固县| 石棉县| 隆昌县| 长宁区| 揭西县| 萨迦县| 邢台县| 青铜峡市|