## print("Hello World!"); ## ## // variable ## var season = "rain"; ## ## print("I like ", season); ## ## // To comment use '//' ## // print("Hello Mars!") ## ## // Store a number in a variable. ## var number = 10; ## print('The answer is:', number); ## ## // Use square brackets [] to make a list. ## var numbers = [0, 1, 1, 2, 3, 5]; ## print('List of numbers:', numbers); ## ## // Make a list of strings. ## var strings = ['a', 'b', 'c', 'd', 'e']; ## print('List of strings:', strings); ## ## // Use curly brackets {} to make a dictionary of key:value pairs. ## var object = { ## season: 'longrain', ## year: 2019, ## crops: ['maize', 'rice'] ## }; ## ## print('Dictionary:', object); ## ## // Access dictionary items using square brackets. ## print('Print year:', object['year']); ## ## // Access dictionary items using dot notation. ## print('Print stuff:', object.crops); ## ## ## // Basic structure ## var myFunction = function(parameter) { ## // do something ## statement; ## return statement; ## }; ## ## ## // Basic structure ## var addnumber = function(number) { ## var newvalue = number + 10 ## return newvalue; ## }; ## ## print(addnumber(20)) ## ## // Make a sequence the hard way. ## var eeList = ee.List([1, 2, 3, 4, 5]); ## ## // Make a sequence the easy way! ## var sequence = ee.List.sequence(1, 5); ## print('Sequence:', sequence); ## ## var sum1 = sequence.map(addnumber); ## print("EE server computed objects ", sum1) ## ## var sum2 = numbers.map(addnumber) ## print("EE client computed objects ", sum2) ## var array1 = ee.Array([1,2,3]) //1-D Array ## print(array1.length()); ## var array2 = ee.Array([[1,2,3],[4,5,6]]) ## //2-D Array; use length() to return the lengths of each axis ## print(array2.length()); ## var first = array2D.slice({axis: 0,start: 0,end: 1,step: 1}); ## print(first); ## var image = ee.Image(1); ## print(image); ## var point = ee.Geometry.Point([1.5, 1.5]) ## print(point) ## var pointFeature = ee.Feature(point, ## {name: 'mypoint', location: 'no-idea'}) ## print(pointFeature) ## var collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR'); ## ## // Construct start and end dates: ## var start = ee.Date('2018-01-01'); ## var end = ee.Date('2018-12-31'); ## ## //Filter the Landsat 8 collection using the point and the dates ## var filteredCollection = collection.filterBounds(point).filterDate(start, end); ## ## // Inspect number of tiles returned after the search; we will use the one with more tiles ## print(filteredCollection.size()); ## ## // Also Inspect one file ## var image = filteredCollection.first(); ## print(image); ## ## // print the band names ## print(image.bandNames()); ## ## // Center the map on the image. ## Map.centerObject(point, 9); ## ## // Image display ## Map.addLayer(image, {}, "Landsat surface reflectance"); ## ## // We specify visualization parameters in a JavaScript dictionary for better plots ## var visParams = { ## bands: ['B4', 'B3', 'B2'], ## min: 0, ## max: 3000, ## }; ## ## Map.addLayer(image, visParams, "Landsat surface reflectance"); ## ## var image = filteredCollection ## // Sort the collection by a metadata property. ## .sort('CLOUD_COVER') ## // Get the first image out of this collection. ## .first(); ## ## print('Least cloudy Landsat scene of the year:', image2); ## ## Map.addLayer(image, visParams, "Landsat least cloudy image"); ## // Method 1: Using available function ## var ndvi = image.normalizedDifference(['B5', 'B4']); ## ## // Display the results ## var ndviParams = {min: -1, max: 1, palette: ['blue', 'white', 'green']}; ## Map.addLayer(ndvi, ndviParams, 'NDVI'); ## ## ## // Method 2: Using band arithmetic ## var nir = image.select('B5'); ## var red = image.select('B4'); ## var ndvi2 = nir.subtract(red).divide(nir.add(red)).rename('NDVI'); ## ## // Method 3: Using image expressions ## var ndvi3 = image.expression( ## '(NIR - RED) / (NIR + RED)', { ## 'NIR': image.select('B5'), ## 'RED': image.select('B4') ## }); ## ## // Create binary images from thresholds on NDVI. ## // This threshold is excpected to detect green areas ## var veg = ndvi.gt(0.4); ## ## //Mask areas with the binary image ## var green = veg.updateMask(veg); ## ## // Define visualization parameters for the spectral indices. ## var ndviViz = {min: -1, max: 1, palette: ['FF0000', '00FF00']}; ## ## Map.addLayer(green, ndviViz, 'vegetations'); ## /** ## * Function to mask clouds based on the pixel_qa band of Landsat 8 SR data. ## * @param {ee.Image} image input Landsat 8 SR image ## * @return {ee.Image} cloudmasked Landsat 8 image ## */ ## function maskL8sr(image) { ## // Bits 3 and 5 are cloud shadow and cloud, respectively. ## var cloudShadowBitMask = (1 << 3); ## var cloudsBitMask = (1 << 5); ## // Get the pixel QA band. ## var qa = image.select('pixel_qa'); ## // Both flags should be set to zero, indicating clear conditions. ## var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0) ## .and(qa.bitwiseAnd(cloudsBitMask).eq(0)); ## return image.updateMask(mask); ## } ## ## var image = filteredCollection.first(); ## ## var image_masked = maskL8sr(image); ## ## Map.addLayer(image, visParams, "Landsat tile with cloud"); ## Map.addLayer(image_masked, visParams, "Landsat tile cloud masked"); ## ## ## var maskedCollection = filteredCollection.map(maskL8sr); ## ## Map.addLayer(filteredCollection.median(), visParams, "Landsat composite with cloud"); ## Map.addLayer(maskedCollection.median(), visParams, "Landsat composite cloud masked"); ## // here we compute the following composites ## // general structure: ImageCollection.reduce(ee.Reducer.Name(parameter)); ## ## // Mean ## var mean = maskedCollection.reduce(ee.Reducer.mean()); ## ## //alternate: var mean = maskedCollection.mean(); ## ## // Median ## var med = maskedCollection.reduce(ee.Reducer.median()); ## ## //alternate: var med = maskedCollection.median(); ## ## // Standard Deviation ## var sd = maskedCollection.reduce(ee.Reducer.stdDev()); ## ## var CRS = 'EPSG:4326'; // Only if you want export in specific reference system ## ## mean = mean.select(["B2_mean","B3_mean","B4_mean","B5_mean"]) ## ## Export.image.toDrive({ ## image: mean, ## description: 'exporting-composite-to-drive', ## fileNamePrefix: 'ibadan_landsat_composite', ## folder: 'GEE_export', // Name of the Google Drive folder ## scale: 30, ## maxPixels: 1e13, ## crs: CRS ## }); ## ## ## // Simple Cloud Score ## var maskCloudsTOA = function(image, th) { ## var scored = ee.Algorithms.Landsat.simpleCloudScore(image); ## ## //Specify cloud threshold (0-100)- lower number masks out more clouds ## var mask = scored.select(['cloud']).lte(th); ## ## //Make sure no band is just under zero ## var allBandsGT = image.reduce(ee.Reducer.min()).gt(-0.001); ## return image.updateMask(mask.and(allBandsGT)); ## }; ## ## var toa_dataset = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA'); ##