Summary
Adds a spatial index to a shapefile, file geodatabase, or enterprise geodatabase feature class. Use this tool to either add a spatial index to a shapefile or feature class that does not already have one or to re-create an existing spatial index.
Usage
ArcGIS uses spatial indexes to quickly locate features in feature classes. Identifying a feature, selecting features by pointing or dragging a box, and panning and zooming all require ArcMap to use the spatial index to locate features. The spatial index is defined by using a grid-based system that spans the extent of the features in a feature class, like a locator grid you might find on a common road map.
By default, ArcGIS creates and maintains a spatial index for geodatabase feature classes. For a geodatabase feature class to not have a spatial index, you must explicitly remove it using the Remove Spatial Index tool.
The Spatial Grid 1, 2, and 3 parameters only apply to file geodatabases and certain enterprise geodatabase feature classes (those that use SQL Server binary storage, DB2, Oracle binary storage, or Oracle ST_Geometry).
- If you are unfamiliar with setting spatial grid sizes or are unsure of what values to use, use the default values of 0,0,0. When default values are input, this tool will calculate an optimal grid size by examining all the input features.
- Shapefiles and certain enterprise geodatabase feature classes (those that use Oracle Spatial, Informix, and PostgreSQL) do not use spatial indexes based on grid sizes; therefore, no spatial grid values need to be specified.
- Personal geodatabase feature classes receive a spatial index grid size based on the spatial reference properties of the feature class. This index grid cannot be overridden, removed, or modified.
- Spatial Grid 1 is the first, or lowest, grid level and has the smallest cell size. The cells are square, and the parameter value you enter is the length of one side. The unit of measure is that of the spatial reference of the feature class (for example, meters). Spatial Grid 2 should be at least three times as large as Spatial Grid 1, and Spatial Grid 3 at least three times as large as Spatial Grid 2.
If the Input Features already have a spatial index, Spatial Grid 1, 2, 3 displays the current spatial index grid values. If you enter 0,0,0 for the spatial grid parameters and execute this tool, the optimal grid size is calculated, and the index rebuilt. You can then view the calculated grid size by reopening this tool and reentering the feature class or layer (you would then click Cancel, since you do not need to rebuild the index). You can also use the Calculate Default Spatial Grid Index tool to calculate optimal grid sizes.
Adding a new spatial index to an enterprise feature class is a server-intensive operation. It should not be done on large feature classes when a large number of users are logged in to the server.
For SQL Server and Oracle feature classes, if the index already exists, the index will be dropped and recreated. The bounding box will also be recalculated for SQL Server feature classes.
- For more information about spatial indexes, see the following:
Syntax
arcpy.management.AddSpatialIndex(in_features, {spatial_grid_1}, {spatial_grid_2}, {spatial_grid_3})
Parameter | Explanation | Data Type |
in_features | Enterprise feature class, file geodatabase feature class, or shapefile to which a spatial index is to be added or rebuilt. | Feature Layer; Mosaic Layer; Raster Catalog Layer |
spatial_grid_1 (Optional) | The Spatial Grid 1, 2, and 3 parameters apply only to file geodatabase and certain enterprise geodatabase feature classes. If you are unfamiliar with setting grid sizes, leave these options as 0,0,0, and ArcGIS will compute optimal sizes for you. | Double |
spatial_grid_2 (Optional) | Cell size of the second spatial grid. Leave the size at 0 if you only want one grid. Otherwise, set the size to at least three times larger than Spatial Grid 1. | Double |
spatial_grid_3 (Optional) | Cell size of the third spatial grid. Leave the size at 0 if you only want two grids. Otherwise, set the size to at least three times larger than Spatial Grid 2. | Double |
Code sample
AddSpatialIndex Example (Python Window)
The following Python Window script demonstrates how to use the AddSpatialIndex function in immediate mode.
import arcpy
import arcpy.env as ENV
ENV.workspace = "Database Connections/Connection to esoracle.sde"
arcpy.AddSpatialIndex_management("LPI.Land/LPI.PLSSFirstDivision", 500)
AddSpatialIndex Example 2 (stand-alone Python script)
The following Python script demonstrates how to use the AddSpatialIndex function in a stand-alone script.
# Name: AddSpatialIndex_Example2.py
# Description: Add a spatial index to a SDE feature class.
# Import system modules
import arcpy
import arcpy.env as ENV
# Set workspace
ENV.workspace = "Database Connections/Connection to esoracle.sde"
# Set local variables
in_features = "LPI.Land/LPI.PLSSFirstDivision"
spatial_grid_1 = "500"
spatial_grid_2 = ""
spatial_grid_3 = ""
# Execute AddSpatialIndex
arcpy.AddSpatialIndex_management(in_features, spatial_grid_1)
Environments
Licensing information
- Basic: Yes
- Standard: Yes
- Advanced: Yes