Using ArcPy Mapping to Add and Remove Layers in a Map Document
In previous posts on the new ArcPy.Mapping module, introduced at ArcGIS 10, I introduced some of the capabilities of the module and showed you how to programmatically access and manipulate layout elements. Today I’d like to show you how to programmatically add and remove layers and group layers in your map document file with ArcPy.Mapping.
Arcpy.Mapping provides the ability to add layers or group layers into an existing map document file. You can take advantage of the ArcMap “auto-arrange” functionality which automatically places a layer in the data frame for visibility. This is essentially the same functionality provided by the Add Data button in ArcMap which positions a layer in the data frame based on geometry type and layer weight rules. Do note that while it is possible to add layers to a map document, this does not apply to adding layers to a layer file.
When adding a layer to a map document the layer must reference an existing layer found in a layer file on disk, the same map document and data frame, the same map document with a different data frame, or a completely separate map document. To add a layer to a map document you must first create an instance of the Layer class and then call the AddLayer() function passing in the new layer along with the data frame where it should be placed and rules for how it is positioned. I’ve shown this in the code example below.
Let’s discuss what this code block accomplishes. In the first two lines we simply reference arcpy and get a reference to the currently active map document. Next, we create a new variable called ‘df’ which holds a reference to the Crime data frame which was obtained through the ListDataFrames() function which filtered the data frames to return the specific Crime data frame. A new Layer instance, called ‘addLayer’ is then created from a layer file stored on disk. This layer file is called School_Districts.lyr. Finally, we call the AddLayer() function, passing in the data frame where the layer will reside along with a reference to the layer, and a parameter indicating that we would like to use the auto arrange feature.
In addition to providing the capability of adding a layer to a map document, arcpy.mapping also provides an AddLayerToGroup() function which can be used to add a layer to a group layer. The layer can be added to the top or bottom of the group layer or you can use auto-arrange for placement. You may also add layers to an empty group layer. However, just as with regular layer, group layers can’t be added to a layer file.
You can also remove layers from a map document. RemoveLayer() removes a layer or group layer from a specific data frame. In the event that two layers have the same name only the first is removed unless your script is set up to iterate.
You can also insert layers into specific positions in a data frame or group layer. The InsertLayer() method allows for more precise positioning of a new layer into a data frame or 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. The figure below show District_Crime_Join as the reference layer. Using this reference layer you could place the School_Districts layer either before or after the layer.
MoveLayer() provides the ability to reposition the layer within a data frame or 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.
Next time we’ll cover map printing and exporting with ArcPy.Mapping.
Want to learn more about the ArcPy Mapping module and other programming ArcGIS Desktop with Python topics? The next session of our Internet based, instructor led course “GIS Programming 101: Mastering Python for Geoprocessing in ArcGIS” begins October 25th, 2010.