代码如下(以鹿邑县为例)
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'}}));





代码如下(以鹿邑县为例):
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');

