1. First prepare the "go to cloud" function
//landsat5、7 EVI指数计算
// SR数据去云
function rmCloud(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);
}
The above step is the simplest operation, which is basically used for processing landsat-sr series data.
2. Selection of data
//这是我自己的行政矢量边界,这回用的是山西省界
var hh= ee.FeatureCollection("users/bqt2000204051/shanxijie");
//这部分代码是进行数据的选择和EVI指数的遍历计算,我进行的是年统计
var image= ee.ImageCollection("LANDSAT/LT05/C01/T1_SR")
.filterBounds(hh)
.filterDate("1986-1-1","1987-1-1")
.map(rmCloud)
.map(function(image) {
var evi = image.expression(
'2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))', {
'NIR': image.select('B4'),
'RED': image.select('B3'),
'BLUE': image.select('B1')
});
return image.addBands(evi.rename('EVI')).clip(hh);
})
;
print("image",image);
Map.centerObject(hh, 3);
//在这里进行自己影像的选择,这里选择第一张影像,另外,裁剪自己的边界。
var scol_clip1 =image.first().clip(hh);
//其他指数可以参照一下函数
//NDVI
function NDVI(image) {
return image.addBands(
image.normalizedDifference(["B5", "B4"])
.rename("NDVI"));
}
//NDWI
function NDWI(image) {
return image.addBands(
image.normalizedDifference(["B3", "B5"])
.rename("NDWI"));
}
//NDBI
function NDBI(image) {
return image.addBands(
image.normalizedDifference(["B6", "B5"])
.rename("NDBI"));
}
I will not explain the calculation process of each index in the above part, because the index is the result of the band operation, and you can formulate it yourself.
3. Data normalization processing function
function normalization(image,region,scale){
var mean_std = image.reduceRegion({
reducer: ee.Reducer.mean()
.combine(ee.Reducer.stdDev(),null, true),
geometry: region,
scale: scale,
maxPixels: 10e9,
// tileScale: 16
});
// use unit scale to normalize the pixel values
var unitScale = ee.ImageCollection.fromImages(
image.bandNames().map(function(name){
name = ee.String(name);
var band = image.select(name);
var mean=ee.Number(mean_std.get(name.cat('_mean')));
var std=ee.Number(mean_std.get(name.cat('_stdDev')));
var max=mean.add(std.multiply(3));
var min=mean.subtract(std.multiply(3));
var band1=ee.Image(min).multiply(band.lt(min)).add(ee.Image(max).multiply(band.gt(max)))
.add(band.multiply(ee.Image(1).subtract(band.lt(min)).subtract(band.gt(max))));
var result_band=band1.subtract(min).divide(max.subtract(min));
return result_band;
})).toBands().rename(image.bandNames());
return unitScale;
}
This piece of code comes from the great god of Pyrs : (2 messages) Google Earth Engine Notes - Image Normalization and Outlier Removal_Pyrs' Blog-CSDN Blog The main idea is to calculate the mean, standard deviation and within 3 times the standard deviation error to remove outliers.
3. Data normalization processing function
//归一化前的结果
var before_chart=ui.Chart.image.histogram(scol_clip.select(["B4","EVI"]), hh, 1000);
print(before_chart);
//让影像进行归一化函数处理后的结果
var normal_image=normalization(scol_clip,hh,1000)
print(normal_image)
//归一化后的结果
var after_chart=ui.Chart.image.histogram(normal_image.select(["B4","EVI"]), hh, 1000)
print(after_chart)
There is a little problem here, that 1000 in =ui.Chart.image.histogram() represents the pixel value:
scale (Number, optional):
The pixel scale used when applying the histogram reducer, in meters.
When your operating range exceeds the maximum amount of calculation, choose to increase your own pixel value to solve this problem!
The above are the statistics before normalization. In order to clearly compare with the EVI band, I used a B4 band as a reference!
This is the normalized distribution of statistics!
4. Data download and export
Export.image.toDrive({
image:normal_image.select("EVI"),
description: '1986_shanxi_EVI_sr',
folder: 'hulunbeier',
scale: 1000,
region:hh
});
Map.addLayer(image, {},'EVI');