- Programming ArcGIS 10.1 with Python Cookbook
- Eric Pimpler
- 436字
- 2021-07-30 17:29:57
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:
- Open
c:\ArcpyBook\Ch3\Crime_Ch3.mxd
with ArcMap. - Click on the Python window button from the main ArcMap toolbar.
- Import the
arcpy.mapping
module:import arcpy.mapping as mapping
- Reference the currently active document (
Crime_Ch3.mxd
), and assign the reference to a variable:mxd = mapping.MapDocument("CURRENT")
- Create a
for
loop that will loop through all the data frames in the map document:for df in mapping.ListDataFrames(mxd):
- 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)
- Create a
for
loop that will loop through the layers. There will only be one, but we'll create the loop anyway. In thefor
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()
- 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()
- 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 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()
.
- Advanced Quantitative Finance with C++
- Vue.js 3.x快速入門
- Visual FoxPro程序設計教程(第3版)
- OpenNI Cookbook
- C語言從入門到精通(第4版)
- Mastering Apache Spark 2.x(Second Edition)
- Learning Hunk
- SQL經典實例(第2版)
- Python算法指南:程序員經典算法分析與實現
- Odoo 10 Implementation Cookbook
- 從零開始:UI圖標設計與制作(第3版)
- Anaconda數據科學實戰
- 高效使用Greenplum:入門、進階與數據中臺
- MySQL數據庫應用實戰教程(慕課版)
- Android智能手機APP界面設計實戰教程