You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Google Earth Engine(GEE)中生成Landsat逐年影像数量柱状图求助

问题解决:GEE生成逐年Landsat影像数量柱状图

原代码核心问题

  • 手动构造的dataTable格式不符合Google Charts输入要求,正确的二维表结构应该是每一行对应一组[年份, 影像数量]的配对,你直接把年份列表和数值列表作为单独行传入,无法被图表组件识别。
  • 坐标轴配置颠倒:横轴应该对应年份,纵轴对应影像数量,原代码中的hAxis和vAxis属性设置完全相反。
  • 操作效率问题:将影像集合转为全量List再按年过滤的写法在影像数量大时极易触发GEE运算超时,更合理的方案是直接在影像集合层面添加年份属性后做聚合统计。

修正后可运行代码

var locat = ee.Geometry.Point(-40.967627131182304, -21.33089753065459); //兴趣点坐标

// 定义时间范围
var start='1985-01-01';
var end='2020-12-31';

// 过滤各系列Landsat SR影像并统一波段名
var collection8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
  .filterDate(start, end)
  .filterBounds(locat)
  .select(['B2','B3','B4','B5','B6','B7'],
  ['blue','green','red','NIR','SWIR1','SWIR2']);
var collection7 = ee.ImageCollection('LANDSAT/LE07/C01/T1_SR')
  .filterDate(start, end)
  .filterBounds(locat)
  .select(['B1','B2','B3','B4','B5','B7'],
  ['blue','green','red','NIR','SWIR1','SWIR2']);
var collection5 = ee.ImageCollection('LANDSAT/LT05/C01/T1_SR')
  .filterDate(start, end)
  .filterBounds(locat)
  .select(['B1','B2','B3','B4','B5','B7'],
  ['blue','green','red','NIR','SWIR1','SWIR2']);
var collection4 = ee.ImageCollection('LANDSAT/LT04/C01/T1_SR')
  .filterDate(start, end)
  .filterBounds(locat)
  .select(['B1','B2','B3','B4','B5','B7'],
  ['blue','green','red','NIR','SWIR1','SWIR2']);

// 合并所有影像集合
var collection=collection8.merge(collection7).merge(collection5).merge(collection4);

// 给每景影像添加年份属性,用于后续聚合
collection = collection.map(function(img){
  var year = img.date().get('year');
  return img.set('year', year);
});

// 直接统计每年的影像数量,无需转List操作
var yearCount = collection.aggregate_histogram('year');

// 直接基于统计结果生成柱状图,无需手动构造数据表
var chart = ui.Chart.array.values({
  array: yearCount.values(),
  axis: 0,
  xLabels: yearCount.keys()
}).setChartType('ColumnChart').setOptions({
  title: 'Landsat images per Year',
  legend: {position: 'none'},
  hAxis: {title: '年份', titleTextStyle: {italic: false, bold: true}},
  vAxis: {title: '影像数量', titleTextStyle: {italic: false, bold: true}},
  colors: ['#1d6b99']
});
print(chart);

关键修改说明

  • 新增了为每景影像添加year属性的步骤,直接用GEE内置的aggregate_histogram方法完成按年计数,运算效率提升明显,避免超时风险。
  • 取消了手动构造数据表的操作,直接调用ui.Chart.array.values接口传入统计结果和年份标签,自动生成符合要求的图表输入结构。
  • 修正了坐标轴的配置和标签,和预期输出的柱状图逻辑对齐。

内容的提问来源于stack exchange,提问作者pbastosd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 06:15:02