本文为两年内的ndvi时空变化曲线,其中chongming为所使用的研究区域,需自行加载或按需要更换其他区域
function NDVI_V3(img) {
var ndvi = img.normalizedDifference(["B5","B4"]);
return ndvi;
}
var image=ee.Image('LC08_118038_20210208')
var bandNames = image.bandNames();
// Check if "pixel_qa" band exists
if (bandNames.contains("pixel_qa")) {
print("Pixel QA band exists");
} else {
print("Pixel QA band does not exist");
}
//筛选Landsat8影像云覆盖最少影像
var cm = ee.Geometry.Point(121.5,31.65);
Map.centerObject(cm,12);
var visParam_NDVI = {
min: -0.1,
max: 0.9,
palette: 'FFFFFF, CE7E45, DF923D, F1B555, FCD163, 99B718, 74A901, 66A000, 529400,' +
'3E8601, 207401, 056201, 004C00, 023B01, 012E01, 011D01, 011301'
};
//--------------------------------Landsat8获取 NDVI
//Landsat8去云函数
function maskL8sr(image) {
// Bits 3 and 5 are cloud shadow and cloud, respectively.
var cloudShadowBitMask = (1 << 3);
var cloudsBitMask = (1 << 5);
// Get the pixel QA band.
var qa = image.select('pixel_qa');
// Both flags should be set to zero, indicating clear conditions.
var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0)
.and(qa.bitwiseAnd(cloudsBitMask).eq(0));
return image.updateMask(mask);
}
function createNDVI_L8(image){
var ndvi = image.normalizedDifference(["B5","B4"]).rename('NDVI');
return image.addBands(ndvi);
}
var L8_COL = ee.ImageCollection("LANDSAT/LC08/C01/T1_SR")
.filterDate("2019-01-01", "2021-01-01")
.filterBounds(chongming)
.map(maskL8sr)
.map(createNDVI_L8)
.select('NDVI');
print(L8_COL)
Map.addLayer(L8_COL.mean().clip(chongming), visParam_NDVI, 'L8_col');
//趋势线代码
var L8_chart = ui.Chart.image.series({
imageCollection: L8_COL.select('NDVI'),
region: chongming,
reducer: ee.Reducer.mean(),
scale: 500
}).setOptions({
interpolateNulls: true,
lineWidth: 2,
title: 'Landsat8 NDVI Time Seires',
vAxis: {title: 'NDVI'},
hAxis: {title: 'Date'},
trendlines: { 0: {title: 'NDVI_trend',type:'linear', showR2: true, color:'red', visibleInLegend: true}}
});
print(L8_chart);
// 点击事件 get coordinate
Map.onClick(function(coords) {
var lon = coords.lon;
var lat = coords.lat;
print("Clicked point coordinates: ", lon, lat);
// 点要素
var park = ee.Feature(
ee.Geometry.Point(lon,lat), {'label': 'park'});
//定义图表及样式
var ndviTimeSeries = ui.Chart.image.seriesByRegion({
imageCollection: L8_COL.select('NDVI'),
regions: park,
reducer: ee.Reducer.mean(),
// band: 'NDVI',
scale: 30,
xProperty: 'system:time_start',
seriesProperty: 'label'
});
ndviTimeSeries.setChartType('ScatterChart');
ndviTimeSeries.setOptions({
title: 'landsat8数据作物时间序列NDVI变化',
vAxis: {
title: 'NDVI'
},
lineWidth: 1,
pointSize: 4,
// series: {
// 0: {color: COLOR.GAOLIANG},
// 1: {color: COLOR.YUMI},
// 2: {color: COLOR.DADOU}
// }
});
print(ndviTimeSeries);
var yAxisOptions = {
title: 'NDVI',
minValue: -0.5,
maxValue: 1,
ticks: 10,
};
var L8_chart2 = ui.Chart.image.series({
imageCollection: L8_COL.select('NDVI'),
region: park,
reducer: ee.Reducer.mean(),
scale: 30
}).setOptions({
interpolateNulls: true,
lineWidth: 2,
title: 'Landsat8 NDVI Time Seires',
vAxis: yAxisOptions ,
hAxis: {title: 'Date'},
// trendlines: { 0: {title: 'NDVI_trend',type:'linear', showR2: true, color:'red', visibleInLegend: true}}
});
print(L8_chart2);
});