0

Situation: My colleague and I have made our first script in Google Earth Engine Code Editor that will be published as an Earth Engine App. The script shows 33 basin-polygons that the user can click on, and a chart will appear for the chosen basin. The data in the chart is coming from a csv file and the basins are imported as shapefile.

Problem: We have troubles with the script running slow when clicked on a basin-polygon, we suspect that it is because .getInfo() is being used in 6 different places in the script.

Solutions we have tried: We tried .evaluate(), which we had difficulties with in our script, since we are not used to GEE coding.

Question:

  • How can we avoid using .getInfo()?

The whole script is shown here in this link: Link to whole script

Pieces of codes that contains .getInfo():

// First place where .getInfo() appears 

// Register a click handler for the map that adds the clicked polygon to the updated map overlay
function handleMapClick(location) {
  selectedPoints = []; //list of points that the selected polygon contains
  selectedPoints.push([location.lon, location.lat]); // adds clicked polygon to selection
  var myBasin = getSelectedBasin;
  // if statement for showing a chart when clicking on a polygon and not showing a chart when clicked somewhere else
  var basinSize=false;
  basinSize=(myBasin().size().getInfo() >0);
  if (basinSize){
    var p=theyCLickedOnABasin(myBasin);
  } else {
    var ps=theyClickedAwayFromABasin();
  }
}

// Second place where .getInfo() appears

// Finding a common maximum value 
  var max_val = max_val_3.max(max_val_4.max(min_val_3.max(min_val_4)));
  // Defining the y-axis limits
  var ylim = max_val.divide(ee.Number(20)).ceil().multiply(ee.Number(20)).getInfo(); 
  // Defining the 4 data that will be shown on y-axis on chart
  var myY=[SSI_W3RA_data.getInfo(),SSI_MCMC_data.getInfo(),GW_W3RA_data.getInfo(),GW_MCMC_data.getInfo()];
  

1 Answer 1

1

In order to interact with UI controls, sometimes you have to pull some data into the browser, but there are some techniques that can minimize the number of times you have to do that. For instance, instead of this:

   // Defining the y-axis limits
   var ylim = max_val.divide(ee.Number(20)).ceil().multiply(ee.Number(20)).getInfo(); 
   // Defining the 4 data that will be shown on y-axis on chart
   var myY=[SSI_W3RA_data.getInfo(),SSI_MCMC_data.getInfo(),GW_W3RA_data.getInfo(),GW_MCMC_data.getInfo()];

You could do all of that in a single getInfo call, which will be 5x faster (because they're executed in sequence).

var values = ee.Dictionary({
    ylim: max_val.divide(ee.Number(20)).ceil().multiply(ee.Number(20)),
    myY: [SSI_W3RA_data, SSI_MCMC_data, GW_W3RA_data, GW_MCMC_data]
}).getInfo()

var ylim = values.ylim
var myY = values.myY

And, instead of complicating your click handlers with ee.Algorithms.If (a function you should avoid as much as possible), I'd just do a reduceRegion with a first reducer whenever there's a click, and get "all" the information about whatever's under the click at once, and then use normal javascript ifs after that. Do all your testing and comparing client side, instead of server side.

3
  • Thank you for your answer! I tried to implement the changes you suggested, and the first part worked like a charm. I however have difficulties understanding how to use reduceRegion for a featureCollection. Could you be so kind and give an example of how to use it? I would really appreciate it.
    – hvaras
    Commented Nov 24, 2024 at 17:57
  • My mistake; for a feature collection, it would be a filter, not a reduceRegion. collection.filterBounds(point) Commented Nov 26, 2024 at 14:57
  • Thank you for your help @NoelGorelick. Although I am not sure how to use ee.FeatureCollection.filterBounds instead of ee.Algorithms.If. StackExchange has closed my question since the question was not focusing on only one problem. I have made a new question focusing on replacing ee.Algorithms.If, which is linked here: link
    – hvaras
    Commented Dec 27, 2024 at 17:07

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.