1

I've been trying to use rasterio.merge.merge() but it seems I have a nodata issue forbidding it (Rasterio.merge.merge() does not work): there is no value defined as nodata for my rasters (they all have the same metadata). I would like to change it to nan in format float32. How could I do this, on Python ?

The metadata of the raster is:

print('Raster 1: \n', raster1.profile)
Raster 1: 
 {'driver': 'GTiff', 'dtype': 'float32', 'nodata': None, 'width': 3532, 'height': 3537, 'count': 16, 'crs': CRS.from_epsg(32607), 'transform': Affine(30.0, 0.0, 484200.0,
       0.0, -30.0, 6719670.0), 'blockxsize': 256, 'blockysize': 256, 'tiled': True, 'compress': 'lzw', 'interleave': 'pixel'}

And the format of one nan cell is: enter image description here

1
  • Try opening the dataset with rasterio.open(path, nodata=np.nan)
    – mikewatt
    Mar 31, 2022 at 19:31

1 Answer 1

1

Open the dataset in append mode "r+" and then set dataset.nodata (source):

with rio.open(src_filename, "r+") as src:
       src.nodata = np.nan

For example:

import numpy as np
import rasterio as rio

src_filename = '/path/to/some.tif'

with rio.open(src_filename) as src:
       print(src.read(masked=True))
       print(src.nodata)

with rio.open(src_filename, "r+") as src:
       src.nodata = np.nan
       print(src.read(masked=True))
       print(src.nodata)

Output:

[[[nan nan  3. ...  4. nan nan]
  [nan  3. nan ... nan  5. nan]
  [ 3. nan  4. ... nan nan  4.]
  ...
  [ 6. nan nan ...  5.  3.  3.]
  [ 4. nan  6. ...  6.  5.  5.]
  [nan  4. nan ...  6.  4.  3.]]]
None
[[[-- -- 3.0 ... 4.0 -- --]
  [-- 3.0 -- ... -- 5.0 --]
  [3.0 -- 4.0 ... -- -- 4.0]
  ...
  [6.0 -- -- ... 5.0 3.0 3.0]
  [4.0 -- 6.0 ... 6.0 5.0 5.0]
  [-- 4.0 -- ... 6.0 4.0 3.0]]]
nan
1
  • Thank you, it works wonders !!
    – vdc
    Mar 31, 2022 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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