I am trying to stack four bands into one TIFF, this is the code I'm using:
red_raster = rasterio.open(r"red.tif")
red = red_raster.read(1, masked=True)
nir_raster = rasterio.open(r"nir.tif")
nir = nir_raster.read(1, masked=True)
green_raster = rasterio.open(r"green.tif")
green = green_raster.read(1, masked=True)
blue_raster = rasterio.open(r"blue.tif")
blue = blue_raster.read(1, masked=True)
out_img = "stack.tif"
out_meta = red_raster.meta.copy()
out_meta.update({"count": 4,
"nodata": -10000})
stack = np.dstack((blue, green, red, nir))
with rasterio.open(out_img, "w", **out_meta) as dest:
dest.write(stack, 4)
I am getting the error:
ValueError: Source shape (1, 10980, 10980, 4) is inconsistent with given indexes 1
I tried following the steps here: Using Rasterio or GDAL to stack multiple bands without using subprocess commands
but I don't understand exactly what they are doing and am still getting errors.
rio stack red.tif nir.tif green.tif blue.tif -o stack.tif
. When you haverasterio
installed, you also haverio
.