GEE随机森林分类代码(L5L8)

首先,显示图像来打点分类

Map.centerObject(geometry,10);
Map.addLayer(geometry);
var s2 = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
                  .filterDate('2020-11-01', '2021-02-28')
                  .filterBounds(geometry)
                  .filter(ee.Filter.lte('CLOUD_COVER',5))//云量设置
                   .median();
var visualization = {
  min: 0.0,
  max: 60000,
  bands: ['SR_B5', 'SR_B4', 'SR_B3'],
};
var clip_L8 = s2.clip(geometry)
Map.addLayer(s2.clip(geometry), visualization, '假彩色');
// 导出裁剪后的影像到 Google Drive
Export.image.toDrive({
  image: clip_L8,
  description: 'clipped_image',
  folder: 'your_folder_name',  // 替换为你想保存影像的 Google Drive 文件夹
  scale: 30,  // 替换为你想要的分辨率
  region: geometry,
  maxPixels: 1e13,
  crs: 'EPSG:4326'  // 替换为你的投影系统
});    

我的分类

Landsat5适用 
Map.centerObject(geometry,10);
Map.addLayer(geometry);
var s2 = ee.ImageCollection('LANDSAT/LT05/C02/T1_L2')
                  .filterDate('2010-11-18', '2010-11-20')
                  .filterBounds(geometry)
                  .filter(ee.Filter.lte('CLOUD_COVER',5))//云量设置
var rgbVis = {
  min: 0.0,
  max: 60000,
  bands: ['SR_B4', 'SR_B3', 'SR_B2'],
};
//Map.addLayer(s2.clip(geometry), rgbVis, '假彩色');
var add_RS_index = function(img){
  var ndvi = img.normalizedDifference(['SR_B4', 'SR_B3']).rename('NDVI').copyProperties(img,['system:time_start']);
  var ndwi = img.normalizedDifference(['SR_B2', 'SR_B4']).rename('NDWI').copyProperties(img,['system:time_start']);
  var ndmi = img.expression('(SWIR1 - GREEN) / (SWIR1 + GREEN)', 
  {
    'SWIR1': img.select('SR_B5'),
    'GREEN': img.select('SR_B2')
  }).rename('NDMI').copyProperties(img,['system:time_start']);
  // Remove BSI and EVI
  var ibi = img.expression('(2 * SWIR1 / (SWIR1 + NIR) - (NIR / (NIR + RED) + GREEN / (GREEN + SWIR1))) / (2 * SWIR1 / (SWIR1 + NIR) + (NIR / (NIR + RED) + GREEN / (GREEN + SWIR1)))', {
    'SWIR1': img.select('SR_B5'),
    'NIR': img.select('SR_B4'),
    'RED': img.select('SR_B3'),
    'GREEN': img.select('SR_B2')
  }).rename('IBI').copyProperties(img,['system:time_start']);
  // Add NDSSI and NDBI
  var ndssi = img.expression('(GREEN - SWIR1) / (GREEN + SWIR1)', 
  {
    'GREEN': img.select('SR_B2'),
    'SWIR1': img.select('SR_B5')
  }).rename('NDSSI').copyProperties(img,['system:time_start']);
  var ndbi = img.normalizedDifference(['SR_B4', 'SR_B7']).rename('NDBI').copyProperties(img,['system:time_start']);
  return img.addBands([ndvi, ndwi, ndmi, ibi, ndssi, ndbi]);
};
// Apply the function to the image collection
var s2_with_indices = s2.map(add_RS_index);
// 从图像集合中选择波段并计算中位数
var bands = ['SR_B2','SR_B3', 'SR_B4', 'SR_B5', 'SR_B7', 'NDVI', 'NDWI', 'NDSSI'];
var imgcol_median = s2_with_indices.select(bands).median();
// 处理 DEM 数据
var aoi_dem = dem.select('elevation').clip(geometry).rename('DEM');
var construct_img = imgcol_median.addBands(aoi_dem).clip(geometry);
var fc = vegetation.merge(water).merge(Tidalflat).merge(other).merge(spartina).merge(nomalforest).merge(plowland);
var train_data=construct_img.sampleRegions({
  collection:fc,
  properties: ['landcover'],
  scale: 10
});
print(fc);
var sampleData =train_data.randomColumn('random')
var sample_training = sampleData.filter(ee.Filter.lte("random", 0.8));
var sample_validate  =sampleData.filter(ee.Filter.gt("random", 0.8));
var rf = ee.Classifier.smileRandomForest({
  numberOfTrees: 20,
  bagFraction: 0.8
}).train({
  features: sample_training,
  classProperty: 'landcover',
  inputProperties: bands
});
var img_classfication = construct_img.classify(rf); 
//运用测试样本分类,确定要进行函数运算的数据集以及函数
var test =sample_validate.classify(rf);
//计算混淆矩阵
var confusionMatrix = test.errorMatrix('landcover', 'classification');
print('confusionMatrix',confusionMatrix);//面板上显示混淆矩阵
print('overall accuracy', confusionMatrix.accuracy());//面板上显示总体精度
print('kappa accuracy', confusionMatrix.kappa());//面板上显示kappa值
print('consumersAccuracy', confusionMatrix.consumersAccuracy());
print('producersAccuracy', confusionMatrix.producersAccuracy());
Map.centerObject(geometry)
Map.addLayer(geometry);
Map.addLayer(img_classfication.clip(geometry), {min: 1, max: 7, palette: ['blue', 'green', 'yellow', 'purple', 'orange', 'pink', 'brown',]});
var class1=img_classfication.clip(geometry)
//导出分类图
Export.image.toDrive({
       image: class1,
       description: 'rfclass',
       fileNamePrefix: 'rf',  //文件命名
       folder: "class",  //保存的文件夹
       scale: 10,  //分辨率
       region: geometry,  //研究区
       maxPixels: 1e13,  //最大像元素,默认就好
       crs: "EPSG:4326"  //设置投影
   });  
