Untitled

mail@pastecode.io avatar
unknown
javascript
7 months ago
7.1 kB
1
Indexable
Never
var scale = 13;
var scaleChart = 30;
var maxCloudLevel = 16 // IMAGES WITH CLOUNDS WILL BE IGNORED!
var filterDateStart = '2017-01-01';
var filterDateEnd = '2017-12-31';
//Geometry start
var ROI = 
    /* color: #d63000 */
    /* displayProperties: [
      {
        "type": "rectangle"
      }
    ] */
    ee.Geometry.Polygon(
        [[[-42.4620728015091, -20.69186828506718],
          [-42.4620728015091, -20.702225871612377],
          [-42.45168728820344, -20.702225871612377],
          [-42.45168728820344, -20.69186828506718]]], null, false);
//Geometry end

// CENTER
Map.centerObject(ROI);
//TRUE COLOR START
var datasetCOLOR = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
function applyScaleFactors(image) {
  var opticalBands = image.select('SR_B.').multiply(0.0000275).add(-0.2);
  var thermalBands = image.select('ST_B.*').multiply(0.00341802).add(149.0);
  return image.addBands(opticalBands, null, true)
              .addBands(thermalBands, null, true);
}
datasetCOLOR = datasetCOLOR.map(applyScaleFactors);

var datasetCOLORCloudfiltered = datasetCOLOR.filterDate(filterDateStart, filterDateEnd)
                            .filterBounds(ROI)
                            .filterMetadata('CLOUD_COVER','less_than', maxCloudLevel)
                            .mean()
                            .clip(ROI);
var visualizationCOLOR = {
  bands: ['SR_B4', 'SR_B3', 'SR_B2'],
  min: 0.0,
  max: 0.3,
};
datasetCOLOR = datasetCOLOR.filterMetadata('CLOUD_COVER','less_than', maxCloudLevel);
// EVI e NDVI
//processing for data collection only
function EVICalc(image){
  var processedEVI = image.expression('2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))',{
  'NIR' : image.select('SR_B5'),
  'RED' : image.select('SR_B4'),
  'BLUE' : image.select('SR_B2')}).rename('EVI');
  return image.addBands(processedEVI)
}
function NDVICalc(image){
  var processedNDVI = image.expression('(NIR - RED) / (NIR + RED)',{
  'NIR' : image.select('SR_B5'),
  'RED' : image.select('SR_B4')}).rename('NDVI')
  return image.addBands(processedNDVI)
}

datasetCOLOR = datasetCOLOR.map(EVICalc);
datasetCOLOR = datasetCOLOR.map(NDVICalc);

//map layering
var EVI = datasetCOLORCloudfiltered.expression('2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))',{
  'NIR' : datasetCOLORCloudfiltered.select('SR_B5'),
  'RED' : datasetCOLORCloudfiltered.select('SR_B4'),
  'BLUE' : datasetCOLORCloudfiltered.select('SR_B2')})
var NDVI = datasetCOLORCloudfiltered.expression('(NIR - RED) / (NIR + RED)',{
  'NIR' : datasetCOLORCloudfiltered.select('SR_B5'),
  'RED' : datasetCOLORCloudfiltered.select('SR_B4')})
  


//CLOUD filtering OK!
Map.addLayer(datasetCOLORCloudfiltered, visualizationCOLOR, 'True Color (432)');
Map.addLayer(EVI, {min: -1, max: 1, palette: ['black','white','green']}, 'EVI');
Map.addLayer(NDVI, {min: -1, max: 1, palette: ['black','white','green']}, 'NDVI');
Map.addLayer(ROI, {}, 'Serra do Brigadeiro');

//Chart EVI year only
var chartEVIYear =
    ui.Chart.image
        .seriesByRegion({
          imageCollection: datasetCOLOR.filterDate(filterDateStart, filterDateEnd),
          band: 'EVI',
          regions: ROI,
          reducer: ee.Reducer.mean(),
          scale: scaleChart
        })
        .setOptions({
          title: 'EVI for selected area',
          hAxis: {title: 'Date', titleTextStyle: {italic: false, bold: true}},
          vAxis: {
            title: 'index',
            titleTextStyle: {italic: false, bold: true}
          },
          lineWidth: 5,
          colors: ['e37d05'],
          curveType: 'function'
        });
