基础知识
GEE = Google Earth Engine
海量遥感数据
集成众多算法
大范围处理能力
GEE语法
本地(client) | GEE(server) | |
---|---|---|
符串 | var str="hello"; | var str=ee.String("hello") |
数字 | var num=3; | var num=ee.Number(3) |
列表 | var lst=[1,2,3]; | var lst=ee.List([1,2,3]) |
对象 | var obj={a1:"x",a2:2,a3:[1,2,3]}; | var obj=ee.Dictionary({a1:"x",a2:2,a3:[1,2,3]}) |
函数 | function a(e){return e;} | function a(e){return e;} |
print(num) | print(num+2) | print(num.add(2)) ,print(num.getinfo()+2) |
for(var i=0; i<lst.size();i++){print(i)} | for(var i=0; i<lst.size().getInfo();i++){print(i)} | |
print(str) | print(str+',"world"') | print(str.getInfo()+', "GEE"') |
实例:
var m_string = ee.String('Hello, GEE');
var m_num = ee.Number(42);
var m_list = ee.List([0,1,2,3]);
var m_dict = ee.Dictionary({
e:Math.E,
pi:Math.PI,
phi: (1+Math.sqrt(5))/2
});
function function(element){
return element;
}
快捷键
功能 | 快捷键 |
---|---|
在英文模式下 | / |
注释: | ctrl+/ |
自动补全 | ctrl+space |
获取影像(Image)
Function | conception |
---|---|
ee.ImageCollection | Image Collection |
ee.Image | Image |
Image的创建 |
// 1. 使用 image id 创建
var img=ee.Image('JAXA/ALOS/AW3D30/V2_2');
print(img)
print(img.bandNames()) //显示波段数
// 1.1 加载 multiband 影像
var vipara={
bands: ['B5','B4','B3'],
min: 0,
max: 0.5,
gama: [0.95, 1.1, 1]
};
Map.setCenter(-122.1899,37.50,10);
Map.addLayer(img,vipara,'false color',false); // 先不显示
// 2. 使用 imageCollection 创建
var img=ee.ImageCollection('COPERNICUS/S2_SR')
.filterBounds(ee.Geometry.Point(110.39, 19.98))
.filterDate('2021-01-01', '2021-12-31')
.sort('CLOUDY_PIXEL_PERCENTAGE')
.first();
Map.centerObject(img,11);
print(img)
Map.addLayer(img, {bands: ['B4', 'B3', 'B2'], min: 0, max: 2000}, 'first');
可用数据汇总:
Earth Engine Data Catalog, landsat, Sentinel-2, Sentinel-1, MODIS …
帮助文件API DOCS
加载显示 Sentinel-2
// load image through ImageCollection
var first = ee.ImageCollection('COPERNICUS/S2_SR')
.filterBounds(ee.Geometry.Point(110.39, 19.98))
.filterDate('2021-01-01', '2021-12-31')
.sort('CLOUDY_PIXEL_PERCENTAGE')
.first();
Map.centerObject(first);
Map.addLayer(first, {bands: ['B4', 'B3', 'B2'], min: 0, max: 2000}, 'first'); //加载显示多波段
加载显示 Landsat
var image = ee.ImageCollection("LANDSAT/LC08/C01/T1_SR")
.filterBounds(geometry)
.filterDate('2020-01-01','2020-12-31')
.sort('CLOUDY_PIXEL_PERCENTAGE')
.first();
print(image)
// Center the map and display the image.
Map.setCenter(110.39,19.98); //
Map.centerObject(image,12)
Map.addLayer(image, {bands: ['B4','B3','B2'], min: 0,max: 2000}, 'Landsat-8'); //加载显示多波段
定量指数
// 加载影像 Load an image.
var image = ee.ImageCollection("LANDSAT/LC08/C01/T1_TOA")
.filterBounds(geometry)
.filterDate('2020-01-01','2020-12-31')
.sort('CLOUDY_PIXEL_PERCENTAGE')
.first();
print("image", image);
// Define the visualization parameters.
var vizParams = {
bands: ['B5', 'B4', 'B3'],
min: 0, // or min: [0, 0.2, 0.3],
max: 0.5, // or max: [0.8, 0.5, 0.7],
gamma: [0.95, 1.1, 1]
};
// 显示影像 Center the map and display the image.
Map.setCenter(110.39,19.98, 10); //
Map.addLayer(image, vizParams, 'faulse color image');
计算 NDVI
// 计算NDVI compute ndvi
// defien the visualizaiton parameters
var visParam_NDVI = {
min: -0.2,
max: 1,
palette: 'FFFFFF, CE7E45, DF923D, F1B555, FCD163, 99B718, 74A901, 66A000, 529400,' +
'3E8601, 207401, 056201, 004C00, 023B01, 012E01, 011D01, 011301'
}; //加载显示单波段要用的参数
var imgNDVI = image.normalizedDifference(["B5", "B4"]).rename("imgNDVI") //rename
.set("system:time_start", image.get("system:time_start"))
.set("CLOUD_COVER",image.get("CLOUD_COVER"))
.set("UTM_ZONE", image.get("UTM_ZONE"))
.set("WRS_PATH", image.get("WRS_PATH"))
.set("WRS_ROW",image.get("WRS_ROW"));
Map.addLayer(imgNDVI, visParam_NDVI,'image NDVI'); //加载显示单波段
print("imgNDVI", imgNDVI);
计算 NDWI
// 计算NDWI compute ndwi
var imgMNDWI = image.normalizedDifference(["B3", "B6"]).rename("imgNDWI");
//设定最大值、最小值的颜色
Map.addLayer(imgMNDWI, {min:0, max:1, palette:['#000000', '0000FF']},'image MNDWI');
//设置掩膜,并将颜色设置为 blue
Map.addLayer(imgMNDWI.selfMask(), {palette:['0000FF']},'image MNDWI2');
//将 大于0 的值设置为 red
var waterMask = imgMNDWI.gte(0);
Map.addLayer(waterMask.selfMask(), {'palette':'red'}, 'waterMask');
计算EVI
// 计算EVI Compute the EVI using an expression.
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')
});
Map.addLayer(evi, {min:-1, max:1, palette:['FF0000', '00FF00']}, 'EVI');
function EVI_Calculate(img) {
// .multiply(0.0001)
var blue = img.select("B2").multiply(0.0001);
var red = img.select("B4").multiply(0.0001);
var nir = img.select("B5").multiply(0.0001);
var evi = img.expression(
'2.5 * ((B8 - B3) / (B8 + 6 * B3 - 7.5 * B2 + 1))',
{
"B2": blue,
"B3": red,
"B8": nir
}
).rename('EVI');
return evi;
}
image 逻辑运算
// 导入研究区(北京)分类、区域边界等数据
Imports (4 entries)
var CLCD_2020: Image users/pathToData1
var CLCD_2000: Image users/pathToData2
var AOI: Polygon, 4 vertices
var ChinaProvince: Table user/PathToChinaProvince
// 获取并加载 北京市 区域
var Beijing = ChinaProvince.filter(ee.Filter.eq("City",'北京市'));
var roi = Beijing;
Map.centerObject(roi);
Map.addLayer(roi, {'color':'blue'}, 'studyArea',true, 0.5);
/********************************************************
1-Cropland
2-Forest
3-Shrub
4-Grassland
5-Water
6-Sonw/Ice
7-Barren
8-Impervious
9-Wetland
********************************************************/
// 加载 2020 年分类数据,并以随机颜色显示
var CLCD_2020 = CLCD_2020.clip(roi);
Map.addLayer(CLCD_2020.randomVisualizer(), null, 'CLCD_2020',false);
print(CLCD_2020.bandNames());
// 提取不透水层,即 urban area,并显示
var urban_beijing_2020 = CLCD_2020.clip(roi).eq(8);
var urban_beijing_2000 = CLCD_2000.clip(roi).eq(8);
Map.addLayer(urban_beijing_2020,{palette:['gray','red']},'urban2020',false)
// 计算2000和2020年都为不透水层的区域
var urban_beijing_or = urban_beijing_2020.or(urban_beijing_2000);
// Map.addLayer(urban_beijing_or.selfMask(),{'palette':['red','green']},'urban_bjor');
Map.addLayer(urban_beijing_or.selfMask(), {'palette':['red']},'urban_beijing_or',false);
// 计算 同为 不透水层的区域
var urban_beijing_and = urban_beijing_2020.and(urban_beijing_2000);
Map.addLayer(urban_beijing_and.selfMask(), {'palette':['yellow']},'urban_beijing_and');
问题
- 计算每一类样本数?
- 计算类别数?
image 分层抽样
即,从已有的类别中得到样本点
// 导入研究区(北京)分类、区域边界等数据
Imports (4 entries)
var CLCD_2020: Image users/pathToData1
var AOI: Polygon, 4 vertices
var ChinaProvince: Table user/PathToChinaProvince
// 抽取并显示研究区位置
var Beijing = ChinaProvince.filter(ee.Filter.eq("City",'北京市'));
var roi = Beijing;
Map.centerObject(roi);
Map.addLayer(roi, {'color':'grey'}, 'studyArea');
/********************************************************
1-Cropland
2-Forest
3-Shrub
4-Grassland
5-Water
6-Sonw/Ice
7-Barren
8-Impervious
9-Wetland
********************************************************/
// 以随机颜色显示北京市 2020年分类数据
var CLCD_2020 = CLCD_2020.clip(roi);
Map.addLayer(CLCD_2020.randomVisualizer(), null, 'CLCD_2020',false);
// 提取北京 2020年 不透水层区域,并将波段命名为 impervious
var impervious_beijing = CLCD_2020.eq(8).clip(roi).rename('Impervious')
print(impervious_beijing)
// 为分层抽样做准备
var impervious_beijing = CLCD_2020.eq(8).clip(roi).rename('Impervious')
.set('system:footprint',roi.geometry());
print(impervious_beijing)
Map.addLayer(impervious_beijing.selfMask(), {'palette':['red']},'impervious_beijing');
// 从不透水层波段提取 2000 个样本点
var impervious_sample = impervious_beijing.selfMask().stratifiedSample({
numPoints:2000,
classBand:'Impervious',
geometries:true
});
Map.addLayer(impervious_sample, null, 'impervious_sample');
print("impervious_sample size",impervious_sample.size())
image 之 blend 和 where
// 导入研究区(北京)分类、区域边界等数据
Imports (4 entries)
var CLCD_2020: Image users/pathToData1
var CLCD_2000: Image users/pathToData2
var AOI: Polygon, 4 vertices
var ChinaProvince: Table user/PathToChinaProvince
// 获取并加载 北京市 区域
var Beijing = ChinaProvince.filter(ee.Filter.eq("City",'北京市'));
var roi = Beijing;
Map.centerObject(roi,8); // 缩放比例为 8 [0-24]
Map.addLayer(roi, {'color':'grey'}, 'studyArea');
// 加载并显示研究区 哨兵数据
var imgS2 = ee.ImageCollection('COPERNICUS/S2_SR')
.filterBounds(roi)
.filterDate('2020-01-01', '2020-12-31')
.sort('CLOUDY_PIXEL_PERCENTAGE',false)
.mosaic().clip(roi)
.divide(1e4);
Map.addLayer(imgS2, {bands: ['B4', 'B3', 'B2'], min: 0, max: 0.3}, 'imgS2');
var urban = CLCD_2020.clip(roi).eq(8);
Map.addLayer(urban.selfMask(), {'palette':'yellow'}, 'urban');
// blend
var imgS2_blend_urban = imgS2.blend(urban);
Map.addLayer(imgS2_blend_urban, {bands:['B4', 'B3', 'B2'],min:0,max:0.3},'imgS2_blend_urban');
// where
var tmp_red = imgS2.select('B4').where(urban,1);
var tmp_green = imgS2.select('B3').where(urban,0);
var tmp_blue = imgS2.select('B2').where(urban,0);
var tmp = ee.Image.cat([tmp_red,tmp_green,tmp_blue]);
Map.addLayer(tmp, {bands:['B4', 'B3', 'B2'],min:0,max:0.3},'tmp');
没听明白,回头自己看帮助
image 之 unmask
// 导入研究区(北京)分类、区域边界等数据
Imports (4 entries)
var CLCD_2020: Image users/pathToData1
var CLCD_2000: Image users/pathToData2
var AOI: Polygon, 4 vertices
var ChinaProvince: Table user/PathToChinaProvince
// 获取并加载 北京市 区域
var Beijing = ChinaProvince.filter(ee.Filter.eq("City",'北京市'));
var roi = Beijing;
Map.centerObject(roi);
Map.addLayer(roi, {'color':'grey'}, 'studyArea');
// Return the bounding box of this geometry.
var roiBound = roi.geometry().bounds();
Map.addLayer(roiBound, {'color':'red'}, 'StudyBound');
var imgS2 = ee.ImageCollection('COPERNICUS/S2_SR')
.filterBounds(roi)
.filterDate('2020-01-01', '2020-12-31')
.sort('CLOUDY_PIXEL_PERCENTAGE',false)
.mosaic().clip(roi)
.divide(1e4);
Map.addLayer(imgS2, {bands: ['B4', 'B3', 'B2'], min: 0, max: 0.3}, 'imgS2');
var CLCD_2020 = CLCD_2020.clip(roi);
Map.addLayer(CLCD_2020.randomVisualizer(), null, 'CLCD_2020');
// 如果类别值是从 0 开始,类别值+1,并将背景值设置为 -999
var CLCD_2020_filled = CLCD_2020.add(1).unmask(-999).clip(roiBound);
Map.addLayer(CLCD_2020_filled.randomVisualizer(), null, 'CLCD_2020_filled');
List 和 collection概念
无序结构 | 有序结构 |
---|---|
ee.ImageColletion | ee.List |
ee.FeatureCollection | ee.Array |
List 概念
万物可为 List
- 空:
var lst=[]
, var lst=ee.List([]) - 数值:
var lst=[1, 2, 3]
, var lst=ee.List([1,2,3]) - 字符串:
var lst=['a', 'b', 'c']
- 栅格数据:
- 适量数据:
List
常用函数
sequence
函数
var tmpList1 = ee.List([1,2,3,4,5]);
print("tmpList:", tmpList1);
// 函数生成序列
var tmpList2 = ee.List.sequence(1,10);
print("tmpList2:", tmpList2);
cat
拼接函数
// cat, 拼接list
var tmpList3 = tmpList1.cat(tmpList2);
print(tmpList3);
get
获取元素值
// get,获取元素值
// note that the index starts with zero
print("tmpList3 get(1)", tmpList3.get(1));
size
测量list大小
// size
var listSize = tmpList3.size();
print("listSize size", listSize);
add
追加元素
// add, 追加元素
var tmpList3 = tmpList3.add('GEE');
print("tmpList3 add",tmpList3);
insert
插入元素
// insert, 插入元素
var tmpList3 = tmpList3.insert(0,'a');
print("tmpList3 insert",tmpList3);
sort
排序
// sort should be run for list with values of same type
var tmpList3 = tmpList3.sort();
print("tmpList3 sort",tmpList3);
slice
切片
// 切片
var tmpList3 = tmpList3.slice(0,5,2); // (start,end,step)
print("tmpList3 slice",tmpList3);
List 之 map
// 客户端 list
var clientList = [];
for(var i = 0; i < 8; i++) {
clientList.push(i + 1);
}
print('clst',clientList);
// 服务器 list
var serverList = ee.List.sequence(0, 7);
serverList = serverList.map(function(n) {
return ee.Number(n).add(1);
}); // 隐函数
print('slst',serverList);
// 使用 map 的另外一种方式
function addNum(n){
return ee.Number(n).add(1); // 用Number强制转换为服务器版本
}
var serverList = ee.List.sequence(0,7);
serverList = serverList.map(addNum);
print('slst',serverList)
// list 返回值为 feature
var tmpList = ee.List.sequence(0, 11);
print("tmpList",tmpList);
var tmpListV2 = tmpList.map(function(item){
var tmpFea = ee.Feature(null).set('Num',item);
return tmpFea;
});
print("tmpListV2",tmpListV2);
// list 返回值为 image
var tmpListV3 = tmpList.map(function(item){
var tmpImg = ee.Image.constant(item).set('Num',item)//.set('month',ee.Number(item).add(1));
return tmpImg;
});
print("tmpListV3",tmpListV3);
// 查找满足条件的元素位置
print(serverList.indexOf(4))
- list 返回值可以和原来 list 不同
- Map 函数没有考虑对应元素之间的顺序结构关系
ImageCollection
- Landsat
- Sentinel-1
- Sentinel-2
- MODIS
- Cropland Datasets
GEE Dataset catalog
ImageCollection 函数
// 导入矢量文件
Imports (1 entry)
var AOI: Polygon, 4 vertices
// 加载并显示矢量文件
var roi = AOI;
Map.centerObject(roi,7);
Map.addLayer(roi, {'color':'grey'}, 'StudyArea');
// imageCollection 旋转影像
var S2Col = ee.ImageCollection('COPERNICUS/S2_SR')
.filterBounds(roi)
.filterDate('2020-11-01','2020-12-01');
.map(function(img){return img.clip(roi) }) //提前 clip
.select(['B4','B3','B2'],['red','green','blue']); //选择指定波段,并重命名
var vis = {bands: ['B4', 'B3', 'B2'], min: 0, max: 5000};
first
第一景影像
“`javascript
// first image
var S2Col_first = S2Col.first();
Map.addLayer(S2Col_first, vis, ‘S2Col_first’);
print(“S2Col_first”,S2Col_first);
- `mean`
javascript
// 所有第 i 波段在该像素位置的均值
var S2Col_mean = S2Col.mean().clip(roi);
Map.addLayer(S2Col_mean, vis, ‘S2Col_mean’);
print(“S2Col_mean”,S2Col_mean);
- `median`
javascript
// 所有第 i 波段在该像素位置的中值
var S2Col_median = S2Col.median(); //所有与研究区相交的影像
Map.addLayer(S2Col_median, vis, ‘S2Col_median’);
print(“S2Col_median”,S2Col_median);
- `size`
javascript
// 共有 47 景影像?
var S2Col_size = S2Col.size();
print(“S2Col_size”,S2Col_size);
## ImageCollection 之 Map
只能返回 `feature` 或 `image`
javascript
// 导入矢量文件
Imports (1 entry)
var AOI: Polygon, 4 vertices
// 加载并显示研究区
var roi = AOI;
Map.centerObject(roi);
Map.addLayer(roi, {‘color’:’grey’}, ‘StudyArea’);
// 定义函数
function addNDVI_V1(img){
var ndvi = ee.Image(img).normalizedDifference([‘B8′,’B4’]).rename(‘NDVI’);
//return img.addBands(ndvi);
return ee.Feature(null);
}
function addNDVI_V2(img){
var ndvi = ee.Image(img).normalizedDifference([‘B8′,’B4’]).rename(‘NDVI’);
return ndvi;
}
function addNDVI_V3(img){
var ndvi = ee.Image(img).normalizedDifference([‘nir’,’red’]).rename(‘NDVI’);
return img.addBands(ndvi);
}
var S2Col_V1 = ee.ImageCollection(‘COPERNICUS/S2_SR’)
.filterBounds(roi)
.filterDate(‘2020-11-01′,’2020-12-01’)
.map(addNDVI_V1);
var S2Col_V2 = ee.ImageCollection(‘COPERNICUS/S2_SR’)
.filterBounds(roi)
.filterDate(‘2020-11-01′,’2020-12-01’)
.map(addNDVI_V2);
var S2Col_V3 = ee.ImageCollection(‘COPERNICUS/S2_SR’)
.filterBounds(roi)
.filterDate(‘2020-11-01′,’2020-12-01’)
.select([“B8″,”B4”],[‘nir’,’red’])
.map(addNDVI_V3);
print(“S2Col_V1”,S2Col_V1);
print(“S2Col_V2”,S2Col_V2);
print(“S2Col_V3”,S2Col_V3);
- 没听明白将这个内容是干啥的?没讲清楚,后面看示例吧
- `print`, `Map.addLayer` 等属于本地函数,最好不要出现在 `GEE` 服务器代码里
## 例子:可用性分析与频率计算
javascript
var ChinaProvince = ee.FeatureCollection(“users/studyroomGEE/ChinaShp/ChinaProvince”);
var Guangdong = ChinaProvince.filter(ee.Filter.eq(“City”,’广东省’));
var roi = Guangdong;
Map.addLayer(roi, {‘color’:’grey’}, ‘studyArea’);
Map.centerObject(roi);
var year_start = 2020;
var year_end = 2020;
var imgCol = ee.ImageCollection(“LANDSAT/LC08/C01/T1_SR”)
.filterBounds(roi)
.filter(ee.Filter.calendarRange(year_start, year_end,’year’));
// compute the image total size
var imgSize = imgCol.size(); //filter后,数据集中影像的个数?
print(“imgSize”,imgSize); // result is 265
/*
compute the image avaibility. There are several ways to achieve that.
1: use the count function; count 表示每个元素可用的影像的数量
2: use the sum function
**/
var imgCount_V1 = imgCol.select(“B1”).count().clip(roi);
var imgCount_V2= imgCol.select(“B1”).map(function(img){
return img.multiply(0).add(1).toInt();
}).sum().clip(roi);
// define image render parameters
var visParam = {
min: 10,
max: 40,
palette: ‘FFFFFF, CE7E45, DF923D, F1B555, FCD163, 99B718, 74A901, 66A000, 529400,’ +
‘3E8601, 207401, 056201, 004C00, 023B01, 012E01, 011D01, 011301’
};
Map.addLayer(imgCount_V1,visParam,”imgCount_V1″,false);
Map.addLayer(imgCount_V2,visParam,”imgCount_V2″,false);
/*
compute the water frequency
**/
var waterIndex = imgCol.map(function(img){
var mndwi = img.normalizedDifference([‘B3′,’B6’]).gt(0).rename(‘MNDWI’);
return img.addBands(mndwi);
})
var waterFrequency = waterIndex.select(“MNDWI”).sum().divide(imgCount_V1).clip(roi);
// 该位置所有水体值的和,除以影像的个数
Map.addLayer(waterFrequency, {min: 0, max:1, palette: [‘000000’, ‘0000FF’]}, ‘waterFrequency’)
- 此处可用于红树林的提取,怎么用?(植被指数概率图?)
## 例子:imagecollection (条带号制作)
[代码](https://code.earthengine.google.com/f9ff81c85cc70d42f6771bcc0a0d8e6d)
javascript
var ChinaProvince = ee.FeatureCollection(“users/studyroomGEE/ChinaShp/ChinaProvince”);
var Guangzhou = ChinaProvince.filter(ee.Filter.eq(“City”,’广东省’));
var roi = Guangzhou;
Map.addLayer(roi, {‘color’:’grey’}, ‘studyArea’);
Map.centerObject(roi);
// set time parameter
var year_start = 2018;
var year_end = 2020;
var imgCol = ee.ImageCollection(“LANDSAT/LC08/C01/T1_SR”)
.filterBounds(roi)
.filter(ee.Filter.calendarRange(year_start, year_end,’year’))
Map.addLayer(imgCol.first());
print(“imgCol”,imgCol.first()) // 第一景影像的信息
// // compute the image total size
var imgSize = imgCol.size();
print(‘imgSize’,imgSize) // 826
var imgTiles = ee.FeatureCollection(imgCol.map(function(img){
var tmpFootprint = ee.Image(img).geometry();
var WRS_PATH = ee.Number(ee.Image(img).get(“WRS_PATH”));
var WRS_ROW = ee.Number(ee.Image(img).get(“WRS_ROW”));
return ee.Feature(tmpFootprint,null).set(“WRS_PATH”,WRS_PATH).set(“WRS_ROW”,WRS_ROW);
})).distinct([“WRS_PATH”,’WRS_ROW’]);
// print(“imgTiles”,imgTiles); // 打印所有的 imgTiles 信息
var styling1 = {color:’red’,fillColor:’00000000′}
// print(“imgTiles”,imgTiles.limit(5)); // 打印前五个
print(“imgTiles size”,imgTiles.size()); // 21, imgTiles 大小
Map.addLayer(imgTiles.style(styling1), {}, ‘imgTiles’);
// 打印出所有行列号,并排除重复值
// print(“imgTiles”, imgTiles.aggregate_array(“WRS_PATH”).distinct());
// print(“imgTiles”, imgTiles.aggregate_array(“WRS_ROW”).distinct())
# List 和 ImageCollection 配合
## 若干年份最少云量影像的建立
[代码](https://code.earthengine.google.com/fe3e994f34566d2b1afa91e88196cece)
javascript
Imports (1 entry)
var AOI: Polygon, 4 vertices
var roi = AOI;
Map.addLayer(roi);
Map.centerObject(roi, 9);
var startyear = 2016;
var endyear = 2018;
var LS7 = ee.ImageCollection(‘LANDSAT/LC08/C01/T1_SR’)
.filterBounds(roi)
.filterDate(startyear+’-01-01′, endyear+1+’-01-01′);
// Get least cloudy annual scene / mosaic via the cc metadata property.
// years = [2000, 2001, 2002]
var years = ee.List.sequence(startyear, endyear).getInfo();
print(‘years’,years)
var annual_least_cc = years.map(function(year){
// Or get mosaic of least cloudy annual scenes.
var annual = LS7.filterDate(year+’-01-01′, (year+1)+’-01-01′)
.sort(‘CLOUD_COVER’, false); // mosaic puts last element on top.
var annual_img = ee.Image(annual.mosaic()).clip(roi);
Map.addLayer(annual_img.divide(10000), {bands: [‘B3’, ‘B2’, ‘B1’], min: 0, max: 0.3}, “layer”+year);
return annual_img;
})
annual_least_cc = ee.ImageCollection(annual_least_cc);
print(annual_least_cc);
## 月合成影像
[代码](https://code.earthengine.google.com/c60499b4dbe5b53bcb66e3e3c1ab7407)
javascript
Imports (1 entry)
var AOI: Polygon, 4 vertices
var roi = AOI;
//show the study area
Map.centerObject(roi,5);
Map.addLayer(roi, {color: “gray”}, “roi”);
var S2 = ee.ImageCollection(‘COPERNICUS/S2_SR’); //S2_SR
var S2SRCol = S2//.filter(ee.Filter.lt(‘CLOUDY_PIXEL_PERCENTAGE’,20))
.filterDate(‘2020-03-01’, ‘2020-11-01’)
.filterBounds(roi)
var monthcomposite = ee.List.sequence(0, 1*6).map(function(n) {
var start = ee.Date(‘2020-04-01’).advance(n, ‘month’);
var end = start.advance(1, ‘month’);
return S2SRCol.filterDate(start, end).median().clip(roi);
}).flatten();
print(monthcomposite);
var imageCol = ee.ImageCollection.fromImages(monthcomposite);
var sCol = imageCol.toList(imageCol.size());
var ls = [];
for (var i=0; i<sCol.length().getInfo(); i++) {
var imageData = ee.Image(sCol.get(i));
Map.addLayer(imageData, {bands:[‘B11′,’B8′,’B4’],min:0,max:3000}, “timeWindow_”+i);
}
“`