GEE之基于Landsat8的交互式点击生成NDVI时间序列

 本文为两年内的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);
});

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值