RasterToNumPyArray (arcpy)

摘要

将栅格转换为 NumPy 数组。

讨论

Python NumPy 数组专用于处理大型数组。很多现有 Python 函数都是为了处理 NumPy 数组而创建的,NumPy 数组是包含在 Python 的 SciPy 科学计算包中的最著名数组。您可能想要将 ArcGIS 栅格数据转换为 NumPy 数组以

  1. 执行可以应用到 NumPy 数组上的许多现有 Python 函数中的一个(例如,对数据运行过滤器、执行多维分析或使用优化例程)。
  2. 通过访问 NumPy 数组中的各个像元来开发自定义函数(例如,执行邻域标记、更改各个像元值或者对整个栅格数据执行累积运算)。

如果数组的定义(左下角以及行数和列数)超出 in_raster 的范围,则数组值将分配为 NoData。

如果 lower_left_corner 与像元角不重合,则会自动捕捉到最近像元角的左下角,该捕捉方法采用的规则与“捕捉栅格”环境设置中的规则相同。RasterToNumPy 函数内的捕捉操作不会与“捕捉栅格”环境设置相混淆;该函数只使用相同的交互;请参见:

语法

RasterToNumPyArray (in_raster, {lower_left_corner}, {ncols}, {nrows}, {nodata_to_value})
参数说明数据类型
in_raster

The input raster to convert to a NumPy array.

Raster
lower_left_corner

The lower left corner within the in_raster from which to extract the processing block to convert to an array. The x- and y-values are in map units.

(默认值为 origin of inRaster)

Point
ncols

The number of columns from the lower_left_corner in the in_raster to convert to the NumPy array.

(默认值为 number of columns in inRaster)

Integer
nrows

The number of rows from the lower_left_corner in the in_raster to convert to the NumPy array.

(默认值为 number of rows in inRaster)

Integer
nodata_to_value

The value to assign the in_raster NoData values in the resulting NumPy array. The data type depends on the type of the in_raster.

If no value is specified, the NoData values in in_raster will be assigned the value associated with NoData in in_raster.

Variant
返回值
数据类型说明
NumPyArray

输出的 NumPy 数组。

代码实例

RasterToNumPy 示例

将栅格数据转换为 NumPy 数组旨在计算整个栅格行中的像元值的百分比。然后,将会创建一个新的栅格数据。

import arcpy
import numpy

my_array = arcpy.RasterToNumPyArray('C:/data/inRaster')
my_array_sum = my_array.sum(1)
my_array_sum.shape = (my_array.shape[0], 1)
my_array_perc = (my_array * 1.0) / my_array_sum
new_raster = arcpy.NumPyArrayToRaster(my_array_perc)
new_raster.save("C:/output/fgdb.gdb/PercentRaster")

相关主题

9/15/2013