Google Earth Engine – Filtering Out Null Values from Feature Collection

feature-collectionfiltergoogle-earth-enginegoogle-earth-engine-javascript-api

I'm having an issue with filtering out null values from a feature collection. I wrote a map function to get the land surface temperature value for every feature in the collection, which ran fine. Then when I tried exporting the feature collection, I got the error:

FeatureCollection (Error)
Error in map(ID=0000000000000000c265):
Element.get: Parameter 'object' is required.

I think it is because there isn't an LST value for that point, which is (-112.86, 41.95) from 2006-11-06 to 2006-12-06. It seems like for points with no values it just puts an error. I want to get rid of these features.

I tried to apply a filter (line 17), which I thought would work, but it just kept running until I got a "Number (Error) Computation timed out" error. I'm not sure why this is happening, as after googling, a filter seems like the right thing to do.

I am a complete beginner.

Code:
https://code.earthengine.google.com/f3fc6c4209fbb4e32e372d1d469f4e5d

Asset (Data table):
https://code.earthengine.google.com/?asset=projects/ee-akhilapram/assets/final-water-data-copy-4

var lst = ee.ImageCollection('MODIS/006/MOD11A1');
var lst_main = lst.select('LST_Day_1km');

var data = ee.FeatureCollection('projects/ee-akhilapram/assets/final-water-data-copy-4');

var lst = function(feature) {
  var date = ee.Date(feature.get('Time'));
  var lst = lst_main.filterDate(date, date.advance(feature.get('Time difference'), 'months'));
  return feature.set({'lst':lst.mean().sample(feature.geometry(), 10).first().get('LST_Day_1km')});
};

var hi = data.map(lst);
var bye = hi.filter(ee.Filter.neq('lst', null));
print(hi.filter(ee.Filter.eq("system:index", '0000000000000000c265')).select('lst')); //.get('SiteNo'))

Best Answer

You're correct that the problem is that there's no LST for some dates. The actual error is coming because you're attempting to get() from the first() item in a collection of 0 length. You can avoid all that.

Use reduceRegion(), not sample() (since you don't need each point to generate a collection), and just assign the reduceRegion results (a dictionary) to the feature directly. Then filter for null results.

var lst = function(feature) {
  var date = ee.Date(feature.get('Time'));
  var lst = lst_main.filterDate(date, date.advance(feature.get('Time difference'), 'months'));
  return feature.set(lst.mean().reduceRegion(ee.Reducer.first(), feature.geometry(), 10))
};

var hi = data.map(lst);
var bye = hi.filter(ee.Filter.neq('LST_Day_1km', null));
Related Question