利用GEE盘算城市遥感生态指数(RSEI)——Landsat 8为例

打印 上一主题 下一主题

主题 552|帖子 552|积分 1656


前言

城市生态与人类生活息息相关,快速 、准确 、客 观地了解城市生态状态已成为生态范畴的一个研究重点 。基于遥感技术,提出一个完全基 于遥感技术 ,以天然因子为主的遥感生态指数 (RSEI)来对城市的生态状态举行快速监测与评价 。该指数利用主身分分析技术集成了植被指数 、湿度分量、地表温度和建筑指数等 4个评价指标,它们分别代表了绿度、湿度、热度和干度等4大生态要素。
本文基于GEE平台,实现RSEI算法。
运行结果


第一步:界说研究区,自行更换本身的研究区

代码如下(示例):
  1. // 第一步:定义研究区,自行更换自己的研究区
  2. var roi =
  3.     /* color: #98ff00 */
  4.     /* displayProperties: [
  5.       {
  6.         "type": "rectangle"
  7.       }
  8.     ] */
  9.     ee.Geometry.Polygon(
  10.         [[[120.1210075098537, 35.975189051414006],
  11.           [120.1210075098537, 35.886229778229115],
  12.           [120.25764996590839, 35.886229778229115],
  13.           [120.25764996590839, 35.975189051414006]]], null, false);
  14.          
  15. Map.centerObject(roi);
复制代码
第二步:加载数据集,界说去云函数

代码如下(示例):
  1. // 第二步:加载数据集,定义去云函数
  2. function removeCloud(image){
  3.   var qa = image.select('BQA')
  4.   var cloudMask = qa.bitwiseAnd(1 << 4).eq(0)
  5.   var cloudShadowMask = qa.bitwiseAnd(1 << 8).eq(0)
  6.   var valid = cloudMask.and(cloudShadowMask)
  7.   return image.updateMask(valid)
  8. }
  9. // 数据集去云处理
  10. var L8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
  11.            .filterBounds(roi)
  12.            .filterDate('2018-01-01', '2019-12-31')
  13.            .filterMetadata('CLOUD_COVER', 'less_than',50)
  14.            .map(function(image){
  15.                     return image.set('year', ee.Image(image).date().get('year'))                           
  16.                   })
  17.            .map(removeCloud)
  18. // 影像合成
  19. var L8imgList = ee.List([])
  20. for(var a = 2018; a < 2020; a++){
  21.    var img = L8.filterMetadata('year', 'equals', a).median().clip(roi)
  22.    var L8img = img.set('year', a)
  23.    L8imgList = L8imgList.add(L8img)
  24. }
复制代码
第三步:主函数,盘算生态指标

