Rasterio for Geotiff – How to Update Metadata

gdalgeotiff-tiffpythonrasterio

Is there a way to update metadata of an existing geotiff file? I'm particularly interested in setting the nodata value. The following does not appear to work

import rasterio
from pprint import pprint

path = '/path/to/file.tif'

with rasterio.open(path, 'r+') as src:
    src.meta['nodata'] = -32767
    pprint(src.meta)

Best Answer

The previous answers are misleading or wrong. To modify the nodata value of a GeoTIFF with Rasterio, do this

with rasterio.open(tiffname, 'r+') as dataset:
    dataset.nodata = -32767

The project has tests of this usage that you may see also: https://github.com/mapbox/rasterio/blob/master/tests/test_update.py#L59-L64. Note that you may have to close and reopen the file (as done in the test) to see the nodata value take effect in your program.

The meta property of a dataset is a copy of some of its important metadata. Modifying that object has no effect on the dataset.