Using the output of a tool as an input to another tool

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.

Getting ready

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.

How to do it...

Follow these steps to learn how to access the currently active map document in ArcMap:

  1. Open ArcMap with a new map document file (.mxd).
  2. Click on the Add Data button and add the streams and schools shapefiles from c:ArcpyBookdataTravisCounty.
  3. Click on the Python window button.
  4. Import the arcpy module:
    import arcpy
  5. Set the workspace:
    arcpy.env.workspace = "c:/ArcpyBook/data/TravisCounty"
  6. Start a 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'
  7. Execute the Buffer tool by passing in the input feature class, output feature class, distance, and several optional variables that control the look of the output buffer.
    arcpy.Buffer_analysis(streams, streamsBuffer, distance,'FULL','ROUND','ALL')
  8. Create a temporary layer for the schools by using the MakeFeatureLayer tool:
    arcpy.MakeFeatureLayer_management(schools2mile, schoolsLyrFile)
  9. Select all schools within a half mile of a stream by using the SelectLayerByLocation tool:
    arcpy.SelectLayerByLocation_management(schoolsLyrFile, 'intersect', streamsBuffer)
  10. Add the except block to catch any errors:
    except Exception as e:
      print(e.message)
  11. The entire script should appear as follows:
    How to do it...
  12. You can check your work by examining the c:ArcpyBookcodeCh5ToolOutputUsedAsInput.py solution file.
  13. Run the script to view the results shown in the following screenshot:
    How to do it...

How it works...

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.

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

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