Using the current and scratch workspace environments

Almost all geoprocessing tools take datasets as input and produce new datasets. When using geoprocessing tools, the one thing you want to avoid is typing in long dataset names, such as:

E:\Data\D052753_a\infrastructure\BK16_c1\approved.gdb\roads\mjrst.

Entering such a long dataset name is tedious, frustrating, and error prone. That's why geoprocessing allows you to drag datasets or layers onto a tool dialog box, use the browse button Open to navigate to a dataset, or select a layer from a drop-down list. Additionally, there are two features designed to make specifying input and output datasets much easier: the current workspace and scratch workspace environment settings.

Here are the key points about the current and scratch workspaces:

Setting the current workspace environment

Steps:
  1. In ArcMap, click Geoprocessing > Environments. This opens the Environment Settings window.
  2. Expand the Workspace category and enter the path to the workspace. The illustration below shows setting the current workspace to D:\ArcTutor\BuildingaGeodatabase\Montgomery.gdb\Landbase, a feature dataset within the geodatabase.

    Setting the current workspace environment

    You can set the current workspace to a system folder, a geodatabase, or a feature dataset within a geodatabase.

  3. Click OK.
  4. Alternatively, you can browse to a geodatabase in the Catalog window, right-click the geodatabase, then click Make Default Geodatabase. The current and scratch workspaces will be set to this default geodatabase.

There are several other methods to set environments, and you can set environments so that they apply for all tools, the execution of one tool, a model, a model process, or a script.

Learn more about environment settings

Using base names

The main idea behind the current and scratch workspace is that you set a workspace once, then use only the base name when entering input and output paths. A dataset name has two components, the workspace and the base name, as illustrated below.

Dataset name
Catalog path, path, location, and dataset name. In practical terms, they all mean the same thing—the string used to specify a dataset.

Example of using base names

The illustration below shows an example geodatabase that is used as the current workspace. The current workspace is set to D:\BuildingaGeodatabase\Montgomery.gdb\Landbase.

Contents of Montgomery.gdb

Once the workspace has been set, you can enter just the base name whenever a dataset name is needed. The example below shows using the Clip tool.

  • The base name (Blocks) is joined with the current workspace to form the dataset name (D:\BuildingaGeodatabase\Montgomery.gdb\Landbase\Blocks). The Input Features parameter is then replaced with this dataset name.
  • A unique output dataset name is autogenerated. The base name is the same as the input base name (Blocks) appended with an underscore; the name of the tool—Clip in this example; and, if necessary to ensure a unique name, a number.
    Entering a base name for the input
  • If you do not want to use the autogenerated output name, you can delete the autogenerated name and input a base name, and it will be expanded to a dataset name, as shown below.

    Using base name for an output dataset

After you've run a tool, you may find that output isn't written where you expect—perhaps you made a mistake when entering the output name, or you've just forgotten where it was written. If this is the case, open the Results window; it will contain a record of the tool you've run along with its input and output datasets.

You can also use a base name in the batch grid, as shown below.

Using current workspace with the batch grid

Learn more about batch processing

Displaying the current workspace in a tool dialog

You can pause the pointer over the browse button Open for a moment, and the current workspace is displayed, as shown below.

Displaying the current workspace in a tool dialog box

When you click the browse button, the Browse dialog box opens in the current workspace.

Current workspace and base name in scripting

In the Python window, the workspace environment sets the current workspace. After setting the workspace, you can use the base name of any dataset within the workspace, as shown below.

>> import arcpy
>> arcpy.env.workspace = "c:/projects/RedRiverBasin/data.mdb"
>> arcpy.Intersect_analysis("roads ; streams ", "stream_crossings", "#", 1.5, "point")

Following is a Python script example that shows use of the workspace command:

# Purpose: Determine the type of vegetation within 100 meters of all stream crossings

# Import the ArcPy site-package
import arcpy

try:
    # Set the workspace (to avoid having to type in the full path to the data every time)
    arcpy.env.workspace = "c:/projects/RedRiverBasin/data.mdb"

    # Process: Find all stream crossings (points)
    arcpy.Intersect_analysis("roads ; streams ", "stream_crossings", "#", 1.5, "point")

    # Process: Buffer all stream crossings by 100 meters
    arcpy.Buffer_analysis("stream_crossings","stream_crossings_100m", "100 meters")

    # Process: Clip the vegetation feature class to stream_crossings_100m
    arcpy.Clip_analysis("vegetation", "stream_crossings_100m", "veg_within_100m_of_crossings")

    # Process: Summarize how much (area) of each type of vegetation is found within 100 meters of the stream crossings
    arcpy.Statistics_analysis("veg_within_100m_of_crossings", "veg_within_100m_of_crossings_stats","shape_area sum","veg_type")

except:
    # If an error occurred while running a tool print the messages
    print arcpy.GetMessages()

The scratch workspace environment

In addition to the current workspace, there is the scratch workspace environment setting. The scratch workspace setting is accessed and set the same way that you access and set the current workspace.

The primary purpose for the scratch workspace environment is for use by ModelBuilder. ModelBuilder needs a workspace to write intermediate datasets—datasets that are of no use once a model is run. Although its primary purpose is for ModelBuilder, there may be times when you want to set it for tool dialog boxes. Or, more likely, you set the scratch workspace to use in ModelBuilder and forgot to reset it before executing a tool using its dialog box.

If you set the scratch workspace environment, tools will use it to autogenerate output dataset names instead of the current workspace, as shown below.

Example of autogenerated output using scratch workspace
CautionCaution:

If you enter a base name for an output, the current workspace is used to construct the dataset name, not the scratch workspace.

The rule is that anytime a base name is used, it is joined with the current workspace, not the scratch workspace. Be aware of this when writing scripts. In the code snippet below, the output dataset, "stream_crossings", will be written to the current workspace, not the scratch workspace.

    arcpy.env.workspace = "c:/projects/RedRiverBasin/data.mdb"
    arcpy.env.scratchWorkspace = "c:/projects/Scratch/scratch.gdb"
    arcpy.Intersect_analysis("roads ; streams ", "stream_crossings", "#", 1.5, "point")
CautionCaution:

Because the scratch workspace is meant for temporary data, do not set it to an ArcSDE geodatabase. This could cause performance problems—you may be writing temporary scratch data to an enterprise database. It's suggested that you always use a file geodatabase (rather than a personal geodatabase or a shapefile workspace) for the scratch workspace.

Autogenerated output dataset names

All tools automatically create an output dataset name for you. The logic for generating the output name is as follows:

Related Topics


12/15/2011