Sentinel-2:中文名哨兵2号卫星。
NDVI的计算公式:NDVI = (近红外波段 - 红波段) / (近红外波段 + 红波段)
针对每种卫星的波段,选用的波段都有所不同,公式如下:
代码如下(以鹿邑县为例)
注意:分辨率为10m
//以鹿邑县为例子
var geometry = ee.FeatureCollection('users/www1573979951/luyixian')
Map.centerObject(geometry,7)
//颜色设置
var colorizedVis = {
min: -0.8,
max: 0.8,
palette: ['blue', 'white', 'green'],
};
//使用QA波段去云
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).set(image.toDictionary(image.propertyNames()));
}
//NDVI的计算公式
function createNDVI(image){
var ndvi = image.normalizedDifference(["B8","B4"]).rename('NDVI');
return image.addBands(ndvi);
}
//特别注意的是,在数学变换之后,保持原始影像的属性,所以这里.set(image.toDictionary(image.propertyNames()));
var S2_COL = ee.ImageCollection("COPERNICUS/S2")
.filterDate("2020-01-01", "2020-12-31")
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',20))
.filterBounds(geometry)
.map(maskS2clouds)
.map(createNDVI)
.select('NDVI');
print(S2_COL)
Map.addLayer(S2_COL.mean().clip(geometry), colorizedVis, 'col');
//趋势线代码
var S2_chart = ui.Chart.image.series({
imageCollection: S2_COL.select('NDVI'),
region: geometry,
reducer: ee.Reducer.mean(),
scale: 500
}).setOptions({
interpolateNulls: true,
lineWidth: 2,
title: 'NDVI Time Seires',
vAxis: {title: 'NDVI'},
hAxis: {title: 'Date'},
trendlines: { 0: {title: 'NDVI_trend',type:'linear', showR2: true, color:'red', visibleInLegend: true}}
});
print(S2_chart);
遥感影像截图:
数据集截图(51个数据集):
表格数据以及变化趋势截图:
CSV数据:
分辨率:10m
代码如下(以鹿邑县为例):
//以鹿邑县为例子
var geometry = ee.FeatureCollection('users/www1573979951/luyixian')
Map.centerObject(geometry,7)
//颜色设置
var colorizedVis = {
min: -0.8,
max: 0.8,
palette: ['blue', 'white', 'green'],
};
//使用QA波段去云
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).set(image.toDictionary(image.propertyNames()));
}
//NDVI的计算公式
function createNDVI(image){
var ndvi = image.normalizedDifference(["B8","B4"]).rename('NDVI');
return image.addBands(ndvi);
}
//特别注意的是,在数学变换之后,保持原始影像的属性,所以这里.set(image.toDictionary(image.propertyNames()));
var S2_COL = ee.ImageCollection("COPERNICUS/S2")
.filterDate("2020-01-01", "2020-12-31")
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',20))
.filterBounds(geometry)
.map(maskS2clouds)
.map(createNDVI)
.select('NDVI');
// Map.addLayer(S2_COL.mean().clip(geometry), colorizedVis, 'col');
print(S2_COL)
function exportImageCollection(imgCol) {
var indexList = imgCol.reduceColumns(ee.Reducer.toList(), ["system:index"]).get("list");
indexList.evaluate(function(indexs) {
for (var i=0; i<indexs.length; i++) {
var image = imgCol.filter(ee.Filter.eq("system:index", indexs[i])).first();
Map.addLayer(image.clip(geometry), colorizedVis, 'sentinel_10_ndvi_2020_'+indexs[i]);
//tif数据下载
Export.image.toDrive({
image: image.clip(geometry),
description: 'sentinel_10_ndvi_2020_'+indexs[i],
fileNamePrefix: 'sentinel_10_ndvi_2020_'+indexs[i],
folder: 'sentinel_10',
region: geometry,
scale: 10,
crs: "EPSG:4326",
maxPixels: 1e13
});
}
});
}
exportImageCollection(S2_COL);
分辨率:10m
代码如下(以鹿邑县为例):
var roi = ee.FeatureCollection('users/www1573979951/luyixian');; //table为自己上传的矢量边界
//去云
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')
.filterBounds(roi)
.filterDate('2020-01-01','2020-12-31')
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 10))
.map(maskS2clouds);
print(dataset)
var rgbVis = {
min: 0.0,
max: 0.3,
bands: ['B4', 'B3', 'B2'],
};
// Map.addLayer(dataset.mean().clip(roi), rgbVis, 'dataset');
Map.centerObject(roi,7)
//显示roi
var styling = {color:"red",fillColor:"00000000"};
Map.addLayer(roi.style(styling),{},"boundary")
function exportImageCollection(imgCol) {
var indexList = imgCol.reduceColumns(ee.Reducer.toList(), ["system:index"]).get("list");
indexList.evaluate(function(indexs) {
for (var i=0; i<indexs.length; i++) {
var image = imgCol.filter(ee.Filter.eq("system:index", indexs[i])).first();
// image = image.toInt16();
image = image.clip(roi);
Map.addLayer(image, rgbVis, indexs[i]);//查看具体的遥感影像
//tif数据下载
Export.image.toDrive({
image: image,
description: 'sentinel_'+indexs[i],
fileNamePrefix: 'sentinel_'+indexs[i],
folder: 'sentinel',
region: roi,
scale: 10,
crs: "EPSG:4326",
maxPixels: 1e13
});
}
});
}
exportImageCollection(dataset);
注意:分辨率为10m
代码如下(以鹿邑县为例):
//以鹿邑县为例子
var geometry = ee.FeatureCollection('users/www1573979951/luyixian')
Map.centerObject(geometry,7)
//颜色设置
var colorizedVis = {
min: -0.8,
max: 0.8,
palette: ['blue', 'white', 'green'],
};
//使用QA波段去云
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).set(image.toDictionary(image.propertyNames()));
}
//NDVI的计算公式
function createNDVI(image){
var ndvi = image.normalizedDifference(["B8","B4"]).rename('NDVI');
return image.addBands(ndvi);
}
//特别注意的是,在数学变换之后,保持原始影像的属性,所以这里.set(image.toDictionary(image.propertyNames()));
var S2_COL = ee.ImageCollection("COPERNICUS/S2")
.filterDate("2020-01-01", "2020-12-31")
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',20))
.filterBounds(geometry)
.map(maskS2clouds)
.map(createNDVI)
.select('NDVI');
print(S2_COL)
Map.addLayer(S2_COL.median().clip(geometry), colorizedVis, 'col');
var years = ee.List.sequence(2020, 2020);
var months = ee.List.sequence(1, 12);
var S2_monthlymeanNDVI = ee.ImageCollection.fromImages(
years.map(function (y) {
return months.map(function(m) {
return S2_COL.filter(ee.Filter.calendarRange(y,y, 'year')).filter(ee.Filter.calendarRange(m, m, 'month')).mean().set('year', y).set('month', m).set('system:time_start', ee.Date.fromYMD(y, m, 1));
});
}).flatten()
);
// Create a monthly time series chart.
var plotNDVI = ui.Chart.image.seriesByRegion(S2_monthlymeanNDVI, geometry,ee.Reducer.mean(),
'NDVI',500,'system:time_start')
.setChartType('LineChart').setOptions({
interpolateNulls: true,
title: 'NDVI Monthly time series',
hAxis: {title: 'Date'},
vAxis: {title: 'NDVI',viewWindowMode: 'explicit', viewWindow: {max: 0.7,min: 0.3,},gridlines: {count: 10,}},
trendlines: { 0: {title: 'NDVI_trend',type:'linear', showR2: true, color:'red', visibleInLegend: true}}});
// Display.
print(plotNDVI);
注意:分辨率为10m
代码如下(以鹿邑县为例):
//以鹿邑县为例子
var geometry = ee.FeatureCollection('users/www1573979951/luyixian')
Map.centerObject(geometry,7)
//颜色设置
var colorizedVis = {
min: -0.8,
max: 0.8,
palette: ['blue', 'white', 'green'],
};
//使用QA波段去云
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).set(image.toDictionary(image.propertyNames()));
}
//NDVI的计算公式
function createNDVI(image){
var ndvi = image.normalizedDifference(["B8","B4"]).rename('NDVI');
return image.addBands(ndvi);
}
//特别注意的是,在数学变换之后,保持原始影像的属性,所以这里.set(image.toDictionary(image.propertyNames()));
var S2_COL = ee.ImageCollection("COPERNICUS/S2")
.filterDate("2020-01-01", "2020-12-31")
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',20))
.filterBounds(geometry)
.map(maskS2clouds)
.map(createNDVI)
.select('NDVI');
// Map.addLayer(S2_COL.mean().clip(geometry), colorizedVis, 'col');
var years = ee.List.sequence(2020, 2020);
var months = ee.List.sequence(1, 12);
var S2_monthlymeanNDVI = ee.ImageCollection.fromImages(
years.map(function (y) {
return months.map(function(m) {
return S2_COL.filter(ee.Filter.calendarRange(y,y, 'year')).filter(ee.Filter.calendarRange(m, m, 'month')).mean().set('year', y).set('month', m).set('system:time_start', ee.Date.fromYMD(y, m, 1));
});
}).flatten()
);
print(S2_monthlymeanNDVI)
function exportImageCollection(imgCol) {
var indexList = imgCol.reduceColumns(ee.Reducer.toList(), ["system:index"]).get("list");
indexList.evaluate(function(indexs) {
for (var i=0; i<indexs.length; i++) {
var image = imgCol.filter(ee.Filter.eq("system:index", indexs[i])).first();
Map.addLayer(image.clip(geometry), colorizedVis, 'sentinel_10_ndvi_2020_'+indexs[i+1]);
//tif数据下载
Export.image.toDrive({
image: image.clip(geometry),
description: 'sentinel_10_ndvi_2020_'+indexs[i+1],
fileNamePrefix: 'sentinel_10_ndvi_2020_'+indexs[i+1],
folder: 'sentinel_10',
region: geometry,
scale: 10,
crs: "EPSG:4326",
maxPixels: 1e13
});
}
});
}
exportImageCollection(S2_monthlymeanNDVI);
代码如下(以鹿邑县9月份为例)
var roi = ee.FeatureCollection('users/www1573979951/luyixian');; //table为自己上传的矢量边界
//去云
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')
.filterBounds(roi)
.filterDate('2020-09-01','2020-09-30')//自己更改时间时间获取影像
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 10))
.map(maskS2clouds);
print(dataset)
var rgbVis = {
min: 0.0,
max: 0.3,
bands: ['B4', 'B3', 'B2'],
};
Map.addLayer(dataset.mean().clip(roi), rgbVis, 'dataset');
Map.centerObject(roi,7)
//显示roi
var styling = {color:"red",fillColor:"00000000"};
Map.addLayer(roi.style(styling),{},"boundary")
//tif数据下载
Export.image.toDrive({
image: dataset.mean().clip(roi),
description: 'sentinel_09',
fileNamePrefix: 'sentinel_09',
folder: 'sentinel',
region: roi,
scale: 10,
crs: "EPSG:4326",
maxPixels: 1e13
});