print(chartEVIYear);
//Chart NDVI year only
var chartNDVIYear =
    ui.Chart.imagelandsat 8
        .seriesByRegion({
          imageCollection: datasetCOLOR.filterDate(filterDateStart, filterDateEnd),
          band: 'NDVI',
          regions: ROI,
          reducer: ee.Reducer.mean(),
          scale: scaleChart
        })
        .setOptions({
          title: 'NDVI for selected area',
          hAxis: {title: 'Date', titleTextStyle: {italic: false, bold: true}},
          vAxis: {
            title: 'index',
            titleTextStyle: {italic: false, bold: true}
          },
          lineWidth: 5,
          colors: ['1d6b99'],
          curveType: 'function'
        });
print(chartNDVIYear);
//Chart NDVI&EVI year only
var chartBOTHYear =
    ui.Chart.image
        .series({
          imageCollection: datasetCOLOR.filterDate(filterDateStart, filterDateEnd).select(['NDVI', 'EVI']),
          region: ROI,
          reducer: ee.Reducer.mean(),
          scale: scaleChart,
          xProperty: 'system:time_start'
        })
        .setSeriesNames(['EVI', 'NDVI'])
        .setOptions({
          title: 'Average Vegetation Index Value by Date for selected area',
          hAxis: {title: 'Date', titleTextStyle: {italic: false, bold: true}},
          vAxis: {
            title: 'Vegetation index',
            titleTextStyle: {italic: false, bold: true}
          },
          lineWidth: 5,
          colors: ['e37d05', '1d6b99'],
          curveType: 'function'
        });
print(chartBOTHYear);

// whole dadaset now

//Chart EVI
var chartEVI =
    ui.Chart.image
        .seriesByRegion({
          imageCollection: datasetCOLOR,
          band: 'EVI',
          regions: ROI,
          reducer: ee.Reducer.mean(),
          scale: scaleChart
        })
        .setOptions({
          title: 'EVI for selected area',
          hAxis: {title: 'Date', titleTextStyle: {italic: false, bold: true}},
          vAxis: {
            title: 'index',
            titleTextStyle: {italic: false, bold: true}
          },
          lineWidth: 5,
          colors: ['e37d05'],
          curveType: 'function'
        });
print(chartEVI);
//Chart NDVI
var chartNDVI =
    ui.Chart.image
        .seriesByRegion({
          imageCollection: datasetCOLOR,
          band: 'NDVI',
          regions: ROI,
          reducer: ee.Reducer.mean(),
          scale: scaleChart
        })
        .setOptions({
          title: 'NDVI for selected area',
          hAxis: {title: 'Date', titleTextStyle: {italic: false, bold: true}},
          vAxis: {
            title: 'index',
            titleTextStyle: {italic: false, bold: true}
          },
          lineWidth: 5,
          colors: ['1d6b99'],
          curveType: 'function'
        });
print(chartNDVI);
//Chart NDVI&EVI
var chartNDVIEVI =
    ui.Chart.image
        .series({
          imageCollection: datasetCOLOR.select(['NDVI', 'EVI']),
          region: ROI,
          reducer: ee.Reducer.mean(),
          scale: scaleChart,
          xProperty: 'system:time_start'
        })
        .setSeriesNames(['EVI', 'NDVI'])
        .setOptions({
          title: 'Average Vegetation Index Value by Date for selected area',
          hAxis: {title: 'Date', titleTextStyle: {italic: false, bold: true}},
          vAxis: {
            title: 'Vegetation index',
            titleTextStyle: {italic: false, bold: true}
          },
          lineWidth: 5,
          colors: ['e37d05', '1d6b99'],
          curveType: 'function'
        });
print(chartNDVIEVI);