GEE joined collection is not working in a function

381 views Asked by At

I am working with two collections of satellite data. I want to select specific bands from "collection 1", join them to "collection 2", and then run a function. Unfortunately, the function does not work with the joined data, although it works for "collection 1".

Here is an example just using B10 of Sentinel-2

//identifying area and date
var geometry = ee.Geometry.Point([4,45]);

Map.centerObject(geometry,10);
var start = '2019-03-10';
var end   = '2019-05-10';


//my function
function testing(img){
  img = img.updateMask(img.select(['B10']).gt(200).focal_min(2).focal_max(2).not());
  return img;
}

//my two collections
var collection1 = ee.ImageCollection('COPERNICUS/S2').filterDate(start,end)
                  .filterBounds(geometry);
  
var B10s=collection1.select('B10');
//print('B10s',B10s);


var collection2 = ee.ImageCollection('COPERNICUS/S2_SR')
                  .filterDate(start,end)
                  .filterBounds(geometry);
                  
// joining the collections
var filtering = ee.Filter.equals({
  leftField: 'system:time_start',
  rightField: 'system:time_start'
});

var simpleJoin = ee.Join.inner();
var innerJoin = simpleJoin.apply(collection2, B10s, filtering);
var joined = innerJoin.map(function(feature) {
  return ee.Image.cat(feature.get('primary'), feature.get('secondary'));
});

print('Joined', joined);

//just to visualize one image
//var coll1 = ee.Image(collection1.first());
//Map.addLayer(coll1, {bands:['B2'], min:0, max:5000},'B2Coll1 test');

//running the function for collection 1 works
var test = collection1.map(testing);
var tess = ee.Image(test.first());
Map.addLayer(tess, {bands:['B2'], min:0, max:5000},'B2 test');

//here when running with the joined collection, there is a problem
var TestingJoined = joined.map(testing);

The error is: img.select(...).gt is not a function

How do I make this work?

2

There are 2 answers

0
ie-con On

Ok. I solved it. Thank you for your input. I needed to use a cast in the function. Now it works.

function testing(img){
  img = ee.Image(img).updateMask(ee.Image(img).select(['B10']).gt(200).focal_min(2).focal_max(2).not());
  return img;
}
0
Ryanman On

When you debug, does the join work as intended? Is there any issue with the datetimes being too-precise to allow a full join?

The second route I would go down is ensuring that the joined object is identical to collections. I doubt this would be the case without you casting it or something (though I'm not familiar with this library). Your "testing" function that you're mapping to these collections may work with just the unjoined ones. If you provide the actual 'Problem' or error output that would be immensely helpful.