TransWikia.com

Downsides to saving rasters with NaN as no data values?

Geographic Information Systems Asked on November 19, 2021

I am working with a bunch of satellites images downloaded from Google Earth Engine as .tifs. I have masked some of the pixels in Earth Engine with values of -999999 where there are clouds. Everytime I open them up in Python, I have to remove the -999999 values with something like

with rasterio.open(raster_file, 'r') as src:
    raster = src.read(1)
    raster[raster == -999999] = np.nan
    raster[raster== 0] = np.nan

I am wondering, can I just save the .tifs with np.nan instead of -999999, or could this cause some unforeseen problem down the road? I ask because I have never seen rasters with NaN for NoData values, usually it is some impossibly large number like -3.4028231e+38 or -999999. So perhaps there is some reason I am not seeing.

One Answer

Generally, it's a lot better to use NaN.

I can think of one reason not to, however. NaN only exists with floating-point data types. So if you need to write out or read a GeoTIFF with integer values (e.g. perhaps you have some final classification with five classes, represented as integers 1-5), then you cannot use NaN and save it out with a UInt8 datatype (for example). The same goes for using a Byte type for a binary classification: I do this a lot with modelling, since I don't do much actual image analysis—but then I usually don't need NaN here, since the ones represent pixels that meet the criteria, and the zeroes represent everything else, including pixels that were not considered by the model, i.e. were NaN. It's useful to use these datatypes since they make output datasets a lot smaller.

Fortunately you can still use an integer to represent NaN and then declare it to be a special value, with SetNoDataValue: Python-gdal write a GeoTiff with binary color and NaN

So the downside would be: if your raster is best represented with integers, don't write an output with floats (including NaN), but delcare a special integer value, e.g. 999, instead.

Answered by alphabetasoup on November 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP