var geometry=table.geometry();
var terrain = ee.Algorithms.Terrain(ee.Image('CGIAR/SRTM90_V4')).clip(geometry);
Map.addLayer(terrain,{bands:['slope'],min:0,max:1},'terrain');
Map.centerObject(table);
//-------------------------------去云 ----------------------------------------------//
function sentinel2toa(img) {
var toa = img.select(['B1','B2','B3','B4','B5','B6','B7','B8','B8A','B9','B10', 'B11','B12'],
['aerosol', 'blue', 'green', 'red', 're1','re2','re3', 'nir','nir2', 'h2o', 'cirrus','swir1', 'swir2'])
.divide(10000)
.addBands(img.select(['QA60']))
.set('solar_azimuth',img.get('MEAN_SOLAR_AZIMUTH_ANGLE'))
.set('solar_zenith',img.get('MEAN_SOLAR_ZENITH_ANGLE'))
return toa;
}
function ESAcloud(toa) {
var qa = toa.select('QA60');
var cloudBitMask = Math.pow(2, 10);
var cirrusBitMask = Math.pow(2, 11);
var clear = qa.bitwiseAnd(cloudBitMask).eq(0).and(
qa.bitwiseAnd(cirrusBitMask).eq(0));
var cloud = clear.eq(0);
return cloud;
}
function shadowMask(toa,cloud){
var azimuth =ee.Number(toa.get('solar_azimuth')).multiply(Math.PI).divide(180.0).add(ee.Number(0.5).multiply(Math.PI));
var zenith =ee.Number(0.5).multiply(Math.PI ).subtract(ee.Number(toa.get('solar_zenith')).multiply(Math.PI).divide(180.0));
var nominalScale = cloud.projection().nominalScale();
var cloudHeights = ee.List.sequence(200,10000,500);
var shadows = cloudHeights.map(function(cloudHeight){
cloudHeight = ee.Number(cloudHeight);
var shadowVector = zenith.tan().multiply(cloudHeight);
var x = azimuth.cos().multiply(shadowVector).divide(nominalScale).round();
var y = azimuth.sin().multiply(shadowVector).divide(nominalScale).round();
return cloud.changeProj(cloud.projection(), cloud.projection().translate(x, y));
});
var potentialShadow = ee.ImageCollection.fromImages(shadows).max();
potentialShadow = potentialShadow.and(cloud.not());
var darkPixels = toa.normalizedDifference(['green', 'swir2']).gt(0.25).rename(['dark_pixels']);
var shadow = potentialShadow.and(darkPixels).rename('shadows');
return shadow;
}
function cloud_and_shadow_mask(img) {
var toa = sentinel2toa(img);
var cloud = ESAcloud(toa);
var shadow = shadowMask(toa,cloud);
var mask = cloud.or(shadow).eq(0);
return toa.updateMask(mask);
}
function get_dately_water(year,mounth) {
var startDate = ee.Date.fromYMD(year, mounth, 1);
var endDate;
if(mounth==2)
endDate = ee.Date.fromYMD(year, mounth, 28);
else
endDate = ee.Date.fromYMD(year, mounth, 30);
/*数LANDSAT/LC08/C01/T1据 */
/* var landsatCollection = ee.ImageCollection("LANDSAT/LC08/C01/T1")
.filterDate(startDate, endDate)
.filterBounds(geometry)
.filterMetadata('CLOUD_COVER_LAND', 'less_than', 30);
// 生成无云的数据集并且合成为一副
var composite = ee.Algorithms.Landsat.simpleComposite({
collection: landsatCollection,
asFloat: true
});*/
/*哨兵数据*/
var composite = ee.ImageCollection('COPERNICUS/S2')
.filterDate(startDate, endDate)
.filterBounds(geometry)
//.filterMetadata('CLOUD_COVER_LAND', 'less_than', 30)
.map(cloud_and_shadow_mask)
.mean();
var img = composite.clip(geometry);
var slope = terrain.select(['slope']);
var sr = ee.Image(img).addBands(slope);
var bands = sr.bandNames();
var training = sr.sampleRegions({
collection: table2,
properties: ['value'],
scale: 5,
tileScale: 6
});
// 创建一个分类回归树
var withRandom = training.randomColumn('random');//样本点随机的排列
// 我们想保留一些数据进行测试,以避免模型过度拟合。
var split = 0.8;
var trainingPartition = withRandom.filter(ee.Filter.lt('random', split));//筛选80%的样本作为训练样本
var testingPartition = withRandom.filter(ee.Filter.gte('random', split));//筛选20%的样本作为测试样本
// 选择分类的属性
var classProperty = 'value';
//分类方法选择smileCart() randomForest() minimumDistance libsvm
var classifier = ee.Classifier.smileRandomForest(10).train({
features: trainingPartition,
classProperty: 'value',
inputProperties: bands
});
// Classify the input imagery.
var classified = sr.classify(classifier);
var test = testingPartition.classify(classifier);//运用测试样本分类,确定要进行函数运算的数据集以及函数
var confusionMatrix = test.errorMatrix('value', 'classification');//计算混淆矩阵
print('时间', year,mounth);
print('confusionMatrix', confusionMatrix);//面板上显示混淆矩阵
//print('consumers accuracy', confusionMatrix.consumersAccuracy());
// print('producers accuracy', confusionMatrix.producersAccuracy());
print('overall accuracy', confusionMatrix.accuracy());//面板上显示总体精度
print('kappa accuracy', confusionMatrix.kappa());//面板上显示kappa值
var result = classified.updateMask(classified.gte(1)).updateMask(slope.lte(10));
//显示分类结果
//Map.addLayer(result, { min: 0, max: 1, palette: ['white', 'blue'] }, year*100+mounth+"SWarter");
Export.image.toDrive({
image: result,
region: table,
// fileDimensions:2560,
scale: 30,
maxPixels: 1e13,
folder: "SWater",
description: year*100+mounth+"SWarter"
});
}
for(var i=1;i<=4;i++){
var mounth =i ;
get_dately_water(2021,mounth);
}