NumPyArrayToRaster results in NoData values.

963
5
12-09-2018 05:26 PM
MoritzS_
New Contributor

Hello, I have followed this example on the documentation:

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")

This code creates a raster file in my geodatabase, but the rasterfile appears to be empty. When I add the raster to ArcMap, the raster is invisible. Only if I check to "Display NoData values as" a color, I can see the raster. It seems like the raster is full of NoData.

How can I get a simple random NumPy array to correctly show up in a map document?

Thanks in advance!

0 Kudos
5 Replies
DanPatterson_Retired
MVP Esteemed Contributor
a = np.random.randint(0, 100, 2500).reshape(50, 50)
a.save("c:/temp/myarray.tif")

NumPyArrayToRaster—ArcPy Functions | ArcGIS Desktop 

random.random_integer is being deprecated so you should have gotten a warning unless you are using an older version of numpy.

Now as for the saving, don't save it into a geodatabase and expect it to behave unless it is within the expected extent of the gdb and has the same coordinate system.  Make a tif and you need to specify the lower-left corner of the array and an appropriate cell size if you expect to see the raster at all.

Add it to a separate dataframe/map without one of those basemap things which will set the projection to web Mercator.

Numpy plays nice with Arc* but you can't skip a proper spatial reference and extent delineation via LL corner and cell size, followed by a Define Projection to solidify its coordinate system.

MoritzS_
New Contributor

Hello Dan,

thanks for your assistance!

I have tried your suggestion, but with no luck. First, I can't seem to directly save a NumPy array as a tif, using  a.save().

Saving the tiff outside of my geodatabase after I converted it to a raster creates a tiff file that is solid black when previewed in the windows explorer.

But when I add the raster to ArcMap, I have the same problem as initially: the raster is invisible, and appears to have only NoData values. When I set the symbology to display NoData as a certain color, the whole raster will have this color.

I don't think it is a problem with the extent or projection, because  NumPyArrayToRaster should take care of those and I keep my settings consistent. After all I was already able to see the raster at the right place with the right size, but only after I displayed the NoData values as a certain color.

What I noticed is that the high and low values seem to be off in the symbology of the raster...

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Don't know what your setup issue is then, because it works as I describe.

MoritzS_
New Contributor

Hello Dan,

thanks to your help I found out what the problem was. Turns out it had to do with the extent after all. I had the processing extent in my script set to that of a feature. But I did not specify the lower_left_corner parameter when I converted the array. This resulted in a lower left corner of 0,0. This pont lied outside the actual extent, resulting in an empty raster.

Adding the origin point using the XMin and YMin values of the extent solved the problem:

arcpy.NumPyArrayToRaster(myArray, arcpy.Point(arcpy.env.extent.XMin, arcpy.ent.extent.YMin))

Thanks for your help!

Greetings!

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

glad it worked out

0 Kudos