Available with Spatial Analyst license.
A Raster object references a raster dataset and, if used in a map display, may be associated with a raster layer in the table of contents. The relationships between the raster dataset, the Raster object, and the raster layer are maintained in most cases, but it is important to understand these relationships to work productively with Spatial Analyst map algebra.
Raster objects are created in the following ways:
- The output of a map algebra expression is a Raster object. One of the most important behaviors of a Raster object is that when it's created as primary output from a map algebra expression, it references a temporary raster.
>>> outRas = Slope("elevation") >>> print outRas(isTemporary) True
- A raster dataset can be cast as a Raster object. When a permanent raster dataset is cast as a Raster object, the dataset remains permanent.
>>> outRas = Raster("C:/Data/elevation") >>> print outRas(isTemporary) False
When working in the Python window, the result of a map algebra expression is added to the Contents display with the same name as the Raster object. When a dataset is cast as a raster, no layer is added to the table of contents.
Save a temporary raster
When data associated with the Raster object is temporary, it will be deleted when the ArcGIS session ends (the object goes out of scope or the script completes) unless the data is saved. When a raster is saved, the layer and object reflect the updated raster properties, including name, path, catalogPath, and the isTemporary status of the dataset. You can save a temporary dataset in the following ways:
- To save a temporary raster dataset through the associated Raster object, use the Raster object's save method.
- To persist a temporary raster dataset associated with a map layer, save the map project. When the project is saved, the raster dataset persists to disk at its current location with its automatically generated name.
Reuse a Raster object
Object names must be unique. When an object name is reused, the original object is overwritten. In the following example, the output of Slope is replaced when outRas is reused as output of the Aspect expression.
outRas = Slope("inRas1")
outRas = Aspect("inRas2")
Layers are added to the contents with the same name as the Raster object. When a Raster object name is reused, multiple layers with the same name will be added to the contents window. In the previous example, outRas is added twice. The first time is when the Slope expression is run. This first outRas layer references the result of Slope and can be used in the Python window until the Aspect expression is run. When Aspect is run, a second layer named outRas is added, the object is overwritten, and now the layer references the dataset and layer resulting from Aspect.
If the Raster object referencing a dataset has been overwritten, the raster dataset can be recast as a Raster object using the layer or dataset name.
Delete a Raster object
When a Raster object is deleted, what happens to the associated dataset (and possible layer) depends on the status of the data. If the data is permanent, deleting the Raster object does not affect the associated dataset or layer. If a dataset is temporary, the effects of deleting the Raster object depend on whether there is a layer also referencing the raster dataset. If no layer is associated with the temporary dataset and the object is deleted, the dataset is also deleted. If a layer is associated with the temporary dataset and the object is deleted, the temporary data remains.
Caution:Temporary data referenced by a layer is not deleted when the Raster object is deleted; however, all temporary data is deleted when the application is closed, unless it is saved, regardless of existing layers or objects.
The following example shows how to delete a Raster object named outRas:
outRas = Slope("C:/Data/elevation")
Assign the Raster object to a new Raster object
Assigning a Raster object to a new Raster object does not copy the associated dataset or layer. Assigning a Raster object to a new Raster object creates a second object that references the original object. In the following example, both outRas1 and outRas2 reference the same raster dataset:
outRas1 = Slope("elevation")
# Assigns Raster object to a new Raster object and save the raster dataset
outRas2 = outRas1
In this example, both outRas1 and outRas2 reference the same dataset. Therefore, when outRas2 is saved, both objects will reflect the permanent status, the new location, and the new name of the saved dataset, outslope. If outRas1 is a layer in the table of contents and you persist the data through the layer property or save the map document, both objects, outRas1 and outRas2, will reference the saved dataset.