[GIS] Removing some bands or layers from a tiff using gdal

gdallayersmulti-bandrastervector

I have been working on a multiband raster image. It is a pix image. I need to just work on the 26 bands that are raster files and not the 4 bitmaps that are are also included in the pix image. I could force the loop that will be going through it to only go through 25 times but that wont work when the pix images change and the number of bands change. Right now I am using gdal and have it all in a 3D array. I am pretty sure the bitmaps would be the last five layers of the array. I do not have anything to tackle this problem so I have no code.

Basically I just need to delete 4 layers from the pix image and work with 26 layers. When I call raster_image.rastercount, instead of it printing 30, I want it to print 26. I have gone through the gdal library, I may have missed something but I couldn't find anything. Is there a way to delete entire bands using gdal?

I have attached a screenshot of how the bands looks and what one of the bitmaps look like when displayed. Is there anyone that can help? Or just tell me it is impossible.

I am trying to use the GetGeomType just to see how it works and if it will work for me. But it does not seem to work:

    raster_image = gdal.Open(in_raster)
    band3 = raster_image.GetGeomType(3)
    print  band3

Above is how I am calling it but it gives me the error:

AttributeError: 'Dataset' object has no attribute 'GetGeomType'

Then trying it by first getting the raster band and then checking the geom type I get:

    band3 = raster_image.GetRasterBand(3).GetGeomType()
    print band3

AttributeError: 'Band' object has no attribute 'GetGeomType'

bands and bitmaps

Best Answer

You could create a GDALDataset with as many bands as you have raster bands, then copy the data from each of your bands into the corresponding band in the GDALDataset. Here's some example code in C++ (since that's where I'm most familiar with GDAL).

//create the dataset
const char *filename = "example.tif";
GDALDriver *pDriverTiff = GetGDALDriverManager->getDriverByName("GTiff");
GDALDataset *pRasterDS = pDriverTiff->create(filename, 300, 300, 25, GDT_Float32, NULL);//for create(filename, x size, ysize, number of bands, data type, options)

//fill each raster band
int bands = 25; //this would be the number of bands you want to copy i.e. total bands-bitmaps
int rows = 300; //rows in the raster or 3d array
int cols = 300; //cols in the raster or 3d array

for (int i=0; i<band; i++)
{
    for (int j=0; j<rows; j++)
    {
         for (int k=0; k<cols; k++)
         {
              //here is where you put the code to copy from your 3d array
              // copy array position [i,j,k] to band i, row j, col k, in the GDALDataset
         }

    }   
}

There might be a more efficient way to do this that I am not aware of, but I have used this method many times to add and delete bands from raster datasets.