3

I have a .tif raster file which contains integer values at 1, 2, 3, 4 and 5. Importing this file into ArcMap also shows a number of cells with Nodata value where the above classes are not present.

I was able to convert a tile from this .tif file into a numpy array using the Arcpy documentation. I basically ran the following code

point = arcpy.Point(XCoord, YCoord)
test = arcpy.RasterToNumpyArray(RasterFileName, point, 10,10)

Where XCoord and YCoord are specifically selected to be in an area where all the returned raster values should be Nodata (according to ArcMap). Instead, I get the following output when I print out test:

array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

Why are these not the values corresponding to ArcMap?. For another set of coordinates in the region where there is data I get the following:

array([[2, 3, 2, 1, 2, 2, 2, 1, 1, 2],
       [3, 3, 2, 1, 2, 2, 2, 1, 2, 2],
       [3, 2, 2, 2, 2, 2, 2, 2, 3, 4],
       [3, 2, 2, 3, 2, 3, 3, 3, 3, 4],
       [2, 2, 3, 3, 2, 2, 3, 2, 2, 2],
       [1, 3, 3, 3, 2, 2, 2, 1, 1, 2],
       [1, 2, 2, 2, 2, 2, 2, 2, 1, 2],
       [1, 2, 2, 2, 2, 2, 3, 3, 2, 2],
       [1, 1, 1, 1, 1, 2, 4, 4, 2, 2],
       [1, 1, 1, 1, 1, 2, 3, 3, 3, 3]])

This corresponds to the ArcMap information in that tile. Therefore I am sure that the raster is converting correctly.

How does ArcMap differentiate between NoDATA and 0 valued raster cells when in numpy NoDATA values are not allowed? Is the returned numpy array the actual data or is it simply a higher level representation of something else?

2

Below is some raster data (happens to be a flow direction grid) where white is NODATA. The Black point is the lower left corner and the black square is a 10 x 10 pixel extent such as yours

Sample data

If I run the following code:

point = arcpy.Point(441179,90512)
test = arcpy.RasterToNumPyArray("flwdir",point,10,10)

Then test is:

array([[  2,   2,   2,   4,   0,   0,   0,   0,   0,   0],
       [  2,   1,   1,   1,   0,   0,   0,   0,   0,   0],
       [  1,   1,   1,   1,   0,   0,   0,   0,   0,   0],
       [  1,   4,   1,   2,   0,  32, 128,   0,   0,   0],
       [  1,   2,   0,   0,   0,  16,   1,   0,   0,   0],
       [  1,   4,   1,   0,   0,  16,   1,   0,   0,   0],
       [  1,   4,   2,   0,  16,  32,   1,   0,   0,   1],
       [  4,   0,   0,   0,   4,   2,   4,   0,   2,   4],
       [  1,   0,   0,   2,   2,   2,   4,   0,   0,  16],
       [  2,   0,   2,   2,   2,   4,   8,   0,   0,   4]], dtype=uint8)

All the NODATA have been converted to zero.

If you read the help file for conversion function there is one last parameter you had not included nodata_to_value.

So if you run this code:

test = arcpy.RasterToNumPyArray("flwdir",point,10,10,-9)

the output is now:

array([[  2,   2,   2,   4,  -9,  -9,  -9,  -9,  -9,  -9],
       [  2,   1,   1,   1,  -9,  -9,  -9,  -9,  -9,  -9],
       [  1,   1,   1,   1,  -9,  -9,  -9,  -9,  -9,  -9],
       [  1,   4,   1,   2,  -9,  32, 128,  -9,  -9,  -9],
       [  1,   2,  -9,  -9,  -9,  16,   1,  -9,  -9,  -9],
       [  1,   4,   1,  -9,  -9,  16,   1,  -9,  -9,  -9],
       [  1,   4,   2,  -9,  16,  32,   1,  -9,  -9,   1],
       [  4,  -9,  -9,  -9,   4,   2,   4,  -9,   2,   4],
       [  1,  -9,  -9,   2,   2,   2,   4,  -9,  -9,  16],
       [  2,  -9,   2,   2,   2,   4,   8,  -9,  -9,   4]], dtype=int16)

So by declaring what NODATA should encode into you can distinguish between real zero values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.