官术网_书友最值得收藏!

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().

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:\ArcpyBook\Ch3\Crime_Ch3.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_Ch3.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:\ArcpyBook\data\CityOfSanAntonio.gdb\Crimes2009")
    
  8. Insert the layer into the data frame:
    mapping.InsertLayer(df,refLayer,insertLayer,"BEFORE")
  9. 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 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:

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.

主站蜘蛛池模板: 嵩明县| 洮南市| 琼结县| 金湖县| 比如县| 曲周县| 沈阳市| 额尔古纳市| 金平| 丽水市| 平阴县| 庐江县| 雷波县| 塔城市| 沽源县| 万年县| 宁海县| 阜阳市| 启东市| 南郑县| 永善县| 望奎县| 滦平县| 义乌市| 连江县| 紫阳县| 襄樊市| 康平县| 博白县| 阿巴嘎旗| 阿鲁科尔沁旗| 曲阜市| 铁岭市| 伊宁市| 江陵县| 芮城县| 宜良县| 财经| 和顺县| 永靖县| 荣成市|