TransWikia.com

How to call the output of one function into another in Google Earth Engine?

Geographic Information Systems Asked by rao on February 7, 2021

i have to run a nested loop in GEE. I have two feature collections. I have to copy the id of features from one featureCollection to the other if they overlap (Eg. if feature1.contains(feature2) then copy the ‘id’ property of feature1 into feature2)

function getCols(tableMetadata) {
var eelist = tableMetadata.features[0].geometry.coordinates;
eelist = eelist.map(function(list_element) {
var a = ee.Feature(ee.Geometry.Polygon(list_element[0])).area(5);
var out =  ee.Feature(ee.Geometry.Polygon(list_element[0])).set('area',a);
return out;
});

}

function getCols_table3(tableMetadata){
var feat = tableMetadata.features
feat = feat.map(function(lake) {
var condition1 = ee.Feature(feat).contains(out);
//return condition1;
var id = lake.properties.Name
ee.Algorithms.If(condition1, ee.Feature(out).set('name',id),lake)
return id;
})
print(x)
}

As can be seen, i have to call the output of getCols function (out) into getCols_table3 function. How to get this working or is there any other way to do it?

One Answer

You will need to return a value from the function to use the output from that function. So similar to your case when you apply a function while mapping over a feature collection, you need to return the value from the function you want to use later. See this simple example:

function returnValue(featureCollection) {
  var IDs = featureCollection.map(function(feat){
    // do something with each feature
    return feat.set('contains', feature.contains(feat));
  });
  // make sure to return a value from the function
  return IDs;
}

// use the function and print the output
var output = returnValue(sampleFC);
print(output);

Next, you'd probably want to do it for two feature collections. That means you will have to map over both feature collections, and then flatten the result. I think you'd better afterwards filter on the features you want (trues) than using a IF-statement.

function returnValue(featureCollection, feature) {
  var IDs = featureCollection.map(function(feat){
    // get IDs of both features
    var ID1 = feat.id(); var ID2 = feature.id();
    var propName = ee.String('FC1_').cat(ID1).cat('_contains_').cat('FC2_').cat(ID2)
    // do something with each feature
    return feat.set(propName, feature.contains(feat), 'forFilter', feature.contains(feat));
  });
  // make sure to return a value from the function
  return IDs;
}

// use the function and print the output
var output = sampleFC2.map(function(feature){
  return returnValue(sampleFC1, feature);
}).flatten();

print('True outputs',output.filter(ee.Filter.eq('forFilter', true)));

Hope this solves your problem.

Link code 1 and link code 2, where I just drew some sample geometries.

Answered by Kuik on February 7, 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