0

I'm working on a set of images that have the same bandnames, but are present in a different order in each of the images. Following the API documentation I was in the understanding that bands would be matched by name, e.g. for ee.Image.subtract():

Subtracts the second value from the first for each matched pair of bands in image1 and image2. If either image1 or image2 has only 1 band, then it is used against all the bands in the other image. If the images have the same number of bands, but not the same names, they're used pairwise in the natural order.

However, the order of the bands, not the bandnames themselves, seems the way the bands are matched.

A reproducible example (https://code.earthengine.google.com/6725c103d2c71900ee144f31105398bf):

// Mock ROI
var ROI = ee.Geometry.Polygon([[8,47], [9,47], [9,49], [8,49], [8,47]], null, false)

// Image 1: 3 random bands. Band 4 is constant with a value of 1
var image1 = ee.Image.random(1)
                .addBands(ee.Image.random(2))
                .addBands(ee.Image.random(3))
                .addBands(ee.Image.constant(1))
                .rename(['b1', 'b2', 'b3', 'b4'])

// Image 2: again 3 random bands and band 4 constant with value of 1. But bands in different order.
var image2 = ee.Image.random(11)
                .addBands(ee.Image.random(12))
                .addBands(ee.Image.constant(1))
                .addBands(ee.Image.random(13))
                .rename(['b1', 'b2', 'b4', 'b3'])

// Subtract image2 from image1
var subtracted = image1.subtract(image2)

// Print mean value in ROI - for 'b4' this *should* equal 0 (1 minus 1)
print('Mean value, unsorted bands', subtracted.reduceRegion({reducer: ee.Reducer.mean(), geometry: ROI, scale: 1e3}))
// >>> b4: 0.5004240454234745

// Sort the bands
var image2_sorted = image2.select(['b1', 'b2', 'b3', 'b4'])

// Subtract sorted image2 from image1
var subracted_sorted = image1.subtract(image2_sorted)

// Print mean value in ROI - for 'b4' this *should* equal 0 (1 minus 1)
print('Mean value, sorted bands', subracted_sorted.reduceRegion({reducer: ee.Reducer.min(), geometry: ROI, scale: 1e3}))
// >>> b4: 0

The same seems to be happening for other mathematical operators like .add(), .multiply(), .divide(), etc.

Of course, the fix is straightforward (just make sure that all bands are in the same order). But I'm mostly trying to understand why the bands aren't matched by name? Maybe I'm just missing something obvious and this is the expected behaviour?

1 Answer 1

-1

The band names are allowed to an index which is in a sorted format. As given on one of their reference pages.

The bands of the primary input image are available using the built-in function b(), as b(0) or b('band_name').

So, the band name could be anything 'b1' or 'X1'. It will not be sorted based on its name but the index or order in which it has been allotted.

enter image description here | enter image description here

Your Answer

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

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