GCGM
GCGM

Reputation: 1073

Saving array as Geotiff using rasterio

I have the following numpy array:

supervised.shape
(1270, 1847)

I am trying to use the following code to save it to GeoTIFF using rasterio:

with rasterio.open('/my/path/ReferenceRaster.tif') as src:
    ras_meta = src.profile

with rasterio.open('/my/output/path/output_supervised.tif', 'w', **ras_meta) as dst:
    dst.write(supervised)

Where ras_meta is:

{'driver': 'GTiff', 'dtype': 'float32', 'nodata': None, 'width': 1847, 'height': 1270, 'count': 1, 'crs': CRS.from_epsg(32736), 'transform': Affine(10.0, 0.0, 653847.1979372115,
       0.0, -10.0, 7807064.5603836905), 'tiled': False, 'interleave': 'band'}

I am facing the following error which I cannot understand since both the reference raster and my supervised array have the same shape

ValueError: Source shape (1270, 1847) is inconsistent with given indexes 1

Any idea what is the issue here? I am not completly understanding the meaning of the error.

Upvotes: 11

Views: 9492

Answers (1)

jdmcbr
jdmcbr

Reputation: 6406

write expects an array with shape (band, row, col). You can either reshape your array, or you can use write(supervised, indexes=1).

Upvotes: 8

Related Questions