Spring 2013

Avoid GUI Headaches

A case for scripting geoprocessing tools

By Drew Flater, Geoprocessing Team


This article as a PDF.

Sometimes the repetitive nature of a task can make using a tool dialogtime-consuming and inefficient.

One example would be selecting a large number of fields for the Statistics Field(s) parameter of the Summary Statistics (Analysis) tool. With this parameter, you select fields from the input table that you want to summarize on (e.g., calculate the mean or sum of a numeric field). While this interaction for a single field is quick and easy, imagine if you had to do this for hundreds of fields in a demographic dataset. Repeatedly picking a single field and the corresponding statistic is time-consuming, inefficient, and frustrating.

Luckily there is more than one way to run a geoprocessing tool. One of those—Python scripting—is a very good way to make repetitive tasks easy. Let's consider how Python scripting can accomplish the task of getting a sum statistic for every numeric field in a dataset.

The ArcGIS scripting package ArcPy has functions for doing all kinds of GIS tasks. One, the ListFields function, is especially important for this scenario. The ListFields function, as its name suggests, returns a list of all the attribute fields in a specified dataset. Listing a dataset's fields and checking that the field type is numeric are key steps in scripting this task.

Another key step in writing the script is constructing the Statistics Field(s) parameter with the correct Python syntax. In a Python script, the Statistics Field(s) parameter is best represented by a list of lists. You can think of the outer/main list as the full Statistics Field(s) parameter table in the tool dialog box and each sublist as a single row in that table.

Just like the Summary Statistics (Analysis) tool dialog box, the Statistics Field(s) parameter table has two columns. The first column is for the field name, and the second column is the statistic to calculate for that field.

In Python scripting, each sublist (row) has two elements that correspond directly to the columns in the parameter table. The sublists are constructed by putting together the field name and sum statistic type while iterating through the list of fields. But remember, this is only done after checking whether the field type is numeric, because it is impossible to calculate the Sum of other types such as text or dates.

As Listing 1 illustrates, in just a few lines of code, this repetitive and time-consuming task has been automated.

Python scripting is one of several ways to run ArcGIS geoprocessing tools. Scripting a geoprocessing tool can often help work around problems that occur because a tool dialog parameter is tricky or requires repetitive action.

You can use the powerful and wide-ranging functions available in ArcPy to help you with many GIS tasks, such as using the ListFields function to return a list of all attribute fields in a dataset, which can be subsequently looped through. Using ArcPy functions together with geoprocessing tools can help you be more productive and avoid those awful GUI headaches. Learn more about the ways Python can make your life easier at Python for ArcGIS on the ArcGIS Resources site.

# Script that runs the Summary Statistic tool to Sum every numeric attribute
# of Census tracts by unique County IDs
import arcpy

# Local variables
intable = "C:/Data/f.gdb/CensusTracts"
outtable = "C:/Data/f.gdb/CensusTracts_SumStats_Counties"
casefield = "CNTY_FIPS"
# Create a new empty list to store pairs of field + statistic
stats = []

# Loop through all fields in the Input Tablew
for field in arcpy.ListFields(intable):
    # Just find the fields that have a numeric type
    if field.type in ("Double", "Integer", "Single", "SmallInteger"):
        # Add the field name and Sum statistic type as a list
        # to the list of fields to summarize (makes a list of lists)
        stats.append([field.name, "Sum"])

# After looping, the Statistics list of lists will look like
# [["HOUSEHOLDS", "Sum"], ["MALES", "Sum"], ...]

# Run the Summary Statistics tool with the Statistics list of lists
arcpy.Statistics_analysis(intable, outtable, stats, casefield)
Listing 1: Python script automating parameter input for the Summary Statistics (Analysis) tool

Contact Us | Privacy | Legal | Site Map