Using a ValueTable to provide multivalue input to a tool

Many geoprocessing tools have input parameters that accept more than one value. For example, the multiring buffer tool accepts multiple buffer distances, the delete field tool accepts multiple fields that can be deleted, and there are many other examples. In this recipe, you will learn how to create a ValueTable object that serves as multivalue input to a tool.

Getting ready

There are three ways to specify a multivalue parameter: as a Python list, a string with each value separated by semicolons, or an ArcPy ValueTable object. In this recipe, we're going to take a look at how to specify mutlivalue input parameters by using ValueTable.

How to do it…

Follow these steps to learn how to use a ValueTable to submit multiple values to a tool:

  1. Open IDLE (or your favorite Python development environment) and create a new script called ValueTable.py.
  2. Import arcpy and set the workspace:
    import arcpy
    
    arcpy.env.workspace = r"c:ArcyBookdata"
  3. Create a new ValueTable object:
    import arcpy
    
    arcpy.env.workspace = r"c:ArcyBookdata"
    vTab = arcpy.ValueTable()
  4. Create three rows for the table and assign them distances of 5, 10, and 20:
    vTab = arcpy.ValueTable()
    vTab.setRow(0, "5")
    vTab.setRow(1, "10")
    vTab.setRow(2, "20")
  5. Define variables for the input feature class, output feature class, distance, and buffer units. The distance variable (dist) is created as a reference to the ValueTable, which you have already created:
    vTab = arcpy.ValueTable()
    vTab.setRow(0, "5")
    vTab.setRow(1, "10")
    vTab.setRow(2, "20")
    
    inFeature = 'Hospitals.shp'
    outFeature = 'HospitalMBuff.shp'
    dist = vTab
    bufferUnit = "meters"
  6. Call the MultipleRingBuffer tool and pass the variables as parameters:
    inFeature = 'Hospitals.shp'
    outFeature = 'HospitalMBuff.shp'
    dist = vTab
    bufferUnit = "meters"
    
    arcpy.MultipleRingBuffer_analysis(inFeature, outFeature, dist, bufferUnit, '', 'ALL')
    print("Multi-Ring Buffer Complete")
  7. You can check your work by examining the C:ArcpyBookcodeCh12ValueTable.py solution file.
  8. Save and run the script. Examine the output to see the multiple buffer rings.

How it works…

ValueTable is a simple virtual table that you can use as input to tools that accept multiple values. In this recipe, we created a ValueTable object, added three values, and then passed this object into the MultipleRingBuffer tool. The MultipleRingBuffer tool used this information to create new polygon layers based on the buffer distances provided in the ValueTable object.

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

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