19

This seems like it should be straightforward but I can't for the life of me figure it out.

Given an existing GeoTIFF containing a single band, how can you add another band to that GeoTIFF? Is the only way to do this to use GDALCreate() to create a new output file with an additional band and then copy the existing band from the original file to the new file? That's the only solution I've come up with so far but it just seems like there should be a simple way to add a band to the existing dataset.

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Jan 31, 2013 at 23:30
2
  • Hi @TheOx, I have a same problem. I want to make a composite image color from band 2,3,4 of LDCM single bands. Do you have solution for your question yet? Commented Jun 11, 2014 at 9:32
  • 1
    @Jackie see kyle's comment in the accepted answer about using the vrt driver to create a copy, add a band, then create a copy of that back to a geotiff with the GeoTiff driver. Commented Jun 11, 2014 at 13:50

3 Answers 3

13

To expand on Luke's answer and provide a concrete example in Python, here's a snippet that adds an alpha band to a source raster and saves it as a PNG.

from osgeo import gdal
src_ds = gdal.OpenShared(input_path)
mask_ds = gdal.OpenShared(mask_path)
mask = mask_ds.GetRasterBand(1).ReadAsArray()
tmp_ds = gdal.GetDriverByName('MEM').CreateCopy('', src_ds, 0)
tmp_ds.AddBand()
tmp_ds.GetRasterBand(4).WriteArray(mask)
dst_ds = gdal.GetDriverByName('PNG').CreateCopy(output_path, tmp_ds, 0)
del dst_ds

I used the MEM driver instead of VRT since the latter does not support WriteRaster() and WriteArray() (error "Writing through VRTSourcedRasterBand is not supported."). Using the vrt driver might still be possible through some other methods, I suppose.

answered Mar 17, 2016 at 15:56
10

The solution, if the driver supports it, is to call GDALOpen() with GA_Update access then use GDALAddBand or GDALDataset::AddBand. However, the geotiff driver doesn't support AddBand.

answered Feb 1, 2013 at 0:21
5
  • 4
    Very few drivers support add band, I don't believe the geotiff driver does. The only one I know that supports it is the vrt. Commented Feb 1, 2013 at 0:28
  • Well @kyle is right - the geotiff driver doesn't support AddBand, but the method in this answer is correct if the driver supports it and was what I was looking for so I'm accepting it. Commented Feb 1, 2013 at 0:49
  • Doh! Forgot about that little detail. @TheOx I don't think you should accept my answer as it is not correct re. the orig question which specifically related to geotiffs. Commented Feb 1, 2013 at 2:46
  • @Luke: Edit your answer to specify that's the general solution if the driver supports it and I'll accept it. There appears to be no other way to do this for GeoTiff short of the method in my original question. Commented Feb 1, 2013 at 14:53
  • 2
    Use the vrt driver, CreateCopy(), AddBand(), CreateCopy() with Geotiff Driver. Commented Feb 1, 2013 at 15:51
0

The Geotiff driver does not support the AddBand() method. To see this, get the last GDAL error just after the AddBand() line:

tmp_ds.AddBand();
String lastError = gdal.GetLastErrorMsg();
// lastError: "MyGeo.tif: Dataset does not support the AddBand() method."

Please note that my code is in Java but you already get the idea.

If you are bound to the Geotiff format then it seems creating the needed band/bands up front when initializing the Dataset is the way to go.

answered May 27 at 13:24

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.