Raster (arcpy)
Resumen
Creates a raster object that can be used in Python scripting or in a Map Algebra expression. A raster object is a variable which references a raster dataset.
A raster object can be created in two ways. You can create a raster object by supplying the path to an existing raster on disk, or it can be the result of any Map Algebra statement that results in a raster output.
An Extensión ArcGIS Spatial Analyst is necessary to execute Map Algebra statements.
Debate
The raster object created from existing data can be used in subsequent Map Algebra expressions and will have all the associated raster properties and methods.
# out_raster is a resultant raster object
out_raster = Raster("c:/data/inraster")
Any Spatial Analyst tool or operator (see Working with operators) that produces an output raster to the left of the equal sign creates a raster object. For example, in the following expression, out_raster is a raster object.
out_raster = Slope("inelevation")
When a raster object is returned from a Map Algebra expression, by default, the object (the variable and associated dataset) is temporary.
The temporary dataset associated with a raster object can become permanent with the following mechanisms:
- By calling the save method on the raster object.
- Call Make Permanent by right-clicking on the layer with an associated raster object in the TOC and specifying an output name.
- Saving the map project (.MXD, .3DD, and so on) that contains any layers with associated raster objects.
In case 3, the dataset referenced by the raster object will automatically be permanently saved to disk with a default name.
If the referenced raster is not made permanent by any of the three cases above, the variable and the referenced raster dataset will be deleted when the variable goes out of scope, such as when a stand-alone script is completed or ArcGIS is closed. When a raster object references permanent data on disk, the data is not deleted.
Certain operators exist in both Map Algebra and in Python. If you want an operator to work on rasters (as opposed to scalars) the input rasters must be cast as a raster object by calling the Raster class constructor: Raster("inRaster").
# The Spatial Analyst plus operator is used on the input rasters creating
# an output raster object
out_raster = Raster("input1") + Raster("input2")
# The Python plus operator is used on numbers creating a scalar variable
out_var = 4 + 7
# When there is a mix of rasters with numbers the Spatial Analyst
# operator is used creating an output raster object
out_raster = Raster("input") + 10
Certain properties associated with the raster object are only available if the referenced raster dataset is permanent. When the referenced raster dataset is temporary, these properties will be assigned None. The affected properties are catalogPath, compressionType, format, hasRAT, name, and path.
Once permanent, the referenced raster dataset cannot return to the temporary state.
Sintaxis
Parámetro | Explicación | Tipo de datos |
inRaster |
The input raster dataset. | Raster |
Propiedades
Propiedad | Explicación | Tipo de datos |
bandCount (Sólo lectura) |
The number of bands in the referenced raster dataset. | Integer |
catalogPath (Sólo lectura) |
The full path and the name of the referenced raster dataset. | String |
compressionType (Sólo lectura) |
The compression type. The following are the available types:
| String |
extent (Sólo lectura) |
The extent of the referenced raster dataset. | Extent |
format (Sólo lectura) |
The raster format
| String |
hasRAT (Lectura y escritura) |
Identifies if there is an associated attribute table: True if an attribute table exists or False if no attribute table exists. | Boolean |
height (Sólo lectura) |
The number of rows. | Integer |
isInteger (Sólo lectura) |
The integer state: True if the raster dataset has integer type. | Boolean |
isTemporary (Sólo lectura) |
The state of the referenced raster dataset: True if the raster dataset is temporary or False if permanent. | Boolean |
maximum (Sólo lectura) |
The maximum value in the referenced raster dataset. | Double |
mean (Sólo lectura) |
The mean value in the referenced raster dataset. | Double |
meanCellHeight (Sólo lectura) |
The cell size in the y direction. | Double |
meanCellWidth (Sólo lectura) |
The cell size in the x direction. | Double |
minimum (Sólo lectura) |
The minimum value in the referenced raster dataset. | Double |
name (Sólo lectura) |
The name of the referenced raster dataset. | String |
noDataValue (Sólo lectura) |
The NoData value of the referenced raster dataset. | Double |
path (Sólo lectura) |
The full path and name of the referenced raster dataset. | String |
pixelType (Sólo lectura) |
The pixel type of the referenced raster dataset. The types are
| String |
spatialReference (Sólo lectura) |
The spatial reference of the referenced raster dataset. | SpatialReference |
standardDeviation (Sólo lectura) |
The standard deviation of the values in the referenced raster dataset. | Double |
uncompressedSize (Sólo lectura) |
The size of the referenced raster dataset on disk. | Double |
width (Sólo lectura) |
The number of columns. | Integer |
Descripción general de los métodos
Método | Explicación |
save ({name}) |
Permanently saves the dataset the raster object references. |
Métodos
Parámetro | Explicación | Tipo de datos |
name |
The name to assign to the raster dataset on disk. | String |
Ejemplo de código
Creates a Raster object from a raster dataset and gets properties for analysis.
import arcpy
my_raster = arcpy.Raster('elevation')
my_min = my_raster.minimum
my_max = my_raster.maximum
my_area = (my_raster.width * my_raster.height) * my_raster.meanCellWidth
Creates a Raster object, gets properties, creates a random error raster (+/- 3 feet), adds it to an elevation raster, and converts its units from feet to meters.
import arcpy
from arcpy.sa import *
elev_raster = Raster('c:/data/elevation')
my_extent = elev_raster.extent
my_cellsize = (elev_raster.meanCellHeight + elev_raster.meanCellWidth) / 2
res01 = arcpy.CreateRandomRaster_management("", "error3", "UNIFORM 0.0 3.0",
my_extent, my_cellsize)
elev_meters = (elev_raster + Raster(res01)) * 0.3048
elev_meters.save("c:/output/fgdb.gdb/elevM_err")