├── 02_1_prototypes.js ├── 05_url_parameters.js ├── 03_throttle_debounce.js ├── 02_2_logger.js ├── 01_1_app_assets.js ├── README.md ├── 00_1_app_pattern_template.js ├── 01_2_app_skeleton.js ├── 04_timelapse.js ├── 01_3_app_layers.js ├── 01_4_app_layer_controls.js ├── 01_5_app_layer_legend.js ├── 01_6_app_layer_legend_controls.js ├── 01_7_app_query_alert_date.js ├── LICENSE └── app.js /02_1_prototypes.js: -------------------------------------------------------------------------------- 1 | // Copyright 2024 Google LLC 2 | 3 | /* 4 | * Demonstrate Javascript prototypes 5 | * 6 | */ 7 | 8 | function Person(name, surname, location) { 9 | this.name = name 10 | this.surname = surname 11 | this.location = location 12 | } 13 | 14 | Person.prototype.getFullName = function() { 15 | return this.name + ' ' + this.surname 16 | } 17 | 18 | // ======================= test 19 | 20 | var p1 = new Person('Homer', 'Simpson') 21 | var p2 = new Person('Donald', 'Duck') 22 | 23 | print(p1.getFullName()) 24 | print(p2.getFullName()) 25 | -------------------------------------------------------------------------------- /05_url_parameters.js: -------------------------------------------------------------------------------- 1 | // Copyright 2024 Google LLC 2 | 3 | /** 4 | * Demonstrate URL parameter updates 5 | * 6 | * @author Gennadii Donchyts (dgena@google.com) 7 | * @author Tyler Erickson (tyler@vorgeo.com) 8 | */ 9 | 10 | /******************************************************************************* 11 | * Behaviors * 12 | ******************************************************************************/ 13 | 14 | Map.onChangeZoom(function() { 15 | ui.url.set('zoom', Map.getZoom()); 16 | }); 17 | 18 | Map.onChangeBounds(function(center) { 19 | ui.url.set('lon', parseFloat(center.lon).toFixed(4)); 20 | ui.url.set('lat', parseFloat(center.lat).toFixed(4)); 21 | }); 22 | 23 | /******************************************************************************* 24 | * Initialize * 25 | ******************************************************************************/ 26 | 27 | var lat = ui.url.get('lat', 25); 28 | var lon = ui.url.get('lon', 0); 29 | var zoom = ui.url.get('zoom', 3); 30 | Map.setCenter(lon, lat, zoom); 31 | -------------------------------------------------------------------------------- /03_throttle_debounce.js: -------------------------------------------------------------------------------- 1 | // Copyright 2024 Google LLC 2 | 3 | /* 4 | * Demonstrate widget event throttling 5 | */ 6 | 7 | Map.setOptions('TERRAIN'); 8 | 9 | var image = ee.Image("CGIAR/SRTM90_V4"); 10 | 11 | var panel = ui.Panel([], null, { 12 | width: '400px', 13 | height: '250px', 14 | position: 'bottom-left' 15 | }); 16 | 17 | Map.add(panel); 18 | 19 | function updateHistogram() { 20 | var bounds = ee.Geometry(Map.getBounds(true)); 21 | var scale = Map.getScale() * 5; 22 | 23 | print('Updating histogram ... ', Map.getZoom(), bounds); 24 | 25 | var chart = ui.Chart.image.histogram(image, bounds, scale, 30); 26 | panel.widgets().reset([chart]); 27 | } 28 | 29 | // Define functions that update the histogram using different strategies. 30 | 31 | function manualUpdate() { 32 | print('Updating histogram when script is run'); 33 | updateHistogram(); 34 | } 35 | 36 | function updateOnChange() { 37 | print('Updating histogram after map change'); 38 | Map.onChangeZoom(updateHistogram); 39 | Map.onChangeBounds(updateHistogram); 40 | } 41 | 42 | function updateOnChangeWithDebounce() { 43 | print('Updating histogram 2 seconds after map change'); 44 | var updateHistogramDebounced = ui.util.debounce(updateHistogram, 2000); 45 | Map.onChangeZoom(updateHistogramDebounced); 46 | Map.onChangeBounds(updateHistogramDebounced); 47 | } 48 | 49 | // Uncomment one of the following function calls 50 | // manualUpdate(); 51 | // updateOnChange(); 52 | updateOnChangeWithDebounce(); 53 | -------------------------------------------------------------------------------- /02_2_logger.js: -------------------------------------------------------------------------------- 1 | // Copyright 2024 Google LLC 2 | 3 | /* 4 | * Demonstrate creating a Logger widget 5 | * 6 | */ 7 | 8 | function Logger(delay) { 9 | // Create a panel for displaying log messages. 10 | this.panel = ui.Panel(null, ui.Panel.Layout.Flow('vertical'), { 11 | backgroundColor: '#00000000', 12 | color: '00000000', 13 | position: 'bottom-right' 14 | }); 15 | 16 | this.delay = delay; 17 | 18 | // If a delay is not specified, default to 1 second. 19 | if(typeof(this.delay) === 'undefined') { 20 | this.delay = 1000; 21 | } 22 | 23 | // Specify a default maximum message count. 24 | this.maxCount = 5; 25 | 26 | Map.widgets().add(this.panel); 27 | }; 28 | 29 | // Add a prototype function that displays a message. 30 | Logger.prototype.info = function(message, color) { 31 | var self = this; 32 | 33 | var label = ui.Label(message, { 34 | backgroundColor: '#00000066', 35 | color: typeof(color) == 'undefined' ? 'ffffff' : color, 36 | fontSize: '14px', 37 | margin: '2px', 38 | padding: '2px' 39 | }); 40 | 41 | self.panel.widgets().add(label); 42 | self.panel.style().set({ shown: true }); 43 | 44 | if(self.panel.widgets().length() > self.maxCount) { 45 | self.panel.widgets().remove(self.panel.widgets().get(0)); 46 | } 47 | 48 | // Configure the widget to be removed after a specified time delay 49 | ui.util.setTimeout(function() { 50 | self.panel.widgets().remove(label); 51 | 52 | // If all the widget have been removed, hide the panel. 53 | if(self.panel.widgets().length() === 0) { 54 | self.panel.style().set({ shown: false }); 55 | } 56 | }, self.delay); 57 | }; 58 | 59 | Logger.prototype.setMaxCount = function(maxCount) { 60 | this.maxCount = maxCount; 61 | }; 62 | 63 | 64 | // ======================================= test 65 | 66 | var log = new Logger(3000); 67 | log.setMaxCount(10); 68 | 69 | log.info('Querying image data ...'); 70 | log.info('Querying image data v2 ...'); 71 | log.info('Querying image data v3 ...'); 72 | log.info('Querying image data v4 ...'); 73 | 74 | // Call functions after a specified delay. 75 | ui.util.setTimeout(function() { log.info('Querying image data v5 ...') }, 1000); 76 | ui.util.setTimeout(function() { log.info('Querying image data v6 ...', 'yellow') }, 500); 77 | ui.util.setTimeout(function() { log.info('Querying image data v7 ...', 'lime') }, 500); 78 | ui.util.setTimeout(function() { log.info('Querying image data v8 ...') }, 500); 79 | -------------------------------------------------------------------------------- /01_1_app_assets.js: -------------------------------------------------------------------------------- 1 | // Copyright 2024 Google LLC 2 | 3 | /** 4 | * STEP 1: Visualize our App map layers 5 | * 6 | * @author Gennadii Donchyts (dgena@google.com) 7 | * @author Tyler Erickson (tyler@vorgeo.com) 8 | */ 9 | 10 | /******************************************************************************* 11 | * Model * 12 | ******************************************************************************/ 13 | 14 | var m = {}; 15 | 16 | // use RADD code from: https://code.earthengine.google.com/a8b49975261d3040d203e01ee48617ca 17 | m.radd = ee.ImageCollection('projects/radar-wur/raddalert/v1'); 18 | m.regions = ['africa', 'sa', 'asia', 'ca']; 19 | 20 | m.forest_baseline = m.radd.filterMetadata('layer','contains','forest_baseline') 21 | .mosaic(); 22 | 23 | m.radd_alert = ee.ImageCollection( 24 | m.regions.map( 25 | function(region) { 26 | return m.radd.filterMetadata('layer', 'contains', 'alert') 27 | .filterMetadata('geography', 'equals', region) 28 | .sort('system:time_end', false) 29 | .first(); 30 | } 31 | )).mosaic(); 32 | 33 | /******************************************************************************* 34 | * Styling * 35 | ******************************************************************************/ 36 | 37 | var s = {}; 38 | 39 | // RADD alert: 2 = unconfirmed (low confidence) alert; 3 = confirmed (high confidence) alert 40 | s.visAlertConfidence = { 41 | min: 2, 42 | max: 3, 43 | palette: ['00FFFF', 'EA7E7D'] 44 | }; 45 | s.visForestBaseline = { 46 | palette: ['black'], 47 | opacity: 0.3 48 | }; 49 | s.visAlertDate = { 50 | min: 20000, 51 | max: 24000, 52 | palette: ["ffffcc", "ffeda0", "fed976", "feb24c", "fd8d3c", 53 | "fc4e2a", "e31a1c", "bd0026", "800026"] 54 | }; 55 | 56 | /******************************************************************************* 57 | * Components * 58 | ******************************************************************************/ 59 | 60 | var c = {}; 61 | 62 | /******************************************************************************* 63 | * Composition * 64 | ******************************************************************************/ 65 | 66 | Map.addLayer( 67 | m.forest_baseline, 68 | s.visForestBaseline, 69 | 'Forest baseline'); 70 | Map.addLayer( 71 | m.radd_alert.select('Alert'), 72 | s.visAlertConfidence, 73 | 'RADD alert'); 74 | Map.addLayer( 75 | m.radd_alert.select('Date'), 76 | s.visAlertDate, 77 | 'RADD alert date'); 78 | 79 | /******************************************************************************* 80 | * Behaviors * 81 | ******************************************************************************/ 82 | 83 | /******************************************************************************* 84 | * Initialize * 85 | ******************************************************************************/ 86 | 87 | Map.setOptions('HYBRID'); 88 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Earth Engine Apps Training 2 | 3 | Earth Engine Apps training based on in-person sessions at the Geo for Good 2024 mini-summits in [São Paulo, Brazil](https://earthoutreachonair.withgoogle.com/events/geoforgood24-saopaulo) and [Dublin, Ireland](https://earthoutreachonair.withgoogle.com/events/geoforgood24-dublin). 4 | 5 | Copies of the code scripts found in this repository can be opened directly in the [Earth Engine Code Editor](https://developers.google.com/earth-engine/guides/quickstart_javascript) by clicking the links below. 6 | 7 | | Description | Code Editor Link | 8 | |--------|-------------| 9 | | Script Template | [00_1_app_pattern_template.js](https://code.earthengine.google.com/?scriptPath=users%2Fdgena%2Fee-training-2024-apps%3A00_1_app_pattern_template.js) | 10 | | STEP 1: Visualize our App map layers | [01_1_app_assets.js](https://code.earthengine.google.com/?scriptPath=users%2Fdgena%2Fee-training-2024-apps%3A01_1_app_assets.js) | 11 | | STEP 2: Layout the App and build the App UI | [01_2_app_skeleton.js](https://code.earthengine.google.com/?scriptPath=users%2Fdgena%2Fee-training-2024-apps%3A01_2_app_skeleton.js) | 12 | | STEP 3: Add layer images to the map | [01_3_app_layers.js](https://code.earthengine.google.com/?scriptPath=users%2Fdgena%2Fee-training-2024-apps%3A01_3_app_layers.js) | 13 | | STEP 4: Add layer controls | [01_4_app_layer_controls.js](https://code.earthengine.google.com/?scriptPath=users%2Fdgena%2Fee-training-2024-apps%3A01_4_app_layer_controls.js) | 14 | | STEP 5: Build layer legend based on layer type | [01_5_app_layer_legend.js](https://code.earthengine.google.com/?scriptPath=users%2Fdgena%2Fee-training-2024-apps%3A01_5_app_layer_legend.js) | 15 | | STEP 6: Build layer legend controls | [01_6_app_layer_legend_controls.js](https://code.earthengine.google.com/?scriptPath=users%2Fdgena%2Fee-training-2024-apps%3A01_6_app_layer_legend_controls.js) | 16 | | STEP 7: Display layer info on map click | [01_7_app_query_alert_date.js](https://code.earthengine.google.com/?scriptPath=users%2Fdgena%2Fee-training-2024-apps%3A01_7_app_query_alert_date.js) | 17 | | Demonstrate Javascript prototypes | [02_1_prototypes.js](https://code.earthengine.google.com/?scriptPath=users%2Fdgena%2Fee-training-2024-apps%3A02_1_prototypes.js) | 18 | | Demonstrate creating a Logger widget | [02_2_logger.js](https://code.earthengine.google.com/?scriptPath=users%2Fdgena%2Fee-training-2024-apps%3A02_2_logger.js) | 19 | | Demonstrate widget event throttling | [03_throttle_debounce.js](https://code.earthengine.google.com/?scriptPath=users%2Fdgena%2Fee-training-2024-apps%3A03_throttle_debounce.js) | 20 | | Visualize imagery with timelapse prototype | [04_timelapse.js](https://code.earthengine.google.com/?scriptPath=users%2Fdgena%2Fee-training-2024-apps%3A04_timelapse.js) | 21 | | Demonstrate URL parameter updates | [05_url_parameters.js](https://code.earthengine.google.com/?scriptPath=users%2Fdgena%2Fee-training-2024-apps%3A05_url_parameters.js) | 22 | | The final app! | [app.js](https://code.earthengine.google.com/?scriptPath=users%2Fdgena%2Fee-training-2024-apps%3Aapp.js) | 23 | -------------------------------------------------------------------------------- /00_1_app_pattern_template.js: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Google LLC 2 | 3 | /** 4 | * UI Pattern Template 5 | * 6 | * This script is a template for organizing code into distinct sections 7 | * to improve readability/maintainability: 8 | * Model, Components, Composition, Styling, Behaviors, Initialization 9 | * 10 | * Source: https://code.earthengine.google.com/bab500e5290d579f8d5f1cc5715314cf 11 | * 12 | * @author Tyler Erickson (tyler@vorgeo.com) 13 | * @author Justin Braaten (braaten@google.com) 14 | */ 15 | 16 | /******************************************************************************* 17 | * Model * 18 | * 19 | * A section to define information about the data being presented in your 20 | * app. 21 | * 22 | * Guidelines: Use this section to import assets and define information that 23 | * are used to parameterize data-dependant widgets and control style and 24 | * behavior on UI interactions. 25 | ******************************************************************************/ 26 | 27 | // Define a JSON object for storing the data model. 28 | var m = {}; 29 | 30 | /* Example 31 | // Selected year. 32 | m.year = null; 33 | */ 34 | 35 | 36 | /******************************************************************************* 37 | * Components * 38 | * 39 | * A section to define the widgets that will compose your app. 40 | * 41 | * Guidelines: 42 | * 1. Except for static text and constraints, accept default values; 43 | * initialize others in the initialization section. 44 | * 2. Limit composition of widgets to those belonging to an inseparable unit 45 | * (i.e. a group of widgets that would make no sense out of order). 46 | ******************************************************************************/ 47 | 48 | // Define a JSON object for storing UI components. 49 | var c = {}; 50 | 51 | /* Example 52 | c.legend = { 53 | title: ui.Label(); 54 | } 55 | */ 56 | 57 | 58 | /******************************************************************************* 59 | * Composition * 60 | * 61 | * A section to compose the app i.e. add child widgets and widget groups to 62 | * first-level parent components like control panels and maps. 63 | * 64 | * Guidelines: There is a gradient between components and composition. There 65 | * are no hard guidelines here; use this section to help conceptually break up 66 | * the composition of complicated apps with many widgets and widget groups. 67 | ******************************************************************************/ 68 | 69 | /* Example 70 | ui.root.clear(); 71 | ui.root.add(c.controlPanel); 72 | ui.root.add(c.map); 73 | */ 74 | 75 | 76 | /******************************************************************************* 77 | * Styling * 78 | * 79 | * A section to define and set widget style properties. 80 | * 81 | * Guidelines: 82 | * 1. At the top, define styles for widget "classes" i.e. styles that might be 83 | * applied to several widgets, like text styles or margin styles. 84 | * 2. Set "inline" style properties for single-use styles. 85 | * 3. You can add multiple styles to widgets, add "inline" style followed by 86 | * "class" styles. If multiple styles need to be set on the same widget, do 87 | * it consecutively to maintain order. 88 | ******************************************************************************/ 89 | 90 | // Define a JSON object for defining CSS-like class style properties. 91 | var s = {}; 92 | 93 | /* Example 94 | s.legend.title = { 95 | fontWeight: 'bold', 96 | fontSize: '12px', 97 | color: '383838' 98 | }; 99 | c.legend.title.style().set(s.legend.title); 100 | */ 101 | 102 | 103 | /******************************************************************************* 104 | * Behaviors * 105 | * 106 | * A section to define app behavior on UI activity. 107 | * 108 | * Guidelines: 109 | * 1. At the top, define helper functions and functions that will be used as 110 | * callbacks for multiple events. 111 | * 2. For single-use callbacks, define them just prior to assignment. If 112 | * multiple callbacks are required for a widget, add them consecutively to 113 | * maintain order; single-use followed by multi-use. 114 | * 3. As much as possible, include callbacks that update URL parameters. 115 | ******************************************************************************/ 116 | 117 | /* Example 118 | // Handles updating the legend when band selector changes. 119 | function updateLegend() { 120 | c.legend.title.setValue(c.bandSelect.getValue() + ' (%)'); 121 | } 122 | */ 123 | 124 | 125 | /******************************************************************************* 126 | * Initialize * 127 | * 128 | * A section to initialize the app state on load. 129 | * 130 | * Guidelines: 131 | * 1. At the top, define any helper functions. 132 | * 2. As much as possible, use URL params to initial the state of the app. 133 | ******************************************************************************/ 134 | 135 | /* Example 136 | // Selected year. 137 | m.year = 2020; 138 | */ 139 | -------------------------------------------------------------------------------- /01_2_app_skeleton.js: -------------------------------------------------------------------------------- 1 | // Copyright 2024 Google LLC 2 | 3 | /** 4 | * STEP 2: Layout the App and build the App UI 5 | * 6 | * @author Gennadii Donchyts (dgena@google.com) 7 | * @author Tyler Erickson (tyler@vorgeo.com) 8 | */ 9 | 10 | /******************************************************************************* 11 | * Model * 12 | ******************************************************************************/ 13 | 14 | var m = {}; 15 | 16 | // use RADD code from: https://code.earthengine.google.com/a8b49975261d3040d203e01ee48617ca 17 | m.radd = ee.ImageCollection('projects/radar-wur/raddalert/v1'); 18 | m.regions = ['africa', 'sa', 'asia', 'ca']; 19 | 20 | m.forest_baseline = m.radd.filterMetadata('layer','contains','forest_baseline') 21 | .mosaic(); 22 | 23 | m.radd_alert = ee.ImageCollection( 24 | m.regions.map( 25 | function(region) { 26 | return m.radd.filterMetadata('layer', 'contains', 'alert') 27 | .filterMetadata('geography', 'equals', region) 28 | .sort('system:time_end', false) 29 | .first(); 30 | } 31 | )).mosaic(); 32 | 33 | m.layerInfos = [ 34 | { 35 | name: 'Alert Date', 36 | description: '' 37 | }, 38 | { 39 | name: 'Confidence', 40 | description: '' 41 | }, 42 | { 43 | name: 'Primary humid tropical forest', 44 | description: 'Primary humid tropical forest mask 2001 from Turubanova et al (2018) with annual (Africa: 2001 - 2018; Other geographies: 2001 - 2019) forest loss (Hansen et al 2013) and mangroves (Bunting et al 2018) removed.' 45 | }, 46 | ]; 47 | 48 | /******************************************************************************* 49 | * Styling * 50 | ******************************************************************************/ 51 | 52 | var s = {}; 53 | 54 | // RADD alert: 2 = unconfirmed (low confidence) alert; 3 = confirmed (high confidence) alert 55 | s.visAlertConfidence = { 56 | min: 2, 57 | max: 3, 58 | palette: ['00FFFF', 'EA7E7D'] 59 | }; 60 | s.visForestBaseline = { 61 | palette: ['black'], 62 | opacity: 0.3 63 | }; 64 | s.visAlertDate = { 65 | min: 20000, 66 | max: 24000, 67 | palette: ["ffffcc", "ffeda0", "fed976", "feb24c", "fd8d3c", 68 | "fc4e2a", "e31a1c", "bd0026", "800026"] 69 | }; 70 | 71 | s.panelLeft = {width: '400px'}; 72 | s.titleLabel = {fontSize: '22px', fontWeight: 'bold'}; 73 | s.layerPanel = {border: '1px solid black', margin: '5px 5px 0px 5px'}; 74 | s.layerPanelName = {fontWeight: 'bold'}; 75 | s.layerPanelDescription = {color: 'grey', fontSize: '11px'}; 76 | 77 | /******************************************************************************* 78 | * Components * 79 | ******************************************************************************/ 80 | 81 | var c = {}; 82 | 83 | c.titleLabel = ui.Label('Demo App', s.titleLabel); 84 | c.infoPanel = ui.Label('Lorem ipsum odor amet, consectetuer adipiscing elit. Ultrices facilisis ultricies nec nibh integer leo. Libero scelerisque purus ex ultricies ipsum ornare platea euismod. Cursus blandit duis ligula lobortis rhoncus quam per eu. Dictumst proin elementum sociosqu nascetur mi integer massa euismod.'); 85 | 86 | c.buildLayerPanelName = function(layerInfo) { 87 | return ui.Label(layerInfo.name, s.layerPanelName); 88 | }; 89 | c.buildLayerPanelDesc = function(layerInfo) { 90 | return ui.Label(layerInfo.description, s.layerPanelDescription); 91 | }; 92 | 93 | c.buildLayerPanel = function(layerInfo) { 94 | var layerPanel = ui.Panel([ 95 | c.buildLayerPanelName(layerInfo), 96 | c.buildLayerPanelDesc(layerInfo) 97 | ], 98 | ui.Panel.Layout.flow('vertical'), s.layerPanel); 99 | return layerPanel; 100 | }; 101 | 102 | c.layersPanel = ui.Panel(m.layerInfos.map(c.buildLayerPanel)); 103 | 104 | c.buildUI = function() { 105 | var panelLeft = ui.Panel([ 106 | c.titleLabel, 107 | c.infoPanel, 108 | c.layersPanel 109 | ], 110 | ui.Panel.Layout.flow('vertical'), 111 | s.panelLeft 112 | ); 113 | ui.root.widgets().insert(0, panelLeft); 114 | }; 115 | 116 | /******************************************************************************* 117 | * Composition * 118 | ******************************************************************************/ 119 | 120 | Map.addLayer( 121 | m.forest_baseline, 122 | s.visForestBaseline, 123 | 'Forest baseline'); 124 | Map.addLayer( 125 | m.radd_alert.select('Alert'), 126 | s.visAlertConfidence, 127 | 'RADD alert'); 128 | Map.addLayer( 129 | m.radd_alert.select('Date'), 130 | s.visAlertDate, 131 | 'RADD alert date'); 132 | 133 | /******************************************************************************* 134 | * Behaviors * 135 | ******************************************************************************/ 136 | 137 | var b = {}; 138 | 139 | /******************************************************************************* 140 | * Initialize * 141 | ******************************************************************************/ 142 | 143 | var App = {}; 144 | 145 | App.setupMap = function() { 146 | Map.setOptions('SATELLITE'); 147 | Map.style().set({ cursor: 'crosshair' }); 148 | Map.setCenter(10, -20, 3); 149 | }; 150 | 151 | App.run = function() { 152 | App.setupMap(); 153 | c.buildUI(); 154 | }; 155 | 156 | App.run(); 157 | -------------------------------------------------------------------------------- /04_timelapse.js: -------------------------------------------------------------------------------- 1 | // Copyright 2024 Google LLC 2 | 3 | /** 4 | * Visualize imagery with timelapse prototype 5 | * 6 | * @author Gennadii Donchyts (dgena@google.com) 7 | * @author Tyler Erickson (tyler@vorgeo.com) 8 | */ 9 | 10 | /******************************************************************************* 11 | * Model * 12 | ******************************************************************************/ 13 | 14 | var m = {}; 15 | 16 | m.start = '2022'; 17 | m.end = '2023'; 18 | 19 | var images = ee.ImageCollection('COPERNICUS/S2_HARMONIZED') 20 | .filterDate(m.start, m.end); 21 | 22 | 23 | var clouds = ee.ImageCollection('GOOGLE/CLOUD_SCORE_PLUS/V1/S2_HARMONIZED') 24 | .filterDate(m.start, m.end) 25 | .select('cs_cdf'); 26 | 27 | // Combine collections 28 | images = images.linkCollection(clouds, ['cs_cdf']); 29 | 30 | /******************************************************************************* 31 | * Styling * 32 | ******************************************************************************/ 33 | 34 | var s = {}; 35 | 36 | s.vis = { 37 | bands: ['B12', 'B8', 'B3'], 38 | min: 500, 39 | max: 3500 40 | }; 41 | 42 | 43 | /******************************************************************************* 44 | * Components * 45 | ******************************************************************************/ 46 | 47 | var c = {}; 48 | 49 | function Timelapse() { 50 | this.panel = null; 51 | this.mapControl = Map; // default map 52 | this.mapLayers = []; 53 | this.currentIndex = 0; 54 | } 55 | 56 | Timelapse.prototype.buildUI = function() { 57 | var self = this; 58 | var label = ui.Label(); 59 | 60 | var slider = ui.Slider(0, 1, 0, 1); 61 | slider.style().set({ width: '300px' }); 62 | slider.onSlide(function(i) { 63 | self.mapLayers[self.currentIndex].setOpacity(0); 64 | self.currentIndex = i; 65 | self.mapLayers[self.currentIndex].setShown(true); 66 | self.mapLayers[self.currentIndex].setOpacity(1); 67 | label.setValue(self.mapLayers[self.currentIndex].getName()); 68 | }); 69 | 70 | var button = ui.Button('Clear'); 71 | button.style().set({ 72 | margin: '0px', 73 | padding: '0px', 74 | width: '60px' 75 | }); 76 | 77 | button.onClick(function() { 78 | self.clear(); 79 | }); 80 | 81 | label.setValue(self.mapLayers[self.currentIndex].getName()); 82 | 83 | slider.setMax(self.mapLayers.length - 1); 84 | 85 | self.panel = ui.Panel([ 86 | label, 87 | ui.Panel([slider, button], ui.Panel.Layout.flow('horizontal')) 88 | ]); 89 | 90 | return self.panel; 91 | }; 92 | 93 | Timelapse.prototype.inspect = function(images, pt, vis) { 94 | 95 | var self = this; 96 | 97 | self.images = images; 98 | self.pt = pt; 99 | 100 | self.clear(); 101 | 102 | var scale = self.mapControl.getScale() ; 103 | var r = 200; 104 | var d = 50; 105 | var aoi = pt.buffer(r * scale); 106 | 107 | var mask = ee.Image().paint(pt.buffer(scale * (r - d)), 1).fastDistanceTransform().sqrt() 108 | .reproject(ee.Projection('EPSG:3857').atScale(scale)) 109 | .unitScale(0, d) 110 | .clip(aoi); 111 | mask = ee.Image(1).subtract(mask).pow(2).selfMask(); 112 | 113 | images.aggregate_array('system:time_start') 114 | .evaluate(function(times) { 115 | times.map(function(t, i) { 116 | var image = images 117 | .filter(ee.Filter.eq('system:time_start', t)) 118 | .first() 119 | .select(s.vis.bands) 120 | .clip(aoi) 121 | .updateMask(mask); 122 | 123 | var mapLayer = ui.Map.Layer(image, vis, new Date(t).toISOString(), true, i === 0 ? 1 : 0); 124 | self.mapLayers.push(mapLayer); 125 | self.mapControl.layers().add(mapLayer); 126 | }); 127 | 128 | if(self.panel) { 129 | self.mapControl.remove(self.panel); 130 | } 131 | 132 | self.panel = self.buildUI(); 133 | self.mapControl.add(self.panel); 134 | }); 135 | }; 136 | 137 | Timelapse.prototype.clear = function() { 138 | var self = this; 139 | 140 | self.mapControl.widgets().remove(self.panel); 141 | self.panel = null; 142 | self.panel = null; 143 | 144 | self.mapLayers.map(function(mapLayer) { 145 | self.mapControl.layers().remove(mapLayer); 146 | }); 147 | self.mapLayers.length = 0; 148 | self.currentIndex = 0; 149 | self.pt = null; 150 | self.images = null; 151 | }; 152 | 153 | c.timelapse = new Timelapse(); 154 | 155 | /******************************************************************************* 156 | * Composition * 157 | ******************************************************************************/ 158 | 159 | Map.setOptions('HYBRID'); 160 | Map.style().set({ cursor: 'crosshair' }); 161 | 162 | Map.addLayer(images.select(s.vis.bands) 163 | .reduce(ee.Reducer.percentile([25])) 164 | .rename(s.vis.bands), 165 | s.vis, 166 | 'images', 167 | false); 168 | Map.addLayer(clouds, {}, 'clouds', false); 169 | 170 | /******************************************************************************* 171 | * Behaviors * 172 | ******************************************************************************/ 173 | 174 | Map.onClick(function(pt) { 175 | pt = ee.Geometry.Point([pt.lon, pt.lat]); 176 | 177 | // Remove cloudy images 178 | var scale = Map.getScale(); 179 | var geom = pt.buffer(scale*50, scale*5); 180 | var imagesLocal = images.filterBounds(pt) 181 | .map(function(i) { 182 | var quality = i.select('cs_cdf') 183 | .reduceRegion(ee.Reducer.mean(), geom, scale*5); 184 | 185 | return i.set({ quality: quality.values().get(0) }); 186 | }) 187 | .filter(ee.Filter.gt('quality', 0.7)); 188 | 189 | // Inspect images 190 | c.timelapse.inspect(imagesLocal, pt, s.vis); 191 | }); 192 | 193 | /******************************************************************************* 194 | * Initialize * 195 | ******************************************************************************/ 196 | -------------------------------------------------------------------------------- /01_3_app_layers.js: -------------------------------------------------------------------------------- 1 | // Copyright 2024 Google LLC 2 | 3 | /** 4 | * STEP 3: Add layer images to the map 5 | * 6 | * @author Gennadii Donchyts (dgena@google.com) 7 | * @author Tyler Erickson (tyler@vorgeo.com) 8 | */ 9 | 10 | /******************************************************************************* 11 | * Model * 12 | ******************************************************************************/ 13 | 14 | var m = {}; 15 | 16 | // use RADD code from: https://code.earthengine.google.com/a8b49975261d3040d203e01ee48617ca 17 | m.radd = ee.ImageCollection('projects/radar-wur/raddalert/v1'); 18 | m.regions = ['africa', 'sa', 'asia', 'ca']; 19 | 20 | m.forest_baseline = m.radd.filterMetadata('layer','contains','forest_baseline') 21 | .mosaic(); 22 | 23 | m.radd_alert = ee.ImageCollection( 24 | m.regions.map( 25 | function(region) { 26 | return m.radd.filterMetadata('layer', 'contains', 'alert') 27 | .filterMetadata('geography', 'equals', region) 28 | .sort('system:time_end', false) 29 | .first(); 30 | } 31 | )).mosaic(); 32 | 33 | m.getAlertDate = function() { 34 | return m.radd_alert.select('Date'); 35 | }; 36 | 37 | m.getAlertConfidence = function() { 38 | return m.radd_alert.select('Alert'); 39 | }; 40 | 41 | 42 | m.layerInfos = [ 43 | { 44 | name: 'Alert Date', 45 | description: '', 46 | image: m.getAlertDate(), 47 | legend: { 48 | type: 'continuous', 49 | palette: ["ffffcc", "ffeda0", "fed976", "feb24c", "fd8d3c", "fc4e2a", "e31a1c", "bd0026", "800026"], 50 | min: 20000, 51 | max: 24000 52 | }, 53 | shown: true 54 | }, 55 | { 56 | name: 'Confidence', 57 | description: "", 58 | image: m.getAlertConfidence(), 59 | legend: { 60 | type: 'discrete', 61 | palette: ['00ffff', 'ea7e7d'], 62 | min: 2, 63 | max: 3 64 | }, 65 | shown: false 66 | }, 67 | { 68 | name: 'Primary humid tropical forest', 69 | description: 'Primary humid tropical forest mask 2001 from Turubanova et al (2018) with annual (Africa: 2001 - 2018; Other geographies: 2001 - 2019) forest loss (Hansen et al 2013) and mangroves (Bunting et al 2018) removed.', 70 | image: m.forest_baseline, 71 | legend: { 72 | type: 'discrete', 73 | palette: ['black'], 74 | opacity: 0.3 75 | }, 76 | shown: true 77 | }, 78 | ]; 79 | 80 | /******************************************************************************* 81 | * Styling * 82 | ******************************************************************************/ 83 | 84 | var s = {}; 85 | 86 | // RADD alert: 2 = unconfirmed (low confidence) alert; 3 = confirmed (high confidence) alert 87 | s.visAlertConfidence = { 88 | min: 2, 89 | max: 3, 90 | palette: ['00FFFF', 'EA7E7D'] 91 | }; 92 | s.visForestBaseline = { 93 | palette: ['black'], 94 | opacity: 0.3 95 | }; 96 | s.visAlertDate = { 97 | min: 20000, 98 | max: 24000, 99 | palette: ["ffffcc", "ffeda0", "fed976", "feb24c", "fd8d3c", 100 | "fc4e2a", "e31a1c", "bd0026", "800026"] 101 | }; 102 | 103 | s.panelLeft = {width: '400px'}; 104 | s.titleLabel = {fontSize: '22px', fontWeight: 'bold'}; 105 | s.layerPanel = {border: '1px solid black', margin: '5px 5px 0px 5px'}; 106 | s.layerPanelName = {fontWeight: 'bold'}; 107 | s.layerPanelDescription = {color: 'grey', fontSize: '11px'}; 108 | 109 | /******************************************************************************* 110 | * Components * 111 | ******************************************************************************/ 112 | 113 | var c = {}; 114 | 115 | c.titleLabel = ui.Label('Demo App', s.titleLabel); 116 | c.infoPanel = ui.Label('Lorem ipsum odor amet, consectetuer adipiscing elit. Ultrices facilisis ultricies nec nibh integer leo. Libero scelerisque purus ex ultricies ipsum ornare platea euismod. Cursus blandit duis ligula lobortis rhoncus quam per eu. Dictumst proin elementum sociosqu nascetur mi integer massa euismod.'); 117 | 118 | c.buildLayerPanelName = function(layerInfo) { 119 | return ui.Label(layerInfo.name, s.layerPanelName); 120 | }; 121 | c.buildLayerPanelDesc = function(layerInfo) { 122 | return ui.Label(layerInfo.description, s.layerPanelDescription); 123 | }; 124 | 125 | c.buildLayerLegendPanel = function(layerInfo) { 126 | return ui.Label(''); 127 | }; 128 | 129 | c.buildLayerLegendPanel = function(layerInfo) { 130 | return ui.Label(''); 131 | }; 132 | c.buildLayerControlsPanel = function(layerInfo) { 133 | return ui.Panel([ 134 | ui.Label(''), 135 | ui.Label('') 136 | ], 137 | ui.Panel.Layout.Flow('vertical') 138 | ); 139 | }; 140 | 141 | c.buildLayerPanel = function(layerInfo) { 142 | var layerPanel = ui.Panel([ 143 | c.buildLayerPanelName(layerInfo), 144 | ui.Panel([ 145 | c.buildLayerLegendPanel(layerInfo), 146 | c.buildLayerControlsPanel(layerInfo), 147 | ], ui.Panel.Layout.flow('horizontal')), 148 | c.buildLayerPanelDesc(layerInfo) 149 | ], 150 | ui.Panel.Layout.flow('vertical'), s.layerPanel); 151 | return layerPanel; 152 | }; 153 | 154 | c.layersPanel = ui.Panel(m.layerInfos.map(c.buildLayerPanel)); 155 | 156 | c.buildUI = function() { 157 | var panelLeft = ui.Panel([ 158 | c.titleLabel, 159 | c.infoPanel, 160 | c.layersPanel 161 | ], 162 | ui.Panel.Layout.flow('vertical'), 163 | s.panelLeft 164 | ); 165 | ui.root.widgets().insert(0, panelLeft); 166 | }; 167 | 168 | c.addMapLayers = function() { 169 | m.layerInfos.slice(0).reverse().map( 170 | function(layerInfo) { 171 | var legend = layerInfo.legend; 172 | 173 | var visParams = { 174 | min: legend.min, 175 | max: legend.max, 176 | palette: legend.palette 177 | }; 178 | 179 | var layer = ui.Map.Layer( 180 | layerInfo.image, 181 | visParams, 182 | layerInfo.name, 183 | layerInfo.shown, 184 | legend.opacity); 185 | Map.layers().add(layer); 186 | 187 | layerInfo.layer = layer; 188 | }); 189 | }; 190 | 191 | /******************************************************************************* 192 | * Composition * 193 | ******************************************************************************/ 194 | 195 | Map.addLayer( 196 | m.forest_baseline, 197 | s.visForestBaseline, 198 | 'Forest baseline'); 199 | Map.addLayer( 200 | m.radd_alert.select('Alert'), 201 | s.visAlertConfidence, 202 | 'RADD alert'); 203 | Map.addLayer( 204 | m.radd_alert.select('Date'), 205 | s.visAlertDate, 206 | 'RADD alert date'); 207 | 208 | /******************************************************************************* 209 | * Behaviors * 210 | ******************************************************************************/ 211 | 212 | var b = {}; 213 | 214 | /******************************************************************************* 215 | * Initialize * 216 | ******************************************************************************/ 217 | 218 | var App = {}; 219 | 220 | App.setupMap = function() { 221 | Map.setOptions('SATELLITE'); 222 | Map.style().set({ cursor: 'crosshair' }); 223 | Map.setCenter(10, -20, 3); 224 | }; 225 | 226 | App.run = function() { 227 | App.setupMap(); 228 | c.addMapLayers(); 229 | c.buildUI(); 230 | }; 231 | 232 | App.run(); 233 | -------------------------------------------------------------------------------- /01_4_app_layer_controls.js: -------------------------------------------------------------------------------- 1 | // Copyright 2024 Google LLC 2 | 3 | /** 4 | * STEP 4: Add layer controls 5 | * 6 | * @author Gennadii Donchyts (dgena@google.com) 7 | * @author Tyler Erickson (tyler@vorgeo.com) 8 | */ 9 | 10 | /******************************************************************************* 11 | * Model * 12 | ******************************************************************************/ 13 | 14 | var m = {}; 15 | 16 | // use RADD code from: https://code.earthengine.google.com/a8b49975261d3040d203e01ee48617ca 17 | m.radd = ee.ImageCollection('projects/radar-wur/raddalert/v1'); 18 | m.regions = ['africa', 'sa', 'asia', 'ca']; 19 | 20 | m.forest_baseline = m.radd.filterMetadata('layer','contains','forest_baseline') 21 | .mosaic(); 22 | 23 | m.radd_alert = ee.ImageCollection( 24 | m.regions.map( 25 | function(region) { 26 | return m.radd.filterMetadata('layer', 'contains', 'alert') 27 | .filterMetadata('geography', 'equals', region) 28 | .sort('system:time_end', false) 29 | .first(); 30 | } 31 | )).mosaic(); 32 | 33 | m.getAlertDate = function() { 34 | return m.radd_alert.select('Date'); 35 | }; 36 | 37 | m.getAlertConfidence = function() { 38 | return m.radd_alert.select('Alert'); 39 | }; 40 | 41 | 42 | m.layerInfos = [ 43 | { 44 | name: 'Alert Date', 45 | description: '', 46 | image: m.getAlertDate(), 47 | legend: { 48 | type: 'gradient', 49 | palette: ["ffffcc", "ffeda0", "fed976", "feb24c", "fd8d3c", "fc4e2a", "e31a1c", "bd0026", "800026"], 50 | min: 20000, 51 | max: 24000 52 | }, 53 | shown: true 54 | }, 55 | { 56 | name: 'Confidence', 57 | description: "", 58 | image: m.getAlertConfidence(), 59 | legend: { 60 | type: 'discrete', 61 | palette: ['00ffff', 'ea7e7d'], 62 | min: 2, 63 | max: 3 64 | }, 65 | shown: false 66 | }, 67 | { 68 | name: 'Primary humid tropical forest', 69 | description: 'Primary humid tropical forest mask 2001 from Turubanova et al (2018) with annual (Africa: 2001 - 2018; Other geographies: 2001 - 2019) forest loss (Hansen et al 2013) and mangroves (Bunting et al 2018) removed.', 70 | image: m.forest_baseline, 71 | legend: { 72 | type: 'discrete', 73 | palette: ['black'], 74 | opacity: 0.3 75 | }, 76 | shown: true 77 | }, 78 | ]; 79 | 80 | /******************************************************************************* 81 | * Styling * 82 | ******************************************************************************/ 83 | 84 | var s = {}; 85 | 86 | // RADD alert: 2 = unconfirmed (low confidence) alert; 3 = confirmed (high confidence) alert 87 | s.visAlertConfidence = { 88 | min: 2, 89 | max: 3, 90 | palette: ['00FFFF', 'EA7E7D'] 91 | }; 92 | s.visForestBaseline = { 93 | palette: ['black'], 94 | opacity: 0.3 95 | }; 96 | s.visAlertDate = { 97 | min: 20000, 98 | max: 24000, 99 | palette: ["ffffcc", "ffeda0", "fed976", "feb24c", "fd8d3c", 100 | "fc4e2a", "e31a1c", "bd0026", "800026"] 101 | }; 102 | 103 | s.panelLeft = {width: '400px'}; 104 | s.titleLabel = {fontSize: '22px', fontWeight: 'bold'}; 105 | s.layerPanel = {border: '1px solid black', margin: '5px 5px 0px 5px'}; 106 | s.layerPanelName = {fontWeight: 'bold'}; 107 | s.layerPanelDescription = {color: 'grey', fontSize: '11px'}; 108 | 109 | /******************************************************************************* 110 | * Components * 111 | ******************************************************************************/ 112 | 113 | var c = {}; 114 | 115 | c.titleLabel = ui.Label('Demo App', s.titleLabel); 116 | c.infoPanel = ui.Label('Lorem ipsum odor amet, consectetuer adipiscing elit. Ultrices facilisis ultricies nec nibh integer leo. Libero scelerisque purus ex ultricies ipsum ornare platea euismod. Cursus blandit duis ligula lobortis rhoncus quam per eu. Dictumst proin elementum sociosqu nascetur mi integer massa euismod.'); 117 | 118 | c.buildLayerPanelName = function(layerInfo) { 119 | return ui.Label(layerInfo.name, s.layerPanelName); 120 | }; 121 | c.buildLayerPanelDesc = function(layerInfo) { 122 | return ui.Label(layerInfo.description, s.layerPanelDescription); 123 | }; 124 | 125 | c.buildLayerLegendPanel = function(layerInfo) { 126 | return ui.Label( 127 | '', 128 | {border: '1px solid red'} // For development 129 | ); 130 | }; 131 | 132 | c.buildLayerControlsPanel = function(layerInfo) { 133 | 134 | function onLayerShownChanged(v) { 135 | layerInfo.layer.setShown(v); 136 | } 137 | 138 | var layerShownCheckbox = ui.Checkbox('', 139 | layerInfo.shown, 140 | onLayerShownChanged 141 | ); 142 | 143 | function onLayerOpacityChanged(v) { 144 | layerInfo.layer.setOpacity(v); 145 | } 146 | 147 | var layerOpacitySlider = ui.Slider(0, 1, 1, 0.1, null, 'horizontal', false, { stretch: 'horizontal' }); 148 | layerOpacitySlider.onSlide(onLayerOpacityChanged); 149 | 150 | return ui.Panel([ 151 | layerShownCheckbox, 152 | layerOpacitySlider 153 | ], 154 | ui.Panel.Layout.Flow('horizontal'), { 155 | border: '1px solid red', 156 | width: '200px' 157 | }); 158 | }; 159 | 160 | c.buildLayerPanel = function(layerInfo) { 161 | var layerPanel = ui.Panel([ 162 | c.buildLayerPanelName(layerInfo), 163 | ui.Panel([ 164 | c.buildLayerLegendPanel(layerInfo), 165 | ui.Label('', { stretch: 'horizontal' }), 166 | c.buildLayerControlsPanel(layerInfo), 167 | ], ui.Panel.Layout.flow('horizontal')), 168 | c.buildLayerPanelDesc(layerInfo) 169 | ], 170 | ui.Panel.Layout.flow('vertical'), s.layerPanel); 171 | return layerPanel; 172 | }; 173 | 174 | c.layersPanel = ui.Panel(m.layerInfos.map(c.buildLayerPanel)); 175 | 176 | c.buildUI = function() { 177 | var panelLeft = ui.Panel([ 178 | c.titleLabel, 179 | c.infoPanel, 180 | c.layersPanel 181 | ], 182 | ui.Panel.Layout.flow('vertical'), 183 | s.panelLeft 184 | ); 185 | ui.root.widgets().insert(0, panelLeft); 186 | }; 187 | 188 | c.addMapLayers = function() { 189 | m.layerInfos.slice(0).reverse().map( 190 | function(layerInfo) { 191 | var legend = layerInfo.legend; 192 | 193 | var visParams = { 194 | min: legend.min, 195 | max: legend.max, 196 | palette: legend.palette 197 | }; 198 | 199 | var layer = ui.Map.Layer( 200 | layerInfo.image, 201 | visParams, 202 | layerInfo.name, 203 | layerInfo.shown, 204 | legend.opacity); 205 | Map.layers().add(layer); 206 | 207 | layerInfo.layer = layer; 208 | }); 209 | }; 210 | 211 | /******************************************************************************* 212 | * Composition * 213 | ******************************************************************************/ 214 | 215 | Map.addLayer( 216 | m.forest_baseline, 217 | s.visForestBaseline, 218 | 'Forest baseline'); 219 | Map.addLayer( 220 | m.radd_alert.select('Alert'), 221 | s.visAlertConfidence, 222 | 'RADD alert'); 223 | Map.addLayer( 224 | m.radd_alert.select('Date'), 225 | s.visAlertDate, 226 | 'RADD alert date'); 227 | 228 | /******************************************************************************* 229 | * Behaviors * 230 | ******************************************************************************/ 231 | 232 | var b = {}; 233 | 234 | /******************************************************************************* 235 | * Initialize * 236 | ******************************************************************************/ 237 | 238 | var App = {}; 239 | 240 | App.setupMap = function() { 241 | Map.setOptions('SATELLITE'); 242 | Map.style().set({ cursor: 'crosshair' }); 243 | Map.setCenter(10, -20, 3); 244 | }; 245 | 246 | App.run = function() { 247 | App.setupMap(); 248 | c.addMapLayers(); 249 | c.buildUI(); 250 | }; 251 | 252 | App.run(); 253 | -------------------------------------------------------------------------------- /01_5_app_layer_legend.js: -------------------------------------------------------------------------------- 1 | // Copyright 2024 Google LLC 2 | 3 | /** 4 | * STEP 5: Build layer legend based on layer type 5 | * 6 | * @author Gennadii Donchyts (dgena@google.com) 7 | * @author Tyler Erickson (tyler@vorgeo.com) 8 | */ 9 | 10 | /******************************************************************************* 11 | * Model * 12 | ******************************************************************************/ 13 | 14 | var m = {}; 15 | 16 | // use RADD code from: https://code.earthengine.google.com/a8b49975261d3040d203e01ee48617ca 17 | m.radd = ee.ImageCollection('projects/radar-wur/raddalert/v1'); 18 | m.regions = ['africa', 'sa', 'asia', 'ca']; 19 | 20 | m.forest_baseline = m.radd.filterMetadata('layer','contains','forest_baseline') 21 | .mosaic(); 22 | 23 | m.radd_alert = ee.ImageCollection( 24 | m.regions.map( 25 | function(region) { 26 | return m.radd.filterMetadata('layer', 'contains', 'alert') 27 | .filterMetadata('geography', 'equals', region) 28 | .sort('system:time_end', false) 29 | .first(); 30 | } 31 | )).mosaic(); 32 | 33 | m.getAlertDate = function() { 34 | return m.radd_alert.select('Date'); 35 | }; 36 | 37 | m.getAlertConfidence = function() { 38 | return m.radd_alert.select('Alert'); 39 | }; 40 | 41 | 42 | m.layerInfos = [ 43 | { 44 | name: 'Alert Date', 45 | description: '', 46 | image: m.getAlertDate(), 47 | legend: { 48 | type: 'gradient', 49 | palette: ["ffffcc", "ffeda0", "fed976", "feb24c", "fd8d3c", "fc4e2a", "e31a1c", "bd0026", "800026"], 50 | min: 20000, 51 | max: 24000 52 | }, 53 | shown: true 54 | }, 55 | { 56 | name: 'Confidence', 57 | description: "", 58 | image: m.getAlertConfidence(), 59 | legend: { 60 | type: 'discrete', 61 | palette: ['00ffff', 'ea7e7d'], 62 | min: 2, 63 | max: 3 64 | }, 65 | shown: false 66 | }, 67 | { 68 | name: 'Primary humid tropical forest', 69 | description: 'Primary humid tropical forest mask 2001 from Turubanova et al (2018) with annual (Africa: 2001 - 2018; Other geographies: 2001 - 2019) forest loss (Hansen et al 2013) and mangroves (Bunting et al 2018) removed.', 70 | image: m.forest_baseline, 71 | legend: { 72 | type: 'discrete', 73 | palette: ['black'], 74 | opacity: 0.3 75 | }, 76 | shown: true 77 | }, 78 | ]; 79 | 80 | /******************************************************************************* 81 | * Styling * 82 | ******************************************************************************/ 83 | 84 | var s = {}; 85 | 86 | // RADD alert: 2 = unconfirmed (low confidence) alert; 3 = confirmed (high confidence) alert 87 | s.visAlertConfidence = { 88 | min: 2, 89 | max: 3, 90 | palette: ['00FFFF', 'EA7E7D'] 91 | }; 92 | s.visForestBaseline = { 93 | palette: ['black'], 94 | opacity: 0.3 95 | }; 96 | s.visAlertDate = { 97 | min: 20000, 98 | max: 24000, 99 | palette: ["ffffcc", "ffeda0", "fed976", "feb24c", "fd8d3c", 100 | "fc4e2a", "e31a1c", "bd0026", "800026"] 101 | }; 102 | 103 | s.panelLeft = {width: '400px'}; 104 | s.titleLabel = {fontSize: '22px', fontWeight: 'bold'}; 105 | s.layerPanel = {border: '1px solid black', margin: '5px 5px 0px 5px'}; 106 | s.layerPanelName = {fontWeight: 'bold'}; 107 | s.layerPanelDescription = {color: 'grey', fontSize: '11px'}; 108 | 109 | /******************************************************************************* 110 | * Components * 111 | ******************************************************************************/ 112 | 113 | var c = {}; 114 | 115 | c.titleLabel = ui.Label('Demo App', s.titleLabel); 116 | c.infoPanel = ui.Label('Lorem ipsum odor amet, consectetuer adipiscing elit. Ultrices facilisis ultricies nec nibh integer leo. Libero scelerisque purus ex ultricies ipsum ornare platea euismod. Cursus blandit duis ligula lobortis rhoncus quam per eu. Dictumst proin elementum sociosqu nascetur mi integer massa euismod.'); 117 | 118 | c.buildLayerPanelName = function(layerInfo) { 119 | return ui.Label(layerInfo.name, s.layerPanelName); 120 | }; 121 | c.buildLayerPanelDesc = function(layerInfo) { 122 | return ui.Label(layerInfo.description, s.layerPanelDescription); 123 | }; 124 | 125 | c.buildLayerLegendPanel = function(layerInfo) { 126 | function createLayerLegendGradient(layerInfo) { 127 | return ui.Label('', { 128 | border: '1px solid red', 129 | }); 130 | } 131 | 132 | function createLayerLegendDiscrete(layerInfo) { 133 | return ui.Label('', { 134 | border: '1px solid red', 135 | }); 136 | } 137 | 138 | var legendBuilders = { 139 | 'gradient': createLayerLegendGradient, 140 | 'discrete': createLayerLegendDiscrete 141 | }; 142 | 143 | return legendBuilders[layerInfo.legend.type](layerInfo); 144 | }; 145 | 146 | c.buildLayerControlsPanel = function(layerInfo) { 147 | 148 | function onLayerShownChanged(v) { 149 | layerInfo.layer.setShown(v); 150 | } 151 | 152 | var layerShownCheckbox = ui.Checkbox('', 153 | layerInfo.shown, 154 | onLayerShownChanged 155 | ); 156 | 157 | function onLayerOpacityChanged(v) { 158 | layerInfo.layer.setOpacity(v); 159 | } 160 | 161 | var layerOpacitySlider = ui.Slider(0, 1, 1, 0.1, null, 'horizontal', false, { stretch: 'horizontal' }); 162 | layerOpacitySlider.onSlide(onLayerOpacityChanged); 163 | 164 | return ui.Panel([ 165 | layerShownCheckbox, 166 | layerOpacitySlider 167 | ], 168 | ui.Panel.Layout.Flow('horizontal'), { 169 | border: '1px solid red', 170 | width: '200px' 171 | }); 172 | }; 173 | 174 | c.buildLayerPanel = function(layerInfo) { 175 | var layerPanel = ui.Panel([ 176 | c.buildLayerPanelName(layerInfo), 177 | ui.Panel([ 178 | c.buildLayerLegendPanel(layerInfo), 179 | ui.Label('', { stretch: 'horizontal' }), 180 | c.buildLayerControlsPanel(layerInfo), 181 | ], ui.Panel.Layout.flow('horizontal')), 182 | c.buildLayerPanelDesc(layerInfo) 183 | ], 184 | ui.Panel.Layout.flow('vertical'), s.layerPanel); 185 | return layerPanel; 186 | }; 187 | 188 | c.layersPanel = ui.Panel(m.layerInfos.map(c.buildLayerPanel)); 189 | 190 | c.buildUI = function() { 191 | var panelLeft = ui.Panel([ 192 | c.titleLabel, 193 | c.infoPanel, 194 | c.layersPanel 195 | ], 196 | ui.Panel.Layout.flow('vertical'), 197 | s.panelLeft 198 | ); 199 | ui.root.widgets().insert(0, panelLeft); 200 | }; 201 | 202 | c.addMapLayers = function() { 203 | m.layerInfos.slice(0).reverse().map( 204 | function(layerInfo) { 205 | var legend = layerInfo.legend; 206 | 207 | var visParams = { 208 | min: legend.min, 209 | max: legend.max, 210 | palette: legend.palette 211 | }; 212 | 213 | var layer = ui.Map.Layer( 214 | layerInfo.image, 215 | visParams, 216 | layerInfo.name, 217 | layerInfo.shown, 218 | legend.opacity); 219 | Map.layers().add(layer); 220 | 221 | layerInfo.layer = layer; 222 | }); 223 | }; 224 | 225 | /******************************************************************************* 226 | * Composition * 227 | ******************************************************************************/ 228 | 229 | Map.addLayer( 230 | m.forest_baseline, 231 | s.visForestBaseline, 232 | 'Forest baseline'); 233 | Map.addLayer( 234 | m.radd_alert.select('Alert'), 235 | s.visAlertConfidence, 236 | 'RADD alert'); 237 | Map.addLayer( 238 | m.radd_alert.select('Date'), 239 | s.visAlertDate, 240 | 'RADD alert date'); 241 | 242 | /******************************************************************************* 243 | * Behaviors * 244 | ******************************************************************************/ 245 | 246 | var b = {}; 247 | 248 | /******************************************************************************* 249 | * Initialize * 250 | ******************************************************************************/ 251 | 252 | var App = {}; 253 | 254 | App.setupMap = function() { 255 | Map.setOptions('SATELLITE'); 256 | Map.style().set({ cursor: 'crosshair' }); 257 | Map.setCenter(10, -20, 3); 258 | }; 259 | 260 | App.run = function() { 261 | App.setupMap(); 262 | c.addMapLayers(); 263 | c.buildUI(); 264 | }; 265 | 266 | App.run(); 267 | -------------------------------------------------------------------------------- /01_6_app_layer_legend_controls.js: -------------------------------------------------------------------------------- 1 | // Copyright 2024 Google LLC 2 | 3 | /** 4 | * STEP 6: Build layer legend controls 5 | * 6 | * @author Gennadii Donchyts (dgena@google.com) 7 | * @author Tyler Erickson (tyler@vorgeo.com) 8 | */ 9 | 10 | /******************************************************************************* 11 | * Model * 12 | ******************************************************************************/ 13 | 14 | var m = {}; 15 | 16 | // use RADD code from: https://code.earthengine.google.com/a8b49975261d3040d203e01ee48617ca 17 | m.radd = ee.ImageCollection('projects/radar-wur/raddalert/v1'); 18 | m.regions = ['africa', 'sa', 'asia', 'ca']; 19 | 20 | m.forest_baseline = m.radd.filterMetadata('layer','contains','forest_baseline') 21 | .mosaic(); 22 | 23 | m.radd_alert = ee.ImageCollection( 24 | m.regions.map( 25 | function(region) { 26 | return m.radd.filterMetadata('layer', 'contains', 'alert') 27 | .filterMetadata('geography', 'equals', region) 28 | .sort('system:time_end', false) 29 | .first(); 30 | } 31 | )).mosaic(); 32 | 33 | m.getAlertDate = function() { 34 | return m.radd_alert.select('Date'); 35 | }; 36 | 37 | m.getAlertConfidence = function() { 38 | return m.radd_alert.select('Alert'); 39 | }; 40 | 41 | 42 | m.layerInfos = [ 43 | { 44 | name: 'Alert Date', 45 | description: '', 46 | image: m.getAlertDate(), 47 | legend: { 48 | type: 'gradient', 49 | palette: ["ffffcc", "ffeda0", "fed976", "feb24c", "fd8d3c", "fc4e2a", "e31a1c", "bd0026", "800026"], 50 | min: 20000, 51 | max: 24000, 52 | labelMin: '2000', 53 | labelMax: '2024' 54 | }, 55 | shown: true 56 | }, 57 | { 58 | name: 'Confidence', 59 | description: "", 60 | image: m.getAlertConfidence(), 61 | legend: { 62 | type: 'discrete', 63 | palette: ['00ffff', 'ea7e7d'], 64 | min: 2, 65 | max: 3, 66 | labels: ['2', '3'] 67 | }, 68 | shown: false 69 | }, 70 | { 71 | name: 'Primary humid tropical forest', 72 | description: 'Primary humid tropical forest mask 2001 from Turubanova et al (2018) with annual (Africa: 2001 - 2018; Other geographies: 2001 - 2019) forest loss (Hansen et al 2013) and mangroves (Bunting et al 2018) removed.', 73 | image: m.forest_baseline, 74 | legend: { 75 | type: 'discrete', 76 | palette: ['black'], 77 | opacity: 0.3, 78 | labels: [''] 79 | }, 80 | shown: true 81 | }, 82 | ]; 83 | 84 | /******************************************************************************* 85 | * Styling * 86 | ******************************************************************************/ 87 | 88 | var s = {}; 89 | 90 | // RADD alert: 2 = unconfirmed (low confidence) alert; 3 = confirmed (high confidence) alert 91 | s.visAlertConfidence = { 92 | min: 2, 93 | max: 3, 94 | palette: ['00FFFF', 'EA7E7D'] 95 | }; 96 | s.visForestBaseline = { 97 | palette: ['black'], 98 | opacity: 0.3 99 | }; 100 | s.visAlertDate = { 101 | min: 20000, 102 | max: 24000, 103 | palette: ["ffffcc", "ffeda0", "fed976", "feb24c", "fd8d3c", 104 | "fc4e2a", "e31a1c", "bd0026", "800026"] 105 | }; 106 | 107 | s.panelLeft = {width: '400px'}; 108 | s.titleLabel = {fontSize: '22px', fontWeight: 'bold'}; 109 | s.layerPanel = {border: '1px solid black', margin: '5px 5px 0px 5px'}; 110 | s.layerPanelName = {fontWeight: 'bold'}; 111 | s.layerPanelDescription = {color: 'grey', fontSize: '11px'}; 112 | 113 | /******************************************************************************* 114 | * Components * 115 | ******************************************************************************/ 116 | 117 | var c = {}; 118 | 119 | c.titleLabel = ui.Label('Demo App', s.titleLabel); 120 | c.infoPanel = ui.Label('Lorem ipsum odor amet, consectetuer adipiscing elit. Ultrices facilisis ultricies nec nibh integer leo. Libero scelerisque purus ex ultricies ipsum ornare platea euismod. Cursus blandit duis ligula lobortis rhoncus quam per eu. Dictumst proin elementum sociosqu nascetur mi integer massa euismod.'); 121 | 122 | c.buildLayerPanelName = function(layerInfo) { 123 | return ui.Label(layerInfo.name, s.layerPanelName); 124 | }; 125 | c.buildLayerPanelDesc = function(layerInfo) { 126 | return ui.Label(layerInfo.description, s.layerPanelDescription); 127 | }; 128 | 129 | c.buildLayerLegendPanel = function(layerInfo) { 130 | function createLayerLegendGradient(layerInfo) { 131 | var colorBar = ui.Thumbnail({ 132 | image: ee.Image.pixelLonLat().select(0), 133 | params: { 134 | bbox: [0, 0, 1, 0.1], dimensions: '100x10', format: 'png', 135 | min: 0, max: 1, palette: layerInfo.legend.palette 136 | }, 137 | style: {stretch: 'horizontal', margin: '0px 8px', maxHeight: '24px'}, 138 | }); 139 | 140 | var legendLabels = ui.Panel({ 141 | widgets: [ 142 | ui.Label(layerInfo.legend.labelMin, {margin: '4px 8px'}), 143 | ui.Label(layerInfo.legend.labelMax, {margin: '4px 8px', stretch: 'horizontal', textAlign: 'right'}) 144 | ], 145 | layout: ui.Panel.Layout.flow('horizontal') 146 | }); 147 | 148 | return ui.Panel([colorBar, legendLabels]); 149 | } 150 | 151 | function createLayerLegendDiscrete(layerInfo) { 152 | var labels = layerInfo.legend.palette.map(function(color, i) { 153 | return ui.Panel([ 154 | ui.Label('', { 155 | width: '15px', 156 | height: '15px', 157 | backgroundColor: color, 158 | border: '1px solid black', 159 | margin: '3px 10px' 160 | }), 161 | ui.Label(layerInfo.legend.labels[i], { 162 | margin: '3px 10px 0px 0px' 163 | }) 164 | ], ui.Panel.Layout.flow('horizontal')); 165 | }); 166 | 167 | var panel = ui.Panel({ 168 | widgets: labels, 169 | layout: ui.Panel.Layout.flow('vertical') 170 | }); 171 | 172 | return panel; 173 | } 174 | 175 | var legendBuilders = { 176 | 'gradient': createLayerLegendGradient, 177 | 'discrete': createLayerLegendDiscrete 178 | }; 179 | 180 | return legendBuilders[layerInfo.legend.type](layerInfo); 181 | }; 182 | 183 | c.buildLayerControlsPanel = function(layerInfo) { 184 | 185 | function onLayerShownChanged(v) { 186 | layerInfo.layer.setShown(v); 187 | } 188 | 189 | var layerShownCheckbox = ui.Checkbox('', 190 | layerInfo.shown, 191 | onLayerShownChanged 192 | ); 193 | 194 | function onLayerOpacityChanged(v) { 195 | layerInfo.layer.setOpacity(v); 196 | } 197 | 198 | var layerOpacitySlider = ui.Slider(0, 1, 1, 0.1, null, 'horizontal', false, { stretch: 'horizontal' }); 199 | layerOpacitySlider.onSlide(onLayerOpacityChanged); 200 | 201 | return ui.Panel([ 202 | layerShownCheckbox, 203 | layerOpacitySlider 204 | ], 205 | ui.Panel.Layout.Flow('horizontal'), { 206 | width: '200px' 207 | }); 208 | }; 209 | 210 | c.buildLayerPanel = function(layerInfo) { 211 | var layerPanel = ui.Panel([ 212 | c.buildLayerPanelName(layerInfo), 213 | ui.Panel([ 214 | c.buildLayerLegendPanel(layerInfo), 215 | ui.Label('', { stretch: 'horizontal' }), 216 | c.buildLayerControlsPanel(layerInfo), 217 | ], ui.Panel.Layout.flow('horizontal')), 218 | c.buildLayerPanelDesc(layerInfo) 219 | ], 220 | ui.Panel.Layout.flow('vertical'), s.layerPanel); 221 | return layerPanel; 222 | }; 223 | 224 | c.layersPanel = ui.Panel(m.layerInfos.map(c.buildLayerPanel)); 225 | 226 | c.buildUI = function() { 227 | var panelLeft = ui.Panel([ 228 | c.titleLabel, 229 | c.infoPanel, 230 | c.layersPanel 231 | ], 232 | ui.Panel.Layout.flow('vertical'), 233 | s.panelLeft 234 | ); 235 | ui.root.widgets().insert(0, panelLeft); 236 | }; 237 | 238 | c.addMapLayers = function() { 239 | m.layerInfos.slice(0).reverse().map( 240 | function(layerInfo) { 241 | var legend = layerInfo.legend; 242 | 243 | var visParams = { 244 | min: legend.min, 245 | max: legend.max, 246 | palette: legend.palette 247 | }; 248 | 249 | var layer = ui.Map.Layer( 250 | layerInfo.image, 251 | visParams, 252 | layerInfo.name, 253 | layerInfo.shown, 254 | legend.opacity); 255 | Map.layers().add(layer); 256 | 257 | layerInfo.layer = layer; 258 | }); 259 | }; 260 | 261 | /******************************************************************************* 262 | * Composition * 263 | ******************************************************************************/ 264 | 265 | Map.addLayer( 266 | m.forest_baseline, 267 | s.visForestBaseline, 268 | 'Forest baseline'); 269 | Map.addLayer( 270 | m.radd_alert.select('Alert'), 271 | s.visAlertConfidence, 272 | 'RADD alert'); 273 | Map.addLayer( 274 | m.radd_alert.select('Date'), 275 | s.visAlertDate, 276 | 'RADD alert date'); 277 | 278 | /******************************************************************************* 279 | * Behaviors * 280 | ******************************************************************************/ 281 | 282 | var b = {}; 283 | 284 | /******************************************************************************* 285 | * Initialize * 286 | ******************************************************************************/ 287 | 288 | var App = {}; 289 | 290 | App.setupMap = function() { 291 | Map.setOptions('SATELLITE'); 292 | Map.style().set({ cursor: 'crosshair' }); 293 | Map.setCenter(10, -20, 3); 294 | }; 295 | 296 | App.run = function() { 297 | App.setupMap(); 298 | c.addMapLayers(); 299 | c.buildUI(); 300 | }; 301 | 302 | App.run(); 303 | -------------------------------------------------------------------------------- /01_7_app_query_alert_date.js: -------------------------------------------------------------------------------- 1 | // Copyright 2024 Google LLC 2 | 3 | /** 4 | * STEP 7: Display layer info on map click 5 | * 6 | * Mention: 7 | * 1. Inform user on what's going on in case of any long-running actions 8 | * 2. Beware of conflicting asynchronous updates of the UI (!) 9 | * 10 | * @author Gennadii Donchyts (dgena@google.com) 11 | * @author Tyler Erickson (tyler@vorgeo.com) 12 | */ 13 | 14 | /******************************************************************************* 15 | * Model * 16 | ******************************************************************************/ 17 | 18 | var m = {}; 19 | 20 | // use RADD code from: https://code.earthengine.google.com/a8b49975261d3040d203e01ee48617ca 21 | m.radd = ee.ImageCollection('projects/radar-wur/raddalert/v1'); 22 | m.regions = ['africa', 'sa', 'asia', 'ca']; 23 | 24 | m.forest_baseline = m.radd.filterMetadata('layer','contains','forest_baseline') 25 | .mosaic(); 26 | 27 | m.radd_alert = ee.ImageCollection( 28 | m.regions.map( 29 | function(region) { 30 | return m.radd.filterMetadata('layer', 'contains', 'alert') 31 | .filterMetadata('geography', 'equals', region) 32 | .sort('system:time_end', false) 33 | .first(); 34 | } 35 | )).mosaic(); 36 | 37 | m.getAlertDate = function() { 38 | return m.radd_alert.select('Date'); 39 | }; 40 | 41 | m.getAlertConfidence = function() { 42 | return m.radd_alert.select('Alert'); 43 | }; 44 | 45 | 46 | m.layerInfos = [ 47 | { 48 | name: 'Alert Date', 49 | description: '', 50 | image: m.getAlertDate(), 51 | legend: { 52 | type: 'gradient', 53 | palette: ["ffffcc", "ffeda0", "fed976", "feb24c", "fd8d3c", "fc4e2a", "e31a1c", "bd0026", "800026"], 54 | min: 20000, 55 | max: 24000, 56 | labelMin: '2000', 57 | labelMax: '2024' 58 | }, 59 | shown: true 60 | }, 61 | { 62 | name: 'Confidence', 63 | description: "", 64 | image: m.getAlertConfidence(), 65 | legend: { 66 | type: 'discrete', 67 | palette: ['00ffff', 'ea7e7d'], 68 | min: 2, 69 | max: 3, 70 | labels: ['2', '3'] 71 | }, 72 | shown: false 73 | }, 74 | { 75 | name: 'Primary humid tropical forest', 76 | description: 'Primary humid tropical forest mask 2001 from Turubanova et al (2018) with annual (Africa: 2001 - 2018; Other geographies: 2001 - 2019) forest loss (Hansen et al 2013) and mangroves (Bunting et al 2018) removed.', 77 | image: m.forest_baseline, 78 | legend: { 79 | type: 'discrete', 80 | palette: ['black'], 81 | opacity: 0.3, 82 | labels: [''] 83 | }, 84 | shown: true 85 | }, 86 | ]; 87 | 88 | 89 | m.toDateFromYYJJJ = function(v) { 90 | v = ee.Number(v); 91 | var year = v.divide(1000).floor().add(2000); 92 | var doy = v.mod(1000); 93 | 94 | return ee.Date.fromYMD(year, 1, 1).advance(doy, 'day'); 95 | }; 96 | 97 | /******************************************************************************* 98 | * Styling * 99 | ******************************************************************************/ 100 | 101 | var s = {}; 102 | 103 | // RADD alert: 2 = unconfirmed (low confidence) alert; 3 = confirmed (high confidence) alert 104 | s.visAlertConfidence = { 105 | min: 2, 106 | max: 3, 107 | palette: ['00FFFF', 'EA7E7D'] 108 | }; 109 | s.visForestBaseline = { 110 | palette: ['black'], 111 | opacity: 0.3 112 | }; 113 | s.visAlertDate = { 114 | min: 20000, 115 | max: 24000, 116 | palette: ["ffffcc", "ffeda0", "fed976", "feb24c", "fd8d3c", 117 | "fc4e2a", "e31a1c", "bd0026", "800026"] 118 | }; 119 | 120 | s.panelLeft = {width: '400px'}; 121 | s.titleLabel = {fontSize: '22px', fontWeight: 'bold'}; 122 | s.layerPanel = {border: '1px solid black', margin: '5px 5px 0px 5px'}; 123 | s.layerPanelName = {fontWeight: 'bold'}; 124 | s.layerPanelDescription = {color: 'grey', fontSize: '11px'}; 125 | 126 | /******************************************************************************* 127 | * Components * 128 | ******************************************************************************/ 129 | 130 | var c = {}; 131 | 132 | c.titleLabel = ui.Label('Demo App', s.titleLabel); 133 | c.infoPanel = ui.Label('Lorem ipsum odor amet, consectetuer adipiscing elit. Ultrices facilisis ultricies nec nibh integer leo. Libero scelerisque purus ex ultricies ipsum ornare platea euismod. Cursus blandit duis ligula lobortis rhoncus quam per eu. Dictumst proin elementum sociosqu nascetur mi integer massa euismod.'); 134 | 135 | c.buildLayerPanelName = function(layerInfo) { 136 | return ui.Label(layerInfo.name, s.layerPanelName); 137 | }; 138 | c.buildLayerPanelDesc = function(layerInfo) { 139 | return ui.Label(layerInfo.description, s.layerPanelDescription); 140 | }; 141 | 142 | c.buildLayerLegendPanel = function(layerInfo) { 143 | function createLayerLegendGradient(layerInfo) { 144 | var colorBar = ui.Thumbnail({ 145 | image: ee.Image.pixelLonLat().select(0), 146 | params: { 147 | bbox: [0, 0, 1, 0.1], dimensions: '100x10', format: 'png', 148 | min: 0, max: 1, palette: layerInfo.legend.palette 149 | }, 150 | style: {stretch: 'horizontal', margin: '0px 8px', maxHeight: '24px'}, 151 | }); 152 | 153 | var legendLabels = ui.Panel({ 154 | widgets: [ 155 | ui.Label(layerInfo.legend.labelMin, {margin: '4px 8px'}), 156 | ui.Label(layerInfo.legend.labelMax, {margin: '4px 8px', stretch: 'horizontal', textAlign: 'right'}) 157 | ], 158 | layout: ui.Panel.Layout.flow('horizontal') 159 | }); 160 | 161 | return ui.Panel([colorBar, legendLabels]); 162 | } 163 | 164 | function createLayerLegendDiscrete(layerInfo) { 165 | var labels = layerInfo.legend.palette.map(function(color, i) { 166 | return ui.Panel([ 167 | ui.Label('', { 168 | width: '15px', 169 | height: '15px', 170 | backgroundColor: color, 171 | border: '1px solid black', 172 | margin: '3px 10px' 173 | }), 174 | ui.Label(layerInfo.legend.labels[i], { 175 | margin: '3px 10px 0px 0px' 176 | }) 177 | ], ui.Panel.Layout.flow('horizontal')); 178 | }); 179 | 180 | var panel = ui.Panel({ 181 | widgets: labels, 182 | layout: ui.Panel.Layout.flow('vertical') 183 | }); 184 | 185 | return panel; 186 | } 187 | 188 | var legendBuilders = { 189 | 'gradient': createLayerLegendGradient, 190 | 'discrete': createLayerLegendDiscrete 191 | }; 192 | 193 | return legendBuilders[layerInfo.legend.type](layerInfo); 194 | }; 195 | 196 | c.buildLayerControlsPanel = function(layerInfo) { 197 | 198 | function onLayerShownChanged(v) { 199 | layerInfo.layer.setShown(v); 200 | } 201 | 202 | var layerShownCheckbox = ui.Checkbox('', 203 | layerInfo.shown, 204 | onLayerShownChanged 205 | ); 206 | 207 | function onLayerOpacityChanged(v) { 208 | layerInfo.layer.setOpacity(v); 209 | } 210 | 211 | var layerOpacitySlider = ui.Slider(0, 1, 1, 0.1, null, 'horizontal', false, { stretch: 'horizontal' }); 212 | layerOpacitySlider.onSlide(onLayerOpacityChanged); 213 | 214 | return ui.Panel([ 215 | layerShownCheckbox, 216 | layerOpacitySlider 217 | ], 218 | ui.Panel.Layout.Flow('horizontal'), { 219 | width: '200px' 220 | }); 221 | }; 222 | 223 | c.buildLayerPanel = function(layerInfo) { 224 | var layerPanel = ui.Panel([ 225 | c.buildLayerPanelName(layerInfo), 226 | ui.Panel([ 227 | c.buildLayerLegendPanel(layerInfo), 228 | ui.Label('', { stretch: 'horizontal' }), 229 | c.buildLayerControlsPanel(layerInfo), 230 | ], ui.Panel.Layout.flow('horizontal')), 231 | c.buildLayerPanelDesc(layerInfo) 232 | ], 233 | ui.Panel.Layout.flow('vertical'), s.layerPanel); 234 | return layerPanel; 235 | }; 236 | 237 | c.layersPanel = ui.Panel(m.layerInfos.map(c.buildLayerPanel)); 238 | 239 | c.buildLayerInspectionPanel = function() { 240 | c.clickedPointLabel = ui.Label('Click on the map to query alert date -->'); 241 | 242 | c.clickedPointLayer = ui.Map.Layer(ee.FeatureCollection([]), { color: 'yellow' }, 'clicked point'); 243 | Map.layers().add(c.clickedPointLayer); 244 | 245 | return c.clickedPointLabel; 246 | }; 247 | 248 | c.buildUI = function() { 249 | var panelLeft = ui.Panel([ 250 | c.titleLabel, 251 | c.infoPanel, 252 | c.layersPanel, 253 | c.buildLayerInspectionPanel() 254 | ], 255 | ui.Panel.Layout.flow('vertical'), 256 | s.panelLeft 257 | ); 258 | ui.root.widgets().insert(0, panelLeft); 259 | }; 260 | 261 | c.addMapLayers = function() { 262 | m.layerInfos.slice(0).reverse().map( 263 | function(layerInfo) { 264 | var legend = layerInfo.legend; 265 | 266 | var visParams = { 267 | min: legend.min, 268 | max: legend.max, 269 | palette: legend.palette 270 | }; 271 | 272 | var layer = ui.Map.Layer( 273 | layerInfo.image, 274 | visParams, 275 | layerInfo.name, 276 | layerInfo.shown, 277 | legend.opacity); 278 | Map.layers().add(layer); 279 | 280 | layerInfo.layer = layer; 281 | }); 282 | }; 283 | 284 | /******************************************************************************* 285 | * Behaviors * 286 | ******************************************************************************/ 287 | 288 | var b = {}; 289 | 290 | b.queryAlertDate = function(pt) { 291 | var lon = pt.lon; 292 | var lat = pt.lat; 293 | pt = ee.Geometry.Point([lon, lat]); 294 | 295 | App.clickedPointLayer.setEeObject(pt); 296 | 297 | // query alert date value at a given point 298 | var image = m.getAlertDate(); 299 | var date_YYJJJ = image.reduceRegion(ee.Reducer.first(), pt, 10).get('Date'); 300 | 301 | var date = m.toDateFromYYJJJ(date_YYJJJ); 302 | 303 | var dateStr = date.format('YYYY-MM-dd'); 304 | 305 | dateStr.evaluate(function(s) { 306 | App.clickedPointLabel.setValue('Clicked point date: ' + s); 307 | }); 308 | }; 309 | 310 | Map.onClick(b.queryAlertDate); 311 | 312 | /******************************************************************************* 313 | * Initialize * 314 | ******************************************************************************/ 315 | 316 | var App = {}; 317 | 318 | App.setupMap = function() { 319 | Map.setOptions('SATELLITE'); 320 | Map.style().set({ cursor: 'crosshair' }); 321 | Map.setCenter(10, -20, 3); 322 | }; 323 | 324 | App.run = function() { 325 | App.setupMap(); 326 | c.addMapLayers(); 327 | c.buildUI(); 328 | }; 329 | 330 | App.run(); 331 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | // Copyright 2024 Google LLC 2 | 3 | /** 4 | * The final app 5 | * 6 | * @author Gennadii Donchyts (dgena@google.com) 7 | * @author Tyler Erickson (tyler@vorgeo.com) 8 | */ 9 | 10 | /******************************************************************************* 11 | * Model * 12 | ******************************************************************************/ 13 | 14 | var m = {}; 15 | 16 | // use RADD code from: https://code.earthengine.google.com/a8b49975261d3040d203e01ee48617ca 17 | m.radd = ee.ImageCollection('projects/radar-wur/raddalert/v1'); 18 | m.regions = ['africa', 'sa', 'asia', 'ca']; 19 | 20 | m.forest_baseline = m.radd.filterMetadata('layer','contains','forest_baseline') 21 | .mosaic(); 22 | 23 | m.radd_alert = ee.ImageCollection( 24 | m.regions.map( 25 | function(region) { 26 | return m.radd.filterMetadata('layer', 'contains', 'alert') 27 | .filterMetadata('geography', 'equals', region) 28 | .sort('system:time_end', false) 29 | .first(); 30 | } 31 | )).mosaic(); 32 | 33 | m.getAlertDate = function() { 34 | return m.radd_alert.select('Date'); 35 | }; 36 | 37 | m.getAlertConfidence = function() { 38 | return m.radd_alert.select('Alert'); 39 | }; 40 | 41 | 42 | m.layerInfos = [ 43 | { 44 | name: 'Alert Date', 45 | description: '', 46 | image: m.getAlertDate(), 47 | legend: { 48 | type: 'gradient', 49 | palette: ["ffffcc", "ffeda0", "fed976", "feb24c", "fd8d3c", "fc4e2a", "e31a1c", "bd0026", "800026"], 50 | min: 20000, 51 | max: 24000, 52 | labelMin: '2000', 53 | labelMax: '2024' 54 | }, 55 | shown: true 56 | }, 57 | { 58 | name: 'Confidence', 59 | description: "", 60 | image: m.getAlertConfidence(), 61 | legend: { 62 | type: 'discrete', 63 | palette: ['00ffff', 'ea7e7d'], 64 | min: 2, 65 | max: 3, 66 | labels: ['2', '3'] 67 | }, 68 | shown: false 69 | }, 70 | { 71 | name: 'Primary humid tropical forest', 72 | description: 'Primary humid tropical forest mask 2001 from Turubanova et al (2018) with annual (Africa: 2001 - 2018; Other geographies: 2001 - 2019) forest loss (Hansen et al 2013) and mangroves (Bunting et al 2018) removed.', 73 | image: m.forest_baseline, 74 | legend: { 75 | type: 'discrete', 76 | palette: ['black'], 77 | opacity: 0.3, 78 | labels: [''] 79 | }, 80 | shown: true 81 | }, 82 | ]; 83 | 84 | /*** 85 | * Convert data from RADD format YYJJJ to ee.Date() 86 | */ 87 | m.toDateFromYYJJJ = function(v) { 88 | v = ee.Number(v); 89 | var year = v.divide(1000).floor().add(2000); 90 | var doy = v.mod(1000); 91 | 92 | return ee.Date.fromYMD(year, 1, 1).advance(doy, 'day'); 93 | }; 94 | 95 | /******************************************************************************* 96 | * Styling * 97 | ******************************************************************************/ 98 | 99 | var s = {}; 100 | 101 | // RADD alert: 2 = unconfirmed (low confidence) alert; 3 = confirmed (high confidence) alert 102 | s.visAlertConfidence = { 103 | min: 2, 104 | max: 3, 105 | palette: ['00FFFF', 'EA7E7D'] 106 | }; 107 | s.visForestBaseline = { 108 | palette: ['black'], 109 | opacity: 0.3 110 | }; 111 | s.visAlertDate = { 112 | min: 20000, 113 | max: 24000, 114 | palette: ["ffffcc", "ffeda0", "fed976", "feb24c", "fd8d3c", 115 | "fc4e2a", "e31a1c", "bd0026", "800026"] 116 | }; 117 | 118 | s.visTimelapse = { 119 | bands: ['B12', 'B8', 'B3'], 120 | min: 500, 121 | max: 3500 122 | }; 123 | 124 | s.panelLeft = {width: '400px'}; 125 | s.titleLabel = {fontSize: '22px', fontWeight: 'bold'}; 126 | s.layerPanel = {border: '1px solid black', margin: '5px 5px 0px 5px'}; 127 | s.layerPanelName = {fontWeight: 'bold'}; 128 | s.layerPanelDescription = {color: 'grey', fontSize: '11px'}; 129 | 130 | /******************************************************************************* 131 | * Components * 132 | ******************************************************************************/ 133 | 134 | var c = {}; 135 | 136 | c.titleLabel = ui.Label('Forest disturbance alert date inspector', s.titleLabel); 137 | c.infoPanel = ui.Label('Lorem ipsum odor amet, consectetuer adipiscing elit. Ultrices facilisis ultricies nec nibh integer leo. Libero scelerisque purus ex ultricies ipsum ornare platea euismod. Cursus blandit duis ligula lobortis rhoncus quam per eu. Dictumst proin elementum sociosqu nascetur mi integer massa euismod.'); 138 | 139 | c.buildLayerPanelName = function(layerInfo) { 140 | return ui.Label(layerInfo.name, s.layerPanelName); 141 | }; 142 | c.buildLayerPanelDesc = function(layerInfo) { 143 | return ui.Label(layerInfo.description, s.layerPanelDescription); 144 | }; 145 | 146 | c.buildLayerLegendPanel = function(layerInfo) { 147 | function createLayerLegendGradient(layerInfo) { 148 | var colorBar = ui.Thumbnail({ 149 | image: ee.Image.pixelLonLat().select(0), 150 | params: { 151 | bbox: [0, 0, 1, 0.1], dimensions: '100x10', format: 'png', 152 | min: 0, max: 1, palette: layerInfo.legend.palette 153 | }, 154 | style: {stretch: 'horizontal', margin: '0px 8px', maxHeight: '24px'}, 155 | }); 156 | 157 | var legendLabels = ui.Panel({ 158 | widgets: [ 159 | ui.Label(layerInfo.legend.labelMin, {margin: '4px 8px'}), 160 | ui.Label(layerInfo.legend.labelMax, {margin: '4px 8px', stretch: 'horizontal', textAlign: 'right'}) 161 | ], 162 | layout: ui.Panel.Layout.flow('horizontal') 163 | }); 164 | 165 | return ui.Panel([colorBar, legendLabels]); 166 | } 167 | 168 | function createLayerLegendDiscrete(layerInfo) { 169 | var labels = layerInfo.legend.palette.map(function(color, i) { 170 | return ui.Panel([ 171 | ui.Label('', { 172 | width: '15px', 173 | height: '15px', 174 | backgroundColor: color, 175 | border: '1px solid black', 176 | margin: '3px 10px' 177 | }), 178 | ui.Label(layerInfo.legend.labels[i], { 179 | margin: '3px 10px 0px 0px' 180 | }) 181 | ], ui.Panel.Layout.flow('horizontal')); 182 | }); 183 | 184 | var panel = ui.Panel({ 185 | widgets: labels, 186 | layout: ui.Panel.Layout.flow('vertical') 187 | }); 188 | 189 | return panel; 190 | } 191 | 192 | var legendBuilders = { 193 | 'gradient': createLayerLegendGradient, 194 | 'discrete': createLayerLegendDiscrete 195 | }; 196 | 197 | return legendBuilders[layerInfo.legend.type](layerInfo); 198 | }; 199 | 200 | c.buildLayerControlsPanel = function(layerInfo) { 201 | 202 | function onLayerShownChanged(v) { 203 | layerInfo.layer.setShown(v); 204 | } 205 | 206 | var layerShownCheckbox = ui.Checkbox('', 207 | layerInfo.shown, 208 | onLayerShownChanged 209 | ); 210 | 211 | function onLayerOpacityChanged(v) { 212 | layerInfo.layer.setOpacity(v); 213 | } 214 | 215 | var layerOpacitySlider = ui.Slider(0, 1, 1, 0.1, null, 'horizontal', false, { stretch: 'horizontal' }); 216 | layerOpacitySlider.onSlide(onLayerOpacityChanged); 217 | 218 | return ui.Panel([ 219 | layerShownCheckbox, 220 | layerOpacitySlider 221 | ], 222 | ui.Panel.Layout.Flow('horizontal'), { 223 | width: '200px' 224 | }); 225 | }; 226 | 227 | c.buildLayerPanel = function(layerInfo) { 228 | var layerPanel = ui.Panel([ 229 | c.buildLayerPanelName(layerInfo), 230 | ui.Panel([ 231 | c.buildLayerLegendPanel(layerInfo), 232 | ui.Label('', { stretch: 'horizontal' }), 233 | c.buildLayerControlsPanel(layerInfo), 234 | ], ui.Panel.Layout.flow('horizontal')), 235 | c.buildLayerPanelDesc(layerInfo) 236 | ], 237 | ui.Panel.Layout.flow('vertical'), s.layerPanel); 238 | return layerPanel; 239 | }; 240 | 241 | c.layersPanel = ui.Panel(m.layerInfos.map(c.buildLayerPanel)); 242 | 243 | c.buildLayerInspectionPanel = function() { 244 | c.clickedPointLabel = ui.Label('Click on the map to query alert date -->'); 245 | 246 | c.clickedPointLayer = ui.Map.Layer(ee.FeatureCollection([]), { color: 'yellow' }, 'clicked point'); 247 | Map.layers().add(c.clickedPointLayer); 248 | 249 | return c.clickedPointLabel; 250 | }; 251 | 252 | c.buildUI = function() { 253 | var panelLeft = ui.Panel([ 254 | c.titleLabel, 255 | c.infoPanel, 256 | c.layersPanel, 257 | c.buildLayerInspectionPanel(), 258 | c.buildHistogramPanel() 259 | ], 260 | ui.Panel.Layout.flow('vertical'), 261 | s.panelLeft 262 | ); 263 | ui.root.widgets().insert(0, panelLeft); 264 | }; 265 | 266 | c.addMapLayers = function() { 267 | m.layerInfos.slice(0).reverse().map( 268 | function(layerInfo) { 269 | var legend = layerInfo.legend; 270 | 271 | var visParams = { 272 | min: legend.min, 273 | max: legend.max, 274 | palette: legend.palette 275 | }; 276 | 277 | var layer = ui.Map.Layer( 278 | layerInfo.image, 279 | visParams, 280 | layerInfo.name, 281 | layerInfo.shown, 282 | legend.opacity); 283 | Map.layers().add(layer); 284 | 285 | layerInfo.layer = layer; 286 | }); 287 | }; 288 | 289 | c.buildHistogramPanel = function() { 290 | var panel = ui.Panel([]); 291 | 292 | function updateHistogram() { 293 | var image = m.getAlertDate(); 294 | var bounds = ee.Geometry(Map.getBounds(true)); 295 | var scale = Map.getScale() * 5; 296 | 297 | var chart = ui.Chart.image.histogram(image, bounds, scale, 100); 298 | App.histogramPanel.widgets().reset([chart]); 299 | } 300 | 301 | var updateHistogramDebounced = ui.util.debounce(updateHistogram, 2000); 302 | 303 | Map.onChangeBounds(function() { App.histogramPanel.widgets().reset([])}); 304 | 305 | Map.onChangeBounds(updateHistogramDebounced); 306 | 307 | App.histogramPanel = panel; 308 | 309 | return panel; 310 | }; 311 | 312 | 313 | c.Timelapse = function() { 314 | this.panel = null; 315 | this.mapControl = Map; // default map 316 | this.mapLayers = []; 317 | this.currentIndex = 0; 318 | }; 319 | 320 | c.Timelapse.prototype.buildUI = function() { 321 | var self = this; 322 | var label = ui.Label(); 323 | 324 | var slider = ui.Slider(0, 1, 0, 1); 325 | slider.style().set({ width: '300px' }); 326 | slider.onSlide(function(i) { 327 | self.mapLayers[self.currentIndex].setOpacity(0); 328 | self.currentIndex = i; 329 | self.mapLayers[self.currentIndex].setShown(true); 330 | self.mapLayers[self.currentIndex].setOpacity(1); 331 | label.setValue(self.mapLayers[self.currentIndex].getName()); 332 | }); 333 | 334 | var button = ui.Button('Clear'); 335 | button.style().set({ 336 | margin: '0px', 337 | padding: '0px', 338 | width: '60px' 339 | }); 340 | 341 | button.onClick(function() { 342 | self.clear(); 343 | }); 344 | 345 | label.setValue(self.mapLayers[self.currentIndex].getName()); 346 | 347 | slider.setMax(self.mapLayers.length - 1); 348 | 349 | self.panel = ui.Panel([ 350 | label, 351 | ui.Panel([slider, button], ui.Panel.Layout.flow('horizontal')) 352 | ]); 353 | 354 | return self.panel; 355 | }; 356 | 357 | c.Timelapse.prototype.inspect = function(images, pt, vis) { 358 | var self = this; 359 | 360 | self.images = images; 361 | self.pt = pt; 362 | 363 | self.clear(); 364 | 365 | var scale = self.mapControl.getScale() ; 366 | var r = 200; 367 | var d = 50; 368 | var aoi = pt.buffer(r * scale); 369 | 370 | var mask = ee.Image().paint(pt.buffer(scale * (r - d)), 1).fastDistanceTransform().sqrt() 371 | .reproject(ee.Projection('EPSG:3857').atScale(scale)) 372 | .unitScale(0, d) 373 | .clip(aoi); 374 | mask = ee.Image(1).subtract(mask).pow(2).selfMask(); 375 | 376 | print('debug', images) 377 | print('debug', images.aggregate_array('system:time_start')) 378 | images.aggregate_array('system:time_start') 379 | .evaluate(function(times) { 380 | times.map(function(t, i) { 381 | var image = images 382 | .filter(ee.Filter.eq('system:time_start', t)) 383 | .first() 384 | .select(s.visTimelapse.bands) 385 | .clip(aoi) 386 | .updateMask(mask); 387 | 388 | var mapLayer = ui.Map.Layer( 389 | image, s.visTimelapse, 390 | new Date(t).toISOString(), 391 | true, 392 | i === 0 ? 1 : 0 393 | ); 394 | self.mapLayers.push(mapLayer); 395 | self.mapControl.layers().add(mapLayer); 396 | }); 397 | 398 | if(self.panel) { 399 | self.mapControl.remove(self.panel); 400 | } 401 | 402 | self.panel = self.buildUI(); 403 | self.mapControl.add(self.panel); 404 | }); 405 | }; 406 | 407 | c.Timelapse.prototype.clear = function() { 408 | var self = this; 409 | 410 | self.mapControl.widgets().remove(self.panel); 411 | self.panel = null; 412 | self.panel = null; 413 | 414 | self.mapLayers.map(function(mapLayer) { 415 | self.mapControl.layers().remove(mapLayer); 416 | }); 417 | self.mapLayers.length = 0; 418 | self.currentIndex = 0; 419 | self.pt = null; 420 | self.images = null; 421 | }; 422 | 423 | c.timelapse = new c.Timelapse(); 424 | 425 | c.Logger = function(delay) { 426 | // Create a panel for displaying log messages. 427 | this.panel = ui.Panel(null, ui.Panel.Layout.Flow('vertical'), { 428 | backgroundColor: '#00000000', 429 | color: '00000000', 430 | position: 'bottom-right' 431 | }); 432 | 433 | this.delay = delay; 434 | 435 | // If a delay is not specified, default to 1 second. 436 | if(typeof(this.delay) === 'undefined') { 437 | this.delay = 1000; 438 | } 439 | 440 | // Specify a default maximum message count. 441 | this.maxCount = 5; 442 | 443 | Map.widgets().add(this.panel); 444 | }; 445 | 446 | // Add a prototype function that displays a message. 447 | c.Logger.prototype.info = function(message, color) { 448 | var self = this; 449 | 450 | var label = ui.Label(message, { 451 | backgroundColor: '#00000066', 452 | color: typeof(color) == 'undefined' ? 'ffffff' : color, 453 | fontSize: '14px', 454 | margin: '2px', 455 | padding: '2px' 456 | }); 457 | 458 | self.panel.widgets().add(label); 459 | self.panel.style().set({ shown: true }); 460 | 461 | if(self.panel.widgets().length() > self.maxCount) { 462 | self.panel.widgets().remove(self.panel.widgets().get(0)); 463 | } 464 | 465 | // Configure the widget to be removed after a specified time delay 466 | ui.util.setTimeout(function() { 467 | self.panel.widgets().remove(label); 468 | 469 | // If all the widget have been removed, hide the panel. 470 | if(self.panel.widgets().length() === 0) { 471 | self.panel.style().set({ shown: false }); 472 | } 473 | }, self.delay); 474 | }; 475 | 476 | c.Logger.prototype.setMaxCount = function(maxCount) { 477 | this.maxCount = maxCount; 478 | }; 479 | 480 | /******************************************************************************* 481 | * Behaviors * 482 | ******************************************************************************/ 483 | 484 | var b = {}; 485 | 486 | b.queryAlertDate = function(pt) { 487 | c.logger.info('Querying alert date from the map ...'); 488 | 489 | var lon = pt.lon; 490 | var lat = pt.lat; 491 | pt = ee.Geometry.Point([lon, lat]); 492 | 493 | c.clickedPointLayer.setEeObject(pt); 494 | 495 | // query alert date value at a given point 496 | var alertDateImage = m.getAlertDate(); 497 | 498 | var date_YYJJJ = alertDateImage.reduceRegion(ee.Reducer.first(), pt, 10).get('Date'); 499 | 500 | if (date_YYJJJ.getInfo()) { 501 | var date = m.toDateFromYYJJJ(date_YYJJJ); 502 | 503 | var dateStr = date.format('YYYY-MM-dd'); 504 | 505 | dateStr.evaluate(function(x) { 506 | c.logger.info('Queried date is: ' + x); 507 | c.clickedPointLabel.setValue('Clicked point date: ' + x); 508 | b.inspect(date.advance(-3, 'month'), date.advance(3, 'month'), pt); 509 | }); 510 | 511 | } else { 512 | c.logger.info('No alert data found at clicked location.'); 513 | c.logger.info('Zoom in further to select alerts.'); 514 | } 515 | }; 516 | Map.onClick(b.queryAlertDate); 517 | 518 | b.inspect = function(start, end, pt) { 519 | c.logger.info('Fetching images for current point ...'); 520 | 521 | var images = ee.ImageCollection('COPERNICUS/S2_HARMONIZED') 522 | .filterDate(start, end) 523 | .filterBounds(pt); 524 | 525 | var clouds = ee.ImageCollection('GOOGLE/CLOUD_SCORE_PLUS/V1/S2_HARMONIZED') 526 | .filterDate(start, end) 527 | .filterBounds(pt) 528 | .select('cs_cdf'); 529 | 530 | images = images.linkCollection(clouds, ['cs_cdf']); 531 | 532 | images.size().evaluate(function(i) { 533 | c.logger.info('Total number of images found: ' + i); 534 | }); 535 | 536 | // filter-out cloudy images 537 | var scale = Map.getScale(); 538 | var geom = pt.buffer(scale*50, scale*5); 539 | 540 | var imagesClean = images.filterBounds(pt).map(function(i) { 541 | var quality = i.select('cs_cdf').reduceRegion(ee.Reducer.mean(), geom, scale*5); 542 | 543 | return i.set({ quality: quality.values().get(0) }); 544 | }) 545 | .filter(ee.Filter.gt('quality', 0.7)); 546 | 547 | imagesClean.size().evaluate(function(i) { 548 | c.logger.info('Number of clean images: ' + i); 549 | }); 550 | 551 | // Inspect images 552 | c.timelapse.inspect(imagesClean, pt, s.vis); 553 | }; 554 | 555 | 556 | /******************************************************************************* 557 | * Initialize * 558 | ******************************************************************************/ 559 | 560 | var App = {}; 561 | 562 | App.setupMap = function() { 563 | Map.setOptions('SATELLITE'); 564 | Map.style().set({ cursor: 'crosshair' }); 565 | Map.setCenter(10, 20, 3); 566 | }; 567 | 568 | App.run = function() { 569 | c.logger = new c.Logger(5000); 570 | App.setupMap(); 571 | c.addMapLayers(); 572 | c.buildUI(); 573 | c.timelapse = new c.Timelapse(); 574 | c.timelapse.index = 3; 575 | }; 576 | 577 | App.run(); 578 | --------------------------------------------------------------------------------