NumPyArrayToRaster

Zusammenfassung

Converts a NumPy array to a raster.

Diskussion

The size and data type of the resulting raster dataset depends on the input array.

NumpyArrayToRaster supports the direct conversion of a 2D NumPy array to a single-band raster, or 3D NumPy array to a multiband raster.

NumpyArrayToRaster supports the direct conversion of a 3D NumPy array to a multidimensional raster dataset, or a 4D NumPy array to a multidimensional multiband raster dataset, by providing the multidimensional information.

  1. 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).
  2. 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.

Having the x_cell_size and the y_cell_size arguments allows support for rectangular cells.

Hinweis:

Rectangular cell sizes are not supported for the Esri Grid format.

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}, {mdinfo})
ParameterErläuterungDatentyp
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).

(Der Standardwert ist 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.

(Der Standardwert ist 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.

(Der Standardwert ist 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.

(Der Standardwert ist None)

Double
mdinfo

The string or JSON that defines the variables and dimensions within the multidimensional raster dataset, used to build multidimensional information. The string should use the following format:

{"variables":[{"name": <variable_name>, "dimensions": [{"name": <dimension_name>, "description": <dimension_description>, "unit": <dimension_unit>, "extent": [<dimension_min>, <dimension_max>], "values": [<dimension_values>,…]},…]}, …]}

(Der Standardwert ist None)

String
Rückgabewert
DatentypErläuterung
Raster

The output raster.

Codebeispiel

NumPyArrayToRaster example 1

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")
NumPyArrayToRaster example 2

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
# ----------------------------------------------------------------------------
NumPyArrayToRaster example 3

Convert a NumPyArray to a multidimensional raster.

import arcpy
import numpy

# Create multidimensional raster object from the NetCDF file
in_raster = Raster("myData.nc", True)
lowerLeft = arcpy.Point(in_raster.extent.XMin, in_raster.extent.YMin)
blockSize = 256

# Convert Raster to Array 
myRasterBlock = arcpy.NumPyArrayToRaster(in_raster, lowerLeft, blockSize, blockSize)

Verwandte Themen