2

I want to know the sequence of the bands (4 and more) of a GeoTIFF file for example whether the sequence is blue, green, red, near infra red, temperature, etc or green, blue, ...

How can I know that in Java, for example in GeoTools library? And by the way, are there specific tags in the GeoTIFF that indicates that (like TIFFTAG_IMAGEDESCRIPTION) or it is up to the image reader software to interpret the bands (even if it assumes the sequence mistakenly and considers blue instead of red and so forth).

1

In GeoTools you can find out the names of the bands and make a guess as to the RGB order but that is all it is a guess.

Consider the following code from the image tutorial:

int numBands = cov.getNumSampleDimensions();
// Get the names of the bands
String[] sampleDimensionNames = new String[numBands];
for (int i = 0; i < numBands; i++) {
  GridSampleDimension dim = cov.getSampleDimension(i);
  sampleDimensionNames[i] = dim.getDescription().toString();
}
final int RED = 0, GREEN = 1, BLUE = 2;
int[] channelNum = { -1, -1, -1 };
// We examine the band names looking for "red...", "green...", "blue...".
// Note that the channel numbers we record are indexed from 1, not 0.
for (int i = 0; i < numBands; i++) {
  String name = sampleDimensionNames[i].toLowerCase();
  if (name != null) {
    if (name.matches("red.*")) {
      channelNum[RED] = i + 1;
    } else if (name.matches("green.*")) {
      channelNum[GREEN] = i + 1;
    } else if (name.matches("blue.*")) {
      channelNum[BLUE] = i + 1;
    }
  }
}
// If we didn't find named bands "red...", "green...", "blue..."
// we fall back to using the first three bands in order
if (channelNum[RED] < 0 || channelNum[GREEN] < 0 || channelNum[BLUE] < 0) {
  channelNum[RED] = 1;
  channelNum[GREEN] = 2;
  channelNum[BLUE] = 3;
}

If for example your bands are called by other names (like band 1, 2 & 3) then you need to refer to the metadata that came with the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.