1

I have a feature collection of points which has a date column within it. I want to loop through the unique dates in the feature collection and then match corresponding dates within an image collection. After matching the features and images I want to extract values from the images using the sampleRegions function.

I have successfully done this manually with one date at the top of my script, and then tried to implement the same thing in a loop which seems to return seemingly empty output.

Here is a link to my code: https://code.earthengine.google.com/ea5f56d6c7b98619fbeffa373c9d95ed

And here is the code:

var bui = ee.ImageCollection("users/spotter/GFWED_v25/MERRA2_BUI"),
    pts = ee.FeatureCollection("users/spotter/ak_ca_combustion/raw/sol_pts");

//print a single pts feature to see what the first Date column item is
print('First Feature', pts.first()); //date is '2001-04-24'


//------------------------this works for extracting a single date without a loop
//the same date in the first feature
var date = ee.Date('2001-04-24');

//filter the pts by this date
var sub_pts = pts.filter(ee.Filter.eq("Date", '2001-04-24'));

//filter the image collection by this date
var bui_sub = bui.filterDate(ee.Date(date), ee.Date(date).advance(1,'day')).mean();

//extract the pts that match the image collection by date
var extract_test = bui_sub.sampleRegions({collection: sub_pts,
                                scale: 460,
                                projection: 'SR-ORG:6974',
                                tileScale: 16,
                                geometries: false
  });

print('Extracted Data', extract_test);


//---------Now I want to do the same thing over many dates at onc

//select specific dates of interest
var all_dates = ['2001-04-27', '2001-04-28', '2001-04-29'];


//loop many dates at once

var feat_iteration = ee.List(all_dates.map(function(date){

  //get sub image collection matching date
  var bui_sub = bui.filterDate(ee.Date(date), ee.Date(date).advance(1,'day')).mean();

  //get sub pts matching date
  var sub_pts = pts.filter(ee.Filter.eq("Date", date));

  //get the values for each point feature in sub_pts to be extracted from bui_sub
  var extract = bui_sub.sampleRegions({collection: sub_pts,
                                scale: 460,
                                projection: 'SR-ORG:6974',
                                tileScale: 16,
                                geometries: false
  });


  //this looks fine
  print('Each Extract', extract);
  return(extract);
}));

//the output data here is formatted much differen't than individual print statements
print(ee.FeatureCollection(feat_iteration));
1

SampleRegions returns a feature collection. Thus, we have a featureCollection at every index of a List (can be client-side or server-side). Therefore, cast the list to a featureCollection and flatten the collection of collections

//loop through all the unique dates in pts, I want this to work across all pts
var feat_iteration = ee.FeatureCollection(all_dates.map(function(date){

  //get sub image collection
  var bui_sub = bui.filterDate(ee.Date(date), ee.Date(date).advance(1,'day')).mean();

  //get sub pts
  var sub_pts = pts.filter(ee.Filter.eq("Date", date));

  //get the values for each point feature in sub_pts to be extracted from bui_sub
  var extract = bui_sub.sampleRegions({collection: sub_pts,
                                scale: 460,
                                projection: 'SR-ORG:6974',
                                tileScale: 16,
                                geometries: false
  });

  return extract;
})).flatten();

link code

| improve this answer | |

Your Answer

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

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