GEE学习-2022.5.13
本次代码来源于微信公众号-GEEer成长日记六十六:基于RF算法识别水稻种植区域
/**
* 去云函数
* 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
*/
// 引入感兴趣区
var ROI = ee.FeatureCollection(users/...);
// 需要提前上传样本点或者直接在GEE里面选择样本点
//直接在GEE里面选取样本点的方法可以参考
[利用GEE code编辑器直接选取样本点](https://blog.csdn.net/chenguizhenaza/article/details/111911232)
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 S2_SR = ee.ImageCollection('COPERNICUS/S2_SR')
.filterDate('2021-04-20', '2021-06-20')
.filterBounds(ROI)
// Pre-filter to get less cloudy granules.
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',40))
.map(maskS2clouds)
.map(function(image){return image.clip(ROI)});
var S2_spring_med= S2_SR.mosaic() // 直接合成
print(S2_spring_med, 'S2_spring_med')
// Display the mosaic.
Map.addLayer(S2_spring_med, imageVisParam, 'SWIR');//这里报错,在删除imageVisParam, 'SWIR'后才正常,没有搞清楚原因。
// 选择用于光谱识别的特征
var bands = ['B1','B4', 'B8', 'B8A', 'B11','B12'];
var bands1 = ['B1','B2', 'B3', 'B4','B5','B6','B7','B8', 'B8A','B9', 'B11','B12'];
var imageCl = S2_spring_med.select(bands);
// 样本点合并
var trainingPts = water_rice.merge(non_water_rice);
print('mergedtrainingPts' ,trainingPts );
// Display training points
Map.addLayer(trainingPts, {color: 'red'}, 'trainingPts');
// 依旧是采集样本点的光谱特征
var training = imageCl
.sampleRegions({
collection: trainingPts,
properties: ['class'],
scale: 10,
tileScale: 8
})
.filter(ee.Filter.neq(
'B1', null)); // Remove null pixels.
var withRandom = training.randomColumn('random');
var split = 0.7; // Roughly 70% training, 30% testing.
var trainingPartition = withRandom.filter(ee.Filter.lt('random', split));
var testingPartition = withRandom.filter(ee.Filter.gte('random', split));
// 随机树的数目为40
var trainedClassifier = ee.Classifier.smileRandomForest({numberOfTrees:40}).train({
features: trainingPartition,
classProperty: 'class',
inputProperties: bands
});
// Classify the test FeatureCollection.
var validated_RF = testingPartition.classify(trainedClassifier);
var classified_RF = imageCl.select(bands).classify(trainedClassifier);
var vis_RF = {min: 0, max: 1,
palette: [ 'green' //0
,'white']//1
}
Map.addLayer(classified_RF, vis_RF, 'Classified');
// Calculate area of each class (based on RF) in square meters.
var areaImage = ee.Image.pixelArea().divide(1000000).addBands(classified_RF);
var areas = areaImage.reduceRegion({
reducer: ee.Reducer.sum().group({
groupField: 1,
groupName: 'class',
}),
geometry: ROI.geometry(),
scale: 500,
maxPixels: 1e13,
tileScale: 8
});
// Print the area calculations.
print('##### CLASS AREA SQ. METERS #####');
print(areas);
案例中的样本最好是点数据,如果是面数据的话,运行时会出现内存溢出的报错。但是可以通过修改tileScale的值来进行改变,tileScale范围为0-16。