0

I have some code that creates FeatureCollection for SST a specific date range, which I combined and export as a csv. A link to which can be found here.

I would like to map the FeatureCollection to a list of dates so I have this FeatureCollection created for each of the dates on the list.

I used this link to help me and was able to create a list of dates. However, I'm struggling to return a Feature Collection for each date.

var reefs_500 = ee.FeatureCollection([
  ee.Feature(ee.Geometry.Point(71.3581, -6.6434).buffer(500), {'id': 'EG_mid'}),
  ee.Feature(ee.Geometry.Point(71.3967, -6.6782).buffer(500), {'id': 'EG_SE'}),
  ee.Feature(ee.Geometry.Point(71.5230, -6.1637).buffer(500), {'id': 'BR_mid'}),
  ee.Feature(ee.Geometry.Point(71.5372, -6.1733).buffer(500), {'id': 'BR_south'})
  ]);

Map.setCenter(72.0,-5.32, 10); 

Map.addLayer(reefs_500, {}, 'reefs_500');

// Create list of dates for time series

var Date_Start = ee.Date('2015-01-01');
var Date_End = ee.Date('2016-01-01');


// Create list of dates for time series
var n_months = Date_End.difference(Date_Start,'month').round();
var dates = ee.List.sequence(0,n_months,1);
var make_datelist = function(n) {
  return Date_Start.advance(n,'month');
};

dates = dates.map(make_datelist);
print('dates',dates);

var fnc = function(d1) {
  var start = ee.Date(d1);
  var end = ee.Date(d1).advance(1,'month');
  var date_range = ee.DateRange(start,end);

// add and rename regions 
// rename feature collections - due to client/server functions, you first have to convert the featureCollection into a list

var FeatCollectAsAList = reefs_500.toList(reefs_500.size());

// map a function over the list to rename the IDs
var reefsEdit = ee.FeatureCollection(FeatCollectAsAList.map(function(feat){
var id = ee.Feature(feat).get('id');
return ee.Feature(feat).set("system:index", id);
}));

/// CREATE SST HEALTH SCORE ///

//select SST image collection from modis, with date and regions filter
var sstCollection = ee.ImageCollection('NASA/OCEANDATA/MODIS-Aqua/L3SMI').select('sst')
.filterDate(date_range)
.filterBounds(reefs_500);

// get collection mean
 var sst_mean = sstCollection.mean();

// health score function

 function sstHS_function(image){
 var sstHS = image.expression(
 "(sst < 18.0) ? 1.0" +
 ":((sst <= 21.0) ? 1.0/2.0*sst + 20.0/3.0" +
 ":(sst < 28.0) ? 0.0" +
 ":(sst <= 30.0) ? 1.0/2.0*sst - 14.0" +
 ": 1.0)",
 {"sst" : image});
 return sstHS;
 }

// apply heath score function to image collection
var sstHS_collection = sstCollection.map(sstHS_function);


// get mean for collection 
var sstHS_mean = sstHS_collection.mean();
sstHS_mean = sstHS_mean.select('constant').rename('SE_score');


//// MAKE FEATURE COLLECTION OF HEALTH SCORE FOR ALL SITES ////

// create reducers to be used

var reducers = ee.Reducer.mean();


// create feature collection

//SST
var sstHS_region = sstHS_mean.reduceRegions({
 reducer: ee.Reducer.mean(),
 collection: reefsEdit,
 scale: 30});

sstHS_region = sstHS_region.map(function(feature) {
  return feature.set('variable', 'SST')
            .set('label', 'Health stressor')
            .set('reliability', 'high')
            .set('date', start);
});

                                 

//// GET MEAN SST FEATURECOLLECTIONS FOR EACH SITE ////

// SST

//caclulate mean per region
var reefs_sstMean = sst_mean.reduceRegions({
  reducer: ee.Reducer.mean(),
  collection: reefsEdit,
  scale: 30});

reefs_sstMean = reefs_sstMean.map(function(feature) {
  return feature.set('variable', 'SST');
});
                    

//// COMBINE THE TWO FEATURECOLLECTIONS


// Use an equals filter to define how the collections match.
var filter = ee.Filter.equals({
  leftField: 'system:index',
  rightField: 'system:index', 
});

// Define the join.
var saveAllJoin = ee.Join.saveAll({
  matchesKey: 'id',
  ordering: 'system:time_start',
  ascending: true
 });


// Apply the join.
var Dat = saveAllJoin.apply(sstHS_region, reefs_sstMean, filter);

// add varible_mean data to dataset for export
 Dat = Dat.map(function(mainFeature){
  var joinedFeaturesList = ee.List(mainFeature.get('id'));
  return mainFeature.set('variable_mean', ee.Feature(joinedFeaturesList.get(0)).get('mean'))
                .set('id', ee.Feature(joinedFeaturesList.get(0)).get('id'));
}); 

 //rename columns
 Dat = Dat.map(function(feat){
   return ee.Feature(feat.geometry(), { 
     id: feat.get('id'),
     date:feat.get('date'),
    variable: feat.get('variable'),
    SE_score: feat.get('mean'),
    variable_mean: feat.get('variable_mean'),
    stress_category: feat.get('label'),
    weighting: feat.get('reliability')
   });
});

return('Dat', Dat);
};

I then map the function to my list of dates

var FC_list = dates.map(fnc);

print('FC_list', FC_list);

But wasn't sure how to convert this into a series of FeatureCollections. I tried using

    var CRSEI = ee.FeatureCollection(FC_list);
    print(CRSEI);

This just converts them into a single FeatureCollection, but there is not data attached. How can I get the FeatureCollection mapped to each one of the dates in the above list, giving a list of Feature Collections, with the data attached, which can be exported as a csv?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.