var sample_point = ee.FeatureCollection("projects/ee/assets/sample") //上传的点矢量shp
var testregion = ee.FeatureCollection('projects/ee/assets/testregion')//掩膜的范围
var colorizedVis = {
min: -0.8,
max: 0.8,
palette: ['blue', 'white', 'green'],
};
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()));
}
function createNDVI(image){
var ndvi = image.normalizedDifference(["B8","B4"]).rename('NDVI');
return image.addBands(ndvi);
}
var S2_NDVI = ee.ImageCollection("COPERNICUS/S2")
.filterDate('2022-07-01', '2022-8-31')
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',20))
.filterBounds(testregion)
.map(maskS2clouds)
.map(createNDVI)
.select('NDVI').mean();
Map.addLayer(S2_NDVI, colorizedVis, 'NDVI');
// 提取像元值
var pts = ee.FeatureCollection(sample_point)
var ft1 = S2_NDVI.sampleRegions({
collection:pts,
properties:ee.List(["FID","lon","lat"]),
scale:10,
geometries:true
});
print('feature point1', ft1)
Export.table.toDrive({
collection: ft1,
folder:'graduate',
description: 'S2NDVI',
fileFormat: 'CSV',
selectors: ["FID","lon","lat","NDVI"]
});