1

I just wrote a code for an online application through GEE. The application works when I click on the "run" button within the GEE interface. However, there is no way to run the code in the app interface because there is no "run" button.

Is there any way to add a button to run the code in the app interface?

1

In your link you have a section labeled PROCESSING STARTS HERE. You could try wrapping your processing steps in a ui.Button object:

var submit = ui.Button({
  label: 'Calculate NDVI',
  onClick: function() {
    var date_start = startDate.getValue();
    var date_end= endDate.getValue();
    // ...
    // The rest of your processing code here
    // Be sure to add your new layer at the end
    // ...
    mapPanel.addLayer(NDVI_annual_recl.select("slope").clip(AOI),BandCompViz);
  }
});

You should also consider whether you want previously calculated images to remain as layers on the map if the user changes the dates and calculates a new NDVI image. Multiple layers on the map can be nice for comparison but can also become cluttered quickly. If you want to clear out the previous layers, add a line at the beginning of the onClick function:

mapPanel.clear() // Or .remove() to specify a single layer

Finally, you'll need to add your new button to the UI panel. You can do this around lines 129-130, where you add the start and end date textboxes.

mainPanel.add(startDate);
mainPanel.add(endDate);
mainPanel.add(submit);
| improve this answer | |
  • The addition of the submit button appears now in the app interface. However, when clicking on it, it does not run the code.The interface of GEE where the code is written does run the code when clicking on the "Calculate NDVI" button. Is there something missing? Link to the code: code.earthengine.google.com/11d82431e1bd741146c6fb8530aef159 Link to the app interface: jtbsurrey.users.earthengine.app/view/collserola-ndvi – Juan TB Jul 4 at 10:27
  • As Kevin Reid mentioned in their answer, when I try to run your linked code I get an error stating that one of the assets cannot be found. The first step to fix your overall issue is to ensure that the Collserola asset is publicly available. – rpalomaki Jul 4 at 13:55
  • Now it is working. As you said, the Collserola asset was not public. – Juan TB Jul 4 at 15:27
0

Earth Engine Apps automatically “run the code”. You can see that it's working in your app because the custom left sidebar appears — if the code were not run there would be just a blank map.

If you need a button to take a specific action or reset things, use a ui.Button set up by your code.

In your case, the problem is not that your code is not running. The problem is that it fails partway though. Looking at errors in the browser JavaScript console, I can see that:

Collection.loadTable: Collection asset 'users/jtbsurrey/Collserola' not found.

Therefore, a problem is that you have not made the asset used by the app public. Fix that, and test further.

| improve this answer | |
  • But in the app (link attached) the code does not work because there is no chance to run it. I need to add the button in the app to run the code. Unlike in the app interface, within the GEE interface the code can be run: code.earthengine.google.com/1d67ac1c2a3f7f4de169970d929d003d I need to attach at the end of the code the ui.Button to run the code in the application but I do not know how to do it. – Juan TB Jul 3 at 15:49
  • @JuanTB Thank you for sharing the working version. In the future, please make sure to include that information in the original question — edit it, don't only add info in comments. I have updated my answer to explain the problem with your app. – Kevin Reid Jul 3 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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