一、使用2018年landsat 8影像数据计算遥感生态指数RSEI
//could masking,GEE上有现有的去云函数
function maskL8sr(image) {
// Bit 0 - Fill
// Bit 1 - Dilated Cloud
// Bit 2 - Cirrus
// Bit 3 - Cloud
// Bit 4 - Cloud Shadow
var qaMask = image.select('QA_PIXEL').bitwiseAnd(parseInt('11111', 2)).eq(0);
var saturationMask = image.select('QA_RADSAT').eq(0);
// Apply the scaling factors to the appropriate bands.
var opticalBands = image.select('SR_B.').multiply(0.0000275).add(-0.2);
var thermalBands = image.select('ST_B.*').multiply(0.00341802).add(149.0);
// Replace the original bands with the scaled ones and apply the masks.
return image.addBands(opticalBands, null, true)
.addBands(thermalBands, null, true)
.updateMask(qaMask)
.updateMask(saturationMask);
}
var dataset1 = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
.filterDate('2018-x-x', '2018-x-x')
.filterBounds(roi)
.filter(ee.Filter.lte('CLOUD_COVER',5))
.sort('CLOUD_COVER')
.map(maskL8sr)
//.mean()
print(dataset1)
var image1=dataset1.mosaic().clip(roi)
print(image1,'maskL8sr')
//Map.addLayer(image1,{bands:["SR_B4","SR_B3","SR_B2"],gamma:1.3,max:108,min:15},'Image1');
//Map.addLayer(roi,{color:'yellow',fillColor: "00000000", width: 1},'roi');
var jrc = jrcwater18.select('b1')
var image = image1.updateMask(jrc.eq(1))
//Map.addLayer(image,{bands:["SR_B4","SR_B3","SR_B2"],gamma:1.3,max:108,min:15},'Image');
print(image,'water and cloud')
//计算SI
function SI_cal(img) {
var blue = img.select("SR_B2");
var red = img.select("SR_B4");
var nir = img.select("SR_B5");
var swir1 = img.select("SR_B6");
var swir2 = img.select("SR_B7");
var SI_temp =((swir1.add(red)).subtract(blue.add(nir)))
.divide((swir1.add(red)).add(blue.add(nir)))
//print(SI_temp);
return SI_temp;
}
//计算IBI
function IBI_cal(img) {
var green = img.select("SR_B3");
var red = img.select("SR_B4");
var nir = img.select("SR_B5");
var swir1 = img.select("SR_B6");
var IBI_temp =(((swir1.multiply(2.0)).divide(swir1.add(nir)))
.subtract((nir.divide(nir.add(red))).add(green.divide(green.add(swir1)))))
.divide((((swir1.multiply(2.0)).divide(swir1.add(nir)))
.add((nir.divide(nir.add(red))).add(green.divide(green.add(swir1))))))
//print(IBI_temp);
return IBI_temp;
}
//计算湿度指数Wet
function Wet_cal(img) {
var blue = img.select("SR_B2");
var green = img.select("SR_B3");
var red = img.select("SR_B4");
var nir = img.select("SR_B5");
var swir1 = img.select("SR_B6");
var swir2 = img.select("SR_B7");
var wet_temp =blue.multiply(0.1511)
.add(green.multiply(0.1973))
.add(red.multiply(0.3283))
.add(nir.multiply(0.3407))
.add(swir1.multiply(-0.7117))
.add(swir2.multiply(-0.4559))
//print(wet_temp);
return wet_temp;
}
//外部导入的NDVI重采样
var NDVI=ndvi.reproject('EPSG:4326',null,1000);
//normalization function
function img_normalize(img){
var minMax = img.reduceRegion({
reducer:ee.Reducer.minMax(),
geometry: roi,
scale: 1000,
maxPixels: 10e13,
})
var year = img.get('year')
var normalize = ee.ImageCollection.fromImages(
img.bandNames().map(function(name){
name = ee.String(name);
var band = img.select(name);
return band.unitScale(ee.Number(minMax.get(name.cat('_min'))), ee.Number(minMax.get(name.cat('_max'))));
})
).toBands().rename(img.bandNames());
return normalize;
}
//使用MODIS的地表温度数据
var dataset2 = ee.ImageCollection('MODIS/061/MOD11A2')
.filterDate('2018-x-x', '2018-x-x')
.mean()
//var dataset_no_water = dataset.updateMask(NDWI_mask).clip(table);
var dataset2_no_water = dataset2.updateMask(jrc.gte(0.1)).clip(roi);
//将地表温度单位转换为摄氏度
var lst = dataset2_no_water.expression(
'a*0.02-273.15',
{
a:dataset2_no_water.select('LST_Day_1km'),
});
var SI = SI_cal(image);
print(SI,'SI');
var IBI = IBI_cal(image);
var NDBSI =(SI.add(IBI)).divide(2.0);
var wet = Wet_cal(image);
var unit_ndvi = img_normalize(NDVI);
image=image.addBands(unit_ndvi.rename('NDVI').toFloat())
var unit_NDBSI = img_normalize(NDBSI);
image=image.addBands(unit_NDBSI.rename('NDBSI').toFloat())
var unit_Wet = img_normalize(wet);
image=image.addBands(unit_Wet.rename('Wet').toFloat())
var unit_lst = img_normalize(lst);
image=image.addBands(unit_lst.rename('LST').toFloat())
print(image)
var bands = ["Wet","NDVI","NDBSI","LST"]
var sentImage =image.select(bands)
var region = roi;
var image2= sentImage.select(bands);
var scale = 1000;
var bandNames = image2.bandNames();
// 图像波段重命名函数
var getNewBandNames = function(prefix) {
var seq = ee.List.sequence(1, bandNames.length());
return seq.map(function(b) {
return ee.String(prefix).cat(ee.Number(b).int());
});
};
//数据平均
var meanDict = image2.reduceRegion({
reducer: ee.Reducer.mean(),
geometry: region,
scale: scale,
maxPixels: 1e9
});
var means = ee.Image.constant(meanDict.values(bandNames));
var centered = image2.subtract(means);
//主成分分析函数,GEE上有
var getPrincipalComponents = function(centered, scale, region) {
// 图像转为一维数组
var arrays = centered.toArray();
// 计算协方差矩阵
var covar = arrays.reduceRegion({
reducer: ee.Reducer.centeredCovariance(),
geometry: region,
scale: scale,
maxPixels: 1e9
});
// 获取“数组”协方差结果并转换为数组。波段与波段之间的协方差
var covarArray = ee.Array(covar.get('array'));
// 执行特征分析,并分割值和向量。
var eigens = covarArray.eigen();
// 特征值的P向量长度
var eigenValues = eigens.slice(1, 0, 1);
//计算主成分载荷
var eigenValuesList = eigenValues.toList().flatten()
var total = eigenValuesList.reduce(ee.Reducer.sum())
var percentageVariance = eigenValuesList.map(function(item) {
return (ee.Number(item).divide(total)).multiply(100).format('%.2f')
})
print('特征值',eigenValues )
print("贡献率", percentageVariance)
// PxP矩阵,其特征向量为行。
var eigenVectors = eigens.slice(1, 1);
// 将图像转换为二维阵列
var arrayImage = arrays.toArray(1);
//使用特征向量矩阵左乘图像阵列
var principalComponents = ee.Image(eigenVectors).matrixMultiply(arrayImage);
// 将特征值的平方根转换为P波段图像。
var sdImage = ee.Image(eigenValues.sqrt())
.arrayProject([0]).arrayFlatten([getNewBandNames('sd')]);
//将PC转换为P波段图像,通过SD标准化。
principalComponents=principalComponents
// 抛出一个不需要的维度,[[]]->[]。
.arrayProject([0])
// 使单波段阵列映像成为多波段映像,[]->image。
.arrayFlatten([getNewBandNames('pc')])
// 通过SDs使PC正常化。
.divide(sdImage);
return principalComponents
};
//进行主成分分析,获得分析结果
var pcImage = getPrincipalComponents(centered, scale, region);
var rsei_un_unit = pcImage.expression(
'constant -pc1',
{
constant: 1,
pc1: pcImage.select('pc1')
});
print(rsei_un_unit,'rsei_un_unit')
var rsei = img_normalize(rsei_un_unit);
//Map.addLayer(rsei, {}, 'PCA');
//Map.addLayer(lst, visParams1, "LST");
//Map.addLayer(NDBSI, {}, "NDBSI");
//Map.addLayer(ndvi, visParams, "NDVI");
//Map.addLayer(wet, visParams2, "Wet");
//print(wet)
print(rsei);
Map.addLayer(rsei, {}, "rsei");
//导出结果
Export.image.toDrive({
image: rsei,//分类结果
description: 'rsei',//文件名
folder: 'test',
scale: 30,//分辨率
region: roi,//区域
maxPixels:34e10//此处值设置大一些,防止溢出
});
二、使用2009年landsat 5影像数据计算遥感生态指数RSEI
//could masking
function maskL5sr(image) {
// Bit 0 - Fill
// Bit 1 - Dilated Cloud
// Bit 2 - Unused
// Bit 3 - Cloud
// Bit 4 - Cloud Shadow
var qaMask = image.select('QA_PIXEL').bitwiseAnd(parseInt('11111', 2)).eq(0);
var saturationMask = image.select('QA_RADSAT').eq(0);
// Apply the scaling factors to the appropriate bands.
var opticalBands = image.select('SR_B.').multiply(0.0000275).add(-0.2);
var thermalBand = image.select('ST_B6').multiply(0.00341802).add(149.0);
// Replace the original bands with the scaled ones and apply the masks.
return image.addBands(opticalBands, null, true)
.addBands(thermalBand, null, true)
.updateMask(qaMask)
.updateMask(saturationMask);
}
var dataset1 = ee.ImageCollection('LANDSAT/LT05/C02/T1_L2')
.filterDate('2009-x-x', '2009-x-x')
.filterBounds(roi)
.filter(ee.Filter.lte('CLOUD_COVER',5))
.sort('CLOUD_COVER')
.map(maskL5sr)
//.mean()
print(dataset1)
var image1=dataset1.mosaic().clip(roi)
print(image1,'maskL5sr')
//Map.addLayer(image1,{bands:["SR_B3","SR_B2","SR_B1"],gamma:1.3,max:108,min:15},'image1');
var jrc = jrcwater09.select('b1')
var image = image1.updateMask(jrc.eq(1))
Map.addLayer(image,{bands:["SR_B3","SR_B2","SR_B1"],gamma:1.3,max:108,min:15},'Imagewc');
print(image,'water and cloud')
function SI_cal(img) {
var blue = img.select("SR_B1");
var red = img.select("SR_B3");
var nir = img.select("SR_B4");
var swir1 = img.select("SR_B5");
var swir2 = img.select("SR_B7");
var SI_temp =((swir1.add(red)).subtract(blue.add(nir)))
.divide((swir1.add(red)).add(blue.add(nir)))
//print(SI_temp);
return SI_temp;
}
function IBI_cal(img) {
var green = img.select("SR_B2");
var red = img.select("SR_B3");
var nir = img.select("SR_B4");
var swir1 = img.select("SR_B5");
var IBI_temp =(((swir1.multiply(2.0)).divide(swir1.add(nir)))
.subtract((nir.divide(nir.add(red))).add(green.divide(green.add(swir1)))))
.divide((((swir1.multiply(2.0)).divide(swir1.add(nir)))
.add((nir.divide(nir.add(red))).add(green.divide(green.add(swir1))))))
//print(IBI_temp);
return IBI_temp;
}
function Wet_cal(img) {
var blue = img.select("SR_B1");
var green = img.select("SR_B2");
var red = img.select("SR_B3");
var nir = img.select("SR_B4");
var swir1 = img.select("SR_B5");
var swir2 = img.select("SR_B7");
var wet_temp =blue.multiply(0.0315)
.add(green.multiply(0.2021))
.add(red.multiply(0.3102))
.add(nir.multiply(0.1594))
.add(swir1.multiply(-0.6806))
.add(swir2.multiply(-0.6109))
//print(wet_temp);
return wet_temp;
}
var NDVI=ndvi2009.reproject('EPSG:4326',null,1000);
//var img_mean=lst.mean().reproject('EPSG:4326',null,1000);
//normalization function
function img_normalize(img){
var minMax = img.reduceRegion({
reducer:ee.Reducer.minMax(),
geometry: roi,
scale: 1000,
maxPixels: 10e13,
})
var year = img.get('year')
var normalize = ee.ImageCollection.fromImages(
img.bandNames().map(function(name){
name = ee.String(name);
var band = img.select(name);
return band.unitScale(ee.Number(minMax.get(name.cat('_min'))), ee.Number(minMax.get(name.cat('_max'))));
})
).toBands().rename(img.bandNames());
return normalize;
}
var dataset2 = ee.ImageCollection('MODIS/061/MOD11A2')
.filterDate('2009-X-X', '2009-X-X')
.mean()
//var dataset_no_water = dataset.updateMask(NDWI_mask).clip(table);
var dataset2_no_water = dataset2.updateMask(jrc.gte(0.1)).clip(roi);
var lst = dataset2_no_water.expression(
'a*0.02-273.15',
{
a:dataset2_no_water.select('LST_Day_1km'),
});
var SI = SI_cal(image);
print(SI,'SI');
var IBI = IBI_cal(image);
var NDBSI =(SI.add(IBI)).divide(2.0);
var wet = Wet_cal(image);
var unit_ndvi = img_normalize(NDVI);
image=image.addBands(unit_ndvi.rename('NDVI').toFloat())
var unit_NDBSI = img_normalize(NDBSI);
image=image.addBands(unit_NDBSI.rename('NDBSI').toFloat())
var unit_Wet = img_normalize(wet);
image=image.addBands(unit_Wet.rename('Wet').toFloat())
var unit_lst = img_normalize(lst);
image=image.addBands(unit_lst.rename('LST').toFloat())
print(image)
var bands = ["Wet","NDVI","NDBSI","LST"]
var sentImage =image.select(bands)
var region = roi;
var image2= sentImage.select(bands);
var scale = 1000;
var bandNames = image2.bandNames();
// 图像波段重命名函数
var getNewBandNames = function(prefix) {
var seq = ee.List.sequence(1, bandNames.length());
return seq.map(function(b) {
return ee.String(prefix).cat(ee.Number(b).int());
});
};
//数据平均
var meanDict = image2.reduceRegion({
reducer: ee.Reducer.mean(),
geometry: region,
scale: scale,
maxPixels: 1e9
});
var means = ee.Image.constant(meanDict.values(bandNames));
var centered = image2.subtract(means);
//主成分分析函数
var getPrincipalComponents = function(centered, scale, region) {
// 图像转为一维数组
var arrays = centered.toArray();
// 计算协方差矩阵
var covar = arrays.reduceRegion({
reducer: ee.Reducer.centeredCovariance(),
geometry: region,
scale: scale,
maxPixels: 1e9
});
// 获取“数组”协方差结果并转换为数组。波段与波段之间的协方差
var covarArray = ee.Array(covar.get('array'));
// 执行特征分析,并分割值和向量。
var eigens = covarArray.eigen();
// 特征值的P向量长度
var eigenValues = eigens.slice(1, 0, 1);
//计算主成分载荷
var eigenValuesList = eigenValues.toList().flatten()
var total = eigenValuesList.reduce(ee.Reducer.sum())
var percentageVariance = eigenValuesList.map(function(item) {
return (ee.Number(item).divide(total)).multiply(100).format('%.2f')
})
print('特征值',eigenValues )
print("贡献率", percentageVariance)
// PxP矩阵,其特征向量为行。
var eigenVectors = eigens.slice(1, 1);
// 将图像转换为二维阵列
var arrayImage = arrays.toArray(1);
//使用特征向量矩阵左乘图像阵列
var principalComponents = ee.Image(eigenVectors).matrixMultiply(arrayImage);
// 将特征值的平方根转换为P波段图像。
var sdImage = ee.Image(eigenValues.sqrt())
.arrayProject([0]).arrayFlatten([getNewBandNames('sd')]);
//将PC转换为P波段图像,通过SD标准化。
principalComponents=principalComponents
// 抛出一个不需要的维度,[[]]->[]。
.arrayProject([0])
// 使单波段阵列映像成为多波段映像,[]->image。
.arrayFlatten([getNewBandNames('pc')])
// 通过SDs使PC正常化。
.divide(sdImage);
return principalComponents
};
//进行主成分分析,获得分析结果
var pcImage = getPrincipalComponents(centered, scale, region);
var rsei_un_unit = pcImage.expression(
'constant - pc1*0.605-pc2*0.395' ,
{
constant: 1,
pc1:pcImage.select('pc1'),
});
print(rsei_un_unit,'rsei_un_unit')
var rsei = img_normalize(rsei_un_unit);
//Map.addLayer(rsei, {}, 'PCA');
//Map.addLayer(lst, visParams1, "LST");
//Map.addLayer(NDBSI, {}, "NDBSI");
//Map.addLayer(ndvi, visParams, "NDVI");
//Map.addLayer(wet, visParams2, "Wet");
//print(wet)
print(rsei);
Map.addLayer(rsei, {}, "rsei");
Export.image.toDrive({
image: rsei,//分类结果
description: 'rsei',//文件名
folder: 'test',
scale: 30,//分辨率
region: roi,//区域
maxPixels:34e10//此处值设置大一些,防止溢出
});