The following code:
arcpy.AddMessage("shape of zOutput {} ".format(zOutput.shape)) arcpy.AddMessage("no of dimensions of zOutput {}".format(zOutput.ndim)) # Convert array to a geodatabase raster myRaster = arcpy.NumPyArrayToRaster(zOutput, arcpy.Point(xmin,ymin), cellsize, cellsize, -999999)
produces the following output. As you can see the numpy array IS two dimensional. Yet NumPyArrayToRaster fails.
shape of zOutput (375, 524)
no of dimensions of zOutput 2
...
ValueError: Argument in_array: A two or three dimensional NumPy array is required.
Is this a bug in NumPyToRaster? Is there some other way to export a numpy array to a raster?
No it works fine
import arcpy
zOutput = np.random.randint(0, 10, size=(10,10)) # ---- some data
zOutput # ---- a raster
array([[6, 1, 6, 2, 4, 9, 7, 6, 5, 4],
[5, 2, 4, 3, 0, 0, 0, 4, 7, 2],
[5, 3, 6, 0, 5, 5, 7, 0, 4, 9],
[1, 4, 6, 4, 8, 2, 8, 5, 3, 6],
[2, 5, 0, 1, 8, 6, 8, 6, 6, 1],
[9, 0, 2, 0, 9, 1, 2, 5, 3, 7],
[2, 9, 5, 9, 0, 0, 6, 0, 0, 3],
[7, 9, 8, 1, 6, 7, 3, 6, 7, 6],
[0, 7, 4, 8, 5, 6, 4, 8, 1, 4],
[4, 6, 7, 0, 9, 9, 3, 3, 6, 0]])
type(zOutput) # ---- the type, shape, whatever you need
numpy.ndarray
# ---- out it goes... then save it, preferably as a *.tif!!!
myRaster = arcpy.NumPyArrayToRaster(zOutput, arcpy.Point(0.0,0.0), 1.0, 1.0, -999999)
myRaster.save("c:/temp/a.tif")
# ---- close the loop as a test
back_in = arcpy.RasterToNumPyArray("c:/temp/a.tif")
type(back_in)
numpy.ndarray
back_in
array([[6, 1, 6, 2, 4, 9, 7, 6, 5, 4],
[5, 2, 4, 3, 0, 0, 0, 4, 7, 2],
[5, 3, 6, 0, 5, 5, 7, 0, 4, 9],
[1, 4, 6, 4, 8, 2, 8, 5, 3, 6],
[2, 5, 0, 1, 8, 6, 8, 6, 6, 1],
[9, 0, 2, 0, 9, 1, 2, 5, 3, 7],
[2, 9, 5, 9, 0, 0, 6, 0, 0, 3],
[7, 9, 8, 1, 6, 7, 3, 6, 7, 6],
[0, 7, 4, 8, 5, 6, 4, 8, 1, 4],
[4, 6, 7, 0, 9, 9, 3, 3, 6, 0]])
It turns out that if the numpy array is masked, the NumpyToRaster fails with the misleading error message about the dimensions of the array. So the solution is to fill the masked array before running the command.