Summary
Converts a NumPy array to a raster.
Discussion
The size and data type of the resulting raster dataset depends on the input array.
Having the x_cell_size and the y_cell_size arguments allows support for rectangular cells.
Note:
Rectangular cell sizes are not supported for the Esri Grid format.
NumpyArrayToRaster supports the direct conversion of multidimensional NumPy arrays to a multiband raster.
- If the input array has two dimensions, it returns a single-band raster, where the size of the raster is defined by the dimensions (rows, columns).
- If the input array has three dimensions, it returns a multiband raster, where the number of bands equals the length of the first dimension and the size of the raster is defined by the second and third dimensions (bands, rows, columns).
- If the input array has three dimensions, and the first dimension has size 1, it returns a single-band raster.
This function honors the following geoprocessing environment settings:
Output Coordinate System, Extent, Snap Raster, Current Workspace, Scratch Workspace
Syntax
NumPyArrayToRaster (in_array, {lower_left_corner}, {x_cell_size}, {y_cell_size}, {value_to_nodata})
Parameter | Explanation | Data Type |
in_array | The NumPy array to convert to a raster. A two- or three-dimensional NumPy array is required. | NumPyArray |
lower_left_corner | A Point object defining the lower left corner of the output raster in map units. The default will set the lower left corner to coordinate (0.0, 0.0). (The default value is None) | Point |
x_cell_size | The cell size in the x direction specified in map units. The input can be a specified cell size (type: double) value, or a raster. When a dataset is specified, the x cell size of that dataset is used for the x cell size of the output raster. If only the x_cell_size is identified and not the y_cell_size, a square cell will result with the specified size. If neither x_cell_size nor y_cell_size is specified, a default of 1.0 will be used for both the x and y cell size. (The default value is 1.0) | Double |
y_cell_size | The cell size in the y direction specified in map units. The input can be a specified cell size (type: double) value, or a raster. When a dataset is specified, the x cell size of that dataset is used for the y cell size of the output raster. If only the y_cell_size is identified and not the x_cell_size, a square cell will result with the specified size. If neither x_cell_size nor y_cell_size is specified, a default of 1.0 will be used for both the x and y cell size. (The default value is 1.0) | Double |
value_to_nodata | The value in the NumPy array to assign to NoData in the output raster. If no value is specified for value_to_nodata, there will not be any NoData values in the resulting raster. (The default value is None) | Double |
Data Type | Explanation |
Raster | The output raster. |
Code sample
A new raster is created from a randomly generated NumPy array.
import arcpy
import numpy
# Create a simple array from scratch using random values
myArray = numpy.random.random_integers(0,100,2500)
myArray.shape = (50,50)
# Convert array to a geodatabase raster
myRaster = arcpy.NumPyArrayToRaster(myArray,x_cell_size=1)
myRaster.save("C:/output/fgdb.gdb/myRandomRaster")
Block process an input multiband raster and calculate cell statistics across the bands. This script converts a multiband raster to a three-dimensional NumPy array and processes the array by dividing it into data blocks. It then calculates the mean of values across the rows of the block, converts the block numpy array to raster, and recombines the bands via mosaicking. A new multiband raster is created.
# Note that, if the input raster is multiband, the data blocks will also be
# multiband, having dimensions (bands, rows, columns). Otherwise, they will
# have dimensions (rows, columns).
import arcpy
import numpy
import os
# Input raster
filein = os.path.join(os.getcwd(),r"input\input.tif")
# Output raster (after processing)
fileout = os.path.join(os.getcwd(),r"output\blockprocessingrdb22.tif")
# Size of processing data block
# where memorysize = datatypeinbytes*nobands*blocksize^2
blocksize = 512
# ----------------------------------------------------------------------------
# Create raster object from file
myRaster = arcpy.Raster(filein)
# Set environmental variables for output
arcpy.env.overwriteOutput = True
arcpy.env.outputCoordinateSystem = filein
arcpy.env.cellSize = filein
# Loop over data blocks
filelist = []
blockno = 0
for x in range(0, myRaster.width, blocksize):
for y in range(0, myRaster.height, blocksize):
# Lower left coordinate of block (in map units)
mx = myRaster.extent.XMin + x * myRaster.meanCellWidth
my = myRaster.extent.YMin + y * myRaster.meanCellHeight
# Upper right coordinate of block (in cells)
lx = min([x + blocksize, myRaster.width])
ly = min([y + blocksize, myRaster.height])
# noting that (x, y) is the lower left coordinate (in cells)
# Extract data block
myData = arcpy.RasterToNumPyArray(myRaster, arcpy.Point(mx, my),
lx-x, ly-y)
# PROCESS DATA BLOCK -----------------------------
# e.g. Calculate mean of each cell of all bands.
myData -= numpy.mean(myData, axis=0, keepdims=True)
# ------------------------------------------------
# Convert data block back to raster
myRasterBlock = arcpy.NumPyArrayToRaster(myData, arcpy.Point(mx, my),
myRaster.meanCellWidth,
myRaster.meanCellHeight)
# Save on disk temporarily as 'filename_#.ext'
filetemp = ('_%i.' % blockno).join(fileout.rsplit('.',1))
myRasterBlock.save(filetemp)
# Maintain a list of saved temporary files
filelist.append(filetemp)
blockno += 1
# Mosaic temporary files
arcpy.Mosaic_management(';'.join(filelist[1:]), filelist[0])
if arcpy.Exists(fileout):
arcpy.Delete_management(fileout)
arcpy.Rename_management(filelist[0], fileout)
# Remove temporary files
for fileitem in filelist:
if arcpy.Exists(fileitem):
arcpy.Delete_management(fileitem)
# Release raster objects from memory
del myRasterBlock
del myRaster
# ----------------------------------------------------------------------------