0

How to find the min and max values of a column in a feature collection, disregarding values that cannot be converted to numbers (e.g., "NA" and "No data")? I still need to find a server-side solution for this, and I'm avoiding using getInfo() because the array returned may be too large.

Consider this feature collection as an example:

var fc = ee.FeatureCollection([
  ee.Feature(null, {a: '10', b: '9.80', c: '100'}),
  ee.Feature(null, {a: 'NA', b: '1.35', c: '200'}),
  ee.Feature(null, {a: 'NA', b: '4.85', c: 'NA'}),
  ee.Feature(null, {a: '30', b: 'NA', c: '300'}),
  ee.Feature(null, {a: '80', b: '1.25', c: '400'}),
]);

2 Answers 2

1

If your data is well-formed, you can simply filter out everything that's "NA" (or any other specific "bad" values like null) and then turn everything that's left into a number with Number.parse.

print(fc.filter(ee.Filter.neq("a", "NA"))
      .map(function(f) {
          return f.set("a", ee.Number.parse(f.get("a")))
      })
      .reduceColumns(ee.Reducer.minMax(), ["a"]))

You'll have to do this separately for each column (which you can do by mapping over propertyNames)

1
  • I found another solution using aggregate_array(), map(), ee.Algorithms.If() and match(). I don't know if it's a good solution in terms of performance, but it worked, and I didn't have to specify the "bad" values. I'll answer the question with the solution. Commented Dec 7, 2023 at 16:20
0

An alternative to the solution proposed by Noel:

var fc = ee.FeatureCollection([
  ee.Feature(null, {a: '10', b: '9.80', c: '100'}),
  ee.Feature(null, {a: 'NA', b: '1.35', c: '200'}),
  ee.Feature(null, {a: '20', b: '4.85', c: 'NA'}),
  ee.Feature(null, {a: '30', b: 'NA', c: '300'}),
  ee.Feature(null, {a: '80', b: '1.25', c: '400'}),
]);

var property = 'a';
var array = fc.aggregate_array(property);

array = array.map(function(s) {
  return ee.Algorithms.If({
    condition: ee.String(s).match('^\\D'),
    trueCase: null,
    falseCase: ee.Number.parse(s)
  });
}, true);

var minMax = array.reduce(ee.Reducer.minMax());
print('minMax', minMax);

// Output
minMax
{
  "max": 80,
  "min": 10
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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