RasterToNumPyArray

Summary

Converts a raster to a NumPy array.

Discussion

A Python NumPy array is designed to deal with large arrays. There are many existing Python functions that have been created to process NumPy arrays, the most noted being contained in the SciPy scientific computing package for Python. You may want to convert an ArcGIS raster to a NumPy array to

  1. Implement one of the many existing Python functions that can be applied to a NumPy array (for example, run filters on the data, perform multidimensional analysis, or utilize optimization routines).
  2. Develop a custom function by accessing the individual cells within the NumPy array (for example, to implement neighborhood notation, change individual cell values, or run accumulative operators on an entire raster).

If the array definition (the lower left corner and the number of rows and columns) exceeds the extent of the in_raster, the array values will be assigned NoData.

If the lower_left_corner does not coincide with the corner of a cell, it will automatically be snapped to the lower left of the nearest cell corner applying the same rules as the Snap Raster environment setting. This snapping action within the RasterToNumPy function is not to be confused with the Snap Raster environment setting; the function only uses the same interaction; see:

Syntax

RasterToNumPyArray (in_raster, {lower_left_corner}, {ncols}, {nrows}, {nodata_to_value})
ParameterExplanationData Type
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.

(The default value is origin of inRaster)

Point
ncols

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

(The default value is 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.

(The default value is 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.

None
Return Value
Data TypeExplanation
Array

The output NumPy array.

Code Sample

RasterToNumPy example

A raster is converted to a NumPy array to calculate the percentage of the cell value in the entire raster row. A new raster is then created.

import arcpy
import numpy 
myArray = arcpy.RasterToNumPyArray('C:/data/inRaster')
myArraySum = myArray.sum(1)
myArraySum.shape = (myArray.shape[0],1)
myArrayPerc = (myArray * 1.0)/ myArraySum
newRaster = arcpy.NumPyArrayToRaster(myArrayPerc)
newRaster.save("C:/output/fgdb.gdb/PercentRaster")


Related Topics


10/28/2011