GEE开发之Landsat8_SR计算NDVI和数据分析


前言:主要介绍NDVI在Landsat8_SR下的计算。


1 NDVI的计算实现

代码如下:

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

1 遥感影像、数据获取

代码如下(以南京市为例):

var geometry = ee.FeatureCollection('users/www1573979951/nanjingshi')
Map.centerObject(geometry,6)
 
var colorizedVis = {
  min: -0.8,
  max: 0.8,
  palette: ['blue', 'white', 'green'],
};

//cloud mask 去云的方法
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);
}

//NDVI的计算
function createNDVI(image) {
  var ndvi = image.normalizedDifference(['B5', 'B4']).rename('NDVI');
  return image.addBands(ndvi);
}
 
var col = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
.map(maskL8sr)
.filterDate('2020-01-01','2020-12-31')
.filterBounds(geometry)
.map(createNDVI)
.select('NDVI');
 
Map.addLayer(col.mean().clip(geometry), colorizedVis, 'col');
print(col);
print(ui.Chart.image.series(col, geometry, ee.Reducer.mean(), 500));

影像截图:
在这里插入图片描述
影像集截图(44个数据):
在这里插入图片描述
表格截图:
在这里插入图片描述
CSV数据截图:
在这里插入图片描述

2 数据的变化趋势

仅仅看时间序列,发现有很多异常值。所以需要观察数据的变化趋势。此代码加入了趋势线的相关系数R2进行分析.

代码如下(以南京市为例):

var geometry = ee.FeatureCollection('users/www1573979951/nanjingshi')

//cloud mask 去云的方法
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);
}

//NDVI的计算
function createNDVI(image) {
  var ndvi = image.normalizedDifference(['B5', 'B4']).rename('NDVI');
  return image.addBands(ndvi);
}
 
var col = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
.map(maskL8sr)
.filterDate('2020-01-01','2020-12-31')
.filterBounds(geometry)
.map(createNDVI)
.select('NDVI');
 
var landsat8trendline = Chart.image.series(col, geometry, ee.Reducer.mean(), 500);
landsat8trendline = landsat8trendline
    .setOptions({
        title: 'Landsat 8 SR NDVI',
        hAxis: {title: 'Date', gridlines: {count: 10}},
        vAxis: {title: 'NDVI',viewWindowMode: 'explicit', viewWindow: {max: 1,min: -0.25,},gridlines: {count: 5,}},
        interpolateNulls: true, 
        lineWidth: 1,
    pointSize: 1,
    trendlines: { 0: {title: 'NDVI_trend',type:'linear', showR2: true,  color:'red', visibleInLegend: true}}
    });
print(landsat8trendline);

运行截图:
在这里插入图片描述

3 月平均数据的变化趋势

接着进行进行月平均分析。

代码如下(以南京市为例):

var geometry = ee.FeatureCollection('users/www1573979951/nanjingshi')

//cloud mask 去云的方法
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);
}

//NDVI的计算
function createNDVI(image) {
  var ndvi = image.normalizedDifference(['B5', 'B4']).rename('NDVI');
  return image.addBands(ndvi);
}
 
var col = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
.map(maskL8sr)
.filterDate('2020-01-01','2020-12-31')
.filterBounds(geometry)
.map(createNDVI)
.select('NDVI');

var years = ee.List.sequence(2020, 2020);
var months = ee.List.sequence(1, 12);
var landsat8monthlymeanNDVI =  ee.ImageCollection.fromImages(
  years.map(function (y) {
    return months.map(function(m) {
    return col.filter(ee.Filter.calendarRange(y,y, 'year')).filter(ee.Filter.calendarRange(m, m, 'month')).mean().set('year', y).set('month', m).set('system:time_start', ee.Date.fromYMD(y, m, 1));
    });
  }).flatten()
);

var monthlymeantrendline = Chart.image.series(landsat8monthlymeanNDVI, geometry, ee.Reducer.mean(), 500);
monthlymeantrendline = monthlymeantrendline
    .setOptions({
        title: 'Landsat 8 SR NDVI',
        hAxis: {title: 'Date', gridlines: {count: 10}},
        vAxis: {title: 'NDVI',viewWindowMode: 'explicit', viewWindow: {max: 1,min: -0.25,},gridlines: {count: 5,}},
        interpolateNulls: true, 
        lineWidth: 1,
    pointSize: 1,
    trendlines: { 0: {title: 'NDVI_trend',type:'linear', showR2: true,  color:'red', visibleInLegend: true}}
    });
print(monthlymeantrendline)

运行截图:
在这里插入图片描述
CSV数据截图:
在这里插入图片描述

  • 4
    点赞
  • 65
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 19
    评论
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

等待着冬天的风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值