I'm trying to create a 3 band RGB image from Sentinel 2 bands but I seem to be trying to write an array of inconsistent shape:
For example:
ValueError: Source shape (1, 10980, 10980, 3) is inconsistent with given indexes 1
I've tried reshaping it but I still get errors.
import numpy as np
import rasterio, os
folder = r"/home/bera/data/Sentinel2/S2B_MSIL2A_20200625T101559_N0214_R065_T33VWD_20200625T134347.SAFE/GRANULE/L2A_T33VWD_A017251_20200625T101903/IMG_DATA/R10m/"
bluefile = os.path.join(folder, "T33VWD_20200625T101559_B02_10m.jp2")
greenfile = os.path.join(folder, "T33VWD_20200625T101559_B03_10m.jp2")
redfile = os.path.join(folder, "T33VWD_20200625T101559_B04_10m.jp2")
#Read as datasets
dsblue = rasterio.open(bluefile)
dsgreen = rasterio.open(greenfile)
dsred = rasterio.open(redfile)
#And to arrays
blue = dsblue.read(1)
green = dsgreen.read(1)
red = dsred.read(1)
#Composite
rgb = np.dstack((red, green, blue))
print(rgb.shape)
#(10980, 10980, 3)
def writetofile(output, array):
with rasterio.open(fp=outfile, mode="w", driver="GTiff",
width=rgb.shape[1], height=rgb.shape[0],
count=3, crs=dsblue.crs,
transform=dsblue.transform, dtype=rgb.dtype) as dst:
dst.write(array, 3)
outfile = r"/home/bera/Desktop/GIStest/rgbtest.tif"
writetofile(output=outfile, array=rgb)
#ValueError: Source shape (1, 10980, 10980, 3) is inconsistent with given indexes 1
from rasterio.plot import reshape_as_raster, reshape_as_image
raster = reshape_as_raster(rgb)
print(raster.shape)
#(3, 10980, 10980)
writetofile(output=outfile, array=raster)
#ValueError: Source shape (1, 3, 10980, 10980) is inconsistent with given indexes 1
image = reshape_as_image(rgb)
print(image.shape)
#(10980, 3, 10980)
writetofile(output=outfile, array=image)
#ValueError: Source shape (1, 10980, 3, 10980) is inconsistent with given indexes 1
dst.write(array)
ordst.write(array, [1,2,3])
(or 0,1,2?) instead?Source shape error
. Thanks anyway