Re: Change the Nodata value in a file.
toggle quoted message. . .
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:
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:
Currently this only changes tif header/metadata information and not the actual data.
bands = src.read(masked=True)
bands.fill_value = 255
kwargs = src.meta.copy()
with rasterio.open('copy.RGB.byte.tif', 'w', **kwargs) as dst:
Options I've seen to accomplish this include
# 1. masked arrays
Or should I be using rasterIO's read_mask(), write_mask() or dataset_mask()?
bands = np.ma.masked_values(src.read(masked=True), 255)
# 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)
# 3. numpy indexing
bands = src.read()
bands==0] = 255
What is the preferred method? I haven't been able to find this in any of the user documentation.
Join email@example.com to automatically receive all group messages.