• 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
    • 2
    • 3
    • 4

    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));
    
    • 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
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    影像截图:
    在这里插入图片描述
    影像集截图(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);
    
    • 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
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

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

    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)
    
    • 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
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

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

  • 相关阅读:
    使用Servlet+Tomcat+MySQL开发-简易版部门信息管理系统(单表CRUD)
    河南灵活就业平台能降低企业所得税吗?
    2022-09-09 Unity InputSystem3——InputAction
    Python专题复习整理
    Postman接口测试工具详解
    MATLAB | 绘图复刻(三) | 分层聚类分析图:树状图+热图
    JAVA电商平台免费搭建 B2B2C商城系统 多用户商城系统 直播带货 新零售商城 o2o商城 电子商务 拼团商城 分销商城
    HarmonyOS NEXT应用开发—Grid和List内拖拽交换子组件位置
    03-系统篇-内存碎片
    阿里云服务器怎么购买价格更便宜?先领优惠券再通过阿里云活动购买最便宜
  • 原文地址:https://blog.csdn.net/qq_32306361/article/details/126087196