基于GEE平台土地类型分类

GEE可以进行大尺度的地物类型分类,主要原理就是根据不同地物类型的光谱反射率的不同,目前地物分类的算法有很多种,比如随机森林算法等等,本文章主要基于随机森林算法进行大尺度地物分类,代码如下所示:

var samples=ee.FeatureCollection(table);
function maskL8sr(image)
{
  var timeStart = image.get('system:time_start');
  var srImageList = ee.ImageCollection(' LANDSAT/LC08/C01/T1_SR')
                  .filterMetadata('system:time_start','equals',timeStart)
                  .toList(5);
  var cloudShadowBitMask = ee.Number(2).pow(3).int();
  var cloudsBitMask = ee.Number(2).pow(5).int();



  var qa = image.select('pixel_qa');


  var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0)
      .and(qa.bitwiseAnd(cloudsBitMask).eq(0));


  return image.updateMask(mask);
}


function maskL7sr(image) 
{
  var timeStart = image.get('system:time_start');
  var srImageList = ee.ImageCollection('LANDSAT/LE07/C01/T1_SR')
                      .filterMetadata('system:time_start','equals',timeStart)
                      .toList(5);
                       
  var qa = image.select('pixel_qa');

  var cloud = qa.bitwiseAnd(1 << 5)
          .and(qa.bitwiseAnd(1 << 7))
          .or(qa.bitwiseAnd(1 << 3))
  var mask = image.mask().reduce(ee.Reducer.min());
  return image.updateMask(cloud.not()).updateMask(mask);
}

function maskL5sr(image) 
{
  var timeStart = image.get('system:time_start');
  var srImageList = ee.ImageCollection('LANDSAT/LT05/C01/T1_SR')
                      .filterMetadata('system:time_start','equals',timeStart)
                      .toList(5);
  var qa = image.select('pixel_qa');
  var cloud = qa.bitwiseAnd(1 << 5)
          .and(qa.bitwiseAnd(1 << 7))
          .or(qa.bitwiseAnd(1 << 3))
  var mask = image.mask().reduce(ee.Reducer.min());
  return image.updateMask(cloud.not()).updateMask(mask);
}

function ND_VI(image,b1,b2,bName)
{
  var VI = image.normalizedDifference([b1,b2]).rename(bName);
  return VI.updateMask(VI.gt(-1).and(VI.lt(1)));
}

function funEVI(image,B1,B2,B3)
{
 
	var VI = image.expression('2.5 * (nir - red) / (nir + 6 * red - 7.5 * blue + 1)',
    {
      blue: image.select(B1).multiply(0.0001),   
      red:  image.select(B2).multiply(0.0001),   
      nir:  image.select(B3).multiply(0.0001)
    }).rename('EVI');
  return VI.updateMask(VI.gt(-1).and(VI.lt(1)));

}

function addLandsatVIs(img)
{
  var NDVI = ND_VI(img,'B4','B3','NDVI');
  var EVI = funEVI(img,'B1','B3','B4');
  var LSWI = ND_VI(img,'B4','B5','LSWI');
  return img.addBands(NDVI).addBands(EVI).addBands(LSWI);
}

var collection_L8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
    .filterBounds(region)
    .filterDate('2013-01-01','2020-01-01')
    .map(maskL8sr)
    .select( 
      ['B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'pixel_qa']
     ,['B1', 'B2', 'B3', 'B4', 'B5', 'B7', 'pixel_qa'])
    .map(addLandsatVIs);

var collection_L7 = ee.ImageCollection('LANDSAT/LE07/C01/T1_SR')
    .filterBounds(region)
    .filterDate('1999-01-01','2020-01-01')
    .map(maskL7sr)
    .select( 
      ['B1', 'B2', 'B3', 'B4', 'B5', 'B7', 'pixel_qa'])
    .map(addLandsatVIs);

var collection_L5 = ee.ImageCollection('LANDSAT/LT05/C01/T1_SR')
    .filterBounds(region)
    .filterDate('1988-01-01','2012-01-01')
    .map(maskL5sr)
    .select( 
      ['B1', 'B2', 'B3', 'B4', 'B5', 'B7', 'pixel_qa'])
    .map(addLandsatVIs);

var collection=ee.ImageCollection(collection_L5.merge(collection_L7).merge(collection_L8));


var LandSatCollection=collection.filterDate('2018-01-01','2019-12-31').filterBounds(region)

Map.addLayer(LandSatCollection.median().clip(region),{min:0,max:3000,bands:['B4','B3','B2']},'Landsat2019')
Map.centerObject(region,7)
var sampleData = samples.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 data=ee.Image.cat(LandSatCollection.median())


var training = data.sampleRegions({
  collection: sample_training, 
  properties: ["attribute"], 
  scale: 30
});

var validation = data.sampleRegions({
  collection: sample_validate, 
  properties: ["attribute"], 
  scale: 30
});


var classifier = ee.Classifier.randomForest(40)
    .train({
      features: training, 
      classProperty: 'attribute', 
      inputProperties: data.bandNames()
    });




var Classified_RF = data.classify(classifier);

Map.addLayer(Classified_RF.clip(region), {min:0,max:3,palette:['red','green','orange','blue']},  'Classified_RF');


var validated = validation.classify(classifier);
var testAccuracy = validated.errorMatrix('attribute', 'classification');
var accuracy = testAccuracy.accuracy();
var userAccuracy = testAccuracy.consumersAccuracy();
var producersAccuracy = testAccuracy.producersAccuracy();
var kappa = testAccuracy.kappa();
print('Validation error matrix:', testAccuracy);
print('Validation overall accuracy:', accuracy);
print('User acc:', userAccuracy);
print('Prod acc:', producersAccuracy);
print('Kappa:', kappa);

其原理主要是基于随机森林算法和光谱反射率及遥感指数进行处理,进而进行分类研究。

你可能感兴趣的:(GEE学习交流)