- Programming ArcGIS with Python Cookbook(Second Edition)
- Eric Pimpler
- 409字
- 2021-07-16 13:32:22
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
. However, 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\Ch2\Crime_Ch2.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_Ch2.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 or you can consult the solution file at
c:\ArcpyBook\code\Ch2\ChangeMapExtent.py
: - Save and run the script. The extent of the data view should update so that it visualizes 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 held the definition expression. The definition expression was set up to find school districts with a name of Lackland ISD. This query string was then applied to the definitionQuery
property. Finally, the df.extent
property was set to the returned value of layer.getExtent()
.
- 大學(xué)計算機(jī)基礎(chǔ)(第二版)
- Mastering Ext JS(Second Edition)
- UML和模式應(yīng)用(原書第3版)
- C語言程序設(shè)計習(xí)題解析與上機(jī)指導(dǎo)(第4版)
- Java Web基礎(chǔ)與實(shí)例教程(第2版·微課版)
- Learn Swift by Building Applications
- Python Geospatial Development(Second Edition)
- 小程序,巧運(yùn)營:微信小程序運(yùn)營招式大全
- Magento 1.8 Development Cookbook
- 軟件測試技術(shù)指南
- Scientific Computing with Scala
- Learning Laravel's Eloquent
- HTML5+CSS3 Web前端開發(fā)技術(shù)(第2版)
- 從零開始學(xué)Linux編程
- MySQL從入門到精通(軟件開發(fā)視頻大講堂)