1

I have a raster, input.tif. It has 4 layers, and uint8 datatype, as shown in the metadata:

import rasterio

with rasterio.open('input.tif') as src: print(src.meta)

Output:

{'driver': 'GTiff', 'dtype': 'uint8', 'nodata': None, 'width': 16540, 'height': 16170, 'count': 4, 'crs': CRS.from_epsg(3006), 'transform': Affine(10.000046118500606, 0.0, 316265.1384,
       0.0, -10.000256301793423, 6271819.2409)}

I would like to take the first band, convert it to uint64, and write the result to output.tif. I do, using NumPy:

with rasterio.open('input.tif') as src:
band1 = src.read(1)
resband = np.uint64(band1)

m = src.meta
m['count'] = 1
m['dtype'] = 'uint64'

with rasterio.open('output.tif','w',**m) as dst:
    dst.write(resband, 1)

However, I get:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [233], in <cell line: 1>()
      7 m['count'] = 1
      8 m['dtype'] = 'uint64'
---> 10 with rasterio.open('output.tif','w',**m) as dst:
     11     dst.write(resband, 1)

File /opt/homebrew/lib/python3.9/site-packages/rasterio/env.py:437, in ensure_env_with_credentials.<locals>.wrapper(args, kwds) 434 session = DummySession() 436 with env_ctor(session=session): --> 437 return f(args, **kwds)

File /opt/homebrew/lib/python3.9/site-packages/rasterio/init.py:163, in open(fp, mode, driver, width, height, count, crs, transform, dtype, nodata, sharing, **kwargs) 161 raise TypeError("invalid driver: {0!r}".format(driver)) 162 if dtype and not check_dtype(dtype): --> 163 raise TypeError("invalid dtype: {0!r}".format(dtype)) 164 if nodata is not None: 165 nodata = float(nodata)

TypeError: invalid dtype: 'uint64'

How can I fix this error?


This question: How to change band type? is similar but does not use rasterio.

Taras
  • 32,823
  • 4
  • 66
  • 137
zabop
  • 2,050
  • 7
  • 35
  • 2
    Try uint32. According to the source code there is no uint64 https://github.com/rasterio/rasterio/blob/master/rasterio/dtypes.py#L21 – Aman Bagrecha Jun 13 '22 at 08:42

1 Answers1

4

According to the rasterio source code for dtypes, valid dtypes does not include uint64. Instead use uint32

Aman Bagrecha
  • 1,055
  • 5
  • 14