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.