Google Earth Engine (GEE)——Based on two phases of sentinel2 remote sensing images, using the random forest method to analyze the statistical results of the area of each class in land classification

Last time, we analyzed how to obtain the image results of land type area conversion. This time, we will perform the statistical calculation of the area after the land type conversion. There are mainly two area statistical calculations. The second is to count the area in the land class. Some functions will be used during the period, as follows:

The link address of the previous article:

https://mp.csdn.net/mp_blog/creation/editor/131506639

function:

ee.Reducer.frequencyHistogram()

Returns a Reducer that returns a (weighted) frequency table of its inputs.

Returns a Reducer that returns a (weighted) frequency table of its input. 

No arguments.

Returns: Reducer

ee.Image.pixelArea()

Generate an image in which the value of each pixel is the area of that pixel in square meters. The returned image has a single band called "area."

Returns a section with an area property

No arguments.

Returns: Image

ee.Reduce.sum()

Returns a Reducer that computes the (weighted) sum of its inputs.

No arguments.

Returns: Reducer

group(groupFieldgroupName)

Groups reducer records by the value of a given input, and reduces each group with the given reducer.

To count the data of a group, you need to use the group function, which will perform statistical analysis on each of them.

Arguments:

this:reducer (Reducer):

The reducer to apply to each group, without the group field.

groupField (Integer, default: 0):

The field that contains record groups.

groupName (String, default: "group"):

The dictionary key that contains the group. Defaults to 'group'.

Returns: Reducer

code:

var bangalore = ee.FeatureCollection('users/ujavalgandhi/public/bangalore_boundary');
var urban = ee.FeatureCollection('users/ujavalgandhi/e2e/urban_gcps');
var bare = ee.FeatureCollection('users/ujavalgandhi/e2e/bare_gcps');
var water = ee.FeatureCollection('users/ujavalgandhi/e2e/water_gcps');
var vegetation = ee.FeatureCollection('users/ujavalgandhi/e2e/vegetation_gcps');
var s2 = ee.ImageCollection('COPERNICUS/S2_SR');
var geometry = bangalore.geometry();
Map.centerObject(geometry);


var rgbVis = {
  min: 0.0,
  max: 3000,
  bands: ['B4', 'B3', 'B2'], 
};

// 2019 Jan
var filtered = s2
  .filter(ee.Filter.date('2019-01-01', '2019-02-01'))
  .filter(ee.Filter.bounds(geometry))
  .select('B.*');

  
var before = filtered.median().clip(geometry);
// Display the input composite.
Map.addLayer(before, rgbVis, 'before');

var training = urban.merge(bare).merge(water).merge(vegetation);

// Overlay the point on the image to get training data.
var training = before.sampleRegions({
  collection: training, 
  properties: ['landcover'], 
  scale: 10
});

// Train a classifier.
var classifier = ee.Classifier.smileRandomForest(50).train({
  features: training,  
  classProperty: 'landcover', 
  inputProperties: before.bandNames()
});

// // Classify the image.
var beforeClassified = before.classify(classifier);
Map.addLayer(beforeClassified,
  {min: 0, max: 3, palette: ['gray', 'brown', 'blue', 'green']}, 'before_classified');


// 2020 Jan
var after = s2
  .filter(ee.Filter.date('2020-01-01', '2020-02-01'))
  .filter(ee.Filter.bounds(geometry))
  .select('B.*')
  .median()
  .clip(geometry);

Map.addLayer(after, rgbVis, 'after');

// Classify the image.
var afterClassified= after.classify(classifier);
Map.addLayer(afterClassified,
  {min: 0, max: 3, palette: ['gray', 'brown', 'blue', 'green']}, 'after_classified');

// Reclassify from 0-3 to 1-4
var beforeClasses = beforeClassified.remap([0, 1, 2, 3], [1, 2, 3, 4]);
var afterClasses = afterClassified.remap([0, 1, 2, 3], [1, 2, 3, 4]);

// Show all changed areas
var changed = afterClasses.subtract(beforeClasses).neq(0);
Map.addLayer(changed, {min:0, max:1, palette: ['white', 'red']}, 'Change');

// 我们将之前的图像乘以 100,然后添加之后的图像
// 生成的像素值将是唯一的,并且代表每个唯一的过渡
// 即 102 是城市到裸露,103 是城市到水等。
var merged = beforeClasses.multiply(100).add(afterClasses).rename('transitions');

// 
var transitionMatrix = merged.reduceRegion({
  reducer: ee.Reducer.frequencyHistogram(), 
  geometry: geometry,
  maxPixels: 1e10,
  scale:10,
  tileScale: 16
});
// 使用频率直方图获取每个类别的像素计数
print(transitionMatrix.get('transitions'));

// 如果我们要计算每个类过渡的面积
// 我们可以使用分组reducer

// Divide by 1e6 to get the area in sq.km.
var areaImage = ee.Image.pixelArea().divide(1e6).addBands(merged);
// 按每个过渡类计算面积
// 使用分组Reducer
var areas = areaImage.reduceRegion({
      reducer: ee.Reducer.sum().group({
      groupField: 1,
      groupName: 'transitions',
    }),
    geometry: geometry,
    scale: 100,
    tileScale: 4,
    maxPixels: 1e10
    }); 

// 对结果进行后处理以生成干净的输出
var classAreas = ee.List(areas.get('groups'));
var classAreaLists = classAreas.map(function(item) {
      var areaDict = ee.Dictionary(item);
      var classNumber = ee.Number(areaDict.get('transitions')).format();
      var area = ee.Number(areaDict.get('sum')).round();//取整 
      return ee.List([classNumber, area]);
    });
var classTransitionsAreaDict = ee.Dictionary(classAreaLists.flatten());
print(classTransitionsAreaDict);

The final statistics of the number of pixels and the area of ​​various conversions 

 The final result is an area statistic in which various types of areas are converted from one type to another.

Guess you like

Origin blog.csdn.net/qq_31988139/article/details/131523334