代码如下(示例):
  1. // 第三步:主函数,计算生态指标
  2. var L8imgCol = ee.ImageCollection(L8imgList)
  3.                  .map(function(img){
  4.                       return img.clip(roi)
  5.                    })
  6.                  
  7. L8imgCol = L8imgCol.map(function(img){
  8.   
  9.   // 湿度函数:Wet
  10.   var Wet = img.expression('B*(0.1509) + G*(0.1973) + R*(0.3279) + NIR*(0.3406) + SWIR1*(-0.7112) + SWIR2*(-0.4572)',{
  11.        'B': img.select(['B2']),
  12.        'G': img.select(['B3']),
  13.        'R': img.select(['B4']),
  14.        'NIR': img.select(['B5']),
  15.        'SWIR1': img.select(['B6']),
  16.        'SWIR2': img.select(['B7'])
  17.      })   
  18.   img = img.addBands(Wet.rename('WET'))
  19.   
  20.   
  21.   // 绿度函数:NDVI
  22.   var ndvi = img.normalizedDifference(['B5', 'B4']);
  23.   img = img.addBands(ndvi.rename('NDVI'))
  24.   
  25.   
  26.   // 热度函数:lst 直接采用MODIS产品
  27.   var lst = ee.ImageCollection('MODIS/006/MOD11A1').map(function(img){
  28.                 return img.clip(roi)
  29.            })
  30.            .filterDate('2014-01-01', '2019-12-31')
  31.   
  32.   var year = img.get('year')
  33.   lst=lst.filterDate(ee.String(year).cat('-01-01'),ee.String(year).cat('-12-31')).select(['LST_Day_1km', 'LST_Night_1km']);
  34.       
  35.   // reproject主要是为了确保分辨率为1000
  36.   var img_mean=lst.mean().reproject('EPSG:4326',null,1000);
  37.   //print(img_mean.projection().nominalScale())
  38.   
  39.   img_mean = img_mean.expression('((Day + Night) / 2)',{
  40.       'Day': img_mean.select(['LST_Day_1km']),
  41.       'Night': img_mean.select(['LST_Night_1km']),
  42.        })
  43.   img = img.addBands(img_mean.rename('LST'))
  44.   
  45.   
  46.   // 干度函数:ndbsi = ( ibi + si ) / 2
  47.   var ibi = img.expression('(2 * SWIR1 / (SWIR1 + NIR) - (NIR / (NIR + RED) + GREEN / (GREEN + SWIR1))) / (2 * SWIR1 / (SWIR1 + NIR) + (NIR / (NIR + RED) + GREEN / (GREEN + SWIR1)))', {
  48.       'SWIR1': img.select('B6'),
  49.       'NIR': img.select('B5'),
  50.       'RED': img.select('B4'),
  51.       'GREEN': img.select('B3')
  52.     })
  53.   var si = img.expression('((SWIR1 + RED) - (NIR + BLUE)) / ((SWIR1 + RED) + (NIR + BLUE))', {
  54.       'SWIR1': img.select('B6'),
  55.       'NIR': img.select('B5'),
  56.       'RED': img.select('B4'),
  57.       'BLUE': img.select('B2')
  58.     })
  59.   var ndbsi = (ibi.add(si)).divide(2)
  60.   return img.addBands(ndbsi.rename('NDBSI'))
  61. })
  62. var bandNames = ['NDVI', "NDBSI", "WET", "LST"]
  63. L8imgCol = L8imgCol.select(bandNames)
  64. //定义归一化函数:归一化
  65. var img_normalize = function(img){
  66.       var minMax = img.reduceRegion({
  67.             reducer:ee.Reducer.minMax(),
  68.             geometry: roi,
  69.             scale: 1000,
  70.             maxPixels: 10e13,
  71.         })
  72.       var year = img.get('year')
  73.       var normalize  = ee.ImageCollection.fromImages(
  74.             img.bandNames().map(function(name){
  75.                   name = ee.String(name);
  76.                   var band = img.select(name);
  77.                   return band.unitScale(ee.Number(minMax.get(name.cat('_min'))), ee.Number(minMax.get(name.cat('_max'))));
  78.                     
  79.               })
  80.         ).toBands().rename(img.bandNames()).set('year', year);
  81.         return normalize;
  82. }
  83. var imgNorcol  = L8imgCol.map(img_normalize);
复制代码
第四步:PCA融合,提取第一主身分

代码如下(示例):
  1. // 第四步:PCA融合,提取第一主成分
  2. var pca = function(img){
  3.       
  4.       var bandNames = img.bandNames();
  5.       var region = roi;
  6.       var year = img.get('year')
  7.       // Mean center the data to enable a faster covariance reducer
  8.       // and an SD stretch of the principal components.
  9.       var meanDict = img.reduceRegion({
  10.             reducer:  ee.Reducer.mean(),
  11.             geometry: region,
  12.             scale: 1000,
  13.             maxPixels: 10e13
  14.         });
  15.       var means = ee.Image.constant(meanDict.values(bandNames));
  16.       var centered = img.subtract(means).set('year', year);
  17.       
  18.       
  19.       // This helper function returns a list of new band names.
  20.       var getNewBandNames = function(prefix, bandNames){
  21.             var seq = ee.List.sequence(1, 4);
  22.             //var seq = ee.List.sequence(1, bandNames.length());
  23.             return seq.map(function(n){
  24.                   return ee.String(prefix).cat(ee.Number(n).int());
  25.               });      
  26.         };
  27.       
  28.       // This function accepts mean centered imagery, a scale and
  29.       // a region in which to perform the analysis.  It returns the
  30.       // Principal Components (PC) in the region as a new image.
  31.       var getPrincipalComponents = function(centered, scale, region){
  32.             var year = centered.get('year')
  33.             var arrays = centered.toArray();
  34.         
  35.             // Compute the covariance of the bands within the region.
  36.             var covar = arrays.reduceRegion({
  37.                   reducer: ee.Reducer.centeredCovariance(),
  38.                   geometry: region,
  39.                   scale: scale,
  40.                   bestEffort:true,
  41.                   maxPixels: 10e13
  42.               });
  43.             
  44.             // Get the 'array' covariance result and cast to an array.
  45.             // This represents the band-to-band covariance within the region.
  46.             var covarArray = ee.Array(covar.get('array'));
  47.             
  48.             // Perform an eigen analysis and slice apart the values and vectors.
  49.             var eigens = covarArray.eigen();
  50.         
  51.             // This is a P-length vector of Eigenvalues.
  52.             var eigenValues = eigens.slice(1, 0, 1);
  53.             // This is a PxP matrix with eigenvectors in rows.
  54.             var eigenVectors = eigens.slice(1, 1);
  55.         
  56.             // Convert the array image to 2D arrays for matrix computations.
  57.             var arrayImage = arrays.toArray(1)
  58.             // Left multiply the image array by the matrix of eigenvectors.
  59.             var principalComponents = ee.Image(eigenVectors).matrixMultiply(arrayImage);
  60.         
  61.             // Turn the square roots of the Eigenvalues into a P-band image.
  62.             var sdImage = ee.Image(eigenValues.sqrt())
  63.             .arrayProject([0]).arrayFlatten([getNewBandNames('SD',bandNames)]);
  64.         
  65.             // Turn the PCs into a P-band image, normalized by SD.
  66.             return principalComponents
  67.             // Throw out an an unneeded dimension, [[]] -> [].
  68.             .arrayProject([0])
  69.             // Make the one band array image a multi-band image, [] -> image.
  70.             .arrayFlatten([getNewBandNames('PC', bandNames)])
  71.             // Normalize the PCs by their SDs.
  72.             .divide(sdImage)
  73.             .set('year', year);
  74.         }
  75.         
  76.         // Get the PCs at the specified scale and in the specified region
  77.         img = getPrincipalComponents(centered, 1000, region);
  78.         return img;
  79.   };
  80.   
  81. var PCA_imgcol = imgNorcol.map(pca)
  82. Map.addLayer(PCA_imgcol.first(), {"bands":["PC1"]}, 'pc1')
