7

I created a layer stack of 20 images (all GTiff-files) using the gdal_merge tool in QGIS, available at:

raster > miscellaneous > merge 

The result is a GTiff layer stack of the 20 images, but the band names are band1, band2, band3...band20, looking at the properties.

Is there a way to rename the band-names, either afterwards, or during creation of the stack?

They are all from different sensors/dates and will be more meaningful to work with other names than band1,band2,band3.

Thanks!

2 Answers 2

11

I also needed to know how to rename bands in an open source environment so spent a whole day looking for answers.

There is no way to name bands in QGIS while merging.

But it can be done after the file is created, by editing their .aux.xml file. It works for both .tif and .img files, as far as I've researched.

The solution is to include after each <PAMRasterBand band="1">, <PAMRasterBand band="2">, etc, a subelement <Description>YourBandName</Description>. I haven't tested it for VRT datasets, but the link at the end suggests that it should be the same procedure.

In ArcMap, you will be able to see the new names in the Properties > Symbology window, when setting your colour composite, also in Table of Contents, under your image layer.

In QGIS, I suspect there is a bug, and this will not work. However, you can still label your bands in a more indirect way by also including after the element the subelement:

<Metadata> <MDI key="Band">YourBandName</MDI> </Metadata> 

This will show up in the Properties > Metadata tab, for each individual band.

Example image attached, only first band was named for demonstration. Checked on ArcMap on Windows and QGIS in Ubuntu.

Sources: http://lists.osgeo.org/pipermail/gdal-dev/2013-October/037321.html

enter image description here

1
  • This looks like a new question rather than an answer. Can you please ask a new question and reference this thread for background information. Commented Sep 30, 2015 at 23:03
0

from the QGIS Python shell:

from qgis.core import QgsProject, QgsMapLayer from osgeo import gdal # Nomi nuovi per le bande new_band_names = ['BandName1', 'BandName2', 'BandName3'] # Modify as per your case # Function to rename the bands of a raster and save as a new file def rename_bands_and_save_new(raster_layer_name): # Find the layer in the QGIS project by its name raster_layer = QgsProject.instance().mapLayersByName(raster_layer_name) if raster_layer: raster_layer = raster_layer[0] # Take the first layer found with that name input_path = raster_layer.dataProvider().dataSourceUri() dataset = gdal.Open(input_path, gdal.GA_ReadOnly) if dataset: # Define the path of the new raster file output_path = input_path.replace('.tif', '_modified.tif') # Create a driver for the TIFF format driver = gdal.GetDriverByName('GTiff') # Create a copy of the original dataset new_dataset = driver.CreateCopy(output_path, dataset, 0) # Rename the bands in the copy num_bands = new_dataset.RasterCount for i in range(min(num_bands, len(new_band_names))): band = new_dataset.GetRasterBand(i + 1) band.SetDescription(new_band_names[i]) # Close the new dataset to save your changes new_dataset = None print(f"Bande rinominate e salvate come {output_path}") else: print(f"Unable to open raster {raster_layer.name()}") # Close the original dataset dataset = None else: print(f"No layers found with name '{raster_layer_name}'") # Example of use of the function layer_name = "NameOfYourRasterLayer" # Edit to the actual name of your layer rename_bands_and_save_new(layer_name) 

script by Federico https://gist.github.com/fgianoli/53620d4f4baef3c7eba1ca21f4802938

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.