Saving array as Geotiff using rasterio
Asked Answered
U

1

9

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.

Unlucky answered 6/11, 2019 at 8:31 Comment(0)
F
7

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

Footing answered 7/11, 2019 at 18:21 Comment(1)
Thanks for the tip. I was already investigating the np.transpose option but you answer definetly put me on the right wayUnlucky

© 2022 - 2024 — McMap. All rights reserved.