在 Google Earth Engine (GEE) 中创建包含每年 Landsat 图像数量的条形图
Create bar chart with number of Landsat images per year in Google Earth Engine (GEE)
我一直在尝试根据包含每年为我的兴趣点聚合的 Landsat 图像数量的列表创建条形图。
我试过从数组创建条形图,我也试过从数据 table。但我错过了诀窍。你能帮我解决这个问题吗?
这是我的代码:
var locat = ee.Geometry.Point(-40.967627131182304, -21.33089753065459); //point of interest
//Select the period
var start='1985-01-1';
var end='2020-12-31';
// Filter Landsat collection for the specified area
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']);
//Merge all the collections
var collection=collection8.merge(collection7).merge(collection5).merge(collection4)
.sort('CLOUD_COVER', true);
//Get the imagery list
var imagery_list =collection.toList(collection.size());
// creating sequence of years
var year_num = ee.List.sequence(1985, 2020, 1);
// mapping sequence of years to string format and slicing only the round numbers of an year
var years = year_num.map(function (years) { return ee.String(years).slice(0,4)});
//Getting the number of Landsat per year
// mapping filter over the list of years (string format) to get the number of elements in the imagery_list that match string year
// mapping filter the over the list of years (string format) to get the number of elements in the imagery_list that match string year
// it returns list with the the number of images per year
// Here I am only interested in the length of the sub lists (the number of images), therefore I used .length() ate the end
var Ltmap = years.map(function(year){
var LtYearLs = imagery_list.filter(ee.Filter.stringContains('SENSING_TIME', year)).length();
return LtYearLs;
});
// create a dictionary with number of images (value) per year (key)
var Ltdict = ee.Dictionary.fromLists(years, Ltmap);
print(Ltdict, 'Ltdict' );
// create a data table or array to produce the bar charts
// here I where I assume is my problem
var dataTable = [
[
{label: 'Image Year', role: 'domain', type: 'string'},
{label: 'Number of images', role: 'data', type: 'number'},
],
[ Ltdict.keys(), Ltdict.values()]
];
// Define the chart and print it to the console.
var chart = ui.Chart(dataTable).setChartType('ColumnChart').setOptions({
title: 'Landsat images per Year',
legend: {position: 'none'},
hAxis: {title: 'Images', titleTextStyle: {italic: false, bold: true}},
vAxis: {title: 'Year', titleTextStyle: {italic: false, bold: true}},
colors: ['1d6b99']
});
print(chart);
我希望得到的是这样的条形图:https://i.stack.imgur.com/MjREF.png
非常感谢您的帮助。
试试这个:
var locat = ee.Geometry.Point(-40.967627131182304, -21.33089753065459); //point of interest
//Select the period
var start='1985-01-1';
var end='2020-12-31';
// Filter Landsat collection for the specified area
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']);
//Merge all the collections
var collection=collection8.merge(collection7).merge(collection5).merge(collection4)
.sort('CLOUD_COVER', true);
var years = ee.List.sequence(ee.Date(start).get('year'), ee.Date(end).get('year'));
/// This function calculates the amount of images per year
var annualFunction = function(collection) {
return ee.ImageCollection.fromImages(
years.map(function (y) {
var w = collection
.filter(ee.Filter.bounds(locat))
.filter(ee.Filter.calendarRange(y, y, 'year'))
.count();
return w.set('year', y)
.set('system:time_start', ee.Date.fromYMD(y, 1, 1));
}).flatten())};
var collectionAnnual = annualFunction(collection)
var chart = ui.Chart.image.series({
imageCollection: collectionAnnual,
region: locat,
reducer: ee.Reducer.sum(),
scale: 30, // resolution of Landsat
})
print(chart)
我一直在尝试根据包含每年为我的兴趣点聚合的 Landsat 图像数量的列表创建条形图。
我试过从数组创建条形图,我也试过从数据 table。但我错过了诀窍。你能帮我解决这个问题吗? 这是我的代码:
var locat = ee.Geometry.Point(-40.967627131182304, -21.33089753065459); //point of interest
//Select the period
var start='1985-01-1';
var end='2020-12-31';
// Filter Landsat collection for the specified area
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']);
//Merge all the collections
var collection=collection8.merge(collection7).merge(collection5).merge(collection4)
.sort('CLOUD_COVER', true);
//Get the imagery list
var imagery_list =collection.toList(collection.size());
// creating sequence of years
var year_num = ee.List.sequence(1985, 2020, 1);
// mapping sequence of years to string format and slicing only the round numbers of an year
var years = year_num.map(function (years) { return ee.String(years).slice(0,4)});
//Getting the number of Landsat per year
// mapping filter over the list of years (string format) to get the number of elements in the imagery_list that match string year
// mapping filter the over the list of years (string format) to get the number of elements in the imagery_list that match string year
// it returns list with the the number of images per year
// Here I am only interested in the length of the sub lists (the number of images), therefore I used .length() ate the end
var Ltmap = years.map(function(year){
var LtYearLs = imagery_list.filter(ee.Filter.stringContains('SENSING_TIME', year)).length();
return LtYearLs;
});
// create a dictionary with number of images (value) per year (key)
var Ltdict = ee.Dictionary.fromLists(years, Ltmap);
print(Ltdict, 'Ltdict' );
// create a data table or array to produce the bar charts
// here I where I assume is my problem
var dataTable = [
[
{label: 'Image Year', role: 'domain', type: 'string'},
{label: 'Number of images', role: 'data', type: 'number'},
],
[ Ltdict.keys(), Ltdict.values()]
];
// Define the chart and print it to the console.
var chart = ui.Chart(dataTable).setChartType('ColumnChart').setOptions({
title: 'Landsat images per Year',
legend: {position: 'none'},
hAxis: {title: 'Images', titleTextStyle: {italic: false, bold: true}},
vAxis: {title: 'Year', titleTextStyle: {italic: false, bold: true}},
colors: ['1d6b99']
});
print(chart);
我希望得到的是这样的条形图:https://i.stack.imgur.com/MjREF.png
非常感谢您的帮助。
试试这个:
var locat = ee.Geometry.Point(-40.967627131182304, -21.33089753065459); //point of interest
//Select the period
var start='1985-01-1';
var end='2020-12-31';
// Filter Landsat collection for the specified area
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']);
//Merge all the collections
var collection=collection8.merge(collection7).merge(collection5).merge(collection4)
.sort('CLOUD_COVER', true);
var years = ee.List.sequence(ee.Date(start).get('year'), ee.Date(end).get('year'));
/// This function calculates the amount of images per year
var annualFunction = function(collection) {
return ee.ImageCollection.fromImages(
years.map(function (y) {
var w = collection
.filter(ee.Filter.bounds(locat))
.filter(ee.Filter.calendarRange(y, y, 'year'))
.count();
return w.set('year', y)
.set('system:time_start', ee.Date.fromYMD(y, 1, 1));
}).flatten())};
var collectionAnnual = annualFunction(collection)
var chart = ui.Chart.image.series({
imageCollection: collectionAnnual,
region: locat,
reducer: ee.Reducer.sum(),
scale: 30, // resolution of Landsat
})
print(chart)