大家好
请您在以下方面帮助我。
我正在尝试使用Landsat 7和8为特定感兴趣的区域(ROI)生成NDVI估计的时间序列。然后,我想将这些估算值与MODIS 16天NDVI综合数据中获得的NDVI值进行比较。虽然我熟悉如何在Landsat空间分辨率(30 m)下获取我的ROI的平均Landsat NDVI值,但我想将该分辨率的像素聚合到MODIS NDVI产品分辨率(500 m)。
我已经尝试在下面提供的基于https://developers.google.com/earth-engine/guides/resample提供的示例的代码中做到这一点,但我在我的尝试中没有成功。我收到错误"ndvi。reducerresolution不是一个函数">
我是否需要为此创建一个函数并将其映射到图像集合上,或者我是否应该指定我希望在绘制图表或将数据导出为csv时执行平均的比例?
提前感谢。
///Add region of interest
var ROI = ROI
Map.addLayer(ROI, {}, 'ROI')
Map.centerObject(ROI, 10)
//Define time of interest
// Ensure that the first image that is collected possesses data to calculate NDVI otherwise the script will not work as required
var startdate = '2013-01-01'
var enddate = '2021-01-01'
var years = ee.List.sequence(ee.Date(startdate).get('year'), ee.Date(enddate).get('year'));
///Create functions to mask clouds
/// see: https://landsat.usgs.gov/sites/default/files/documents/landsat_QA_tools_userguide.pdf
///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);
}
///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);
}
///Import image collections, filter by date and ROI, apply cloud mask and clip to ROI
///Landsat 7 Collection 1 Tier 1 calibrated top-of-atmosphere (TOA) reflectance
var ls7toa = ee.ImageCollection('LANDSAT/LE07/C01/T1_TOA')
.filterBounds(ROI)
.filterDate(startdate, enddate)
.map(function(im) {return maskL7(im)})
.map(function(image){return image.clip(ROI)})
///Landsat 8 Collection 1 Tier 1 calibrated top-of-atmosphere (TOA) reflectance
var ls8toa = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
.filterBounds(ROI)
.filterDate(startdate, enddate)
.map(function(im) {return maskL8(im)})
.map(function(image){return image.clip(ROI)})
///Create function to calculate NDVI using Landsat data
///Calculate NDVI for Landsat 7
var ls7_ndvi = ls7toa.map(function(image) {
var ndvi = image.normalizedDifference(['B4', 'B3']).rename('ndvi');
return image.addBands(ndvi);
});
///Calculate NDVI for Landsat 8
var ls8_ndvi = ls8toa.map(function(image) {
var ndvi = image.normalizedDifference(['B5', 'B4']).rename('ndvi');
return image.addBands(ndvi);
});
///Merge the image collections into one and only select the NDVI data
var landsat = ee.ImageCollection(ls7_ndvi.merge(ls8_ndvi));
var ndvi = landsat.select(['ndvi'])
print(ndvi, 'ndvi')
// Load a MODIS NDVI Collection
var modis = ee.Image(ee.ImageCollection("MODIS/006/MOD13A1")
.first()
.select('NDVI'))
// Get information about the MODIS projection.
var modisProjection = modis.projection();
print('MODIS projection:', modisProjection);
// Get the Landsat NDVI collection at MODIS scale and projection.
var ndviMean = ndvi
// Force the next reprojection to aggregate instead of resampling.
.reduceResolution({
reducer: ee.Reducer.mean(),
})
// Request the data at the scale and projection of the MODIS image.
.reproject({
crs: modisProjection
});
///Create a timer-series plot of NDVI
var chart = ui.Chart.image.series({
imageCollection: ndviMean,
region: ROI,
reducer: ee.Reducer.mean(),
scale: 500,
})
print(chart, "ndvi")
实际上,在ImageCollection上映射一个函数就可以达到这个目的:
///Add region of interest
var ROI = ROI
Map.addLayer(ROI, {}, 'ROI')
Map.centerObject(ROI, 10)
//Define time of interest
// Ensure that the first image that is collected possesses data to calculate NDVI otherwise the script will not work as required
var startdate = '2013-01-01'
var enddate = '2021-01-01'
var years = ee.List.sequence(ee.Date(startdate).get('year'), ee.Date(enddate).get('year'));
///Create functions to mask clouds
/// see: https://landsat.usgs.gov/sites/default/files/documents/landsat_QA_tools_userguide.pdf
///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);
}
///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);
}
///Import image collections, filter by date and ROI, apply cloud mask and clip to ROI
///Landsat 7 Collection 1 Tier 1 calibrated top-of-atmosphere (TOA) reflectance
var ls7toa = ee.ImageCollection('LANDSAT/LE07/C01/T1_TOA')
.filterBounds(ROI)
.filterDate(startdate, enddate)
.map(function(im) {return maskL7(im)})
.map(function(image){return image.clip(ROI)})
///Landsat 8 Collection 1 Tier 1 calibrated top-of-atmosphere (TOA) reflectance
var ls8toa = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
.filterBounds(ROI)
.filterDate(startdate, enddate)
.map(function(im) {return maskL8(im)})
.map(function(image){return image.clip(ROI)})
///Create function to calculate NDVI using Landsat data
///Calculate NDVI for Landsat 7
var ls7_ndvi = ls7toa.map(function(image) {
var ndvi = image.normalizedDifference(['B4', 'B3']).rename('ndvi');
return image.addBands(ndvi);
});
///Calculate NDVI for Landsat 8
var ls8_ndvi = ls8toa.map(function(image) {
var ndvi = image.normalizedDifference(['B5', 'B4']).rename('ndvi');
return image.addBands(ndvi);
});
///Merge the image collections into one and only select the NDVI data
var landsat = ee.ImageCollection(ls7_ndvi.merge(ls8_ndvi));
var ndvi = landsat.select(['ndvi'])
print(ndvi, 'ndvi')
// Load a MODIS NDVI Collection
var modis = ee.Image(ee.ImageCollection("MODIS/006/MOD13A1")
.first()
.select('NDVI'))
// Get information about the MODIS projection.
var modisProjection = modis.projection();
print('MODIS projection:', modisProjection);
// Get the Landsat NDVI collection at MODIS scale and projection.
var landsat_pro = ndvi.first().projection();
var CopyScale = landsat_pro.nominalScale();
print(CopyScale, 'original scale Landsat (m)')
var landsat_resample = function(image){
return image.reproject(landsat_pro, null, 500) // insert here the desired scale in meters
// Force the next reprojection to aggregate instead of resampling.
.reduceResolution({
reducer: ee.Reducer.mean(),
maxPixels: 1024
})
.copyProperties(image)
}
var ndviResample = ndvi.map(landsat_resample)
///Create a timer-series plot of NDVI
var chart = ui.Chart.image.series({
imageCollection: ndviResample,
region: ROI,
reducer: ee.Reducer.mean(),
scale: 500,
})
print(chart, "ndvi")