植被覆盖度(FVC)的计算是遥感上非常重要的一个研究领域,因事务繁忙,今天小编先带来FVC的全文代码,给大家试试,后期会出一期专栏进行代码的详细介绍:

今天福利满满,直接上代码,修改研究区域即可用。

var region=ee.FeatureCollection('users/hesuixinya511/SouthWest_China');
var empty = ee.Image().toByte();
var outline = ee.Image()
                .toByte()
                .paint({
                  featureCollection:region,
                  color:0,
                  width:2
                });
Map.addLayer(outline, {palette: "black"}, "outline");
Map.centerObject(region,6);
var MOD13=ee.ImageCollection("MODIS/006/MOD13Q1")
                 .select('NDVI')
                 .filterBounds(region)
                 .filter(ee.Filter.calendarRange(6, 8, 'month'))
                 .filter(ee.Filter.calendarRange(2020, 2020, 'year'));
var NDVI=MOD13.select('NDVI').mean().clip(region).divide(10000);
var Vis = {
  min: 0.0,
  max: 1.0,
  palette: [
    'FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901',
    '66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01',
    '012E01', '011D01', '011301'
  ],
};
Map.addLayer(NDVI,Vis,'NDVI');
function FVC_Cal_MOD (img){
  var dict = img.reduceRegion({
                    reducer: ee.Reducer.percentile([5, 95]),
                    geometry: region,
                    scale: 500,
                    maxPixels: 1e13,
                    tileScale: 16
                  });
  var _p5 = ee.Number(dict.get("NDVI_p5"));
  var _p95 = ee.Number(dict.get("NDVI_p95"));
  var imgFVC = ((img.subtract(_p5)).divide(_p95.subtract(_p5))).float();
  var FVC=((imgFVC.lt(0)).multiply(0))
  .add(((imgFVC.gt(0)).and(imgFVC.lte(1))).multiply(imgFVC))
  .add((imgFVC.gt(1)).multiply(1));
  return FVC.rename("FVC"); 
}
function addFVCbands(img){
  var FVC=FVC_Cal_MOD (img);
  return img.addBands(FVC);
}
var FVCcollection=MOD13.select('NDVI').map(addFVCbands);
print(FVCcollection,'FVCcolle');
var FVC=FVCcollection.mean().select('FVC').clip(region);
Map.addLayer(FVC,Vis,'FVC');
function getHistogram(Img,bandname,geometry) {
  var hist = Img.reduceRegion({
    reducer: ee.Reducer.autoHistogram(),
    geometry: geometry,
    scale: 250,
    maxPixels: 1e13,
  });
  var histArray = ee.Array(hist.get(bandname));
  var binBottom = histArray.slice(1, 0, 1);
  var nPixels = histArray.slice(1, 1, null);
  var histColumnFromArray =
    ui.Chart.array.values({array: nPixels, axis: 0, xLabels: binBottom})
      .setChartType('LineChart')
      .setOptions({
        title: bandname+' histogram',
        hAxis: {title: bandname},
        vAxis: {title: 'Pixel count'},
        pointSize: 0,
        lineSize: 2,
        colors: ['1b7837'],
        legend: {position: 'none'}
      });
  return histColumnFromArray;
}
var NDVI_histogram=(getHistogram(NDVI,'NDVI',region));
var FVC_histogram=(getHistogram(FVC,'FVC',region));
print(NDVI_histogram);
print(FVC_histogram);
var FVC_Class=FVC.where(FVC.lt(0.1),1)
                 .where(((FVC.gte(0.1)).and(FVC.lt(0.3))),2)
                 .where(((FVC.gte(0.3)).and(FVC.lt(0.5))),3)
                 .where(((FVC.gte(0.5)).and(FVC.lt(0.7))),4)
                 .where(FVC.gt(0.7),5);
Map.addLayer(FVC_Class,{min:1,max:5,palette:['DCDCDC','FFEBCD','99B718','529400','011301']},'FVC_Class');
// 添加图例
function addLegend(palette, names) {
var panel = ui.Panel();
panel.style().set({
  width: '200px',
  position: 'bottom-right'
});
var intro = ui.Panel([
  ui.Label({
    value: '植被覆盖度等级',
    style: {fontSize: '20px', fontWeight: 'bold'}
  }),
]);
panel.add(intro);
Map.add(panel);
// 添加每一列图例颜色以及说明
var addLegendLabel = function(color, name) {
        var showColor = ui.Label({
          style: {
            backgroundColor: color,
            padding: '8px',
            margin: '0 0 8px 0'
          }
        });
        var desc = ui.Label({
          value: name,
          style: {margin: '0 0 4px 4px'}
        });
        //颜色和说明是水平放置
        return ui.Panel({
          widgets: [showColor, desc],
          layout: ui.Panel.Layout.Flow('horizontal')
        });
  };
  //添加所有的图例列表
  for (var i = 0; i < palette.length; i++) {
    var label = addLegendLabel(palette[i], names[i]); 
    panel.add(label);
  }  
//  ui.root.insert(0, legend);
}
var palette = ['DCDCDC','FFEBCD','99B718','529400','011301'];
var names = ["(I) 低覆盖度","(II) 较低覆盖度","(III) 中等覆盖度","(IV) 较高覆盖度","(V) 高覆盖度"];
addLegend(palette, names);
var dict = ee.Image.pixelArea()  
            .addBands(FVC_Class.int())  
            .reduceRegion({  
              reducer:ee.Reducer.sum().group({  
                groupField:1,  
                groupName:'type',  
              }),  
              geometry:region,  
              scale:250,  
              maxPixels:1e13  
            });  
var groups = ee.List(dict.get("groups"));  
var typeNames = ee.List(["I", "II","III","IV","V"]);  
var featureList = groups.map(function(group){  
  group = ee.Dictionary(group);  
  var area = ee.Number(group.get("sum"));  
  area = area.divide(1000000);  //km2
  var type = ee.Number(group.get("type"));  
  var f = ee.Feature(null, {  
    "type": type,   
    "area": area,   
    "name": typeNames.get(type.subtract(1))
  });  
  return f;  
});  
var areaFCol = ee.FeatureCollection(featureList);  
var totalArea = ee.Number(areaFCol.aggregate_sum("area"));  
areaFCol = areaFCol.map(function(f){  
  var typearea = ee.Number(f.get("area"));  
  f = f.set("type_area", typearea);  
  return f;  
});  
var Area_table=(ui.Chart.feature.byFeature(areaFCol.select(['name','type_area']),
    'name')
  .setChartType('Table'));
print(Area_table)

看看结果吧!还顺便帮大家统计了面积和像元直方图哦! 很不错呦!

GEE必须会教程—植被覆盖度(FVC)计算(代码分享)-LMLPHP

希望对大家有帮助,有不懂的大家可以私信联系小编,也可以期待后续小编推出的的详细博客,本期就先到这里了,谢谢大家支持!

05-02 08:50