0

My map layer needs to be updated only when new latest image is added to imageCollection.

I am trying to do the following:

  • Filter out latest image in Collection. -works ok

  • Read image ID. - works ok

  • I am comparing ID I just read with ID from previous cycle. Comparing the id's works... sort of.

  • Next I am trying to create a conditional statement so when ID's are not the same Map is updated. - this part does not work.

I tried to make if() statement with Map.clear(), Map.addLayer() in it. This loop works when condition for if() set locally. When using variable derived from comparing ID's it stop working. I also see on Console that if() statement is processed before ID got from Server and compared.

I try using ee.AlgorithmIf(). In this case both false and true cases got executed every time regardless of condition. I can write there 1 or 0 or true or false - all the same.

My code:

//BC_fires_example_1
var IndexOLD = "2021310042020500000";

// Region of interest.
var area_of_interest = ee.Geometry.BBox(-121.9, 51, -120.5, 51.89);

// Color palette
var DQFVis = {
  min: 0,
  max: 5,
  palette: [
    'blanchedalmond',  // Good quality fire pixel
    'olive',           // Good quality fire free land
    'teal',            // Opaque cloud
                       // Bad surface type, sunglint, LZA threshold exceeded,
    'darkslateblue',   // off earth, or missing input data
    'lemonchiffon',    // Bad input data
    'burlywood'        // Algorithm failure
  ]};

//declaring another function to pass 'index' value
function MoreCode(){
// declaring All code below as function
function AllCode(){

// Time of interest. Set small time interval to get small list
var now = ee.Date(Date.now());
var hourBefore = now.advance(-1, 'hour');
var timeinterval = {
start: hourBefore,
end: now,
};

// Satellite data.
var goes_16_data = ee.ImageCollection('NOAA/GOES/16/FDCF')
.filterDate(timeinterval.start, timeinterval.end)
.filterBounds(area_of_interest);
// get latest record
var goes_16_data_latest = goes_16_data.limit(1,'system:time_start', false);
// select DQF layer for plotting
var dqf = goes_16_data_latest.select('DQF');
//convert to Image to get metadata
var goes_16_latest_image = ee.Image(goes_16_data_latest.first());
// read index
var index = goes_16_latest_image.get('system:index');
print('index', index);
print('IndexOLD returned', IndexOLD);
//IndexBin is binary var, true if index the same as last time
var IndexBin = (ee.Algorithms.IsEqual(index, IndexOLD));
print('IndexBin', IndexBin);

function MapRefresh(){
Map.clear(); 
Map.addLayer(dqf, DQFVis, 'DQF');
}

//Refresh map when IndexBin = false
ee.Algorithms.If(0, 1/*Map.clear()*/, mapRefresh()); //does not work

return IndexOUT;
} //End of AllCode() finction

IndexOLD = AllCode();
}//End of MoreCode() finction
ui.util.setInterval(MoreCode, 6000);

Indexes compared on line 49, ee.AlgorithmIf() on line 58.


1 Answer 1

1

The root of your trouble is that ee.Algorithms.If is a server-side process (like every other object class that starts with ee), whereas the Map is client-side. The Map and the ee.Algorithms object can't talk to each other, any client-side processes that are put into ee.Algorithms.If will be executed automatically, regardless of the input boolean. You can see this in action if you run this line of code:

ee.Algorithms.If(0, print('true'), print('false'));

Both 'true' and 'false' should be printed in the console, because printing is a client-side process and therefore it doesn't know about the outcome of the algorithm. If you want to see more, here's a similar question about conditionally adding a layer to the Map.

In order to update a client-side object like the Map, you have to evaluate your if statement on the client side by passing it into the evaluate() function:

IndexBin.evaluate(function (boolean) {
  if (boolean) {
    print('no map update needed')
  } else {
    Map.clear()
    Map.addLayer(dqf, DQFVis, 'DQF');
  }
});

Although I'm curious what your application is that you're refreshing the map like this in the first place. It's a little unusual to be constantly updating a purely ui object like this in Earth Engine, and if you share more about your use case, there might be an easier/more efficient way to implement what you have in mind.

1
  • Thank you kindly for your advice. I used your code example. It works. But I am doing something else wrong. My goal is to compare index I read from image in current cycle with index read from previous cycle. If the value differs I update map, if it is the same – I do not. Index from previous cycle should be written to IndexOLD. I pass value on index to IndexOut, return it to IndexOLD. My assumption was that var IndexOUT = index; return IndexOUT; would be executed at the end of the cycle after the line var IndexBin = (ee.Algorithms.IsEqual(index, IndexOLD)); But it is not.
    – AndreyGle
    Nov 17, 2021 at 20:41

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.