Skip to main content
deleted 19 characters in body; edited tags
Source Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

I have an image collection with many images (>800) and I want to calculate monthly NDVI and NBR median indexes and export the resulting images in a loop. I had had no problem grouping the images in the same month (in a same year) and calculating NDVI and NBR, but I'm having a really hard time using a loop to export the resulting images with names matching the time period of each one. The "description" and "fileNamePrefix" give some variant of: ee.String({ "type": "Invocation", "arguments": { "date": {.

edit: code added

I have an image collection with many images (>800) and I want to calculate monthly NDVI and NBR median indexes and export the resulting images in a loop. I had no problem grouping the images in the same month (in a same year) and calculating NDVI and NBR, but I'm having a really hard time using a loop to export the resulting images with names matching the time period of each one. The "description" and "fileNamePrefix" give some variant of: ee.String({ "type": "Invocation", "arguments": { "date": {.

edit: code added

I have an image collection with many images (>800) and I want to calculate monthly NDVI and NBR median indexes and export the resulting images in a loop. I had no problem grouping the images in the same month (in a same year) and calculating NDVI and NBR, but I'm having a really hard time using a loop to export the resulting images with names matching the time period of each one. The "description" and "fileNamePrefix" give some variant of: ee.String({ "type": "Invocation", "arguments": { "date": {.

added 1839 characters in body
Source Link
var vegSHP = ee.Geometry.Polygon(
        [[[-47.93318633174054, -15.923525740817693],
          [-47.84117583369367, -15.948285390673487],
          [-47.79070738886945, -15.876308885443688],
          [-47.83565782221213, -15.84506420705287],
          [-47.90844224604025, -15.867191599980972]]]);

var addNDVI = function(image) {
  var ndvi = ee.Image(0).expression(
    '2.5 * ((NIR - RED) / (NIR + RED))*100', {
      'NIR': image.select('B8'),
      'RED': image.select('B4'),
      
    });//.toUint16();
  return image.addBands(ndvi.rename('ndvi'));
};

var image  = ee.ImageCollection('COPERNICUS/S2')
.filterBounds(vegSHP);

var bandasFiltro= image.select('B.*')
.map(addNDVI);
print(bandasFiltro, "bandasFiltro");

var dates = bandasFiltro
    .map(function(bandasFiltro) {
      return ee.Feature(null, {'date': bandasFiltro
      .date()
      .format('YYYY-MM')});
    })
    .distinct('date')
    .aggregate_array('date');

var bandasFiltroData= bandasFiltro
.filterDate(dates.get(1),dates.get(2)) // pegando o primeiro valor da data e o segundo
.median()
.clip(vegSHP)
.set("periodo", ee.String(dates.get(0)).cat("_").cat(dates.get(2)))

for (var i = 0; i < 4-1; i++){ 
  var inicio= dates.get(i);
  var fim= dates.get(i+1);

  var imgMediana= ee.Image(bandasFiltro
  .filterDate(inicio,fim)
  .median()
  .clip(vegSHP))
  .set("periodo", ee.String(inicio).cat("_").cat(fim));
  //print(imgMediana);

  var id= ee.String("sentinel_2_")
  .cat(inicio).replace("\\-", "")
  .cat("_")
  .cat(fim).replace("\\-", "");
  //print(id)

  Export.image.toDrive({
  image:imgMediana,
  description: id,
  folder: 'ProjetoJBB',
  fileNamePrefix: id,
  region: vegSHP,
  crs: 'EPSG:4326',
  scale: 10,
  maxPixels: 10000000000});

}

edit: code added

var vegSHP = ee.Geometry.Polygon(
        [[[-47.93318633174054, -15.923525740817693],
          [-47.84117583369367, -15.948285390673487],
          [-47.79070738886945, -15.876308885443688],
          [-47.83565782221213, -15.84506420705287],
          [-47.90844224604025, -15.867191599980972]]]);

var addNDVI = function(image) {
  var ndvi = ee.Image(0).expression(
    '2.5 * ((NIR - RED) / (NIR + RED))*100', {
      'NIR': image.select('B8'),
      'RED': image.select('B4'),
      
    });//.toUint16();
  return image.addBands(ndvi.rename('ndvi'));
};

var image  = ee.ImageCollection('COPERNICUS/S2')
.filterBounds(vegSHP);

var bandasFiltro= image.select('B.*')
.map(addNDVI);
print(bandasFiltro, "bandasFiltro");