复制代码
第五步:利用PC1,盘算RSEI,并归一化

代码如下(示例):
  1. // 第五步:利用PC1,计算RSEI,并归一化
  2. var RSEI_imgcol = PCA_imgcol.map(function(img){
  3.         img = img.addBands(ee.Image(1).rename('constant'))
  4.         var rsei = img.expression('constant - pc1' , {
  5.              constant: img.select('constant'),
  6.              pc1: img.select('PC1')
  7.          })
  8.         rsei = img_normalize(rsei)
  9.         return img.addBands(rsei.rename('rsei'))
  10.     })
  11. print(RSEI_imgcol)
  12. var visParam = {
  13.     palette: '040274, 040281, 0502a3, 0502b8, 0502ce, 0502e6, 0602ff, 235cb1, 307ef3, 269db1, 30c8e2, 32d3ef, 3be285, 3ff38f, 86e26f, 3ae237, b5e22e, d6e21f, fff705, ffd611, ffb613, ff8b13, ff6e08, ff500d, ff0000, de0101, c21301, a71001, 911003'
  14. };
  15. Map.addLayer(RSEI_imgcol.first().select('rsei'), visParam, 'rsei')
复制代码
完整代码

代码如下(示例):
  1. // 第一步:定义研究区,自行更换自己的研究区
  2. var roi =
  3.     /* color: #98ff00 */
  4.     /* displayProperties: [
  5.       {
  6.         "type": "rectangle"
  7.       }
  8.     ] */
  9.     ee.Geometry.Polygon(
  10.         [[[120.1210075098537, 35.975189051414006],
  11.           [120.1210075098537, 35.886229778229115],
  12.           [120.25764996590839, 35.886229778229115],
  13.           [120.25764996590839, 35.975189051414006]]], null, false);
  14.          
  15. Map.centerObject(roi);
  16. // 第二步:加载数据集,定义去云函数
  17. function removeCloud(image){
  18.   var qa = image.select('BQA')
  19.   var cloudMask = qa.bitwiseAnd(1 << 4).eq(0)
  20.   var cloudShadowMask = qa.bitwiseAnd(1 << 8).eq(0)
  21.   var valid = cloudMask.and(cloudShadowMask)
  22.   return image.updateMask(valid)
  23. }
  24. // 数据集去云处理
  25. var L8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
  26.            .filterBounds(roi)
  27.            .filterDate('2018-01-01', '2019-12-31')
  28.            .filterMetadata('CLOUD_COVER', 'less_than',50)
  29.            .map(function(image){
  30.                     return image.set('year', ee.Image(image).date().get('year'))                           
  31.                   })
  32.            .map(removeCloud)
  33. // 影像合成
  34. var L8imgList = ee.List([])
  35. for(var a = 2018; a < 2020; a++){
  36.    var img = L8.filterMetadata('year', 'equals', a).median().clip(roi)
  37.    var L8img = img.set('year', a)
  38.    L8imgList = L8imgList.add(L8img)
  39. }
  40. // 第三步:主函数,计算生态指标
  41. var L8imgCol = ee.ImageCollection(L8imgList)
  42.                  .map(function(img){
  43.                       return img.clip(roi)
  44.                    })
  45.                  
  46. L8imgCol = L8imgCol.map(function(img){
  47.   
  48.   // 湿度函数:Wet
  49.   var Wet = img.expression('B*(0.1509) + G*(0.1973) + R*(0.3279) + NIR*(0.3406) + SWIR1*(-0.7112) + SWIR2*(-0.4572)',{
  50.        'B': img.select(['B2']),
  51.        'G': img.select(['B3']),
  52.        'R': img.select(['B4']),
  53.        'NIR': img.select(['B5']),
  54.        'SWIR1': img.select(['B6']),
  55.        'SWIR2': img.select(['B7'])
  56.      })   
  57.   img = img.addBands(Wet.rename('WET'))
  58.   
  59.   
  60.   // 绿度函数:NDVI
  61.   var ndvi = img.normalizedDifference(['B5', 'B4']);
  62.   img = img.addBands(ndvi.rename('NDVI'))
  63.   
  64.   
  65.   // 热度函数:lst 直接采用MODIS产品
  66.   var lst = ee.ImageCollection('MODIS/006/MOD11A1').map(function(img){
  67.                 return img.clip(roi)
  68.            })
  69.            .filterDate('2014-01-01', '2019-12-31')
  70.   
  71.   var year = img.get('year')
  72.   lst=lst.filterDate(ee.String(year).cat('-01-01'),ee.String(year).cat('-12-31')).select(['LST_Day_1km', 'LST_Night_1km']);
  73.       
  74.   // reproject主要是为了确保分辨率为1000
  75.   var img_mean=lst.mean().reproject('EPSG:4326',null,1000);
  76.   //print(img_mean.projection().nominalScale())
  77.   
  78.   img_mean = img_mean.expression('((Day + Night) / 2)',{
  79.       'Day': img_mean.select(['LST_Day_1km']),
  80.       'Night': img_mean.select(['LST_Night_1km']),
  81.        })
  82.   img = img.addBands(img_mean.rename('LST'))
  83.   
  84.   
  85.   // 干度函数:ndbsi = ( ibi + si ) / 2
  86.   var ibi = img.expression('(2 * SWIR1 / (SWIR1 + NIR) - (NIR / (NIR + RED) + GREEN / (GREEN + SWIR1))) / (2 * SWIR1 / (SWIR1 + NIR) + (NIR / (NIR + RED) + GREEN / (GREEN + SWIR1)))', {
  87.       'SWIR1': img.select('B6'),
  88.       'NIR': img.select('B5'),
  89.       'RED': img.select('B4'),
  90.       'GREEN': img.select('B3')
  91.     })
  92.   var si = img.expression('((SWIR1 + RED) - (NIR + BLUE)) / ((SWIR1 + RED) + (NIR + BLUE))', {
  93.       'SWIR1': img.select('B6'),
  94.       'NIR': img.select('B5'),
  95.       'RED': img.select('B4'),
  96.       'BLUE': img.select('B2')
  97.     })
  98.   var ndbsi = (ibi.add(si)).divide(2)
  99.   return img.addBands(ndbsi.rename('NDBSI'))
  100. })
  101. var bandNames = ['NDVI', "NDBSI", "WET", "LST"]
  102. L8imgCol = L8imgCol.select(bandNames)
  103. //定义归一化函数:归一化
  104. var img_normalize = function(img){
  105.       var minMax = img.reduceRegion({
  106.             reducer:ee.Reducer.minMax(),
  107.             geometry: roi,
  108.             scale: 1000,
  109.             maxPixels: 10e13,
  110.         })
  111.       var year = img.get('year')
  112.       var normalize  = ee.ImageCollection.fromImages(
  113.             img.bandNames().map(function(name){
  114.                   name = ee.String(name);
  115.                   var band = img.select(name);
  116.                   return band.unitScale(ee.Number(minMax.get(name.cat('_min'))), ee.Number(minMax.get(name.cat('_max'))));
  117.                     
  118.               })
  119.         ).toBands().rename(img.bandNames()).set('year', year);
  120.         return normalize;
  121. }
  122. var imgNorcol  = L8imgCol.map(img_normalize);
  123.   // 第四步:PCA融合,提取第一主成分
  124. var pca = function(img){
  125.       
  126.       var bandNames = img.bandNames();
  127.       var region = roi;
  128.       var year = img.get('year')
  129.       // Mean center the data to enable a faster covariance reducer
  130.       // and an SD stretch of the principal components.
  131.       var meanDict = img.reduceRegion({
  132.             reducer:  ee.Reducer.mean(),
  133.             geometry: region,
  134.             scale: 1000,
  135.             maxPixels: 10e13
  136.         });
  137.       var means = ee.Image.constant(meanDict.values(bandNames));
  138.       var centered = img.subtract(means).set('year', year);
  139.       
  140.       
  141.       // This helper function returns a list of new band names.
  142.       var getNewBandNames = function(prefix, bandNames){
  143.             var seq = ee.List.sequence(1, 4);
  144.             //var seq = ee.List.sequence(1, bandNames.length());
  145.             return seq.map(function(n){
  146.                   return ee.String(prefix).cat(ee.Number(n).int());
  147.               });      
  148.         };
  149.       
  150.       // This function accepts mean centered imagery, a scale and
  151.       // a region in which to perform the analysis.  It returns the
  152.       // Principal Components (PC) in the region as a new image.
  153.       var getPrincipalComponents = function(centered, scale, region){
  154.             var year = centered.get('year')
  155.             var arrays = centered.toArray();
  156.         
  157.             // Compute the covariance of the bands within the region.
  158.             var covar = arrays.reduceRegion({
  159.                   reducer: ee.Reducer.centeredCovariance(),
  160.                   geometry: region,
  161.                   scale: scale,
  162.                   bestEffort:true,
  163.                   maxPixels: 10e13
  164.               });
  165.             
  166.             // Get the 'array' covariance result and cast to an array.
  167.             // This represents the band-to-band covariance within the region.
  168.             var covarArray = ee.Array(covar.get('array'));
  169.             
  170.             // Perform an eigen analysis and slice apart the values and vectors.
  171.             var eigens = covarArray.eigen();
  172.         
  173.             // This is a P-length vector of Eigenvalues.
  174.             var eigenValues = eigens.slice(1, 0, 1);
  175.             // This is a PxP matrix with eigenvectors in rows.
  176.             var eigenVectors = eigens.slice(1, 1);
  177.         
  178.             // Convert the array image to 2D arrays for matrix computations.
  179.             var arrayImage = arrays.toArray(1)
  180.             // Left multiply the image array by the matrix of eigenvectors.
  181.             var principalComponents = ee.Image(eigenVectors).matrixMultiply(arrayImage);
  182.         
  183.             // Turn the square roots of the Eigenvalues into a P-band image.
  184.             var sdImage = ee.Image(eigenValues.sqrt())
  185.             .arrayProject([0]).arrayFlatten([getNewBandNames('SD',bandNames)]);
  186.         
  187.             // Turn the PCs into a P-band image, normalized by SD.
  188.             return principalComponents
  189.             // Throw out an an unneeded dimension, [[]] -> [].
  190.             .arrayProject([0])
  191.             // Make the one band array image a multi-band image, [] -> image.
  192.             .arrayFlatten([getNewBandNames('PC', bandNames)])
  193.             // Normalize the PCs by their SDs.
  194.             .divide(sdImage)
  195.             .set('year', year);
  196.         }
  197.         
  198.         // Get the PCs at the specified scale and in the specified region
  199.         img = getPrincipalComponents(centered, 1000, region);
  200.         return img;
  201.   };
  202.   
  203. var PCA_imgcol = imgNorcol.map(pca)
  204. Map.addLayer(PCA_imgcol.first(), {"bands":["PC1"]}, 'pc1')
  205. // 第五步:利用PC1,计算RSEI,并归一化
  206. var RSEI_imgcol = PCA_imgcol.map(function(img){
  207.         img = img.addBands(ee.Image(1).rename('constant'))
  208.         var rsei = img.expression('constant - pc1' , {
  209.              constant: img.select('constant'),
  210.              pc1: img.select('PC1')
  211.          })
  212.         rsei = img_normalize(rsei)
  213.         return img.addBands(rsei.rename('rsei'))
  214.     })
  215. print(RSEI_imgcol)
  216. var visParam = {
  217.     palette: '040274, 040281, 0502a3, 0502b8, 0502ce, 0502e6, 0602ff, 235cb1, 307ef3, 269db1, 30c8e2, 32d3ef, 3be285, 3ff38f, 86e26f, 3ae237, b5e22e, d6e21f, fff705, ffd611, ffb613, ff8b13, ff6e08, ff500d, ff0000, de0101, c21301, a71001, 911003'
  218. };
  219. Map.addLayer(RSEI_imgcol.first().select('rsei'), visParam, 'rsei')
复制代码

总结

提示:用完代码记得反馈。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

惊雷无声

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表