0

I want to modify values in a specific feature collection. This collection comprises two properties: a land cover label and an elevation value. The objective is to replace features with label 13 and an elevation value greater than 800.

With the following feature collection, and applying the S Verhoef approach, I achieve the replacement by using ee.Algorithms.If method.

var feature_list = [
  ee.Feature(null, {'UUID':1, 'LABEL': 13, 'ELEVATION': 750}),
  ee.Feature(null, {'UUID':2, 'LABEL': 13, 'ELEVATION': 830}),
  ee.Feature(null, {'UUID':3, 'LABEL': 1, 'ELEVATION': 30})
];

var fc = ee.FeatureCollection(feature_list);

var conditional = function(feat) {
  var islabel = ee.Number(feat.get('LABEL')).eq(13);
  var iselev = ee.Number(feat.get('ELEVATION')).gt(800);
  var condition = islabel.multiply(iselev);
  return ee.Algorithms.If(condition, feat.set({'LABEL': 1}), feat);
};
var test = fc.map(conditional)

Following the GEE Coding best practices, ee.Algorithms.If should be avoided. Are there any other approaches to achieve this feature collection update with the Google Earth Engine API?

1 Answer 1

0

As far as I know, I arrived at an answer to the question by utilizing the filter method.

  1. Filter the feature collection to retrieve the features that meet the specified condition.
  2. Modify the target property in the filtered features.
  3. Get features that have not been modified with .not().
  4. Merge the modified feature collection with the unmodified one.
var feature_list = [
  ee.Feature(null, {'UUID': 1, 'LABEL': 13, 'ELEVATION': 750}),
  ee.Feature(null, {'UUID': 2, 'LABEL': 13, 'ELEVATION': 860}),
  ee.Feature(null, {'UUID': 3, 'LABEL': 1, 'ELEVATION': 30})
];

var fc = ee.FeatureCollection(feature_list);

// Function to perform the second step (modify the data)
var updating = function(feat, label){
  /** Update the 'LABEL' property of a selected feature */
  return feat.set({'LABEL': label});
}

// Step 1: Filter the feature collection based on conditions
var custom_filter = ee.Filter.and(
  ee.Filter.eq('LABEL', 13),
  ee.Filter.lt('ELEVATION', 850)
);
var tomodify = fc.filter(custom_filter);
  
// Step 2: Modify the target property in the filtered features
var modified = tomodify.map(function(fc){return updating(fc, 11)});
  
// Step 3: Select the features that have not been modified
// var unmodified = fc.filter('LABEL != 13 OR ELEVATION >= 850');
var unmodified = fc.filter(custom_filter.not());
  
// Step 4: Merge the modified feature collection with the unmodified one
var updated_fc = unmodified.merge(modified);

print(updated_fc);

Any improvement will be helpful.

1
  • 1
    You don't need the inverted join, just filter twice, using .not() on the second one, and merge that with the modified results. Same result, way easier. Commented Mar 25, 2024 at 13:40

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.