目录
1 完整代码
function maskS2clouds(image) {
var qa = image.select('QA60')
var cloudBitMask = 1 << 10;
var cirrusBitMask = 1 << 11;
var mask = qa.bitwiseAnd(cloudBitMask).eq(0).and(
qa.bitwiseAnd(cirrusBitMask).eq(0))
return image.updateMask(mask).divide(10000)
.select("B.*")
.copyProperties(image, ["system:time_start"])
}
var start = '2016-01-01'
var end = '2021-01-01'
var image = s2.filterDate(start, end)
.filter(ee.Filter.lt('CLOUD_COVERAGE_ASSESSMENT',10))
.filterBounds(roi)
.map(function(image) {
var time_start = image.get("system:time_start")
image = image.multiply(0.0001)
image = image.set("system:time_start", time_start)
return image
})
print(image)//会发现有很多元素,这是因为每年不止一副影像,我们要做的就是将每年的一幅放在一个集合里生成新的imagecollection
var yearList = ee.List.sequence(ee.Date(start).get("year"), ee.Number(ee.Date(end).get("year").subtract(1)));
var yearImgList = yearList.map(function(year) {
year = ee.Number(year)
var sdate = ee.Date.fromYMD(year, 1, 1)
var edate = ee.Date.fromYMD(year.add(1), 1, 1)
var tempCol = image.filterDate(sdate, edate)
var img = tempCol.median().clip(roi)
img = img.set("year", year)
img = img.set("system:index", ee.String(year.toInt()))
return img
})
var imgCol = ee.ImageCollection.fromImages(yearImgList);
var params = {
crs: 'EPSG:3857',
framesPerSecond: 1,//帧率
region: roi,
min: 0.0,
max: 0.3,
bands: ['B8','B4','B3'],//选择假彩色
dimensions: 512,
};
print(ui.Thumbnail(imgCol, params))//直接在控制台生成GIF
print(imgCol.getVideoThumbURL(params))//生成网址
Map.centerObject(roi)
Map.addLayer(image.median().clip(roi),{bands:['B8','B4','B3'],min:0,max:0.3})