Adding layers to a map document

There will be many situations where you will need to add a layer to a map document. The mapping module provides this functionality through the AddLayer() function. In this recipe, you will learn how to add a layer to a map document using this function.

Getting ready

The arcpy.mapping module 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 as is provided by the Add Data button in ArcMap, which positions a layer in the data frame based on geometry type and layer weight rules.

Note

Layers can't be added to a layer file (.lyr).

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. A layer can be either a layer in a map document or a layer in a .lyr file. 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.

How to do it…

Follow these steps to learn how to add a layer to a map document:

  1. Open c:ArcpyBookCh2Crime_Ch2.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_Ch2.mxd) and assign the reference to a variable:
    mxd = mapping.MapDocument("CURRENT")
  5. Get a reference to the Crime data frame, which is the first data frame in the list returned by ListDataFrames(). The [0] value, specified at the end of the code, gets the first data frame returned from the ListDataFrames() method, which returns a list of data frames. Lists are 0-based, so in order to retrieve the first data frame, we provide an index of 0:
    df = mapping.ListDataFrames(mxd)[0]
  6. Create a Layer object that references a .lyr file:
    layer = mapping.Layer(r"C:ArcpyBookdataSchool_Districts.lyr")
  7. Add the layer to the data frame:
    mapping.AddLayer(df,layer,"AUTO_ARRANGE")
  8. You can consult the solution file at c:ArcpyBookcodeCh2AddLayersMapDocument.py. Run the script. The School_Districts.lyr file will be added to the data frame, as shown in the following screenshot:
    How to do it…

How it works…

In the first two lines, we simply referenced the arcpy.mapping module and got a reference to the currently active map document. Next, we created a new variable called df, which held a reference to the Crime data frame. This was obtained through the ListDataFrames() function that returned a list of data frame objects. We then used list access to return the first item in the list, which is the Crime data frame. A new Layer instance, called layer, was then created from a layer file stored on disk. This layer file was called School_Districts.lyr. Finally, we called the AddLayer() function, passing in the data frame where the layer should ideally reside along with a reference to the layer, and a parameter indicating that we wanted to use the auto-arrange feature. In addition to allowing ArcMap to automatically place the layer into the data frame using auto-arrange, you can also specifically place the layer at either the top or bottom of the data frame or a group layer using the BOTTOM or TOP position.

There's more…

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 objects, group layers cannot be added to a layer file.

Layers can also be removed from a data frame or group layer. RemoveLayer() is the function used to remove a layer or group layer. In the event that two layers have the same name, only the first is removed, unless your script is set up to iterate.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset