English

How To: Load a layer into ArcMap in a geoprocessing script in Python

Summary

When executing geoprocessing tools in a Python script, it is often desirable to have the output of the geoprocessing tools added to the ArcMap display. This is possible using the MakeFeatureLayer tool coupled with the SetParameterAsText method.

Procedure

Warning:
Due to bug NIM009358, the steps below do not work in ArcGIS version 9.2.


Note:
At version 10, use the new arcpy.mapping AddLayer function. See the Help topic: 'Managing Documents and Layers: AddLayer' in the Related Information section below.

Instructions provided demonstrate how to modify the script to display the output feature class in ArcMap. The following procedure only applies to scripts run from ArcMap.

A basic Python script is used in this example. The script buffers a point layer to create a new output polygon buffer feature class.

The unaltered sample script is as follows:
Code:
# Import system modules
import sys, string, os, win32com.client

# Create the Geoprocessor object
gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")

gp.overwriteoutput = 1

# Load required toolboxes...
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Analysis Tools.tbx")

# Local variables...
cities_Buffer_shp = "C:\\ESRI\\ESRIDATA\\CANADA\\cities_Buffer.shp"
cities_shp = "C:\\ESRI\\ESRIDATA\\CANADA\\cities.shp"

# Process: Buffer...
gp.Buffer_analysis(cities_shp, cities_Buffer_shp, "1 DecimalDegrees",
"FULL", "ROUND", "NONE", "")

  1. Add code to add the toolbox containing the MakeFeatureLayer tool.
    Code:
    gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")

  2. Add code to execute MakeFeatureLayer.
    Code:
    Output_Layer = "cities_Buffer_Layer"

    # Process: Make Feature Layer...
    gp.MakeFeatureLayer_management(cities_Buffer_shp, Output_Layer)

  3. Add code to execute SetParameterAsText.
    Code:
    gp.SetParameterAsText(0, Output_Layer)

  4. The final script looks like this:
    Code:
    # Import system modules
    import sys, string, os, win32com.client

    # Create the Geoprocessor object
    gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")

    gp.overwriteoutput = 1

    # Load required toolboxes...
    gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")
    gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Analysis Tools.tbx")
    # Local variables...
    cities_Buffer_shp = "C:\\ESRI\\ESRIDATA\\CANADA\\cities_Buffer.shp"
    cities_shp = "C:\\ESRI\\ESRIDATA\\CANADA\\cities.shp"
    Output_Layer = "cities_Buffer_Layer"

    # Process: Buffer...
    gp.Buffer_analysis(cities_shp, cities_Buffer_shp, "1 DecimalDegrees",
    "FULL", "ROUND", "NONE", "")

    # Process: Make Feature Layer...
    gp.MakeFeatureLayer_management(cities_Buffer_shp, Output_Layer)

    gp.SetParameterAsText(0, Output_Layer)

  5. Add the script to a toolbox in ArcMap by right-clicking the toolbox in ArcToolBox in ArcMap and selecting Add > Script.
  6. In the Add Script dialog box, type a script Name and Label.
    [O-Image]
  7. Click Next.
  8. In the Add Script dialog box, navigate to the .py file.
    [O-Image]
  9. Click Next.
  10. In the Add Script dialog box, create a new feature layer derived output parameter by filling out the Parameter Properties as shown below. The Display Name is a user defined parameter, and can be named anything. Set the Type to Derived and the Direction to Output.
    [O-Image] Create a feature layer derived output parameter

    Note:
    The first parameter in the SetParameterAsText python geoprocessing method refers to the index number of this parameter list.

  11. Click Finish.
  12. In ArcMap, select Tools > Options > Geoprocessing tab, and ensure that 'Add results of geoprocessing operations to the display' is checked. Click OK in the Options dialog box.
  13. Run the script by double-clicking it in ArcToolBox. The output layer is added to the ArcMap display.

Related Information