1

I want to clip an ImageCollection with a grid (FeatureCollection) in Google Earth Engine. Every image in the ImageCollection should be clipped with every feature in the FeatureCollection. I need all images as separate segments for further calculations. Therefore, the result should be a new ImageCollection with each image segment including all properties of the original ImageCollection.

I work with Sentinel-1 images (SAR) and generated a grid over my study area. The clipping between the ImageCollection and the grid works (see Code snippet or the link for the GEE script). My problem is that I don't know how I can address the several image segments now for further processing.

var imagecollection = Sent1;
var featurecollection = grid;

var clip_fc = function(col) {
return col.clipToCollection(featurecollection);
};
var map_ic = imagecollection.map(clip_fc);

print ('New ImageCollection:', map_ic);
Map.addLayer(map_ic, imageVisParam, 'New ImageCollection');

https://code.earthengine.google.com/479120ce6641a2e49f1f728fb9150806

Does anyone have an idea? I'm not that advanced with JavaScript and short explanations or links would be very helpful as well!

1 Answer 1

3

You can create a function that take as arguments both the image and feature collections and then loop over all the entries of each object using map. Additionally, you need to make a small trick to convert the FeatureCollection coming from the feature collection loop to an ImageCollection and flatten the resulting ImageCollection.

// Function to loop over image and feat collections
var wrap = function(imagecol, featcol){
  // image collection loop
  var resul = imagecol.map(function(image){
    // feature collection loop
    var resul2 = featcol.map(function(feat){
      // make clip and add feat property according to feat id
      return ee.Image(image).clip(ee.Feature(feat))
                            .set({
                              'feat': ee.Feature(feat).id()
                            });  
    });
    // convert the FeatureCollection to list and convert it to ImageCollection
    return ee.ImageCollection.fromImages(resul2.toList(featcol.size()));
  });
  // Flatten, unnest lists
  return ee.ImageCollection(resul.flatten());
};

// Apply function
var map_ic = wrap(imagecollection, featurecollection);

print ('New ImageCollection:', map_ic);

// Add layers
Map.addLayer(ee.Image(map_ic.first()), imageVisParam, 'New ImageCollection');
// Select another image using the feat property
Map.addLayer(ee.Image(map_ic.filter(ee.Filter.eq('feat', '-9,119'))
                            .first()), 
                            imageVisParam, 
                            'New ImageCollection');
1
  • Thanks a lot! It works perfectly!
    – lena
    Mar 7, 2022 at 15:07

Your Answer

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

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