0

I am trying to append a band from a scene in one Landsat 8 image collection to a scene in another Landsat 8 image collection in Google Earth Engine. I am joining the scenes using ee.Join.saveFirst()

The join works and I am able to see the joined scene in the image properties, however, I am not able to access the band.

The resulting image collection behaves differently than a normal image collection.

If I try to map a function (even where input equals output) on the collection, the resulting image collection doesn't know it bands anymore. Applying a reducer doesn't work at all.

Has anyone experienced similar issues and knows a sollution?

Here my code:

function addMask(img){

  // grabbing the attached image
  var fmask_img = img.get('fmask');

  // Trying to access a band in the original image works
  //var b1 = img.select(["B1"]);

  // Trying to access a band in the image doesn't work
  //var fmask = fmask_img.select(["fmask"]);

  //trying to add a band from the attached image to the orignal image doesn't work
  //var newImg = img.addBands(fmask_img, ["fmask"])

  //trying to all bands from the attached image to the orignal image also doesn't work
  //var newImg = img.addBands(fmask_img, ["fmask"])

  //even trying to the origianl image to is self doesn't work
  //var newImg = img.addBands(img)

  return img // returning the unchanged image instead of newImg
}

// Load a primary collection: Landsat imagery.
var primary = ee.ImageCollection('LC8_L1T')
    .filterDate('2014-04-01', '2014-06-01')
    .filterBounds(ee.Geometry.Point(-122.092, 37.42));

// Load a secondary collection: MODIS imagery.
var secondary = ee.ImageCollection('LANDSAT/LC8_L1T_TOA_FMASK')
    .filterDate('2014-04-01', '2014-06-01')
    .filterBounds(ee.Geometry.Point(-122.092, 37.42));

// Create a filter to define a match based on the scene ID.
var sceneFilter = ee.Filter.equals({
  leftField: "LANDSAT_SCENE_ID",
  rightField: "LANDSAT_SCENE_ID"
  })

// Define the join.
var saveFirstJoin = ee.Join.saveFirst({
  matchKey: 'fmask',
  ordering: 'system:time_start',
  ascending: true
});


// Apply the join.
// This returns an image collection
var landsatFmask = saveFirstJoin.apply(primary, secondary, sceneFilter);

// Mapping a simple function where input = output returns an image collection
var fmask = landsatFmask.map(addMask);

// applying a reducer on the orignal collection works
var primary_median = primary.median()

//applying a reducer on the orignalnew collection works doesn't work
//var fmask_median = fmask.median()
//var joined_median = landsatFmask.median()


// Display the result.
// the orginal collection
print('raw:', primary)
// looks the same as the orginal, only difference is that images inside the collection have an additional property "fmask" with the joined image inside
print('Join.saveAll:', landsatFmask);
// looks the same as the joined collection, but now the band list of the images collection is empty ??
print('fmask:', fmask);
1

ok, after playing around with it a bit more it turns out I need to type cast my images in the function

function addMask(img){

  // cast the input image
  var i = ee.Image(img);
  // grabbing the attached image and cast it as well
  var fmask_img = ee.Image(img.get('fmask'));

  //now i can add the band
  var newImg = i.addBands(fmask_img, ["fmask"])

  return newImg 
}

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.