使用GEE获取遥感影像非常迅速便捷。今天主要学习一下如何获取一定时间段的去云哨兵2和Landsat8影像。
废话不多说,直接上代码。直接将矢量区域换成自己研究区即可。
获取并下载Landsat8影像代码如下
var region=ee.FeatureCollection('projects/ee-huiengine/assets/beijing');
function maskL8sr(image) {
// The third bit is cloud and the fourth bit is cloudshadow。
var cloudShadowBitMask = 1 << 4;
var cloudsBitMask = 1 << 3;
// Get QA_PIXEL band.
var qa = image.select('QA_PIXEL');
// Set the cloud mask to zero
var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0)
.and(qa.bitwiseAnd(cloudsBitMask).eq(0));
// Update the cloudmask
return image.updateMask(mask)
.select("SR_B.*")
.copyProperties(image, ["system:time_start"]);
}
// Map the function over 3 months of data and take the median.
// Load Landsat-8 surface reflectance data.
var landsat8 = ee.ImageCollection("LANDSAT/LC08/C02/T1_L2")
.filterBounds(region)
.filterDate('2020-12-01','2021-2-28')
// Pre-filter to get less cloudy granules.
.filter(ee.Filter.lte('CLOUD_COVER',5))
.map(maskL8sr)
.median()
.clip(region);
landsat8 = landsat8.uint16();
print(landsat8);// visualize the datasets
var rgbVis = {
min: 8400,
max: 15000,
gamma:1.5,
bands: ['SR_B4', 'SR_B3', 'SR_B2'],
};
Map.addLayer(landsat8,rgbVis,'landsat8');
var Landsat_collection = landsat8.select('SR_B2','SR_B3','SR_B4','SR_B5');
//Export image to google drive and then download
Export.image.toDrive({
image:Landsat_collection,
description:'s4',
scale:30,
region:region,
folder:"result",
crs: "EPSG:4326",
maxPixels: 1e13
});
获取并下载Sentinel-2影像代码如下
//define your study area here!
var studyarea = table;
Map.addLayer(studyarea)
//prepare sentinel imagecollection/;
/**
* Function to mask clouds using the Sentinel-2 QA band
* @param {ee.Image} image Sentinel-2 image
* @return {ee.Image} cloud masked Sentinel-2 image
*/
function maskS2clouds(image) {
var qa = image.select('QA60');
// Bits 10 and 11 are clouds and cirrus, respectively.
var cloudBitMask = 1 << 10;
var cirrusBitMask = 1 << 11;
// Both flags should be set to zero, indicating clear conditions.
var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
.and(qa.bitwiseAnd(cirrusBitMask).eq(0));
return image.updateMask(mask).divide(10000);
} //去云处理
var dataset = ee.ImageCollection('COPERNICUS/S2_SR')
.filterDate('2020-01-01', '2020-12-31') //影像时间
// Pre-filter to get less cloudy granules.
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',20)) //云量
.map(maskS2clouds) //去云
.map(function(image) {
var ndvi = image.normalizedDifference(['B8', 'B4']).rename('B5');
return image.addBands(ndvi);
}) //添加指数
.map(function(image){
var B1 = image.select("B2").rename("B1");//blue
var B2 = image.select("B3").rename("B2");//green
var B3 = image.select("B4").rename("B3");//red
var B4 = image.select("B8").rename("B4");//nir
return image.addBands(B1)
.addBands(B2)
.addBands(B3)
.addBands(B4)
}) //重命名
.select(["B1","B2","B3","B4","B5"/*,"B6","B7","B8","B9"*/]); //影像集
var study_img = dataset.median().clip(studyarea); //中值影像,裁剪出研究区影像
Map.addLayer(study_img, {bands:["B4", "B3", "B2"], min:0, max:0.3}, 'RGB');
//导出哨兵2号影像数据
Export.image.toDrive({
image:study_img.select(["B1","B2","B3","B4","B5"]),
description:"sentinel2_region",
region:studyarea,
scale:10,
maxPixels:1e13
})