There will be many occasions when you will need to use the output of one tool as input to another tool. This is called tool chaining. An example of tool chaining could involve buffering a stream
layer and then finding all residential properties that fall within the buffer. In this case, the Buffer
tool would output a new layer, which would then be used as an input to the Select by Location
tool or one of the other overlay tools. In this recipe, you will learn how to obtain the output of a tool and use it as input to another tool.
The Buffer
tool creates an output feature class from an input feature layer using a specified distance. This output feature class can be stored in a variable, which can then be used as an input to another tool, such as the Select Layer by Location tool. In this recipe, you will learn how to use the output from the Buffer
tool as an input to the Select Layer by Location tool to find schools that are within a half mile of a stream.
Follow these steps to learn how to access the currently active map document in ArcMap:
.mxd
).c:ArcpyBookdataTravisCounty
.arcpy
module:import arcpy
arcpy.env.workspace = "c:/ArcpyBook/data/TravisCounty"
try
statement and add variables for the streams, buffered streams layer, distance, and schools:try: # Buffer areas of impact around major roads streams = "Streams.shp" streamsBuffer = "StreamsBuffer.shp" distance = "2640 Feet" schools2mile = "Schools.shp" schoolsLyrFile = 'Schools2Mile_lyr'
arcpy.Buffer_analysis(streams, streamsBuffer, distance,'FULL','ROUND','ALL')
MakeFeatureLayer
tool:arcpy.MakeFeatureLayer_management(schools2mile, schoolsLyrFile)
SelectLayerByLocation
tool:arcpy.SelectLayerByLocation_management(schoolsLyrFile, 'intersect', streamsBuffer)
except
block to catch any errors:except Exception as e: print(e.message)
c:ArcpyBookcodeCh5ToolOutputUsedAsInput.py
solution file.The Buffer
tool creates an output feature class, which we call StreamsBuffer.shp
and is stored in a variable called streamsBuffer
. The streamsBuffer
variable is then used as an input to the SelectLayerByLocation
tool as the third parameter being passed to the function. The creation of the Schools2Mile_lyr
layer file accomplishes this output as an input parameter as well. Using the output of one tool simply requires that you create a variable to hold the output data and then it can be reused as needed in other tools.