计算2015年土地利用数据各个地类的面积
方法一:面积被打印到了字典中。discrete_classification是土地利用类型的标签,sum是土地利用数据各个地类的面积(m^2)。怎么制作成时间序列的折线图,后续会补充。
var roi = table
var dataset = ee.Image("COPERNICUS/Landcover/100m/Proba-V-C3/Global/2015")
.select('discrete_classification').clip(roi);
Map.centerObject(roi);
Map.addLayer(dataset, {}, "Land Cover");
var area = ee.Image.pixelArea().addBands(dataset)
.reduceRegion({
reducer: ee.Reducer.sum().group({
groupField: 1,
groupName: "discrete_classification"
}),
geometry: roi,
scale: 100,
maxPixels:10e15,
})
print(area)
var dataset = dataset.set({"area":area})
print(dataset)
var roi = table
// 图例代码来源于其他人的博客
function addLegend(palette, names) {
//图例的底层Panel
var legend = ui.Panel({
style: {
position: 'bottom-right',
padding: '5px 10px'
}
});
//图例标题
var title = ui.Label({
value: '图例',
style: {
fontWeight: 'bold',
color: "red",
fontSize: '16px'
}
});
legend.add(title);
//添加每一列图例颜色以及说明
var addLegendLabel = function(color, name) {
var showColor = ui.Label({
style: {
backgroundColor: '#' + color,
padding: '8px',
margin: '0 0 4px 0'
}
});
var desc = ui.Label({
value: name,
style: {margin: '0 0 4px 8px'}
});
//颜色和说明是水平放置
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]);
legend.add(label);
}
Map.add(legend);
}
var dataset = ee.Image("COPERNICUS/Landcover/100m/Proba-V-C3/Global/2015")
.select('discrete_classification').clip(roi);
Map.centerObject(roi);
Map.addLayer(dataset, {}, "Land Cover");
var area = ee.Image.pixelArea().addBands(dataset)
.reduceRegion({
reducer: ee.Reducer.sum().group({
groupField: 1,
groupName: "discrete_classification"
}),
geometry: roi,
scale: 100,
maxPixels:10e15,
})
var dataset = dataset.set({"area":area})
var dataseicol = ee.ImageCollection(dataset);
var area = dataseicol.reduceColumns(ee.Reducer.toList(), ['area'])
.get('list');
var time = dataseicol.reduceColumns(ee.Reducer.toList(), ['system:time_start'])
.get('list');
var feature = ee.List(time).zip(area).map(function(timeArea){
var time = 2015+"year";
var area = ee.List(timeArea).get(1);
area = ee.Dictionary(area).get("groups");
var feature = ee.List(area).map(function(dict){
dict = ee.Dictionary(dict);
var value = dict.values();
return ee.Feature(null, {
time: time,
land:value.get(0),
area:value.get(1)});
});
return feature;
});
feature = feature.flatten();
var feature = ee.FeatureCollection(feature);
var palette = [
'05450a', '086a10', '54a708', '78d203', '009900', 'c6b044', 'dcd159',
'dade48', 'fbff13', 'b6ff05', '27ff87', 'c24f44', 'a5a5a5', 'ff6d4c',
'69fff8', 'f9ffa4', '1c0dff' ,'009900', 'c6b044', 'dcd159','a5a5a5', 'ff6d4c',
]
var names = [ '20 Shrubs',
'30 Herbaceous vegetation',
'40 Cultivated and managed vegetation / agriculture',
'50 Urban / built up',
'60 Bare / sparse vegetation',
'70 Snow and ice',
'80 Permanent water bodies',
'90 Herbaceous wetland',
'100 Moss and lichen',
'111 Closed forest, evergreen needle leaf',
'112 Closed forest, evergreen broad leaf',
'113 Closed forest, deciduous needle leaf',
'114 Closed forest, deciduous broad leaf',
'115 Closed forest, mixed',
'116 Closed forest, not matching any of the other definitions',
'121 Open forest, evergreen needle leaf',
'122 Open forest, evergreen broad leaf',
'123 Open forest, deciduous needle leaf',
'124 Open forest, deciduous broad leaf',
'125 Open forest, mixed.',
'126 Open forest, not matching any of the other definitions',
'200 Oceans, seas']
// 面积统计
var chart = ui.Chart.feature.groups(feature, "land", "area", "time")
.setSeriesNames({20:'Shrubs',
30:'Herbaceous vegetation',
40:'Cultivated and managed vegetation / agriculture',
50:'Urban / built up',
60:'Bare / sparse vegetation',
70:'Snow and ice',
80:'Permanent water bodies',
90:'Herbaceous wetland',
100:'Moss and lichen',
111:'Closed forest, evergreen needle leaf',
112:'Closed forest, evergreen broad leaf',
113:'Closed forest, deciduous needle leaf',
114:'Closed forest, deciduous broad leaf',
115:'Closed forest, mixed',
116:'Closed forest, not matching any of the other definitions',
121:'Open forest, evergreen needle leaf',
122:'Open forest, evergreen broad leaf',
123:'Open forest, deciduous needle leaf',
124:'Open forest, deciduous broad leaf',
125:'Open forest, mixed.',
126:'Open forest, not matching any of the other definitions',
200:'Oceans, seas',
})
.setOptions({
title: 'area vs Landcover',
hAxis: {'title': 'classfication'},
vAxis: {'title': 'area (m^2)'},
pointSize: 3,
colors:['#05450a', '#086a10', '#54a708', '#78d203', '#009900', '#c6b044', '#dcd159',
'#dade48', '#fbff13', '#b6ff05', '#27ff87', '#c24f44', '#a5a5a5', '#ff6d4c',
'#69fff8', '#f9ffa4', '#1c0dff']
})
print(chart)
// 地图显示
var landcoverVisualization = {palette: palette, min: 1, max: 23, format: 'png'};
Map.addLayer(dataseicol.first().clip(roi), landcoverVisualization, 'landcover');
//添加图例
addLegend(palette, names);
2015\2016\2017\2018\2019年的土地利用数据面积,可以下载到excel求差别和占比。
https://code.earthengine.google.com/f301688d8a0d31b5c2c1959d347eb48e?noload=true