如何从 Google Earth Engine 导出具有用户定义日期格式的属性数据和关联日期的 csv 文件?
How to export a csv file from Google Earth Engine with attribute data and associated dates in a user defined date format?
美好的一天
我正在尝试使用 Landsat 7 和 8 数据在 Google Earth Engine 中生成增强型植被指数 (EVI)。我已经编译了下面的代码来过滤特定时间段和感兴趣区域的图像集合,并屏蔽掉图像中的大部分云层。然后我开始计算 EVI 并将这些值作为属性添加到图像集合中,我可以 select 进行进一步处理。我已分别为 Landsat 7 和 Landsat 8 进行了此过程。但是,由于我对从这两个数据集生成的 EVI 感兴趣,我已将它们合并为一个图像集。
使用此图像集,我想计算特定感兴趣区域的平均 EVI 值,然后将这些值连同相关日期(格式为 dd-mm-yyyy)导出为“.csv “ 文件。我知道这可以通过单击生成的图表并下载相关的“.csv”文件或使用“Export.table.toDrive”功能导出数据来实现。但是,这些选项中的 none 提供结构中的输出,如下面的 table 所示。
此处提供了代码示例。对此的任何帮助将不胜感激。亲切的问候。
/// Add region of interest
var ROI = ee.FeatureCollection("users/shaedengokool/Eben_Sluis_15YO")
Map.addLayer(ROI, {}, 'ROI')
Map.centerObject(ROI, 10)
//1. Import the Landsat 8 TOA image collection.
var l8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
.filterDate('2002-01-01','2020-01-01')
.filterBounds(ROI)
.map(function(image){return image.clip(ROI)})
var L8cloudlessEVI = l8.map(function(image) {
// Get a cloud score in [0, 100].
var cloud = ee.Algorithms.Landsat.simpleCloudScore(image).select('cloud');
// Create a mask of cloudy pixels from an arbitrary threshold.
var mask = cloud.lte(10);
// Compute EVI.
var evi = image.expression(
'2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))', {
'NIR': image.select('B5'),
'RED': image.select('B4'),
'BLUE': image.select('B2')
}).rename('EVI');
// Return the masked image with an EVI band.
return image.addBands(evi).updateMask(mask);
});
var L8EVI_collection = L8cloudlessEVI
//2. Import the Landsat 7 TOA image collection.
var l7 = ee.ImageCollection('LANDSAT/LE07/C01/T1_TOA')
.filterDate('2002-01-01','2020-01-01')
.filterBounds(ROI)
.map(function(image){return image.clip(ROI)})
var L7cloudlessEVI = l7.map(function(image) {
// Get a cloud score in [0, 100].
var cloud = ee.Algorithms.Landsat.simpleCloudScore(image).select('cloud');
// Create a mask of cloudy pixels from an arbitrary threshold.
var mask = cloud.lte(10);
// Compute EVI.
var evi = image.expression(
'2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))', {
'NIR': image.select('B4'),
'RED': image.select('B3'),
'BLUE': image.select('B1')
}).rename('EVI');
// Return the masked image with an EVI band.
return image.addBands(evi).updateMask(mask);
});
var L7EVI_collection = L7cloudlessEVI
var merged = ee.ImageCollection(L7EVI_collection.merge(L8EVI_collection));
print(merged, 'Merged')
var chart = ui.Chart.image.series({
imageCollection: merged.select('EVI'),
region: ROI,
reducer: ee.Reducer.mean(),
scale: 30,
})
print(chart, "EVI")
// get the mean value for the region from each image
var meanEVI = merged.map(function(image){
var date = image.get('system:time_start');
var mean = image.reduceRegion({
reducer: ee.Reducer.mean(),
geometry: ROI,
scale: 30
});
// and return a feature with 'null' geometry with properties (dictionary)
return ee.Feature(null, {'mean': mean.get('EVI'),
'date': date})
});
// Export a .csv table of date, mean NDVI for watershed
Export.table.toDrive({
collection: meanEVI,
description: 'EVI_Timeseries',
folder: 'Genus_Exchange_GEE_Data',
fileFormat: 'CSV',
})
;
在下面的代码中,我将日期添加为图像 属性 并将其导出为 CSV。我还重新安排了一些代码以减少计算时间。
代码如下:
var roi = ee.FeatureCollection("users/shaedengokool/Eben_Sluis_15YO")
// Define time of interest
var startdate = '2019-06-01' // insert
var enddate = '2020-06-01' // insert
var years = ee.List.sequence(ee.Date(startdate).get('year'), ee.Date(enddate).get('year'));
/// EVI calculations from Landsat
// This function masks clouds in Landsat 8 imagery.
function maskL8(im) {
var qa = im.select('BQA');
var mask = qa.eq(2720);
return im.updateMask(mask).copyProperties(im);
}
// This function masks clouds in Landsat 7 imagery.
function maskL7(im) {
var qa = im.select('BQA');
var mask = qa.eq(672);
return im.updateMask(mask).copyProperties(im);
}
// see: https://landsat.usgs.gov/sites/default/files/documents/landsat_QA_tools_userguide.pdf
var ls8toa = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
.filterBounds(roi)
.filterDate(startdate, enddate)
.map(function(im) {return maskL8(im)})
var ls7toa = ee.ImageCollection('LANDSAT/LE07/C01/T1_TOA').filterBounds(roi).filterDate(startdate, enddate)
.map(function(im) {return maskL7(im)})
var ls7_evi = ls7toa.map(function(image) {
var evi = image.expression(
'2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))', {
'NIR': image.select('B4'),
'RED': image.select('B3'),
'BLUE': image.select('B1')
}).rename('EVI')
return image.addBands(evi)
})
var ls8_evi = ls8toa.map(function(image) {
var evi = image.expression(
'2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))', {
'NIR': image.select('B5'),
'RED': image.select('B4'),
'BLUE': image.select('B2')
}).rename('EVI')
return image.addBands(evi)
})
var landsat = ee.ImageCollection(ls7_evi.merge(ls8_evi));
var EVI = landsat.select(['EVI'])
print(EVI, 'EVI')
//create function to calculate mean values for each polygon
var pointsmean = function(image) {
var means = image.reduceRegions({
collection: roi, // used to be roi.select(['Id'])
reducer: ee.Reducer.mean(),
scale: 30
})
// assign time for every feature
means = means.map(function(f) { return f.set({date: image.date().format("YYYY-MM-dd")}) })
return means.copyProperties(image)
};
var finalEVI = EVI.map(pointsmean).flatten()
.sort('date', false)
.select(['date', 'mean'])
print(finalEVI.limit(100), 'final EVI')
Export.table.toDrive({
collection: finalEVI,
description: 'EVI_'+startdate+'TO'+enddate,
fileFormat: 'CSV'
});
美好的一天
我正在尝试使用 Landsat 7 和 8 数据在 Google Earth Engine 中生成增强型植被指数 (EVI)。我已经编译了下面的代码来过滤特定时间段和感兴趣区域的图像集合,并屏蔽掉图像中的大部分云层。然后我开始计算 EVI 并将这些值作为属性添加到图像集合中,我可以 select 进行进一步处理。我已分别为 Landsat 7 和 Landsat 8 进行了此过程。但是,由于我对从这两个数据集生成的 EVI 感兴趣,我已将它们合并为一个图像集。
使用此图像集,我想计算特定感兴趣区域的平均 EVI 值,然后将这些值连同相关日期(格式为 dd-mm-yyyy)导出为“.csv “ 文件。我知道这可以通过单击生成的图表并下载相关的“.csv”文件或使用“Export.table.toDrive”功能导出数据来实现。但是,这些选项中的 none 提供结构中的输出,如下面的 table 所示。
此处提供了代码示例。对此的任何帮助将不胜感激。亲切的问候。
/// Add region of interest
var ROI = ee.FeatureCollection("users/shaedengokool/Eben_Sluis_15YO")
Map.addLayer(ROI, {}, 'ROI')
Map.centerObject(ROI, 10)
//1. Import the Landsat 8 TOA image collection.
var l8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
.filterDate('2002-01-01','2020-01-01')
.filterBounds(ROI)
.map(function(image){return image.clip(ROI)})
var L8cloudlessEVI = l8.map(function(image) {
// Get a cloud score in [0, 100].
var cloud = ee.Algorithms.Landsat.simpleCloudScore(image).select('cloud');
// Create a mask of cloudy pixels from an arbitrary threshold.
var mask = cloud.lte(10);
// Compute EVI.
var evi = image.expression(
'2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))', {
'NIR': image.select('B5'),
'RED': image.select('B4'),
'BLUE': image.select('B2')
}).rename('EVI');
// Return the masked image with an EVI band.
return image.addBands(evi).updateMask(mask);
});
var L8EVI_collection = L8cloudlessEVI
//2. Import the Landsat 7 TOA image collection.
var l7 = ee.ImageCollection('LANDSAT/LE07/C01/T1_TOA')
.filterDate('2002-01-01','2020-01-01')
.filterBounds(ROI)
.map(function(image){return image.clip(ROI)})
var L7cloudlessEVI = l7.map(function(image) {
// Get a cloud score in [0, 100].
var cloud = ee.Algorithms.Landsat.simpleCloudScore(image).select('cloud');
// Create a mask of cloudy pixels from an arbitrary threshold.
var mask = cloud.lte(10);
// Compute EVI.
var evi = image.expression(
'2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))', {
'NIR': image.select('B4'),
'RED': image.select('B3'),
'BLUE': image.select('B1')
}).rename('EVI');
// Return the masked image with an EVI band.
return image.addBands(evi).updateMask(mask);
});
var L7EVI_collection = L7cloudlessEVI
var merged = ee.ImageCollection(L7EVI_collection.merge(L8EVI_collection));
print(merged, 'Merged')
var chart = ui.Chart.image.series({
imageCollection: merged.select('EVI'),
region: ROI,
reducer: ee.Reducer.mean(),
scale: 30,
})
print(chart, "EVI")
// get the mean value for the region from each image
var meanEVI = merged.map(function(image){
var date = image.get('system:time_start');
var mean = image.reduceRegion({
reducer: ee.Reducer.mean(),
geometry: ROI,
scale: 30
});
// and return a feature with 'null' geometry with properties (dictionary)
return ee.Feature(null, {'mean': mean.get('EVI'),
'date': date})
});
// Export a .csv table of date, mean NDVI for watershed
Export.table.toDrive({
collection: meanEVI,
description: 'EVI_Timeseries',
folder: 'Genus_Exchange_GEE_Data',
fileFormat: 'CSV',
})
;
在下面的代码中,我将日期添加为图像 属性 并将其导出为 CSV。我还重新安排了一些代码以减少计算时间。
代码如下:
var roi = ee.FeatureCollection("users/shaedengokool/Eben_Sluis_15YO")
// Define time of interest
var startdate = '2019-06-01' // insert
var enddate = '2020-06-01' // insert
var years = ee.List.sequence(ee.Date(startdate).get('year'), ee.Date(enddate).get('year'));
/// EVI calculations from Landsat
// This function masks clouds in Landsat 8 imagery.
function maskL8(im) {
var qa = im.select('BQA');
var mask = qa.eq(2720);
return im.updateMask(mask).copyProperties(im);
}
// This function masks clouds in Landsat 7 imagery.
function maskL7(im) {
var qa = im.select('BQA');
var mask = qa.eq(672);
return im.updateMask(mask).copyProperties(im);
}
// see: https://landsat.usgs.gov/sites/default/files/documents/landsat_QA_tools_userguide.pdf
var ls8toa = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
.filterBounds(roi)
.filterDate(startdate, enddate)
.map(function(im) {return maskL8(im)})
var ls7toa = ee.ImageCollection('LANDSAT/LE07/C01/T1_TOA').filterBounds(roi).filterDate(startdate, enddate)
.map(function(im) {return maskL7(im)})
var ls7_evi = ls7toa.map(function(image) {
var evi = image.expression(
'2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))', {
'NIR': image.select('B4'),
'RED': image.select('B3'),
'BLUE': image.select('B1')
}).rename('EVI')
return image.addBands(evi)
})
var ls8_evi = ls8toa.map(function(image) {
var evi = image.expression(
'2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))', {
'NIR': image.select('B5'),
'RED': image.select('B4'),
'BLUE': image.select('B2')
}).rename('EVI')
return image.addBands(evi)
})
var landsat = ee.ImageCollection(ls7_evi.merge(ls8_evi));
var EVI = landsat.select(['EVI'])
print(EVI, 'EVI')
//create function to calculate mean values for each polygon
var pointsmean = function(image) {
var means = image.reduceRegions({
collection: roi, // used to be roi.select(['Id'])
reducer: ee.Reducer.mean(),
scale: 30
})
// assign time for every feature
means = means.map(function(f) { return f.set({date: image.date().format("YYYY-MM-dd")}) })
return means.copyProperties(image)
};
var finalEVI = EVI.map(pointsmean).flatten()
.sort('date', false)
.select(['date', 'mean'])
print(finalEVI.limit(100), 'final EVI')
Export.table.toDrive({
collection: finalEVI,
description: 'EVI_'+startdate+'TO'+enddate,
fileFormat: 'CSV'
});