Topics

Change the Nodata value in a file.

Jonas
 

The `r+` trick is very neat for changing the metadata in-place.

But for actually changing the data values to match the new metadata, both your suggestions are fine - either used masked arrays or do the masking yourself. There is no smarter method and depending on your data type, you have to take care to get the masking right (or rely on `read(..., masked=True)` for it), e.g. for floating point comparison or NaN.

Btw, this question is ideal for Stack Overflow... -- in this case you could post and accept your own answer, though. :-D

Sean Gillies
 

Hi,

I'm sorry this isn't more obviously documented. You can change the nodata value of an existing dataset by opening it in r+ mode and setting the attribute.

with rasterio.open("example.tif", "r+") as dataset:
    dataset.nodata = 255

Same goes for the crs and transform attributes. Hope this helps!


On Wed, Dec 19, 2018 at 6:58 AM <christina.ratcliff@...> wrote:
Hi Everyone,

Using RasterIO, what is the correct way to change (or assign if it isn't already set) the no data value for a single or multi band TIF file?

For example using the test rgb.byte.tif file, how do I change the nodata value from 0 to 255?  So far I have tried:
with rasterio.open('RGB.byte.tif') as src:  
bands = src.read(masked=True)
bands.fill_value = 255
# or
# bands.set_fill_value(255)

kwargs = src.meta.copy()
kwargs.update({'nodata': 255})

with rasterio.open('copy.RGB.byte.tif', 'w', **kwargs) as dst:
dst.write(bands)
Currently this only changes tif header/metadata information and not the actual data.

Options I've seen to accomplish this include
# 1. masked arrays
bands = np.ma.masked_values(src.read(masked=True), 255)
dst.write(bands)

# 2. loop through bands and change value
for i in range(1, src.count + 1):
band = src.read(i)
band = np.where(band==0,255,band)
dst.write(bands,i)

# 3. numpy indexing
bands = src.read()
bands[bands==0] = 255
Or should I be using rasterIO's read_mask(), write_mask() or dataset_mask()?

What is the preferred method? I haven't been able to find this in any of the user documentation.

Thanks,



--
Sean Gillies

@cratcliff
 

Hi Everyone,

Using RasterIO, what is the correct way to change (or assign if it isn't already set) the no data value for a single or multi band TIF file?

For example using the test rgb.byte.tif file, how do I change the nodata value from 0 to 255?  So far I have tried:
with rasterio.open('RGB.byte.tif') as src:  
bands = src.read(masked=True)
bands.fill_value = 255
# or
# bands.set_fill_value(255)

kwargs = src.meta.copy()
kwargs.update({'nodata': 255})

with rasterio.open('copy.RGB.byte.tif', 'w', **kwargs) as dst:
dst.write(bands)
Currently this only changes tif header/metadata information and not the actual data.

Options I've seen to accomplish this include
# 1. masked arrays
bands = np.ma.masked_values(src.read(masked=True), 255)
dst.write(bands)

# 2. loop through bands and change value
for i in range(1, src.count + 1):
band = src.read(i)
band = np.where(band==0,255,band)
dst.write(bands,i)

# 3. numpy indexing
bands = src.read()
bands[bands==0] = 255
Or should I be using rasterIO's read_mask(), write_mask() or dataset_mask()?

What is the preferred method? I haven't been able to find this in any of the user documentation.

Thanks,