Using layers and table views
The most common input to a tool is a dataset. You can, of course, always browse to the dataset on disk, but you can also use layers or table views as input.
- A layer is what you see in the ArcMap table of contents. A layer contains the path to the dataset along with symbology information and feature or row selection information.
- Table views are the table equivalent of a layer. They are tables stored in memory and are the same as the table view created when a table is added to ArcMap. You can view tables by clicking the List By Source button found at the top of the table of contents, as illustrated below.
Any tool that operates on a table will also operate on a feature class or feature layer. This is because feature classes are simply tables with an attribute containing the shape of the feature. In all other respects, they are a table.
When you open a tool dialog box in ArcMap or enter a tool name in the Python window, the following occur:
- ArcGIS examines the tool's parameters to see which parameters use an input dataset or table. If one is found, it further examines the type of input dataset. Is it a point feature class? A TIN? A network? A table?
- The ArcMap table of contents is then scanned and filtered to see whether any layers match the parameter's dataset type. If so, a list is built of the matching layers and presented as a drop-down list in the parameter's control. You can choose one of these layers or table views as your input.
The following illustration shows the Buffer tool and the drop-down menu of layers it constructed for the Input Features parameter.
Some tools only accept layers or layer files as input or output. If this is the case with the tool you are using, the display name for the parameter will contain the word layer (for example, Input Feature Layer rather than Input Features).
Using a layer instead of browsing to the dataset on disk is natural and intuitive. But there's much more to using layers than as a simple shortcut to a dataset on disk.
- Layers and table views can contain a selection of features or rows, and geoprocessing tools will perform their operations only on the selected features or rows.
- Layers and table views don't have to come just from the ArcMap table of contents. You can create them with geoprocessing tools so that they can be used in ArcCatalog.
- Layers are short lived; you need to be aware of this when writing scripts and models, since the layer may not exist when the script or model is run at a later time.
The "Make Layer" tools—for example, Make Raster Layer—create a layer from the input data. This layer is temporarily stored in memory; it is not saved on disk and only remains available within the current session. If the session is closed, the layer is deleted. Layers created in ArcCatalog cannot be used in ArcMap, and vice versa. ArcCatalog does not display created layers, but they can be used as inputs to other geoprocessing tools in the session in which you are working.
A layer or table view contains a selection, sometimes called a selection set. A selection is a subset of the features in a feature class or a subset of rows in a table. There are a variety of ways you can select features in ArcMap, such as drawing a shape on the map display to select all features within the shape or using a logical expression. You can also select features and rows using geoprocessing tools found in the Data Management tools Layers and Table Views toolset. In ArcMap, you can see the number of features or rows by clicking the List By Selection button found at the top of the table of contents, as illustrated below.
Almost all geoprocessing tools operate only on the selected set of features or rows. For instance, the Clip tool only uses the currently selected features in both the input features and clip features datasets. The Summary Statistics tool calculates statistics only on the selection. In fact, it is rare that a tool does not operate on selected sets of features. If the tool does not use the selected set, the tool reference page explicitly states that it does not use the selected set.
Tools that modify the schema of a feature class or table, such as Add Field, do not use the selected set since it is impossible to add fields to one set of records and not another.
If the input to a tool is a time-enabled layer, only those features within the specified time period set by the Time Slider window will be processed. With time-enabled layers, you can think of the Time Slider as a way of selecting features for further processing.
You don't need ArcMap in order to create layers. You can use the Make Feature Layer tool found in the Layers and Table Views toolset to create an internal in-memory layer of a feature class in ArcCatalog.
When you create a layer or table view using geoprocessing tools, the new layer or table view is stored in an internal layer list, which is a different list from the ArcMap table of contents. This means that geoprocessing actually keeps two lists of layers and table views:
- The list of layers in the ArcMap table of contents
- The internal list of layers created by geoprocessing tools
Regardless of where the layer is kept, the layers will be shown to you in the same drop-down list in the tool dialog box.
If you execute Make Feature Layer in ArcMap, the layer can automatically be added to the ArcMap table of contents by enabling the Add results of geoprocessing operations to the display option.
Using layers in models and scripts
When you exit an ArcGIS for Desktop application, all layers are removed from memory. A common problem is creating a model or script that uses a layer as a variable—the model or script works fine in the current session but fails in subsequent sessions because the layer is deleted when the session ends. For example, a script might have the following piece of code:
landuse_layer = "Zoning"
In this example, the variable landuse_layer is "hard coded" to Zoning. Later in the script, the landuse_layer variable is used, and the script returns an error because there is no layer with the name Zoning in the current application. The script works fine if you execute it in ArcMap with the Zoning layer in the table of contents but fails when you execute the script in ArcCatalog, or anytime that layer doesn't exist.
A similar situation occurs in a model. You create a model where the Zoning layer is used, and the model fails because there is no layer by that name in the application.
In both situations, you can do one of the following:
- Make the layer name a tool parameter so the user can specify the layer name when executing your tool.
- Create the layer before running the tool.
- Create the layer within the model or script.
Using layer files
Layers can be saved to disk as a layer file (a file with the .lyr extension), then used as input to any tool that accepts a layer.
A layer file contains the same information as an internal layer (such as a layer in the ArcMap table of contents), including the selection of features.
To create a layer file, you can use the Save To Layer File tool. In ArcMap, you can right-click the layer in the table of contents and click Save As Layer File.
For any tool that accepts a layer, you can browse to a layer file (.lyr) on disk just as you would browse to a dataset on disk. However, when you browse to a layer file, some tools are unable to determine the type of data contained in the layer file and will show you all layer files. If you choose an inappropriate layer file, the tool will display an error or warning.
Duplicate layer names—Using group layers
In ArcMap, you are allowed to have multiple layers of the same name in the table of contents. When geoprocessing scans the table of contents to build the list of layers, it doesn't remove duplicate layer names, so the drop-down list of layers will contain all layers, regardless of whether the names are duplicated.
If you pick a layer with a duplicate name, you have no way of telling which layer will be used.