NumPyArrayToRaster (arcpy)
摘要
将 NumPy 数组转换为栅格。
讨论
所生成的栅格数据集的大小和数据类型取决于输入数组。
拥有 x_cell_size 和 y_cell_size 参数可支持矩形像元。
不支持 Esri Grid 格式的矩形像元大小。
NumpyArrayToRaster 支持将多维 NumPy 数组直接转换为多波段栅格。
- 如果输入数组具有两个维度,则会返回单波段栅格,其栅格大小由这两个维度(行、列)定义。
- 如果输入数组具有三个维度,则返回多波段栅格,其波段数等于第一维的长度,而栅格大小由第二维和第三维(波段、行、列)定义。
- 如果输入数组具有三个维度并且第一维的大小为 1,则会返回单波段栅格。
此函数支持以下地理处理环境设置:
语法
参数 | 说明 | 数据类型 |
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). (默认值为 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. (默认值为 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. (默认值为 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. (默认值为 None) | Double |
数据类型 | 说明 |
Raster |
输出栅格。 |
代码实例
从随机生成的 NumPy 数组创建一个新栅格。
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")
块将对输入多波段栅格进行处理并计算所有波段的像元统计数据。该脚本将多波段栅格转换为三维 NumPy 数组,并通过将该数组划分为数据块的方式对其进行处理。接下来,该脚本将计算块中所有行的平均值,将块 numpy 数组转换成栅格,并通过镶嵌重新组合波段。已创建新的多波段栅格。
# 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
# ----------------------------------------------------------------------------