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:
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 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:
- You set the current and scratch workspaces in the Geoprocessing Environments window, accessed by clicking Geoprocessing > Environments.
- The current and scratch workspace settings, like all geoprocessing environments, are saved with your map document.
- In ArcMap, the current and scratch workspaces are automatically set to the default geodatabase. You can always change the current or scratch workspace to something other than this default geodatabase.
- When using a tool dialog box or executing a tool in Python, you can enter the base name of a dataset, and the tool will find it in the current workspace.
- When using the tool dialog box, output dataset names are autogenerated using the current and scratch workspace settings.
Setting the current workspace environment
- In ArcMap, click Geoprocessing > Environments. This opens the Environment Settings window.
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.
You can set the current workspace to a system folder, a geodatabase, or a feature dataset within a geodatabase.
- Click OK.
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.
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.
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.
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.
- 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.
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.
Displaying the current workspace in a tool dialog
You can pause the pointer over the browse button for a moment, and the current workspace is displayed, as shown below.
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.
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")
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:
- If the scratch workspace environment is set, the autogenerated output path will be the scratch workspace.
- If the scratch workspace environment is not set, the current workspace environment is checked. If current workspace is set, the autogenerated output will be the current workspace.
- If neither the scratch or current workspace is set, the autogenerated output path will be the workspace of one of the inputs. In this case, certain restrictions apply. For example, if the workspace is a coverage workspace and the output is a new feature class, the output will be a shapefile to the directory above the coverage workspace. There are other restrictions as well, such as write access. In some cases, the output will be written to the system temp directory.
- If you enter a base name for the output dataset, the current workspace will be used to construct the output path, regardless of whether the scratch workspace is set.