The interaction of the Raster object in ArcGIS
A Raster object references a raster dataset and, if used in ArcMap, 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 two ways:
- The output of a Spatial Analyst Map Algebra expression is a Raster object. One of the most important behaviors of a Raster object is that when it is created as primary output from a Spatial Analyst 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
In some cases, a Raster object is associated with a raster layer. When working in the Python window, with the geoprocessing option Add result of the geoprocessing operations to the display checked on, the result of a Map Algebra expression is added to the table of contents 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 save a temporary dataset associated with a map layer, right-click the layer and select Data > Make Permanent and provide an output location and name.
- To persist a temporary raster dataset associated with a map layer, save the Map Document. When the Map Document 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, then the original object is overwritten. In the example below, 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 table of 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 table of contents. In the example above, outRas is added to the table of contents 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 to the table of contents, and the object will be overwritten and now 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 by using the layer or dataset name.
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, then the dataset is deleted. If there is a layer associated with the temporary dataset and the object is deleted, then the temporary data remains.
Below is an example of 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 example below, 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 the above 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, then both objects, outRas1 and outRas2, will reference the saved dataset.