- Programming ArcGIS 10.1 with Python Cookbook
- Eric Pimpler
- 574字
- 2021-07-30 17:29:58
Inserting layers into a map document
The AddLayer()
function can be used to add a layer to a map document either through auto-arrange or as the first or last layer in a data frame. However, it doesn't provide the control you need for inserting a layer in a specific position within a data frame. For this added control, you can use the InsertLayer()
function. In this recipe, you will learn how to control the placement of layers added to a data frame.
Getting ready
The AddLayer()
function simply adds a layer into a data frame or a group layer, and places the layer automatically using auto-arrange or specific placement at the top or bottom of the data frame or group layer. The InsertLayer()
method allows for more precise positioning of a new layer into a data frame or a group layer. It uses a reference layer to specify a location and the layer is added either before or after the reference layer, as specified in your code. Since InsertLayer()
requires the use of a reference layer, you can't use this method on an empty data frame.
This is illustrated in the following screenshot, where District_Crime_Join
is the reference layer and School_Districts
is the layer to be added. The School_Districts
layer can be placed either before or after the reference layer using InsertLayer()
.

How to do it…
Follow these steps to learn how to use InsertLayer()
to insert a layer into a data frame:
- 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")
- Get a reference to the
Crime
data frame:df = mapping.ListDataFrames(mxd, "Crime")[0]
- Define the reference layer:
refLayer = mapping.ListLayers(mxd, "Burglaries*", df)[0]
- Define the layer to be inserted relative to the reference layer:
insertLayer = mapping.Layer(r"C:\ArcpyBook\data\CityOfSanAntonio.gdb\Crimes2009")
- Insert the layer into the data frame:
mapping.InsertLayer(df,refLayer,insertLayer,"BEFORE")
- Run the script. The
Crimes2009
feature class will be added as a layer to the data frame as seen in the following screenshot:
How it works…
After obtaining references to the arcpy.mapping
module, current map document file, and the Crime
data frame, our script then defines a reference layer. In this case, we use the ListLayers()
function with a wildcard of Burglaries*
, and the Crime
data frame to restrict the list of layers returned to only one item. This item will be the Burglaries in 2009 layer. We use Python list access with a value of 0
to retrieve this layer from the list and assign it to a Layer
object. Next, we define the insert layer, which is a new Layer
object that references the Crimes2009 feature class from the CityOfSanAntonio
geodatabase database. Finally, we call the InsertLayer()
function passing in the data frame, reference layer, layer to be inserted, and keyword indicating that the layer to be inserted should be placed before the reference layer. This is illustrated in the following screenshot:

There's more…
You can also reposition a layer that is already in a data frame or a group layer. The MoveLayer()
function provides the ability to reposition the layer within a data frame or a group layer. Movement of a layer must be within the same data frame. You can't move a layer from one data frame to another. Just as with InsertLayer()
, MoveLayer()
uses a reference layer to reposition the layer.
- Azure IoT Development Cookbook
- Python Geospatial Development(Second Edition)
- Learning Python Design Patterns(Second Edition)
- Nginx實戰:基于Lua語言的配置、開發與架構詳解
- WordPress 4.0 Site Blueprints(Second Edition)
- Android系統級深入開發
- OpenCV 4計算機視覺項目實戰(原書第2版)
- 跟戴銘學iOS編程:理順核心知識點
- SCRATCH編程課:我的游戲我做主
- 大話代碼架構:項目實戰版
- Implementing Domain:Specific Languages with Xtext and Xtend
- Programming MapReduce with Scalding
- PHP面試一戰到底
- Prezi Cookbook
- Visual C#網絡編程