HOW TO
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.
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.
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", "")
Code:
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")
Code:
Output_Layer = "cities_Buffer_Layer"
# Process: Make Feature Layer...
gp.MakeFeatureLayer_management(cities_Buffer_shp, Output_Layer)
Code:
gp.SetParameterAsText(0, Output_Layer)
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)
Note:
The first parameter in the SetParameterAsText python geoprocessing method refers to the index number of this parameter list.
Article ID: 000008997
Get help from ArcGIS experts
Download the Esri Support App