├── 008-weka_learning_vector_quantization_clustering.js ├── 010-modis_zonal_statistics_calculation.js ├── 006-high_forest_potential_map_compared_to_MODIS_forest_classes.js ├── 003-rapideye_spectral_angle_mapper.js ├── 009-landsat8_proba-v_spectra_analysis.js ├── 007-rapideye_tasseled_cap_transformation.js ├── 004-forest_loss_calculation.js ├── 005-loss_calculation_mixed_forest.js ├── 002-landsat8_toa_tasseled_cap_transformation.js ├── README.md ├── 001-classification_ls8_plus_spectra_chart.js └── LICENSE /008-weka_learning_vector_quantization_clustering.js: -------------------------------------------------------------------------------- 1 | /* 2 | Author: René Kopeinig 3 | Script: WEKA Learning Vector Quantization Clustering for Landsat 8 4 | Description: Example which demonstrates clustering that implements WEKA Learning Vector Quantization algorithm based on 5 | T. Kohonen, "Learning Vector Quantization", The Handbook of Brain Theory and Neural Networks, 2nd Edition, MIT Press, 2003, pp. 631-634. 6 | Version: 0.1 7 | */ 8 | 9 | var image = ee.Image('LANDSAT/LC8_L1T/LC80260462017039LGN00') 10 | var region = ee.Geometry.Polygon(image.geometry().getInfo().coordinates); 11 | 12 | function cluster(image, area){ 13 | var training = image.sample({ 14 | region: area, 15 | scale: 30, 16 | numPixels: 5000}) 17 | // Using LVQ with 10 cluster, learning rate of 1 and 10 epochs 18 | var clusterer = ee.Clusterer.wekaLVQ(10, 1, 10, true).train(training) 19 | var result = image.cluster(clusterer); 20 | return result 21 | } 22 | 23 | var result = cluster(image, region) 24 | Map.setCenter(-98.723, 20.228,8) 25 | Map.addLayer(image, {bands: ['B4', 'B3', 'B2'],min:0, max:16000, gamma:0.6}, 'LS8 TOA') 26 | Map.addLayer(result.randomVisualizer()) 27 | -------------------------------------------------------------------------------- /010-modis_zonal_statistics_calculation.js: -------------------------------------------------------------------------------- 1 | /* 2 | Author: René Kopeinig 3 | Script: MODIS regional or zonal statistics calculation 4 | Description: Regional or zonal statistics calculation based on a MODIS Net Primary Productivity 5 | for MODIS LandCover class 'Mixed forest' 6 | Version: 0.1 7 | */ 8 | 9 | // Feature Collection for area of interest 10 | var fc = ee.FeatureCollection(3513341).filter(ee.Filter.or(ee.Filter.eq('Country','Mexico'))); 11 | 12 | // MODIS Net Primary Productivity Yearly 13 | var mod17_2001 = ee.Image('MODIS/006/MOD17A3H/2001_01_01').select(0) 14 | 15 | // MODIS LandCover Type Yearly 16 | var mod51_2001 = ee.Image('MODIS/051/MCD12Q1/2001_01_01').select(0) 17 | 18 | // Masking 19 | var modis_2001_class_1 = mod17_2001.mask(mod51_2001.eq(5)) 20 | 21 | // Regional/Zonal statistic calculation 22 | function region_stats(input, geom){ 23 | var stats_dictionary = input.reduceRegion({ 24 | // Reducer could be min, max, mean, stdev 25 | reducer: ee.Reducer.mean(), 26 | geometry: geom.geometry(), 27 | scale: 500, 28 | maxPixels: 1e9 29 | 30 | }); 31 | return stats_dictionary.getInfo(); 32 | } 33 | 34 | var mean_2001_class = region_stats(modis_2001_class_1, fc) 35 | console.log(['Mexico',mean_2001_class['Npp']]) 36 | -------------------------------------------------------------------------------- /006-high_forest_potential_map_compared_to_MODIS_forest_classes.js: -------------------------------------------------------------------------------- 1 | /* 2 | Author: René Kopeinig 3 | Description: High forest potential map for the Mexican state of Chiapas according to 4 | 'The Nature Conservancy and Alianza México REDD+' 5 | compared with MODIS Land cover classification 2012's forest classes. 6 | Version: 0.1 7 | */ 8 | 9 | // Creating a mask for chiapas 10 | var mexico = ee.FeatureCollection('ft:11_TQMPTmBF6jKCt9-jAg5Y_1LvH2q-QfVj6jGzYJ'); 11 | var chiapas = mexico.filterMetadata('ADMIN_NAME', 'equals', 'Chiapas'); 12 | 13 | //WHRC Pantropical Biomass 14 | var whrc = ee.Image("WHRC/biomass/tropical").clip(chiapas) 15 | 16 | //MODIS Vegetation Continuous Fields Yearly Global (MOD44B.051) 17 | var vcf_2013_treecover = ee.Image('MODIS/051/MOD44B/2013_03_06').clip(chiapas).select(0); 18 | 19 | //Masking high potential forest map (According to 'The Nature Conservancy and Alianza México Redd+') 20 | var high_forest_potential = whrc.mask(whrc.gt(120).or(vcf_2013_treecover.gt(85))) 21 | high_forest_potential = high_forest_potential.where(high_forest_potential,1000) 22 | 23 | // MODIS LAND COVER 2012 Forest classes 24 | var modis_2012 = ee.Image('MODIS/051/MCD12Q1/2012_01_01').select('Land_Cover_Type_1'); 25 | var igbpPalette = [ 26 | '152106', '225129', '369b47', '30eb5b', '387242' // forest classes 27 | ]; 28 | 29 | Map.addLayer(modis_2012.clip(chiapas), {palette: igbpPalette, min: 1, max: 5}, 'MODIS 2012'); 30 | Map.addLayer(high_forest_potential,{palette:['4C5F3E']},'High Forest Potential (The Nature Conservancy and Alianza México REDD+)') -------------------------------------------------------------------------------- /003-rapideye_spectral_angle_mapper.js: -------------------------------------------------------------------------------- 1 | // Author: René Kopeinig 2 | // Script: Spectral Angle Mapper 3 | // Version: 0.1 4 | 5 | var image = ee.Image("CONABIO/RAPIDEYE/2011-02-27T185859_RE5_3A-NAC_9512122_136955"); 6 | addToMap(image, {min:0, max:16000}, "Original RapidEye image", false) 7 | centerMap(-108.91, 27.64, 13) 8 | 9 | function spectral_angle_mapper(img){ 10 | var band1 = img.select("B") 11 | var bandn = img.select("G", "R", "RE", "N"); 12 | var maxObjSize = 256; 13 | var b = band1.divide(bandn); 14 | 15 | var spectralAngleMap = b.atan(); 16 | addToMap(spectralAngleMap, {}, "Spectral Angle Map", false) 17 | 18 | var spectralAngleMap_sin = spectralAngleMap.sin(); 19 | addToMap(spectralAngleMap_sin, {}, "Sinus", false) 20 | 21 | var spectralAngleMap_cos = spectralAngleMap.cos(); 22 | addToMap(spectralAngleMap_cos, {}, "Cosinus", false) 23 | 24 | var sum_cos = spectralAngleMap_cos.reduce(ee.call("Reducer.sum")); 25 | addToMap(sum_cos, {min: 2, max:3}, "Sum Cosinus") 26 | 27 | var sum_sin = spectralAngleMap_sin.reduce(ee.call("Reducer.sum")); 28 | addToMap(sum_sin, {min: 2, max:4}, "Sum Sinus", false) 29 | 30 | var ndvi = img.normalizedDifference(["N", "R"]); 31 | var bundle = ee.Image.cat(ndvi, spectralAngleMap_sin, spectralAngleMap_cos); 32 | 33 | var imageClustered = ee.apply("Test.Clustering.RegionGrow", { 34 | "image": bundle, 35 | "threshold": 0.005, 36 | "useCosine": true, 37 | "maxObjectSize": maxObjSize, 38 | }); 39 | addToMap(imageClustered.select("clusters"), {}, "Clustered RegionGrow", false) 40 | 41 | 42 | var imageConsistent = ee.apply("Test.Clustering.SpatialConsistency", { 43 | "image": imageClustered, 44 | "maxObjectSize": maxObjSize 45 | }); 46 | addToMap(imageConsistent.select("clusters"), {min:100, max:55000}, "Consistent RegionGrow", false) 47 | }; 48 | 49 | spectral_angle_mapper(image); -------------------------------------------------------------------------------- /009-landsat8_proba-v_spectra_analysis.js: -------------------------------------------------------------------------------- 1 | /* 2 | Author: René Kopeinig 3 | Script: Spectra analysis on Landsat 8 TOA & Proba-V S1 TOC 4 | Description: Example of spectra analysis on Landsat 8 TOA & Proba-V S1 TOC on features such as agriculture, urban and water 5 | Version: 0.1 6 | */ 7 | 8 | var ls8 = ee.ImageCollection('LANDSAT/LC8_L1T_TOA') 9 | .filterDate('2017-01-01', '2017-12-31') 10 | .filterMetadata('CLOUD_COVER', 'less_than', 10) 11 | .min() 12 | .slice(0,8) 13 | var probav = ee.ImageCollection('VITO/PROBAV/C1/S1_TOC_100M') 14 | .min() 15 | .slice(0,5) 16 | 17 | var hidalgo = ee.FeatureCollection('ft:11_TQMPTmBF6jKCt9-jAg5Y_1LvH2q-QfVj6jGzYJ').filterMetadata('ADMIN_NAME', 'equals', 'Hidalgo'); 18 | probav=probav.clip(hidalgo) 19 | ls8=ls8.clip(hidalgo) 20 | 21 | Map.setCenter(-98.8934, 20.4716, 8) 22 | //Map.addLayer(probav, {min:0, max:255},'PROBA-V') 23 | Map.addLayer(ls8, {min:0, max:0.2},'LS8_TOA') 24 | 25 | function chart_options(title){ 26 | var options = { 27 | title: title, 28 | hAxis: {title: 'Bands'}, 29 | vAxis: {title: 'Pixel Values'}, 30 | lineWidth: 1, 31 | pointSize: 0.25, 32 | series: { 33 | 0: {color: '00FF00'}, 34 | 1: {color: '996633'}, 35 | 2: {color: '0000FF'} 36 | }}; 37 | return options 38 | } 39 | 40 | var urban = ee.Geometry.MultiPoint([[-98.76434326171875,20.11063974800624], 41 | [-98.78013610839844,20.09580908197506],[-98.74099731445312,20.073560449523114], 42 | [-98.73172760009766,20.066788499743634],[-98.74580383300781,20.12321247264606]]) 43 | 44 | var water = ee.Geometry.MultiPoint([[-99.36567306518555,20.15050362250545], 45 | [-99.3684196472168,20.143573840297726],[-99.37116622924805,20.136160244539198], 46 | [-99.37374114990234,20.13003570447512],[-99.38541412353516,20.121493182058764]]) 47 | 48 | var agriculture = ee.Geometry.MultiPoint([[-98.8388442993164,20.668417753503775], 49 | [-98.8388442993164,20.668417753503775],[-98.83729934692383,20.669542040570413], 50 | [-98.83403778076172,20.675484562510196],[-98.82837295532227,20.67532395686697]]) 51 | 52 | 53 | var points = ee.FeatureCollection([ 54 | ee.Feature(water, {'label': 'Water'}), 55 | ee.Feature(agriculture, {'label': 'Agriculture'}), 56 | ee.Feature(urban, {'label': 'Urban'}) 57 | ]); 58 | 59 | var ls8_spectraChart = Chart.image.regions( 60 | ls8, points, ee.Reducer.mean(), 30, 'label') 61 | .setChartType('LineChart') 62 | .setOptions(chart_options('Landsat 8 L1T TOA Spectra for different features')); 63 | 64 | var probav_spectraChart = Chart.image.regions( 65 | probav, points, ee.Reducer.mean(), 30, 'label') 66 | .setChartType('LineChart') 67 | .setOptions(chart_options('PROBA-V S1 TOC Spectra for different features')); 68 | 69 | // Display the chart. 70 | print(ls8_spectraChart, probav_spectraChart); 71 | -------------------------------------------------------------------------------- /007-rapideye_tasseled_cap_transformation.js: -------------------------------------------------------------------------------- 1 | /* 2 | Author: René Kopeinig 3 | Script: Tasseled Cap Transformation for RapidEye Imagery 4 | Description: Tasseled Cap Transformation for RapidEye Imagery based on the 5 | scientfic work "Derivation of Tasseled Cap Coefficients for RapidEye data" by 6 | M.Schoenert, H.Weichelt, E.Zillmann & C.Jürgens (2014). The bands of the output image are the brightness index, 7 | greenness index and yellowness index. 8 | Link to publication: https://www.researchgate.net/publication/270302804_Derivation_of_Tasseled_Cap_Coefficients_for_RapidEye_data 9 | Note: You might not be able to see the RapidEye scene because of license issues 10 | but feel free to use my code on your RapidEye scene(s). 11 | Version: 0.1 12 | */ 13 | 14 | 15 | var rapid_eye_scene = ee.Image("users/renekope/1648306_2015") 16 | Map.addLayer(rapid_eye_scene, {min:0, max:16000, bands:'b3, b2, b1'}, "RapidEye Scene 2015") 17 | Map.setCenter(-88.9511, 20.0753,12) 18 | 19 | var calculateTasseledCap = function (image){ 20 | var b = image.select("b1", "b2", "b3", "b4", "b5"); 21 | //Coefficients are only for RapidEye Imagery 22 | var brightness_coefficents = ee.Image([0.2435,0.3448,0.4881,0.4930,0.5835]) 23 | var greenness_coefficents = ee.Image([-0.2216,-0.2319,-0.4622,-0.2154,0.7981]) 24 | var yellowness_coefficents = ee.Image([-0.7564,-0.3916,0.5049,0.1400,0.0064]) 25 | 26 | var brightness = image.expression( 27 | '(B * BRIGHTNESS)', 28 | { 29 | 'B':b, 30 | 'BRIGHTNESS': brightness_coefficents 31 | } 32 | ); 33 | var greenness = image.expression( 34 | '(B * GREENNESS)', 35 | { 36 | 'B':b, 37 | 'GREENNESS': greenness_coefficents 38 | } 39 | ); 40 | var yellowness = image.expression( 41 | '(B * WETNESS)', 42 | { 43 | 'B':b, 44 | 'WETNESS': yellowness_coefficents 45 | } 46 | ); 47 | 48 | brightness = brightness.reduce(ee.call("Reducer.sum")); 49 | greenness = greenness.reduce(ee.call("Reducer.sum")); 50 | yellowness = yellowness.reduce(ee.call("Reducer.sum")); 51 | var tasseled_cap = ee.Image(brightness).addBands(greenness).addBands(yellowness).rename('brightness','greenness','yellowness') 52 | return tasseled_cap; 53 | }; 54 | 55 | var rapid_eye_tasseled_cap_transformation = calculateTasseledCap(rapid_eye_scene) 56 | Map.addLayer(rapid_eye_tasseled_cap_transformation.select(2),{min:-4000, max:-1000},'RapidEye Tasseled Cap Transformation Yellowness') 57 | Map.addLayer(rapid_eye_tasseled_cap_transformation.select(1),{min:-4000, max:4000},'RapidEye Tasseled Cap Transformation Greenness') 58 | Map.addLayer(rapid_eye_tasseled_cap_transformation.select(0),{min:6000, max:17000},'RapidEye Tasseled Cap Transformation Brightness') -------------------------------------------------------------------------------- /004-forest_loss_calculation.js: -------------------------------------------------------------------------------- 1 | /* 2 | Author: René Kopeinig 3 | Script: Forest loss calculation 4 | Description: Calculation of overall forest loss between 2010 and 2015 for the consecutive years. 5 | Version: 0.1 6 | */ 7 | 8 | 9 | // Creating a country mask for Chiapas 10 | var mexico = ee.FeatureCollection('ft:11_TQMPTmBF6jKCt9-jAg5Y_1LvH2q-QfVj6jGzYJ'); 11 | var chiapas = mexico.filterMetadata('ADMIN_NAME', 'equals', 'Chiapas'); 12 | 13 | //MODIS Vegetation Continuous Fields Yearly Global (MOD44B.051) 14 | //Select only first band (Percent_Tree_Cover: Tree cover (%)) 15 | var vcf_2010 = ee.Image('MODIS/051/MOD44B/2010_03_06').clip(chiapas).select(0); 16 | var vcf_2011 = ee.Image('MODIS/051/MOD44B/2011_03_06').clip(chiapas).select(0); 17 | var vcf_2012 = ee.Image('MODIS/051/MOD44B/2012_03_05').clip(chiapas).select(0); 18 | var vcf_2013 = ee.Image('MODIS/051/MOD44B/2013_03_06').clip(chiapas).select(0); 19 | var vcf_2014 = ee.Image('MODIS/051/MOD44B/2014_03_06').clip(chiapas).select(0); 20 | var vcf_2015 = ee.Image('MODIS/051/MOD44B/2015_03_06').clip(chiapas).select(0); 21 | 22 | //Function to calculate loss from one to another year 23 | function calculate_loss(img1,img2){ 24 | var change = img2.subtract(img1); 25 | var loss_mask = change.expression( 26 | 'PIXEL <= 0', { 27 | 'PIXEL': change 28 | }); 29 | var loss = change.multiply(loss_mask); 30 | return loss.divide(ee.Image(100)) 31 | } 32 | 33 | //Calculate loss 34 | var loss_2011_2010 = calculate_loss(vcf_2010,vcf_2011) 35 | var loss_2012_2011 = calculate_loss(vcf_2011,vcf_2012) 36 | var loss_2013_2012 = calculate_loss(vcf_2012,vcf_2013) 37 | var loss_2014_2013 = calculate_loss(vcf_2013,vcf_2014) 38 | var loss_2015_2014 = calculate_loss(vcf_2014,vcf_2015) 39 | 40 | // Function to calculate the area of the loss in hectares 41 | function area_of_loss(img, ft){ 42 | var area = img.multiply(ee.Image.pixelArea()); 43 | var statistics = area.reduceRegion({ 44 | reducer: ee.Reducer.sum(), 45 | geometry: ft, 46 | maxPixels: 5e9 47 | }); 48 | var area_loss_object = statistics.get('Percent_Tree_Cover'); 49 | var area_loss = area_loss_object.getInfo() 50 | return area_loss/-10000 51 | } 52 | 53 | // calculate the area of the loss 54 | var area_of_loss_hectares_2011_2010 = area_of_loss(loss_2011_2010, chiapas) 55 | var area_of_loss_hectares_2012_2011 = area_of_loss(loss_2012_2011, chiapas) 56 | var area_of_loss_hectares_2013_2012 = area_of_loss(loss_2013_2012, chiapas) 57 | var area_of_loss_hectares_2014_2013 = area_of_loss(loss_2014_2013, chiapas) 58 | var area_of_loss_hectares_2015_2014 = area_of_loss(loss_2015_2014, chiapas) 59 | 60 | //Create result list 61 | var result_list = [area_of_loss_hectares_2011_2010, area_of_loss_hectares_2012_2011, 62 | area_of_loss_hectares_2013_2012, area_of_loss_hectares_2014_2013, area_of_loss_hectares_2015_2014]; 63 | 64 | //Display result list 65 | console.log(result_list) -------------------------------------------------------------------------------- /005-loss_calculation_mixed_forest.js: -------------------------------------------------------------------------------- 1 | /* 2 | Author: rkope 3 | Description: Calculation of loss in class 'Mixed Forest' 4 | based on the MODIS Land cover classification 2012 between 2012 and 2015 for the consecutive years. 5 | Version: 0.1 6 | */ 7 | 8 | // Creating a country mask for chiapas 9 | var mexico = ee.FeatureCollection('ft:11_TQMPTmBF6jKCt9-jAg5Y_1LvH2q-QfVj6jGzYJ'); 10 | var chiapas = mexico.filterMetadata('ADMIN_NAME', 'equals', 'Chiapas'); 11 | 12 | // MODIS LAND COVER 2012 13 | var modis_2012 = ee.Image('MODIS/051/MCD12Q1/2012_01_01').select('Land_Cover_Type_1'); 14 | var igbpPalette = [ 15 | 'aec3d4', // water 16 | '152106', '225129', '369b47', '30eb5b', '387242', // forest 17 | '6a2325', 'c3aa69', 'b76031', 'd9903d', '91af40', // shrub, grass 18 | '111149', // wetlands 19 | 'cdb33b', // croplands 20 | 'cc0013', // urban 21 | '33280d', // crop mosaic 22 | 'd7cdcc', // snow and ice 23 | 'f7e084', // barren 24 | '6f6f6f' // tundra 25 | ]; 26 | 27 | Map.addLayer(modis_2012.clip(chiapas), {palette: igbpPalette, min: 0, max: 17}, 'MODIS 2012'); 28 | Map.setCenter(-92.5406, 16.4677, 8) 29 | 30 | //MODIS Vegetation Continuous Fields Yearly Global (MOD44B.051) 31 | //Select only first band (Percent_Tree_Cover: Tree cover (%)) 32 | var vcf_2012 = ee.Image('MODIS/051/MOD44B/2012_03_05').clip(chiapas).select(0); 33 | var vcf_2013 = ee.Image('MODIS/051/MOD44B/2013_03_06').clip(chiapas).select(0); 34 | var vcf_2014 = ee.Image('MODIS/051/MOD44B/2014_03_06').clip(chiapas).select(0); 35 | var vcf_2015 = ee.Image('MODIS/051/MOD44B/2015_03_06').clip(chiapas).select(0); 36 | 37 | //Function to calculate loss from one to another year 38 | //taking in consideration a specific modis landcover class 39 | function calculate_loss(img1,img2,class_lc){ 40 | var change = img2.subtract(img1); 41 | var loss_mask = change.expression( 42 | 'PIXEL <= 0 && MODIS == CLASS', { 43 | 'PIXEL': change, 44 | 'MODIS': modis_2012, 45 | 'CLASS': class_lc 46 | }); 47 | var loss = change.multiply(loss_mask); 48 | return loss.divide(ee.Image(100)) 49 | } 50 | // Class Mixed Forest (5) 51 | var lc_class = 5; 52 | 53 | //Calculate loss 54 | var loss_2013_2012 = calculate_loss(vcf_2012,vcf_2013, lc_class) 55 | var loss_2014_2013 = calculate_loss(vcf_2013,vcf_2014, lc_class) 56 | var loss_2015_2014 = calculate_loss(vcf_2014,vcf_2015, lc_class) 57 | 58 | 59 | // Function to calculate the area of the loss in hectares 60 | function area_of_loss(img, ft){ 61 | var area = img.multiply(ee.Image.pixelArea()); 62 | var statistics = area.reduceRegion({ 63 | reducer: ee.Reducer.sum(), 64 | geometry: ft, 65 | maxPixels: 5e9 66 | }); 67 | var area_loss_object = statistics.get('Percent_Tree_Cover'); 68 | var area_loss = area_loss_object.getInfo() 69 | return area_loss/-10000 70 | } 71 | 72 | // calculate the area of the loss 73 | var area_of_loss_hectares_2013_2012 = area_of_loss(loss_2013_2012, chiapas) 74 | var area_of_loss_hectares_2014_2013 = area_of_loss(loss_2014_2013, chiapas) 75 | var area_of_loss_hectares_2015_2014 = area_of_loss(loss_2015_2014, chiapas) 76 | 77 | //Create result list 78 | var result_list = [ 79 | area_of_loss_hectares_2013_2012, area_of_loss_hectares_2014_2013, 80 | area_of_loss_hectares_2015_2014]; 81 | 82 | //Display result list 83 | console.log(result_list) -------------------------------------------------------------------------------- /002-landsat8_toa_tasseled_cap_transformation.js: -------------------------------------------------------------------------------- 1 | // Author: René Kopeinig 2 | // Script: Tasseled Cap Transformation for Landsat 8 3 | // Description: Tasseled Cap Transformation for Landsat 8 based on the 4 | // scientfic work "Derivation of a tasselled cap transformation based on Landsat 8 at-satellite reflectance" by 5 | // M.Baigab, L.Zhang, T.Shuai & Q.Tong (2014). The bands of the output image are the brightness index, 6 | // greenness index and wetness index. 7 | // Version: 0.1 8 | var calculateTasseledCap = function (image){ 9 | var b = image.select("B2", "B3", "B4", "B5", "B6", "B7"); 10 | //Coefficients are only for Landsat 8 TOA 11 | var brightness_coefficents= ee.Image([0.3029, 0.2786, 0.4733, 0.5599, 0.508, 0.1872]) 12 | var greenness_coefficents= ee.Image([-0.2941, -0.243, -0.5424, 0.7276, 0.0713, -0.1608]); 13 | var wetness_coefficents= ee.Image([0.1511, 0.1973, 0.3283, 0.3407, -0.7117, -0.4559]); 14 | var fourth_coefficents= ee.Image([-0.8239, 0.0849, 0.4396, -0.058, 0.2013, -0.2773]); 15 | var fifth_coefficents= ee.Image([-0.3294, 0.0557, 0.1056, 0.1855, -0.4349, 0.8085]); 16 | var sixth_coefficents= ee.Image([0.1079, -0.9023, 0.4119, 0.0575, -0.0259, 0.0252]); 17 | 18 | var brightness = image.expression( 19 | '(B * BRIGHTNESS)', 20 | { 21 | 'B':b, 22 | 'BRIGHTNESS': brightness_coefficents 23 | } 24 | ); 25 | var greenness = image.expression( 26 | '(B * GREENNESS)', 27 | { 28 | 'B':b, 29 | 'GREENNESS': greenness_coefficents 30 | } 31 | ); 32 | var wetness = image.expression( 33 | '(B * WETNESS)', 34 | { 35 | 'B':b, 36 | 'WETNESS': wetness_coefficents 37 | } 38 | ); 39 | var fourth = image.expression( 40 | '(B * FOURTH)', 41 | { 42 | 'B':b, 43 | 'FOURTH': fourth_coefficents 44 | } 45 | ); 46 | var fifth = image.expression( 47 | '(B * FIFTH)', 48 | { 49 | 'B':b, 50 | 'FIFTH': fifth_coefficents 51 | } 52 | ); 53 | var sixth = image.expression( 54 | '(B * SIXTH)', 55 | { 56 | 'B':b, 57 | 'SIXTH': sixth_coefficents 58 | } 59 | ); 60 | brightness = brightness.reduce(ee.call("Reducer.sum")); 61 | greenness = greenness.reduce(ee.call("Reducer.sum")); 62 | wetness = wetness.reduce(ee.call("Reducer.sum")); 63 | fourth = fourth.reduce(ee.call("Reducer.sum")); 64 | fifth = fifth.reduce(ee.call("Reducer.sum")); 65 | sixth = sixth.reduce(ee.call("Reducer.sum")); 66 | var tasseled_cap = ee.Image(brightness).addBands(greenness).addBands(wetness) 67 | .addBands(fourth) 68 | .addBands(fifth) 69 | .addBands(sixth).rename('brightness','greenness','wetness','fourth','fifth','sixth') 70 | return tasseled_cap; 71 | }; 72 | 73 | var country_mask = ee.FeatureCollection(3513341).filter(ee.Filter.or(ee.Filter.eq('Country','Mexico'))); 74 | var landsat8_collection = ee.ImageCollection('LANDSAT/LC8_L1T_TOA') 75 | .filterDate('2016-01-01', '2016-04-19') 76 | .filterMetadata('CLOUD_COVER', 'less_than', 5) 77 | .filterBounds(country_mask) 78 | var landsat8_tasseled_cap = landsat8_collection.map(calculateTasseledCap); 79 | console.log(landsat8_tasseled_cap.getInfo()) 80 | addToMap(landsat8_tasseled_cap,{},'Landsat 8 Tasseled Cap'); -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Google Earth Engine Javascript Examples 2 | This is a collection of examples how to code with Google Earth Engine Javascript API. If you are already signed up in Google Earth Engine, just copy and paste my code in the [Google Earth Engine Sandbox](https://code.earthengine.google.com). 3 |

4 | **Note:** If you are interested in more tutorials or code snippets for Google Earth Engine JavaScript API, feel free to write me an email to rene.kopeinig@list.lu.

5 | 6 | ## Sandbox Tutorials for Google Earth Engine 7 | #### 001 Landsat 8 Classification plus spectra chart 8 | Classification Example for Landsat 8 plus spectra for classes in classified region. 9 |

10 | #### 002 Landsat 8 TOA Tasseled Cap Transformation: 11 | Tasseled Cap Transformation for Landsat 8 based on the scientfic work "Derivation of a tasselled cap transformation based on Landsat 8 at-satellite reflectance" by M.Baigab, L.Zhang, T.Shuai & Q.Tong (2014). The bands of the output image are the brightness index, greenness index and wetness index. 12 |

13 | #### 003 RapidEye Spectral Angle Mapper: 14 | Spectral Angle map calculation for RapidEye imagery. Please note, you might not be able to see the RapidEye imagery because they belong to a private repository. You can still use the Spectral Angle Mapper for Landsat as well. 15 |

16 | #### 004 Forest loss calculation: 17 | Calculation of overall forest loss between 2010 and 2015 for the consecutive years. 18 |

19 | #### 005 Loss calculation for MODIS Landcover Classification's 'Mixed Forest' class: 20 | Calculation of loss in class 'Mixed Forest' based on the MODIS Land cover classification 2012 between 2012 and 2015 for the consecutive years. 21 |

22 | #### 006 High potential forest map compared to MODIS Landcover Classification's forest classes: 23 | High forest potential map for the Mexican state of Chiapas according to [The Nature Conservancy](http://www.nature.org) and [Alianza México REDD+](http://www.alianza-mredd.org/) compared with MODIS Land cover classification 2012's forest classes. 24 |

25 | #### 007 RapidEye Tasseled Cap Transformation: 26 | Tasseled Cap Transformation for RapidEye Imagery based on the scientfic work [Derivation of Tasseled Cap Coefficients for RapidEye data](https://www.researchgate.net/publication/270302804_Derivation_of_Tasseled_Cap_Coefficients_for_RapidEye_data) by M.Schoenert, H.Weichelt, E.Zillmann & C.Jürgens (2014). The bands of the output image are the brightness index, greenness index and yellowness index. Note: You might not be able to see the RapidEye scene because of license issues but feel free to use my code on your RapidEye scene(s). 27 |

28 | #### 008 WEKA Learning Vector Quantization Clustering: 29 | Example which demonstrates clustering that implements WEKA Learning Vector Quantization algorithm for Landsat 8 based on T. Kohonen, "Learning Vector Quantization", The Handbook of Brain Theory and Neural Networks, 2nd Edition, MIT Press, 2003, pp. 631-634. 30 |

31 | #### 009 Spectra analysis on Landsat 8 TOA & Proba-V S1 TOC 32 | Example of spectra analysis on Landsat 8 TOA & Proba-V S1 TOC on features such as agriculture, urban and water. 33 |

34 | #### 010 MODIS regional or zonal statistics calculation 35 | Regional or zonal statistics calculation based on a MODIS Net Primary Productivity ([MOD17A3](https://lpdaac.usgs.gov/dataset_discovery/modis/modis_products_table/mod17a3)) for MODIS Land Cover Type ([MCD12Q1](https://lpdaac.usgs.gov/dataset_discovery/modis/modis_products_table/mcd12q1)) class 'Mixed forest'. Net Primary Productivity defines the rate at which all plants in an ecosystem produce net useful chemical energy. 36 | -------------------------------------------------------------------------------- /001-classification_ls8_plus_spectra_chart.js: -------------------------------------------------------------------------------- 1 | // Author: René Kopeinig 2 | // Script: Classification Example for Landsat8 plus spectra for classes in classified region 3 | // Version: 0.1 4 | 5 | var area = ee.FeatureCollection('ft:1ihcmnTQF2dUYTKXOIYYwYlJzFLSpO7zsIxg0Yqd5'); 6 | var landsat8_collection = ee.ImageCollection('LANDSAT/LC8_L1T').filterDate('2016-01-01', '2016-04-19').min().clip(area); 7 | var madmex = ee.Image("users/renekope/MEX_LC_2010_Landsat_v43") 8 | 9 | //Functions 10 | function NDVI(img) { return img.normalizedDifference(["B5","B4"]); }; 11 | function SAM(img){ 12 | var band1 = img.select("B1") 13 | var bandn = img.select("B2","B3","B4","B5","B6","B7","B8","B9"); 14 | var maxObjSize = 256; 15 | var b = band1.divide(bandn); 16 | var spectralAngleMap = b.atan(); 17 | var spectralAngleMap_sin = spectralAngleMap.sin(); 18 | var spectralAngleMap_cos = spectralAngleMap.cos(); 19 | var sum_cos = spectralAngleMap_cos.reduce(ee.call("Reducer.sum")); 20 | var sum_sin = spectralAngleMap_sin.reduce(ee.call("Reducer.sum")); 21 | var bundle = ee.Image.cat(sum_sin, sum_cos, spectralAngleMap_sin, spectralAngleMap_cos); 22 | return bundle; 23 | }; 24 | function EVI(img){ 25 | var g = ee.Image(2.5); 26 | var e1 = ee.Image(6); 27 | var e2 = ee.Image(7.5); 28 | var e3 = ee.Image(1); 29 | var nir = img.select("B5"); 30 | var red = img.select("B4"); 31 | var blue = img.select("B2"); 32 | var evi = img.expression( 33 | "G*((NIR-RED)/(NIR+E1*RED-E2*BLUE+E3))", 34 | { 35 | "G":g, 36 | "NIR":nir, 37 | "RED":red, 38 | "E1":e1, 39 | "E2":e2, 40 | "BLUE":blue, 41 | "E3":e3 42 | } 43 | ); 44 | return evi; 45 | }; 46 | function ARVI(img){ 47 | var red = img.select("B4"); 48 | var blue = img.select("B2"); 49 | var nir = img.select("B5"); 50 | var tred = red.multiply(red); 51 | var arvi = img.expression( 52 | "NIR - (TRED - BLUE)/(NIR+(TRED-BLUE))",{ 53 | "NIR": nir, 54 | "TRED": tred, 55 | "BLUE": blue 56 | } 57 | ); 58 | return arvi; 59 | }; 60 | function LAI(img){ 61 | var nir = img.select("B5"); 62 | var red = img.select("B4"); 63 | var coeff1 = ee.Image(0.0305); 64 | var coeff2 = ee.Image(1.2640); 65 | var lai = img.expression("(((NIR/RED)*COEFF1)+COEFF2)", 66 | { 67 | "NIR":nir, 68 | "RED":red, 69 | "COEFF1":coeff1, 70 | "COEFF2":coeff2 71 | }); 72 | return lai; 73 | }; 74 | 75 | function calculate_spectral_indices(input){ 76 | var ndvi = NDVI(input); 77 | var sam = SAM(input); 78 | var lai = LAI(input); 79 | var arvi = ARVI(input); 80 | var evi = EVI(input); 81 | input = input.slice(0,9); 82 | var spctrl_indices_stack = ee.Image(ndvi).addBands(input).addBands(lai).addBands(sam).addBands(arvi).addBands(evi); 83 | return spctrl_indices_stack 84 | }; 85 | 86 | function classification(raster_input, vector_input, number_of_training_points, cover, class_algorithm){ 87 | var band_list = raster_input.bandNames(); 88 | for (var i = 0; i < number_of_training_points.length; i++) { 89 | var random_points = ee.FeatureCollection.randomPoints(vector_input, number_of_training_points[i], number_of_training_points[i], 1); 90 | var training = cover.addBands(raster_input).reduceToVectors({ 91 | reducer: "mean", 92 | geometry: random_points, 93 | geometryType: "centroid", 94 | scale: 30, 95 | crs: "EPSG:4326"}); 96 | 97 | var classifier = training.trainClassifier({ 98 | property_list: band_list, 99 | class_property: "label", 100 | classifier_name: class_algorithm}); 101 | var out = raster_input.classify(classifier); 102 | return out; 103 | } 104 | } 105 | var sld = '\ 106 | \ 107 | \ 108 | \ 109 | \ 110 | \ 111 | \ 112 | \ 113 | \ 114 | \ 115 | \ 116 | \ 117 | \ 118 | \ 119 | \ 120 | \ 121 | \ 122 | \ 123 | \ 124 | \ 125 | \ 126 | \ 127 | \ 128 | \ 129 | \ 130 | \ 131 | \ 132 | \ 133 | \ 134 | \ 135 | \ 136 | \ 137 | \ 138 | \ 139 | \ 140 | \ 141 | \ 142 | \ 143 | \ 144 | \ 145 | \ 146 | '; 147 | 148 | var spectral_indices = calculate_spectral_indices(landsat8_collection); 149 | var classification = classification(spectral_indices, area, [1000], madmex, 'Cart'); 150 | addToMap(classification.sldStyle(sld), {}, "Classification with Landsat 8 and MAD-Mex 2010 training dataset") 151 | 152 | function calculate_spectra_chart_classficiation(classifiedImage, inputImage, fusionTable){ 153 | var classNames = ee.List(['Agua', 'Bosque', 'Matorral', 'Agricultura', 'Urbana', 'Selva', 'Otro Vegetation', 'Otro']); 154 | var from = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 98, 99, 100, 123, 124, 200, 210]; 155 | var to = [1, 1, 1, 2, 1, 4, 2, 1, 5, 5, 5, 5, 5, 6, 6, 7, 2, 2, 2, 6, 2, 2, 2, 6, 6, 6, 6, 3, 0, 4, 4, 7, 7, 1, 1, 1, 5, 5]; 156 | classifiedImage = classifiedImage 157 | .remap(from, to); 158 | var input = inputImage.slice(0,8); 159 | var image = input.addBands(classifiedImage); 160 | var wavelengths = [0.44, 0.48, 0.56, 0.65, 0.86, 1.61, 2.1, 2.5]; 161 | var options = { 162 | lineWidth: 1, 163 | pointSize: 2, 164 | hAxis: {title: 'Wavelength (micrometers)'}, 165 | vAxis: {title: 'Reflectance'}, 166 | title: 'Spectra for classes in classified region', 167 | series: { 168 | 0: {color: '00FF00'}, //bosque 169 | 1: {color: 'faff98'}, // agriculture 170 | 2: {color: 'aa007f'}, // selva 171 | 3: {color: 'aa8fff'} // otro vegetacion 172 | } 173 | }; 174 | var chart = Chart.image.byClass( 175 | image, 'remapped', fusionTable, ee.Reducer.mean(), 500, classNames, wavelengths) 176 | .setOptions(options); 177 | print(chart); 178 | }; 179 | calculate_spectra_chart_classficiation(classification, landsat8_collection, area); -------------------------------------------------------------------------------- /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 | --------------------------------------------------------------------------------