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 two 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.
Saving temporary rasters
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). That is, 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. There are several ways to save a temporary dataset.
- To save a temporary raster dataset through the associated Raster object, the Raster object's save method can be used.
- 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 autogenerated name.
Reusing 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 executed. This first outRas layer references the result of Slope and can be used in the Python window until the Aspect expression is executed. When Aspect is executed, a second layer called outRas is added, the object is overwritten, and now references the dataset and layer resulting from Aspect.
Deleting 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 has no bearing on the associated dataset or layer. If a dataset is temporary, the effects of deleting the Raster object depend on if there is a layer also referencing the raster dataset. If there is no layer associated with the temporary dataset and the object is deleted, the dataset is deleted. If there is a layer associated with the temporary dataset and the object is deleted, the temporary data remains.
The following example shows how to delete a Raster object called outRas:
outRas = Slope("C:/Data/elevation") del outRas
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 outRas2.save("C:/output/outslope")
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 your table of contents and you persist the data through the layer property or by saving your map document, both objects, outRas1 and outRas2, will reference the saved dataset.