• GEE开发之Landsat_SR计算地表温度(不推荐)



    前言:此代码过于复杂,了解即可。


    1 LANDSAT/LC08/C01/T1_SR

    • 该数据集是来自Landsat 8 OLI/TIRS传感器的经大气校正的表面反射率。这些图像包含5个可视和近红外(VNIR)波段和2个短波红外(SWIR)波段,分别处理为正射校正表面反射率,以及两个热红外(TIR)波段,处理为正入射校正亮度温度。
    • 这些数据已经使用LaSRC进行了大气校正,包括使用CFMASK生成的云、阴影、水和雪掩模,以及每像素饱和度掩模。
    • 使用标准化参考网格,将采集的数据条打包成重叠的“场景”,覆盖约170km x 183km。

    2 构建FeatureCollection得到每月的温度数据

    代码如下(以鹿邑县为例)

    var geometry = ee.FeatureCollection('users/www1573979951/luyixian')
    Map.centerObject(geometry,7)
    var startYr = 2020;
    var endYr = 2020; 
     
    // year sequence 
    var startDate = ee.Date.fromYMD(startYr,01,01);
    var endDate = ee.Date.fromYMD(endYr+1,01,01);
    
    //去云代码
    function maskL8sr(image) {
      var cloudShadowBitMask = (1 << 3);
      var cloudsBitMask = (1 << 5);
      var qa = image.select('pixel_qa');
      var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0).and(qa.bitwiseAnd(cloudsBitMask).eq(0));
      return image.updateMask(mask);
    }
     
    // Load the collection 
    var l8lst = ee.ImageCollection("LANDSAT/LC08/C01/T1_SR").filterDate(startDate, endDate).filterBounds(geometry).map(maskL8sr);
    print(l8lst);
    print(ui.Chart.image.series(l8lst, geometry, ee.Reducer.mean(), 1000));//打印成折线图
    //** Visualization parameters
    var vizParams = {
      bands: ['B6', 'B5', 'B4'],
      min: 0,
      max: 3200,
      gamma: 1.4,
    };
    Map.addLayer(l8lst.mean().clip(geometry),vizParams,'l8 imagery');
    
    
    //--------------------------------------
     
    //Projection of Landsat imagery,
    //it shows an error if bands of the image don't have all the same
    var l8proj = ee.Image(l8lst.first()).projection();
     
    //** Calculate number of months withiin the specified time period
    var dateDiff = endDate.difference(startDate,'month');
    var noMonths = ee.Number(dateDiff).round().subtract(1);
     
    //** Create list of dates, one for each month in the time period
    var allMonths = ee.List.sequence(0,noMonths,1);
    var dateList = allMonths.map(function(monthNo){
      return startDate.advance(monthNo,'month');
    });
    // print(dateList);
     
    //** FUNCTION: for calculating monthly LST values
    var calcMonthlyLST = function(date){
      
      //** Filter collection by month
      var start = ee.Date(date);
      var end = start.advance(1,'month');
      var collection = l8lst.filterDate(start,end);
      
      //** Create median composite for that month
      var monthMed = collection.median();
      
      //** Create NDVI and Thermal Band images from composite
      var ndvi = monthMed.normalizedDifference(['B5','B4']);
      var thermalImg = monthMed.select('B10').multiply(0.1);
      
      //*8 Calculate min and max NDVI
      var minNDVI = ee.Number(ndvi.reduceRegion({
      reducer: ee.Reducer.min(),
      geometry: geometry,
      scale: 30,
      maxPixels: 1e13
      }).values().get(0));
      
      var maxNDVI = ee.Number(ndvi.reduceRegion({
      reducer: ee.Reducer.max(),
      geometry: geometry,
      scale: 30,
      maxPixels: 1e13
      }).values().get(0));
      
      //** Fraction of vegetation (use 'if' statement so that where entire image is masked and
      //    minNDVI and maxNDVI are null, produce a null image)
      var fv = ee.Algorithms.If({
        condition: minNDVI && maxNDVI,
        trueCase: ndvi.subtract(minNDVI).divide(maxNDVI.subtract(minNDVI)).rename('FV'),
        falseCase: ee.Image()
      });
      //** Recast to ee.Image object
      fv = ee.Image(fv);
     
      //** Emissivity (again use 'if' statement for cases of null inputs)
      var a = ee.Number(0.004);
      var b = ee.Number(0.986);
      var EM = ee.Algorithms.If({
        condition: fv.bandNames(),
        trueCase: fv.multiply(a).add(b).rename('EMM'),
        falseCase: ee.Image()
      });
      //** Recast to ee.Image object
      EM = ee.Image(EM);
      
      //** Land surface temperature (again use 'if' statement for cases of null inputs)
      var LST = ee.Algorithms.If({
        condition: EM.bandNames(),
        trueCase: thermalImg.expression(
        '(Tb/(1 + (0.0010895* (Tb / 1.438))*log(Ep)))-273.15', {
          'Tb': thermalImg.select('B10'),
          'Ep': EM.select('EMM')
        }).rename('LST'),
        falseCase: ee.Image().rename('LST')
      });
     
      //**  Use reduceRegion() to extract per-region value for this month
      var meanVal = ee.Image(LST).reduceRegion({
        reducer: ee.Reducer.mean(),
        geometry: geometry,
        scale: 30,
      maxPixels: 1e13,
        crs: l8proj
      });
     
      //** Create feature with desired data/properties and empty geometry, for charting by feature
      var ft = ee.Feature(null, {
        'system:time_start': date,
        'date': date.format('YYYY-MM-dd'),
        'value': meanVal.get('LST')
      });
      return ft;
    };
     
    //** Create feature collection, one for each month, wherein a property 'status' is
    //    added to indicate whether there is an empty collection for that month 
    var dateFeats = dateList.map(function(date){
      var thisColl = l8lst.filterDate(ee.Date(date), ee.Date(date).advance(1,'month'));
      return ee.Algorithms.If({
        condition: thisColl.first(),
        trueCase: ee.Feature(null, {'date':date, 'status':1}),
        falseCase: ee.Feature(null, {'date':date, 'status':0})
      });
    });
    dateFeats = ee.FeatureCollection(dateFeats);
    // print('dateFeats:', dateFeats);
     
    //** Map over date features and create per-month/year mean LST composites
    var perYrMonthlyLST = dateFeats.map(function(feature){
      var startDate = ee.Date(feature.get('date'));
      
      //** If month represented by this feature has imagery for it,
      //    calculate per-monnth LST; otherwise set LST to 0 (can't be 'null' for charting)
      var outFeat = ee.Algorithms.If({
        condition: ee.Number(feature.get('status')).eq(1),
        trueCase: ee.Feature(calcMonthlyLST(startDate)),
        falseCase: ee.Feature(null, {
          'system:time_start': startDate.millis(),
          'date': startDate,
          'value': 0
        })
      });
      return outFeat;
    });
    perYrMonthlyLST = ee.FeatureCollection(perYrMonthlyLST);
    print(perYrMonthlyLST);
     
    //** Create new graph for monthly temperatures over multiple years
    var multiYrGraph = ui.Chart.feature.byFeature({
      features:perYrMonthlyLST,
      xProperty:'system:time_start',
      yProperties: 'value'});
     
    //** Print graph to console
    print(multiYrGraph.setChartType("ColumnChart").setOptions({vAxis: {title: 'LST [deg. C]'},hAxis: {title: 'Date'}}));
    
    • 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
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 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
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170

    在这里插入图片描述

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    3 通过生成月均值影像得到每月的温度数据

    代码如下(以鹿邑县为例):

    var geometry = ee.FeatureCollection('users/www1573979951/luyixian')
    Map.centerObject(geometry,7)
    
    
    //cloud mask landsat7,landsat5, and landsat8 based on the pixel_qa band of Landsat SR data.
    // function for cloud masking on three types of lan dsat
    
    var LC8_BANDS = ['B4',  'B5', 'B10','pixel_qa']; //Landsat 8
    var LC7_BANDS = ['B3',  'B4','B6','pixel_qa']; //Landsat 7
    var LC5_BANDS = [ 'B3',  'B4','B6','pixel_qa']; //Llandsat 5
    var STD_NAMES = ['red', 'nir', 'temp','qa'];
    
    var cloudmasklandsat7and5and8= function(image){
        var Qlandsat5and7= image.select('qa');
        var cloudShadowBitMask = (1 << 3);
        var cloudsBitMask = (1 << 5);
        var mask5and7=Qlandsat5and7.clip(geometry).bitwiseAnd(cloudShadowBitMask).eq(0)
                                       .and(Qlandsat5and7.bitwiseAnd(cloudsBitMask).eq(0));
        return image.updateMask(mask5and7);
    };
    
    
    
    
    var landsat8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR').select(LC8_BANDS, STD_NAMES)
                      .filterDate('2013-01-01', '2021-12-31')
                      .filterBounds(geometry)
                      .filter(ee.Filter.lt('CLOUD_COVER', 25))
                     .map(cloudmasklandsat7and5and8);
    
    var landsat7 = ee.ImageCollection('LANDSAT/LE07/C01/T1_SR').select(LC7_BANDS, STD_NAMES)
                      .filterDate('1984-01-01', '2012-12-31')
                      .filterBounds(geometry)
                      .filter(ee.Filter.lt('CLOUD_COVER', 25))
                     .map(cloudmasklandsat7and5and8);
    
    var landsat5 = ee.ImageCollection('LANDSAT/LT05/C01/T1_SR').select(LC7_BANDS, STD_NAMES)
                      .filterDate('1984-01-01', '2012-12-31')
                      .filterBounds(geometry)
                      .filter(ee.Filter.lt('CLOUD_COVER', 25))
                     .map(cloudmasklandsat7and5and8);
    
    var landsatcolor={
      min: 0,
      max: 3000,
      gamma: 1.4,
    };
    //Map.addLayer(landsat8,landsatcolor, 'Landsat8' );
    //Map.addLayer(landsat7,landsatcolor, 'Landsat7' );
    //Map.addLayer(landsat5,landsatcolor, 'Landsat5' );
      
      
      // totall image collection
    var landsatimage = ee.ImageCollection(landsat5.merge(landsat7).merge(landsat8));
    Map.addLayer(landsatimage,landsatcolor, 'Landsat' );
    
     //    NDVI
     
     var setNdviMinMax=function (img) {
      var minMax = img
        .select('NDVI')
        .reduceRegion({
          reducer: ee.Reducer.minMax(),
          scale: 30,
          maxPixels: 1e13
        })
        ;
      return img.set({
        'NDVI_min': minMax.get('NDVI_min'),
        'NDVI_max': minMax.get('NDVI_max'),
      }).toFloat();
    };
    var NDVI=function(image){
      var ndvi = image.addBands(image.normalizedDifference(['nir', 'red']).rename('NDVI'));
      return setNdviMinMax(ndvi).clip(geometry).toFloat();
    };
    
      var ndviParams = {
      min: -1,
      max: 1.0,
      palette: [
        'FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901',
        '66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01',
        '012E01', '011D01', '011301'
      ],
    };
    
    var landsatwithndvi=landsatimage.map(NDVI).filter(ee.Filter.notNull(['NDVI_min', 'NDVI_max']));
    Map.addLayer(landsatwithndvi.select('NDVI').filter(ee.Filter.notNull(['NDVI_min', 'NDVI_max'])), ndviParams, 'landsatNdvi');
    
    var addMinMaxBands=function (img) {
      var minMaxBands = ee.Image.constant([
        img.get('NDVI_min'),
        img.get('NDVI_max')])
        .rename(['NDVImin', 'NDVImax']);
      return img.clip(geometry).addBands(minMaxBands).toFloat();
    };
     //2: map the min max function on collection
    var landsatNdviminmax = landsatwithndvi.map(addMinMaxBands).filterBounds(geometry);
    
    //fv 
    var addFVband=function (img) {
      var ndvi = img.select('NDVI');
      var ndviMin = img.select('NDVImin');
      var ndviMax = img.select('NDVImax');
      var fvBand = ndvi
        .subtract(ndviMin)
        .divide(ndviMax.subtract(ndviMin))
        .rename('FV');
      return img.clip(geometry).addBands(fvBand).toFloat();
    };
    //4: fv COLLECTION FOR LANDSAT
    var landsatfv= landsatNdviminmax.map(addFVband);
    
    // Em
    
    var addEMband=function (img){
      var FVb = img.select('FV');
      var a= ee.Number(0.004);
      var b= ee.Number(0.986);
      var EMBand = FVb
        .multiply(a).add(b).rename('EM');
      return img.addBands(EMBand).toFloat();
    };
    
    //5: EM COLLECTION FOR EACH LANDSAT
      
    var landsatEM= landsatfv.map(addEMband);
    
    //6: Thermal landsat 
    var LST=function(image){
      var Thermal = image.addBands(image.select('temp').multiply(0.1).rename('Thermal'));
      return Thermal};
    
    var landsatthermal= landsatEM.map(LST);
    
    var LStfunction = function(image){
      var LSTEQ=image.expression(
        '(Tb/(1 + (0.0010895* (Tb / 1.438))*log(Ep)))-273.15', {
          'Tb': image.select('Thermal'),
          'Ep': image.select('EM')}).rename('LST');
     return  image.addBands(LSTEQ)};
    
    var LST= landsatthermal.map(LStfunction);
    
    Map.addLayer(LST.select('LST'),{min: -30, max: 32, palette: ['white','blue','green','yellow' ,'red']},'LST');
    
    /// End of LST calculation for each image
    
    /// Monthly mean LST
    
    var years = ee.List.sequence(2016, 2020);
    var months = ee.List.sequence(1, 12);
    var monthlyLST =  ee.ImageCollection.fromImages(
      years.map(function (y) {
        return months.map(function(m) {
        return LST.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))
        .set('system:index',y+m);
        });
      }).flatten()
    );
    print(monthlyLST)
    var monthlychart = ui.Chart.image.series(monthlyLST.select('LST'), geometry,ee.Reducer.mean(),500)
                  .setChartType('LineChart').setOptions({
                    interpolateNulls: true,
                    title: 'LST monthly time series',
                    hAxis: {title: 'Date'},
                    vAxis: {title: 'LST',viewWindowMode: 'explicit', gridlines: {count: 10,}},
                    trendlines: { 0: {title: 'LST_trend',type:'linear', showR2: true,  color:'red', visibleInLegend: true}}});
    // Display.
    print(monthlychart);
    Map.addLayer(monthlyLST.select('LST'),{min: -30, max: 32, palette: ['white','blue','green','yellow' ,'red']},'LST_Monthly');
    
    • 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
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 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
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172

    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    cad转pdf怎么转
    实用篇 | 做自己的管理系统 :Pycharm+django+mysql
    关于std::vector<std::string>的操作
    Android使用MPAndroidChart 绘制折线图
    文章生成器免费版
    高数 | 【重积分】三维立体的形心坐标
    linux服务器文件实时同步
    Cy3-PEG-NHS ester,Cy3-聚乙二醇-琥珀酰亚胺活化酯,NHS-PEG-Cy3
    《低代码指南》——低代码+AI,驱动企业数字资产保值增值
    海外接单被没收百万收入并处以罚款,承德的这位程序员到底做了什么?
  • 原文地址:https://blog.csdn.net/qq_32306361/article/details/126230479