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 that are 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. You can choose to have the layer placed at the top or bottom of either. The InsertLayer() method allows you to have 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():

Getting ready

How to do it…

Follow these steps to learn how to use InsertLayer() to insert a layer into a data frame:

  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:
    df = mapping.ListDataFrames(mxd, "Crime")[0]
  6. Define the reference layer:
    refLayer = mapping.ListLayers(mxd, "Burglaries*", df)[0]
  7. Define the layer to be inserted relative to the reference layer:
    insertLayer = mapping.Layer(r"C:ArcpyBookdataCityOfSanAntonio.gdbCrimes2009")
  8. Insert the layer into the data frame:
    mapping.InsertLayer(df,refLayer,insertLayer,"BEFORE")
  9. You can consult the solution file at c:ArcpyBookcodeCh2InsertLayerMapDocument.py to verify the accuracy of your code.
  10. Run the script. The Crimes2009 feature class will be added as a layer to the data frame, as seen in the following screenshot:
    How to do it…

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 used 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 should be the Burglaries in 2009 layer. We used Python list access with a value of 0 to retrieve this layer from the list and assigned it to a Layer object. Next, we defined the insert layer, a new Layer object that references the Crimes2009 feature class from the CityOfSanAntonio geodatabase. Finally, we called 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:

How it works…

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.

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

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