GEE学习笔记二 GEE获取Sentine-2与Landsat8 NDVI数据并进行时序变化分析

本文主要演示利用GEE对Lnadsat8与Sentinel-2数据获取研究区内的NDVI并进行时序变化演示

NDVI的计算公式:NDVI = (近红外波段 - 红波段) / (近红外波段 + 红波段)
因为每种卫星波段不同,所以计算NDVI时选用的波段都有所不同,本文提到的Landsat8与Sentinel-2影像计算NDVI公式如下:

Landsat8: NDVI = (band5 - band4) / (band5 + band4)

Sentinel2: NDVI = (band8 - band4) / (band8 + band4)

通过GEE获取两种影像数据的NDVI以及趋势线代码如下:

var geometry = ee.FeatureCollection('users/huiengine/taian_boundary')
Map.centerObject(geometry,7)

var visParam_NDVI = {
 min: -0.1,
 max: 0.9,
 palette: 'FFFFFF, CE7E45, DF923D, F1B555, FCD163, 99B718, 74A901, 66A000, 529400,' +
   '3E8601, 207401, 056201, 004C00, 023B01, 012E01, 011D01, 011301'
};



//--------------------------------Landsat8获取 NDVI
//Landsat8去云函数
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);
}

function createNDVI_L8(image){
  var ndvi = image.normalizedDifference(["B5","B4"]).rename('NDVI');
  return image.addBands(ndvi);
}

var L8_COL = ee.ImageCollection("LANDSAT/LC08/C01/T1_SR")
.filterDate("2020-04-01", "2020-05-15")
.filterBounds(geometry)
.map(maskL8sr)
.map(createNDVI_L8)
.select('NDVI');
print(L8_COL)
Map.addLayer(L8_COL.mean().clip(geometry), visParam_NDVI, 'L8_col');

//趋势线代码 
var L8_chart = ui.Chart.image.series({
    imageCollection: L8_COL.select('NDVI'),
    region: geometry,
    reducer: ee.Reducer.mean(),
    scale: 500
    }).setOptions({
      interpolateNulls: true,
      lineWidth: 2,
      title: 'Landsat8 NDVI Time Seires',
      vAxis: {title: 'NDVI'},
      hAxis: {title: 'Date'},
      trendlines: { 0: {title: 'NDVI_trend',type:'linear', showR2: true,  color:'red', visibleInLegend: true}}
    });
print(L8_chart);



//--------------------------------Sentinel-2获取 NDVI
//使用QA波段去云
function maskS2clouds(image) {
  var qa = image.select('QA60');
  // Bits 10 and 11 are clouds and cirrus, respectively.
  var cloudBitMask = 1 << 10;
  var cirrusBitMask = 1 << 11;
  // Both flags should be set to zero, indicating clear conditions.
  var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
      .and(qa.bitwiseAnd(cirrusBitMask).eq(0));
  return image.updateMask(mask).divide(10000).set(image.toDictionary(image.propertyNames()));
}

//NDVI的计算公式
function createNDVI(image){
  var ndvi = image.normalizedDifference(["B8","B4"]).rename('NDVI');
  return image.addBands(ndvi);
}

var S2_COL = ee.ImageCollection("COPERNICUS/S2")
.filterDate("2020-04-01", "2020-05-15")
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',20))
.filterBounds(geometry)
.map(maskS2clouds)
.map(createNDVI)
.select('NDVI');
print(S2_COL)
Map.addLayer(S2_COL.mean().clip(geometry), visParam_NDVI, 'S2_col');

//趋势线代码 
var S2_chart = ui.Chart.image.series({
    imageCollection: S2_COL.select('NDVI'),
    region: geometry,
    reducer: ee.Reducer.mean(),
    scale: 500
    }).setOptions({
      interpolateNulls: true,
      lineWidth: 2,
      title: 'Sentinel-2 NDVI Time Seires',
      vAxis: {title: 'NDVI'},
      hAxis: {title: 'Date'},
      trendlines: { 0: {title: 'NDVI_trend',type:'linear', showR2: true,  color:'red', visibleInLegend: true}}
    });
print(S2_chart);





结果显示

Landsat8获取的NDVI图

Sentinel-2获取的NDVI图
GEE学习笔记二 GEE获取Sentine-2与Landsat8 NDVI数据并进行时序变化分析_第1张图片
获取NDVI时间序列曲线及趋势

附上本文代码链接:https://code.earthengine.google.com/5ddaca630b23728664fd88d4a82c8d22

你可能感兴趣的:(GEE学习专栏,云计算,sentinel,学习)