Select to view content in your preferred language

Assistance understanding nodata values in raster to numpy array

898
1
07-11-2022 06:15 AM
WadeWall
Regular Contributor

Hi all,

I am trying to understand no data values in ArcGIS rasters and how to effectively and efficiently handle them when generating numpy arrays.

For example, I have an arcgis raster and the no data value is set to 3.4028235e+38. When I try to convert the raster to a numpy array, I use the following:

nodata = 3.4028235e+38
arr = arcpy.RasterToNumPyArray(img, nodata_to_value = nodata)

However, this does not result in the nodata values being nan in the new numpy array. I think this is because arcpy creates numpy arrays as float32, and the no data is outside that range. Is this correct?

If so, what is the most efficient way to handle no data values in arcgis when converting to a numpy array? I hope it is not reset the default nodata value.

Thanks for any information.

0 Kudos
1 Reply
DanPatterson
MVP Esteemed Contributor

an example of how you can check or accomplish what you want

z = np.arange(0, 9, dtype= np.float32).reshape(3,3)
z
array([[0., 1., 2.],
       [3., 4., 5.],
       [6., 7., 8.]], dtype=float32)

z0 = z.astype(np.float64)  # --- change dtype
nodata = 3.4028235e+38
z0[z0 > 6] = nodata        # --- assign nodata value to a few cells

z0  #  -- your array from RasterToNumPyArray
array([[0.0000000e+00, 1.0000000e+00, 2.0000000e+00],
       [3.0000000e+00, 4.0000000e+00, 5.0000000e+00],
       [6.0000000e+00, 3.4028235e+38, 3.4028235e+38]])

z0[z0 == nodata] = np.nan  # assign np.nan
z0
array([[ 0.,  1.,  2.],
       [ 3.,  4.,  5.],
       [ 6., nan, nan]])

z0.dtype
dtype('float64')

... sort of retired...
0 Kudos