I have been trying to break down my multi-band raster into individual bands with rasterio and I am getting this error:
ValueError: Source shape (11356, 9488) is inconsistent with given indexes 1
here's the code:
import os
import rasterio
src = rasterio.open(wv_tif)
for band in range(1, src.count):
single_band = src.read(band)
# get the output name
out_name = os.path.basename(wv_tif)
file, ext = os.path.splitext(out_name)
name = file + "_" + "B" + str(band) + ".tif"
out_img = os.path.join(folder, name)
print(out_img + " done")
# Copy the metadata
out_meta = src.meta.copy()
out_meta.update({"count": 1})
# save the clipped raster to disk
with rasterio.open(out_img, "w", **out_meta) as dest:
dest.write(single_band)
I have no idea what is going wrong
for band in range(1, src.count + 1):
or you'll miss the last band as therange(start, stop)
function only returns values >= start and < stop. E.g. if you have a 4 band rasterrange(1, 4)
will only have values[1, 2, 3]
.