Google Earth Engine – Property with Null-Value Disappearing When Creating Feature-Collection

google-earth-engine

When I create a feature with a property that has a null-value, that property disappears when adding the feature to a feature-collection. Why does this happen and how to prevent this?

An example (https://code.earthengine.google.com/5dd388c75939997f4d50d0e6628e1c22):

var dict1 = ee.Dictionary({'prop1': null, 'prop2': 400})

print(dict1);

var ft1 = ee.Feature(null, dict1)

print(ft1)

var ftc1 = ee.FeatureCollection(ft1)

print(ftc1)

"dict1" has two items, one of the values is "null". Creating a feature with this dictionary gives a feature "ft1", that has two properties. When adding "ft1" to a feature-collection, the property that has a null-value disappears from the feature.

Best Answer

I got an answer by Noel Gorelick in the GEE Developers Group:

There's no difference to the system between a property with a null value and not having the property at all. In fact, the method for removing properties from a feature is to set them to null. If you want to have a property with some NODATA value, use something other than null.

  • So, why it happens: it's the method to remove properties from a feature.

  • How to prevent it: Set the null-value to some other value.

An example on how to do that:

var dict1 = ee.Dictionary({'prop1': null, 'prop2': 400});

print(dict1);

// Set null values to -9999, works without explicitly specifying the property names.
var set_ndv = function(key, val){
  return ee.List([val, -9999]).reduce(ee.Reducer.firstNonNull());
};

var dict2 = dict1.map(set_ndv);

print(dict2);

// Set null values to -9999, need to specify property names.
// var dict2 = dict1.combine(ee.Dictionary({'prop1': -9999, 'prop2': -9999}), false)
// 
// print(dict2)

var ft1 = ee.Feature(null, dict2);

print(ft1);

var ftc1 = ee.FeatureCollection(ft1);

print(ftc1);

This question is also related to this one: Creating a time-series from an image-collection in GEE including null-values in case all pixels are masked

Related Question