Google Earth Engine Error – Combining Feature ID, Date, and Mean NDVI Value Parameters for Export

google-earth-enginezonal statistics

What I need is to add Point, Object and Date Ids to the before NDVI Mean value. Such that when I export my feature collection it has the point id(Objectid), date id, and mean NDVI parameters. These parameters are highlighted in the image below;
Parameters needed for export

At the moment when I export, I only get the mean ndvi value.
The date and point id are the most important values needed. These parameters are properties of the Invasion_buffers future collection. Looking forward to a solution.

I have written a function but it returns an error; FeatureCollection (Error)
Error in map(ID=00000000000000000008):
Dictionary: Element at position 0 is not a string.

The function;

//Formatting the before invasion mean ndvi values before exporting
var format = function(table, rowId, colId) {
  var rows = table.distinct(rowId); 
  var joined = ee.Join.saveAll('matches').apply({
    primary: rows, 
    secondary: table, 
    condition: ee.Filter.equals({
      leftField: rowId, 
      rightField: rowId
    })
  });
         
  return joined.map(function(row) {
      var values = ee.List(row.get('matches'))
        .map(function(feature) {
          feature = ee.Feature(feature);
          var ndvi = ee.List([feature.get('BF_NDVI'), -9999]).reduce(ee.Reducer.firstNonNull())
          return [feature.get(colId), ee.Number(ndvi).format('%.2f')];
        });
      return row.select([rowId]).set(ee.Dictionary(values.flatten()));
    });
};

// var id = function (feature) {
//   var id = invasionbuffer.get('id').format('%05d');
//   return feature;
// };

var bfndviexp = format(invasionbuffer, 'date', 'BF_NDVI')
print('Bf_Ndvi_Export', bfndviexp)

The function is found in lines 137-170 of this script; https://code.earthengine.google.com/d5e51b426b5e77cdb002c641d2504434

Best Answer

@Shiraz If I correctly understand your problem then you can go with this function.

function dynamicDateBefore(feature){
  var swarmDate = ee.Feature(feature).get('STARTDATE')
  var beforeDate = ee.Date(swarmDate).advance(-11,'day')
  var Collection = ee.ImageCollection("COPERNICUS/S2")
        .filter(ee.Filter.date(beforeDate, swarmDate))
        .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 70))
        .filter(ee.Filter.bounds(invasionbuffer.geometry()))
        .map(maskS2clouds)
  return ee.Image(Collection.median().clip(invasionbuffer.geometry())).set('system:time_start', swarmDate);
}

function dynamicDateAfter(feature){
  var swarmDate = ee.Feature(feature).get('STARTDATE')
  var afterDate = ee.Date(swarmDate).advance(11,'day')
  var Collection = ee.ImageCollection("COPERNICUS/S2")
        .filter(ee.Filter.date(swarmDate, afterDate))
        .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 70))
        .filter(ee.Filter.bounds(invasionbuffer.geometry()))
        .map(maskS2clouds)
  return ee.Image(Collection.median().clip(invasionbuffer.geometry())).set('system:time_start', swarmDate);
}

//Each date iteration uses the date field from the invasion buffer 
//To filter for the before and after image at each invasion buffer
//Map the dynamic date before and after functions defined above 
//To the respective before and after image collections 
var beforeCollection = ee.ImageCollection(invasionbuffer.map(dynamicDateBefore))
var afterCollection = ee.ImageCollection(invasionbuffer.map(dynamicDateAfter))

//Add the before invasion image collections to the map
print('Before_Invasion_Collection', beforeCollection)

//Add the after invasion image collections to the map
print('After_Invasion_Collection', afterCollection)   

var zonal_ndvi = function(image){
var bfndvi = image.addBands(image.normalizedDifference(['B8', 'B4']).rename('NDVI'));  
var Mean_ndvi = ee.Image(bfndvi.select("NDVI")).reduceRegions({
    collection: invasionbuffer,
    reducer: ee.Reducer.mean(),
    scale: 10});
return Mean_ndvi.filter(ee.Filter.notNull(['mean']))
    .map(function(feature) {
      return feature
        .select(['mean', 'OBJECTID'], ['Mean_NDVI', 'OBJECTID'])
        .set({
          'Date': ee.Image(image).date().format('YYYY-MM-dd'),
          'doy':  ee.Date(ee.Image(image).date()).getRelative('day', 'year')
        })
  })}
  
var before_zonal_value =beforeCollection.map(zonal_ndvi).flatten()
print(before_zonal_value,"before_zonal_value")

var after_zonal_value =afterCollection.map(zonal_ndvi).flatten()
print(after_zonal_value,"after_zonal_value")
  


https://code.earthengine.google.com/611dbb2c1d3d396d0669aba6a748f1a5

Related Question