var dates = bandasFiltro
    .map(function(bandasFiltro) {
      return ee.Feature(null, {'date': bandasFiltro
      .date()
      .format('YYYY-MM')});
    })
    .distinct('date')
    .aggregate_array('date');

var bandasFiltroData= bandasFiltro
.filterDate(dates.get(1),dates.get(2)) // pegando o primeiro valor da data e o segundo
.median()
.clip(vegSHP)
.set("periodo", ee.String(dates.get(0)).cat("_").cat(dates.get(2)))

for (var i = 0; i < 4-1; i++){ 
  var inicio= dates.get(i);
  var fim= dates.get(i+1);

  var imgMediana= ee.Image(bandasFiltro
  .filterDate(inicio,fim)
  .median()
  .clip(vegSHP))
  .set("periodo", ee.String(inicio).cat("_").cat(fim));
  //print(imgMediana);

  var id= ee.String("sentinel_2_")
  .cat(inicio).replace("\\-", "")
  .cat("_")
  .cat(fim).replace("\\-", "");
  //print(id)

  Export.image.toDrive({
  image:imgMediana,
  description: id,
  folder: 'ProjetoJBB',
  fileNamePrefix: id,
  region: vegSHP,
  crs: 'EPSG:4326',
  scale: 10,
  maxPixels: 10000000000});

}

edit: code added

removed apology and appreciation
Source Link
Vince
  • 20.3k
  • 16
  • 48
  • 65

I have an image collection with many images (>800) and I want to calculate monthly NDVI and NBR median indexes and export the resulting images in a loop. I had no problem grouping the images in the same month (in a same year) and calculating NDVI and NBR, but I'm having a really hard time using a loop to export the resulting images with names matching the time period of each one. The "description" and "fileNamePrefix" give some variant of: ee.String({ "type": "Invocation", "arguments": { "date": {.

As I could see, I guess I have to first transform the images in a list and only then pass it through a loop. But I couldn't figure how to populate an empty list with new images (the rationale behind being: for each group of images, I'd calculate the median and add the resulting image to a list).

Here is a "clean" script (the one I'm working in is a mess of chunks of code trying to do what I want...): https://code.earthengine.google.com/?scriptPath=users%2Fthiagorbm%2FCerrado%3AProjetoEEJBB.sentinelV2

I'd like the image names to contain the year and month used to obtain the median values.

I'm new to Earth Engine and don't work very often with it. So, sorry if my question is very obvious. I'm not even sure I'm taking the best "path" to do what I want. Any help would be welcome!

I have an image collection with many images (>800) and I want to calculate monthly NDVI and NBR median indexes and export the resulting images in a loop. I had no problem grouping the images in the same month (in a same year) and calculating NDVI and NBR, but I'm having a really hard time using a loop to export the resulting images with names matching the time period of each one. The "description" and "fileNamePrefix" give some variant of: ee.String({ "type": "Invocation", "arguments": { "date": {.

As I could see, I guess I have to first transform the images in a list and only then pass it through a loop. But I couldn't figure how to populate an empty list with new images (the rationale behind being: for each group of images, I'd calculate the median and add the resulting image to a list).

Here is a "clean" script (the one I'm working in is a mess of chunks of code trying to do what I want...): https://code.earthengine.google.com/?scriptPath=users%2Fthiagorbm%2FCerrado%3AProjetoEEJBB.sentinelV2

I'd like the image names to contain the year and month used to obtain the median values.

I'm new to Earth Engine and don't work very often with it. So, sorry if my question is very obvious. I'm not even sure I'm taking the best "path" to do what I want. Any help would be welcome!

I have an image collection with many images (>800) and I want to calculate monthly NDVI and NBR median indexes and export the resulting images in a loop. I had no problem grouping the images in the same month (in a same year) and calculating NDVI and NBR, but I'm having a really hard time using a loop to export the resulting images with names matching the time period of each one. The "description" and "fileNamePrefix" give some variant of: ee.String({ "type": "Invocation", "arguments": { "date": {.

As I could see, I guess I have to first transform the images in a list and only then pass it through a loop. But I couldn't figure how to populate an empty list with new images (the rationale behind being: for each group of images, I'd calculate the median and add the resulting image to a list).

Here is a "clean" script (the one I'm working in is a mess of chunks of code trying to do what I want...): https://code.earthengine.google.com/?scriptPath=users%2Fthiagorbm%2FCerrado%3AProjetoEEJBB.sentinelV2

I'd like the image names to contain the year and month used to obtain the median values.

I'm new to Earth Engine and don't work very often with it. I'm not even sure I'm taking the best "path" to do what I want.

Source Link
Loading