1

I'm new to GEE. I have used this code to view the mosaic data. How should I export each mosaic data with unique file name (date).

var aoi = ee.FeatureCollection('users/sujithapeterraj/HAssan') 

var s2 = ee.ImageCollection('COPERNICUS/S2_SR')
  .filterBounds(aoi)
  .filterDate('2019-01-01', '2019-01-30'); // // %Y-%m-%d

print(s2.first(), 'first image s2');


// Function to mosaic by date, orbit, etc
function mosaicBy(imcol){
  // imcol: An image collection
  // returns: An image collection

  // return the collection as a list of images (not an image collection)
  var imlist = imcol.toList(imcol.size());

  // Get all the dates as list
  var all_dates = imlist.map(function(im){
    return ee.Image(im).date().format("YYYY-MM-dd");
  });

  // get all orbits as list
  var all_orbits = imlist.map(function(im){
    return ee.Image(im).get('SENSING_ORBIT_NUMBER');
  });

  // get all spacecraft names as list
  var all_spNames = imlist.map(function(im){
    return ee.Image(im).get('SPACECRAFT_NAME');
  });

  // this puts dates, orbits and names into a nested list
  var concat_all = all_dates.zip(all_orbits).zip(all_spNames);

  // here we unnest the list with flatten, and then concatenate the list elements with " "
  concat_all = concat_all.map(function(el){
    return ee.List(el).flatten().join(" ");
    });

  // here, just get distinct combintations of date, orbit and name
  var concat_unique = concat_all.distinct();

  // mosaic
  var mosaic_imlist = concat_unique.map(function(d){

    // split into components
    var d1 = ee.String(d).split(" ");

    var date1 = ee.Date(d1.get(0));
    var orbit = ee.Number.parse(d1.get(1)).toInt();
    var spName = ee.String(d1.get(2));

    var im = imcol
      .filterDate(date1, date1.advance(1, "day")) // filter with start date, and then end date, ie date + 1 day
      .filterMetadata('SPACECRAFT_NAME', 'equals', spName)
      .filterMetadata('SENSING_ORBIT_NUMBER','equals', orbit)
      .mosaic();

    return im.set(
        "system:time_start", date1.millis(),
        "system:date", date1.format("YYYY-MM-dd"),
        "system:id", d1);
  });

  return ee.ImageCollection(mosaic_imlist);
}


var s2day = mosaicBy(s2);

print(s2day);

// Make list of image to display each one
var listOfImages = s2day.toList(s2day.size());

Map.centerObject(aoi, 6); 

Map.addLayer(s2day.first(), {bands: 'B4,B3,B2', min: 300, max: 3000});

Map.addLayer(ee.Image(listOfImages.get(1)), {bands: 'B4,B3,B2', min: 300, max: 3000}); // second image
Map.addLayer(ee.Image(listOfImages.get(2)), {bands: 'B4,B3,B2', min: 300, max: 3000}); // third image
Map.addLayer(ee.Image(listOfImages.get(3)), {bands: 'B4,B3,B2', min: 300, max: 3000}); // 4th image
1
  • You might try exploring the API or if you have, explain your confusion. The API contains a class for exporting images and you can iterate over images in the mosaic. It appears that all the elements are present to complete the task. If you are uncertain on how to loop through the objects your might brush up on your Python/JS, you will need it if you plan on continued us of EE.
    – RomaH
    Dec 29, 2021 at 4:59

1 Answer 1

1

Based in the answer to this question, following code can do that.

var aoi = ee.Geometry.Polygon(
        [[[81.0312993277979, 29.97063836127144],
          [81.0312993277979, 28.91826467453208],
          [83.2505376090479, 28.91826467453208],
          [83.2505376090479, 29.97063836127144]]], null, false);

Map.centerObject(aoi);
Map.addLayer(aoi);

//////////////////////////////////////////////////
var s2 = ee.ImageCollection('COPERNICUS/S2_SR')
  .filterBounds(aoi)
  .filterDate('2019-01-01', '2019-01-30'); // // %Y-%m-%d

var scale = s2.first().select('B1').projection().nominalScale();

//print(scale);

//print(s2);

// Function to mosaic by date, orbit, etc
function mosaicBy(imcol){
  // imcol: An image collection
  // returns: An image collection

  // return the collection as a list of images (not an image collection)
  var imlist = imcol.toList(imcol.size());

  // Get all the dates as list
  var all_dates = imlist.map(function(im){
    return ee.Image(im).date().format("YYYY-MM-dd");
  });

  // get all orbits as list
  var all_orbits = imlist.map(function(im){
    return ee.Image(im).get('SENSING_ORBIT_NUMBER');
  });

  // get all spacecraft names as list
  var all_spNames = imlist.map(function(im){
    return ee.Image(im).get('SPACECRAFT_NAME');
  });

  // this puts dates, orbits and names into a nested list
  var concat_all = all_dates.zip(all_orbits).zip(all_spNames);

  // here we unnest the list with flatten, and then concatenate the list elements with " "
  concat_all = concat_all.map(function(el){
    return ee.List(el).flatten().join(" ");
    });

  // here, just get distinct combintations of date, orbit and name
  var concat_unique = concat_all.distinct();

  // mosaic
  var mosaic_imlist = concat_unique.map(function(d){

    // split into components
    var d1 = ee.String(d).split(" ");

    var date1 = ee.Date(d1.get(0));
    var orbit = ee.Number.parse(d1.get(1)).toInt();
    var spName = ee.String(d1.get(2));

    var im = imcol
      .filterDate(date1, date1.advance(1, "day")) // filter with start date, and then end date, ie date + 1 day
      .filterMetadata('SPACECRAFT_NAME', 'equals', spName)
      .filterMetadata('SENSING_ORBIT_NUMBER','equals', orbit)
      .mosaic();

    return im.set(
        "system:time_start", date1.millis(),
        "system:date", date1.format("YYYY-MM-dd"),
        "system:id", d1);
  });

  return ee.ImageCollection(mosaic_imlist);
}

var s2day = mosaicBy(s2);

print(s2day);

// Make list of image to display each one
var listOfImages = s2day.toList(s2day.size());

var dates = listOfImages.map(function(ele) {
  
  return ee.Image(ele).get('system:date');
  
});

print("dates", dates);

dates.getInfo().forEach(function (c) {
  
  var idx = dates.indexOf(c);
  
  var image = ee.Image(listOfImages.get(idx)).toInt16();

  image = image
    .clip(aoi);

  Export.image.toDrive({
      image : image,
      description: 'image'.concat('_', c),
      folder: 'GEE_Folder',
      scale: scale,
      crs : 'EPSG:4326',
      fileFormat: 'GeoTIFF',
      region: aoi
      });

   // display

  Map.addLayer(image, {}, 'image'.concat('_', c));
  
});

After running it in GEE code editor, I got result of following picture.

enter image description here

Into red rectangle, there were loaded 18 images corresponding to my aoi. I only run one task in Tab Tasks and it was successfully (image has 23 bands).

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.