Landsat8适用
Map.centerObject(geometry,10);
Map.addLayer(geometry);
var s2 = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
                  .filterDate('2015-10-15', '2015-10-18')
                  .filterBounds(geometry)
                  .filter(ee.Filter.lte('CLOUD_COVER',6))//云量设置
var rgbVis = {
  min: 0.0,
  max: 60000,
  bands: ['SR_B5', 'SR_B4', 'SR_B3'],
};
//Map.addLayer(s2.clip(geometry), rgbVis, '假彩色');
var add_RS_index = function(img){
  var ndvi = img.normalizedDifference(['SR_B5', 'SR_B4']).rename('NDVI').copyProperties(img,['system:time_start']);
  var ndwi = img.normalizedDifference(['SR_B3', 'SR_B5']).rename('NDWI').copyProperties(img,['system:time_start']);
  var ndmi = img.expression('(SWIR1 - GREEN) / (SWIR1 + GREEN)', 
  {
    'SWIR1': img.select('SR_B6'),
    'GREEN': img.select('SR_B3')
  }).rename('NDMI').copyProperties(img,['system:time_start']);
  // Remove BSI and EVI
  var ibi = img.expression('(2 * SWIR1 / (SWIR1 + NIR) - (NIR / (NIR + RED) + GREEN / (GREEN + SWIR1))) / (2 * SWIR1 / (SWIR1 + NIR) + (NIR / (NIR + RED) + GREEN / (GREEN + SWIR1)))', {
    'SWIR1': img.select('SR_B6'),
    'NIR': img.select('SR_B5'),
    'RED': img.select('SR_B4'),
    'GREEN': img.select('SR_B3')
  }).rename('IBI').copyProperties(img,['system:time_start']);
  // Add NDSSI and NDBI
  var ndssi = img.expression('(GREEN - SWIR1) / (GREEN + SWIR1)', 
  {
    'GREEN': img.select('SR_B3'),
    'SWIR1': img.select('SR_B6')
  }).rename('NDSSI').copyProperties(img,['system:time_start']);
  var ndbi = img.normalizedDifference(['SR_B5', 'SR_B7']).rename('NDBI').copyProperties(img,['system:time_start']);
  return img.addBands([ndvi, ndwi, ndmi, ibi, ndssi, ndbi]);
};
// Apply the function to the image collection
var s2_with_indices = s2.map(add_RS_index);
// 从图像集合中选择波段并计算中位数
var bands = ['SR_B3', 'SR_B4', 'SR_B5', 'SR_B6', 'SR_B7', 'NDVI', 'NDWI', 'NDSSI'];
var imgcol_median = s2_with_indices.select(bands).median();
// 处理 DEM 数据
var aoi_dem = dem.select('elevation').clip(geometry).rename('DEM');
var construct_img = imgcol_median.addBands(aoi_dem).clip(geometry);
var fc = vegetation.merge(water).merge(Tidalflat).merge(other).merge(spartina).merge(nomalforest).merge(plowland);
var train_data=construct_img.sampleRegions({
  collection:fc,
  properties: ['landcover'],
  scale: 10
});
print(fc);
var sampleData =train_data.randomColumn('random')
var sample_training = sampleData.filter(ee.Filter.lte("random", 0.8));
var sample_validate  =sampleData.filter(ee.Filter.gt("random", 0.8));
var rf = ee.Classifier.smileRandomForest({
  numberOfTrees: 20,
  bagFraction: 0.8
}).train({
  features: sample_training,
  classProperty: 'landcover',
  inputProperties: bands
});
var img_classfication = construct_img.classify(rf); 
//运用测试样本分类,确定要进行函数运算的数据集以及函数
var test =sample_validate.classify(rf);
//计算混淆矩阵
var confusionMatrix = test.errorMatrix('landcover', 'classification');
print('confusionMatrix',confusionMatrix);//面板上显示混淆矩阵
print('overall accuracy', confusionMatrix.accuracy());//面板上显示总体精度
print('kappa accuracy', confusionMatrix.kappa());//面板上显示kappa值
print('consumersAccuracy', confusionMatrix.consumersAccuracy());
print('producersAccuracy', confusionMatrix.producersAccuracy());
Map.centerObject(geometry)
Map.addLayer(geometry);
Map.addLayer(img_classfication.clip(geometry), {min: 1, max: 7, palette: ['blue', 'green', 'yellow', 'purple', 'orange', 'pink', 'brown']});
var class1=img_classfication.clip(geometry)
//导出分类图
Export.image.toDrive({
       image: class1,
       description: 'rfclass',
       fileNamePrefix: 'rf',  //文件命名
       folder: "class",  //保存的文件夹
       scale: 10,  //分辨率
       region: geometry,  //研究区
       maxPixels: 1e13,  //最大像元素,默认就好
       crs: "EPSG:4326"  //设置投影
   });  

可能报错及解决:

运行后显示Property 'B2' of feature '1_1_1_1_1_1_1_1_0' is missing 

云量限制过大,放宽条件

 参考波段

 

 参考:

当时前一部分就是按照b站这个老哥来做的,后面他没录上,参考了第二个姐的代码

【google earth engine 随机森林分类详细流程】 https://www.bilibili.com/video/BV18v411a78D/?share_source=copy_web&vd_source=cf8beea9c5503f5fd4699cbf701fc2cf

GEE实现图像随机森林分类 - 简书 (jianshu.com)

【No.9 【Google Earth Engine】GEE 基于landsat8 监督分类土地利用并下载】 https://www.bilibili.com/video/BV1YY411s7tS/?share_source=copy_web&vd_source=cf8beea9c5503f5fd4699cbf701fc2cf

  • 12
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值