TransWikia.com

Feature math/ computation in Google Earth Engine

Geographic Information Systems Asked by hydro9 on February 12, 2021

I’d like to find a way in GEE to do column math. i.e. to calculate percentage of a feature. GEE have band math, but not feature/featureCollection math. for example:

   var feat = ee.FeatureCollection([
  ee.Feature(null, {Id: 0, count: 0 }),
  ee.Feature(null, {Id: 1, count: 10 }),
  ee.Feature(null, {Id: 2, count: 20 }),
  ee.Feature(null, {Id: 3, count: 20 }),
]); 
// add a column/property as the percentage of count
   var feat = ee.FeatureCollection([
      ee.Feature(null, {Id: 0, count: 0 , percent : 0}),// 0/(10+20+20) = 0
      ee.Feature(null, {Id: 1, count: 10 , percent: 0.1}), // 10/(10+20+20) = 0.2
      ee.Feature(null, {Id: 2, count: 20, percent: 0.4 }),
      ee.Feature(null, {Id: 3, count: 20, percent:0.4 }),
    ]); 

2 Answers

I have work it out myself and like to share with this community, and ask to improve the snippet as I feel it is not efficient.

//   
var size = feat.size();
var list_null = ee.List([]); // this is critical, 1st for the loop, 2nd, save loop var!!!
print('null list', list_null);

for (var i = 0; i < 3; i++) { // why use i < size do not work??
      var pct = ee.Number(ee.Feature(feat.toList(feat.size()).get(i)).get('count')).divide(50);
      print(pct);
      var list_null = list_null.add(pct)
      print(list_null);
    }
    
print(list_null.size());

// now add this list back to FC
// this part is from stackExchange
var lists = thirdFeatures.toList(feat.size()).zip(list_null);
print('lists:',lists)

var new_fc = ee.FeatureCollection(lists.map(function(l){
  return ee.Feature(ee.List(l).get(0)).set('pct', ee.List(l).get(1));
}));

print('new fc', new_fc);

Answered by hydro9 on February 12, 2021

This is what I would do:

// get total count 
var total = feat.aggregate_sum('count')

// map over feature collection 
var feat_new = feat.map(function(feature){
  
  // return feature with new "percent" property
  return feature.set('percent', ee.Number(feature.get('count')).divide(total))

})

Answered by korndog on February 12, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP