1

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)
5

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.

  • It depends on what really was the question. If it was "Is there a way to update metadata of an existing geotiff file?" like in the question body the gdal_edit answer is not misleading. If one reads only the title that says "with rasterio" and forgets the gdal tag then gdal_edit if off topic and downvote is appropriate. – user30184 Sep 12 '17 at 19:45
1

I don't know how to do it right with rasterio but the question includes "gdal" tag so it seems that also GDAL based answers are acceptable. In that case this GDAL tool is made for you http://www.gdal.org/gdal_edit.html.

DESCRIPTION

The gdal_edit.py script can be used to edit in place various information of an existing GDAL dataset (projection, geotransform, nodata, metadata).

The script from GDAL trunk can be found at https://trac.osgeo.org/gdal/browser/trunk/gdal/swig/python/scripts/gdal_edit.py.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.