### Create and Print a Simple Label Source: https://developers.google.com/earth-engine/guides/ui_widgets Use ui.Label to display text in the Earth Engine Code Editor. This is a basic example of creating and printing a label. ```JavaScript var label = ui.Label('Cool label!'); print(label); ``` -------------------------------- ### Create and Print a Button with a Callback Source: https://developers.google.com/earth-engine/guides/ui_widgets Use ui.Button to create clickable UI elements. The 'onClick' property takes a callback function that executes when the button is clicked. This example prints the current map center. ```JavaScript var button = ui.Button({ label: 'Get Map Center', onClick: function() { print(Map.getCenter()); } }); print(button); ``` -------------------------------- ### Create and Update Map Layer Source: https://developers.google.com/earth-engine/guides/ui_widgets Demonstrates creating a map, adding a Sentinel-2 image layer, and updating the layer's visualization parameters based on user input from a textbox. The visParams object needs to be reset when updating. ```javascript var consoleMap = ui.Map({ lon: -2.0174, lat: 48.6474, zoom: 13 }); // Create a Layer from this Sentinel-2 image var image = ee.Image('COPERNICUS/S2/20150821T111616_20160314T094808_T30UWU'); var visParams = {bands: ['B4', 'B3', 'B2'], max: 2048, gamma: 1}; var layer = ui.Map.Layer(image, visParams); // Update the map by updating the layers list. var layers = consoleMap.layers(); layers.add(layer); // Make a textbox to accept a gamma value. // Update the layer when the gamma value is entered. var gammaBox = ui.Textbox({ value: 1, onChange: function(value) { // visParams is NOT an ActiveDictionary, so set it again. visParams.gamma = value; layer.setVisParams(visParams); } }); print(ui.Label('Enter a gamma value:')); print(gammaBox); print(consoleMap); ``` -------------------------------- ### Define Visualization Parameters Source: https://developers.google.com/earth-engine/tutorials/tutorial_global_surface_water_04 Sets up visualization parameters for different surface water layers, including min/max values and color palettes for occurrence, change, and water masks. ```JavaScript var VIS_OCCURRENCE = { min: 0, max: 100, palette: ['red', 'blue'] }; var VIS_CHANGE = { min: -50, max: 50, palette: ['red', 'black', 'limegreen'] }; var VIS_WATER_MASK = { palette: ['white', 'black'] }; ``` -------------------------------- ### Defining Development Zones with Relational and Add Operators (Python) Source: https://developers.google.com/earth-engine/guides/image_relational Use relational operators like gt() combined with the add() operator to create distinct zones based on image intensity thresholds. Requires geemap for interactive map display. The result can be visualized with a custom palette. ```python import ee import geemap.core as geemap nl_2012 = ee.Image('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS/F182012') lights = nl_2012.select('stable_lights') zones = lights.gt(30).add(lights.gt(55)).add(lights.gt(62)) map_zones = geemap.Map(center=[48.8683, 2.373], zoom=8) palette = ['000000', '0000FF', '00FF00', 'FF0000'] map_zones.add_layer( zones, {'min': 0, 'max': 3, 'palette': palette}, 'development zones' ) display(map_zones) ``` -------------------------------- ### Summarize Transition Classes and Create Pie Chart Source: https://developers.google.com/earth-engine/tutorials/tutorial_global_surface_water_04 Reduces the transition band within the ROI to calculate the area for each transition class, then maps these statistics to features and visualizes them as a pie chart. ```JavaScript var area_image_with_transition_class = ee.Image.pixelArea().addBands(transition); var reduction_results = area_image_with_transition_class.reduceRegion({ reducer: ee.Reducer.sum().group({ groupField: 1, groupName: 'transition_class_value', }), geometry: roi, scale: 30, bestEffort: true, }); print('reduction_results', reduction_results); var roi_stats = ee.List(reduction_results.get('groups')); var transition_fc = ee.FeatureCollection(roi_stats.map(createFeature)); print('transition_fc', transition_fc); var transition_summary_chart = ui.Chart.feature.byFeature({ features: transition_fc, xProperty: 'transition_class_name', yProperties: ['area_m2', 'transition_class_number'] }) .setChartType('PieChart') .setOptions({ title: 'Summary of transition class areas', slices: createPieChartSliceDictionary(transition_fc), sliceVisibilityThreshold: 0 // Don't group small slices. }); print(transition_summary_chart); ``` -------------------------------- ### Defining Development Zones with Relational and Add Operators (JavaScript) Source: https://developers.google.com/earth-engine/guides/image_relational Use relational operators like gt() combined with the add() operator to create distinct zones based on image intensity thresholds. The result can be visualized with a custom palette. ```javascript var nl2012 = ee.Image('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS/F182012'); var lights = nl2012.select('stable_lights'); var zones = lights.gt(30).add(lights.gt(55)).add(lights.gt(62)); var palette = ['000000', '0000FF', '00FF00', 'FF0000']; Map.setCenter(2.373, 48.8683, 8); Map.addLayer(zones, {min: 0, max: 3, palette: palette}, 'development zones'); ``` -------------------------------- ### Add Cloud Storage Layer to Map Source: https://developers.google.com/earth-engine/guides/ui_widgets Shows how to add a static layer from Cloud Storage to the map. This is recommended for performance when displaying costly data. Ensure the bucket, path, maxZoom, and suffix are correctly specified. ```javascript Map.add(ui.Map.CloudStorageLayer({ bucket: 'earthenginepartners-hansen', path: 'tiles/gfc_v1.4/loss_year', maxZoom: 12, suffix: '.png' })); ``` -------------------------------- ### Create Pie Chart Slice Dictionary Source: https://developers.google.com/earth-engine/tutorials/tutorial_global_surface_water_04 Generates a dictionary suitable for configuring Google Charts pie chart slices, mapping transition class palettes to colors. ```JavaScript function createPieChartSliceDictionary(fc) { return ee.List(fc.aggregate_array("transition_class_palette")) .map(function(p) { return {'color': p}; }).getInfo(); } ``` -------------------------------- ### Extracting Bare Land Using Relational and Boolean Operators (JavaScript) Source: https://developers.google.com/earth-engine/guides/image_relational Use relational operators like lt() and boolean operators like and() to create binary layers based on spectral indices. Mask the resulting binary layer with selfMask() to isolate specific features. ```javascript var image = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318'); var ndvi = image.normalizedDifference(['B5', 'B4']); var ndwi = image.normalizedDifference(['B3', 'B5']); var bare = ndvi.lt(0.2).and(ndwi.lt(0)); Map.setCenter(-122.3578, 37.7726, 12); Map.setOptions('satellite'); Map.addLayer(bare.selfMask(), {}, 'bare'); ``` -------------------------------- ### Extracting Bare Land Using Relational and Boolean Operators (Python) Source: https://developers.google.com/earth-engine/guides/image_relational Use relational operators like lt() and boolean operators like And() to create binary layers based on spectral indices. Mask the resulting binary layer with selfMask() to isolate specific features. Requires geemap for interactive map display. ```python import ee import geemap.core as geemap image = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318') nndvi = image.normalizedDifference(['B5', 'B4']) ndwi = image.normalizedDifference(['B3', 'B5']) bare = ndvi.lt(0.2).And(ndwi.lt(0)) map_bare = geemap.Map(center=[37.7726, -122.3578], zoom=12) map_bare.add_layer(bare.selfMask(), None, 'bare') display(map_bare) ``` -------------------------------- ### Create and Configure a Checkbox Source: https://developers.google.com/earth-engine/guides/ui_widgets Use ui.Checkbox to create a toggleable UI element. The 'onChange' callback receives a boolean indicating the checked state and can be used to control map layers. ```JavaScript var checkbox = ui.Checkbox('Show SRTM layer', true); checkbox.onChange(function(checked) { // Shows or hides the first map layer based on the checkbox's value. Map.layers().get(0).setShown(checked); }); Map.addLayer(ee.Image('CGIAR/SRTM90_V4')); print(checkbox); ``` -------------------------------- ### Export Map Tiles to Cloud Storage (Python) Source: https://developers.google.com/earth-engine/guides/exporting_map_tiles Exports map tiles of a Landsat image for a specified region in California to a Google Cloud Storage bucket using the Earth Engine Python API. Replace 'yourBucketName' with your actual bucket name. This operation may incur charges. ```python import ee import geemap.core as geemap ``` ```python # --- Example Export Map Tiles - basic --- # Specify area to clip/export, setup image and preview on map. export_region = ee.Geometry.BBox(-122.9, 37.1, -121.2, 38.2) landsat_image = ( ee.Image('LANDSAT/LC09/C02/T1_TOA/LC09_044034_20220111') .select(['B4', 'B3', 'B2']) .visualize(min=0.02, max=0.4, gamma=1.2) .clip(export_region) ) m = geemap.Map() m.add_layer(landsat_image, {}, 'landsatImage') m.center_object(export_region) display(m) # Set up Export task. task = ee.batch.Export.map.toCloudStorage( image=landsat_image, description='mapTilesForEE', bucket='yourBucketName', # replace with your GCS bucket name fileFormat='auto', maxZoom=13, region=export_region, writePublicTiles=True, ) task.start() ``` -------------------------------- ### Generate Video Animation from ImageCollection Source: https://developers.google.com/earth-engine/guides/ic_visualization This snippet demonstrates how to generate a video animation from an ImageCollection representing global temperatures over 24 hours. It defines an area of interest, imports the temperature data, sets animation arguments, and prints the animation as a ui.Thumbnail and a URL. ```JavaScript var aoi = ee.Geometry.Polygon( [[[-179.0, 78.0], [-179.0, -58.0], [179.0, -58.0], [179.0, 78.0]]], null, false ); var tempCol = ee.ImageCollection('NOAA/GFS0P25') .filterDate('2018-12-22', '2018-12-23') .limit(24) .select('temperature_2m_above_ground'); var videoArgs = { dimensions: 768, region: aoi, framesPerSecond: 7, crs: 'EPSG:3857', min: -40.0, max: 35.0, palette: ['blue', 'purple', 'cyan', 'green', 'yellow', 'red'] }; print(ui.Thumbnail(tempCol, videoArgs)); print(tempCol.getVideoThumbURL(videoArgs)); ``` -------------------------------- ### Export Map Tiles to Cloud Storage (JavaScript) Source: https://developers.google.com/earth-engine/guides/exporting_map_tiles Exports map tiles of a Landsat image for a specified region in California to a Google Cloud Storage bucket. Ensure you replace 'yourBucketName' with your actual bucket name. This operation may incur charges. ```javascript // --- Example Export Map Tiles - basic --- // Specify area to clip/export, setup image and preview on map. var exportRegion = ee.Geometry.BBox(-122.9, 37.1, -121.2, 38.2); var landsatImage = ee.Image('LANDSAT/LC09/C02/T1_TOA/LC09_044034_20220111') .select(['B4', 'B3', 'B2']) .visualize({min: 0.02, max: 0.4, gamma: 1.2}) .clip(exportRegion); Map.addLayer(landsatImage, {}, 'landsatImage'); Map.centerObject(exportRegion); // Set up Export task. Export.map.toCloudStorage({ image: landsatImage, description: 'mapTilesForEE', bucket: 'yourBucketName', // replace with your GCS bucket name fileFormat: 'auto', maxZoom: 13, region: exportRegion, writePublicTiles: true }); ``` -------------------------------- ### Combine Mean and Standard Deviation Reducers (JavaScript) Source: https://developers.google.com/earth-engine/guides/reducers_intro Load a Landsat 8 image and combine the mean and standard deviation reducers to calculate image statistics. Pixels with a mask of 0 are excluded from the reduction. ```javascript var image = ee.Image('LANDSAT/LC08/C02/T1/LC08_044034_20140318'); var reducers = ee.Reducer.mean().combine({ reducer2: ee.Reducer.stdDev(), sharedInputs: true }); var stats = image.reduceRegion({ reducer: reducers, bestEffort: true, }); print(stats); ``` -------------------------------- ### Combine Mean and Standard Deviation Reducers (Python) Source: https://developers.google.com/earth-engine/guides/reducers_intro Load a Landsat 8 image and combine the mean and standard deviation reducers to calculate image statistics in Python. Pixels with a mask of 0 are excluded from the reduction. ```python import ee import geemap.core as geemap ``` ```python # Load a Landsat 8 image. image = ee.Image('LANDSAT/LC08/C02/T1/LC08_044034_20140318') # Combine the mean and standard deviation reducers. reducers = ee.Reducer.mean().combine( reducer2=ee.Reducer.stdDev(), sharedInputs=True ) # Use the combined reducer to get the mean and SD of the image. stats = image.reduceRegion(reducer=reducers, bestEffort=True) # Display the dictionary of band means and SDs. display(stats) ``` -------------------------------- ### Load Global Surface Water Assets Source: https://developers.google.com/earth-engine/tutorials/tutorial_global_surface_water_04 Loads the Global Surface Water dataset and selects specific bands for analysis. Defines a region of interest (ROI) for subsequent calculations. ```JavaScript var gsw = ee.Image('JRC/GSW1_0/GlobalSurfaceWater'); var occurrence = gsw.select('occurrence'); var change = gsw.select("change_abs"); var transition = gsw.select('transition'); var roi = ee.Geometry.Polygon( [[[105.531921, 10.412183], [105.652770, 10.285193], [105.949401, 10.520218], [105.809326, 10.666006]]]); ``` -------------------------------- ### Set Map Options and Center Source: https://developers.google.com/earth-engine/datasets/catalog/USFS_GTAC_TreeMap_v2022 Configures the map's basemap to 'TERRAIN' and centers the view on CONUS. This sets the initial display for the map. ```javascript // Set basemap Map.setOptions('TERRAIN'); // Center map on CONUS Map.setCenter(-95.712891, 38, 5); ```