English

How To: Use Value Tables in Python as part of a Geoprocessing script

Summary

A Value Table is a flexible object that can be used as input for a multivalue parameter. A number of GP tools can optionally use Value Tables. For example, Union, Merge or any tool that has a multivalue parameter.

Procedure

  • Below is an example of the Merge command NOT using a Value Table:

    Code:
    # Import system modules
    import sys, string, os, win32com.client

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

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

    # Process: Merge...
    gp.Merge_management("C:\\ESRI\\ESRIDATA\\MEX_STATES.shp;C:\\ESRI\\ESRIDATA\\USA_STATES.shp",
    "C:\\ESRI\\ESRIDATA\\STATES_Merge.shp")

    print "done"

    This example uses the text value of the multivalue parameter, which can become difficult to use when there are numerous values with complex paths. The Value Table is used to organize the values into a table, so values can be easily added or removed, eliminating the complexity of parsing a multivalue text string. Below is an example using Merge with a Value Table:

    Code:
    # Import system modules
    import sys, string, os, win32com.client

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

    # Set the workspace.
    gp.Workspace = "C:\\ESRI\\ESRIDATA\\"

    # Merge shape type, input Polyline or Polygon
    shapeType = "Polygon"

    gp.OverwriteOutput = 1

    # Create the value table
    vtab = gp.CreateObject("ValueTable")

    # List all of the feature classes in the dataset
    fcs = gp.ListFeatureClasses()
    fcs.reset()
    fc = fcs.next()

    # Store all the feature classes in a value table
    while fc:
    # Describe the feature class
    dsc = gp.Describe(fc)
    if dsc.Shapetype == shapeType:
    vtab.AddRow(fc)
    fc = fcs.next()

    # Process: Merge...
    gp.Merge_management(vtab, "C:\\ESRI\\ESRIDATA\\STATES_Merge.shp")

    print "done"

  • Value Tables can have many fields. In the examples above, a Value Table of one column is created. This is the default:

    Code:
    vtab = gp.CreateObject("ValueTable")

    Some tools accept Value Tables with multiple columns. For example, the Union_analysis tool accepts in a two column value table consistening of input feature classes and ranks. When populating a Value Table with multiple columns, the column seperator is a space:

    Code:
    # Import system modules
    import sys, string, os, win32com.client

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

    # Create the value table for the Analysis Union tool with 2 columns
    vtab = gp.CreateObject("ValueTable", 2)

    # Set the workspace.
    gp.Workspace = "C:\\ESRI\\ESRIDATA\\"

    gp.OverwriteOutput = 1

    # List all of the feature classes in the dataset
    fcs = gp.ListFeatureClasses()
    fcs.reset()
    fc = fcs.next()

    while fc:
    # Update the value table with a rank of 2 for each record, except MEX_STATES
    if fc <> "MEX_STATES":
    vtab.AddRow(fc + " " + "2")
    else:
    vtab.AddRow(fc + " " + "1")
    fc = fcs.next()

    # Union the feature classes
    gp.Union_analysis(vtab, "union_out")

    print "done"

    However, there may be times when the values put in the Value Table themselves may have spaces. For example paths or shapefiles with spaces in the name. It is therefore good practise to put each value in single quotes. For example:

    Code:
    # Import system modules
    import sys, string, os, win32com.client

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

    # Create the value table for the Analysis Union tool with 2 columns
    vtab = gp.CreateObject("ValueTable", 2)

    # Set the workspace.
    gp.Workspace = "C:\\ESRI\\ESRIDATA\\"

    gp.OverwriteOutput = 1

    # List all of the feature classes in the dataset
    fcs = gp.ListFeatureClasses()
    fcs.reset()
    fc = fcs.next()

    while fc:
    # Update the value table with a rank of 2 for each record, except rivers
    if fc <> "MEX_STATES":
    vtab.AddRow("'" + fc + "'" + " " + "'2'")
    else:
    vtab.AddRow("'" + fc + "'" + " " + "'1'")
    fc = fcs.next()

    # Union the feature classes
    gp.Union_analysis(vtab, "union_out")

    print "done"

  • Value Tables can also be used as a convenient array object, similar to a VB Array. For example:

    Code:
    # Import system modules
    import sys, string, os, win32com.client

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

    size = 2

    # Populate the Value Table with some values
    vt = gp.CreateObject("ValueTable", size)
    vt.SetRow(0, "'E' 'S'")
    vt.SetRow(1, "'R' 'I'")

    # Read the Value Table
    for i in range(vt.rowcount):
    for j in range(vt.columncount):
    print vt.getvalue(i, j)