如何在地球引擎中为分类图像中的类值分配名称?

问题描述

有没有办法在地球引擎的分类图像中为类值添加类名?我正在尝试获取包含类名和值的属性表,包括面积估计值。下面的代码给了我类代码和各自的面积估计。但是,我找不到任何合适的方法在最终的“ee.List”对象中添加类名。强烈要求在这方面提供任何帮助。

// Define a region in which to generate a sample of the input.
var region = ee.Geometry.Rectangle(87.5,21.5,89,23);

// Collect image with a range of dates and area of interest 
var input = ee.ImageCollection("LANDSAT/LC08/C01/T1_SR")
  .filterBounds(region)
  .filterDate('2020-11-01','2020-11-30')
  //.first();
  .map(function(image){return image.clip(region)})
  .mosaic();


// see the details
print(input,'input');


// display the sample region.
Map.setCenter(88.5,22.6,8);
Map.addLayer(ee.Image().paint(region,2),{},'region');


// Add image layer
Map.addLayer(input,{bands: ['B4','B3','B2'],min: 0,max: 3000},'L8');

// Make the training dataset.
var training = input.sample({
  region: region,scale: 30,numPixels: 10000
});

// Instantiate the clusterer and train it.
var clusterer = ee.Clusterer.wekaKMeans(7).train(training);

// Cluster the input using the trained clusterer.
var result = input.cluster(clusterer);

print(result,'result');

// display the clusters with random colors.
Map.addLayer(result.randomVisualizer(),'clusters');

// //Select a class and display it
// //var subset = result.select('cluster').eq(6).selfMask();

// // // //Add the subset to the map
// // // //Map.addLayer(subset,{min: 1,max: 1,palette:['#0000ff']},'class 6');

// // //Now we want to merge class 6 and class 9 into one single class,called water
// // //Define your from and to values
// var fromWater = [4,5];//these are the original class value in cluster image
// var toWater = [1,1];//this is what we want them to be
// var fromAgri = [1,6];
// var toAgri = [1,1];
// var fromBlt = [0,6];
// var toBlt = [1,1];
// var fromVeg = [3,4];
// var toVeg = [1,1];

// //However,all these class can be merged in the following method. The desired
// //class values are to be mentioned inside the square bracket under 'fromClass'
// //and the new class value to be assigned has to be mentioned in the same manner
// //Note that the number of entries in the square bracket should be matched in both cases
var fromAll = [4,5,2,1,6,3,4];
var toAll = [1,4,4];

// //Apply remap function
// var Water = result.remap(fromWater,toWater,'cluster').rename('Water');
// print(Water,'water');
// var Agri = result.remap(fromAgri,toAgri,'cluster').rename('Agri');
// print(Agri,'agri');
// var Blt = result.remap(fromBlt,toBlt,'cluster').rename('Blt');
// print(Blt,'blt');
// var Veg = result.remap(fromVeg,toVeg,'cluster').rename('veg');
// print(Veg,'veg');

// var catimage = ee.Image.cat([Water,Agri,Blt,Veg]);
// print(catimage,'catimage');
// //Map the layer that you have just created
// //Map.addLayer(catimage,'Unsupervised');

// // to convert all desired classified value into single band image
var sband = result.remap(fromAll,toAll,'cluster').rename('class');
print(sband,'sband');

Map.addLayer(sband,{palette:['blue','yellow','red','green'],min:1,max:4},'Unsup');
var names = ["Water","Cropland","Builtup","vegetation"];
// Area computation
var areaimage = ee.Image.pixelArea().addBands(sband);
print(areaimage,'areaimage');

var area = areaimage.reduceRegion({
  reducer:ee.Reducer.sum().group({
    groupField:1,groupName:'class',}),geometry:region,scale:30,maxPixels:1e10
});

print(area,'area');

var roi_stats = ee.List(area.get('groups'));
print('roi_stats',roi_stats);

var classAreaLists = roi_stats.map(function(item) {
  var areaDict = ee.Dictionary(item)
  var classNumber = ee.Number(areaDict.get('class')).format()
  var area = ee.Number(
    areaDict.get('sum')).divide(1e6).round()
  return ee.List([classNumber,area])
});

var result = ee.Dictionary(classAreaLists.flatten());
print(result,'result');


// //Next few lines of codes are for displaying the legend
// set position of panel for adding legend
var legend = ui.Panel({style:{position:"bottom-left",padding:"8px 15px"}});
 
// Create legend title
var legendTitle = ui.Label({value:"Landcover Type",style:{margin:"0 0 4px 0",padding:"0"}});
 
// Add the title to the panel
legend.add(legendTitle);
 
// Creates and styles 1 row of the legend.
var makeRow = function(color,name) {
  // Create the label that is actually the colored Box.
  var colorBox = ui.Label({style:{backgroundColor:color,border:"1px solid black",padding:"8px",margin:"0 0 4px 0"}});

  // Create the label filled with the description text.
  var description = ui.Label({value:name,style:{margin:"0 0 4px 6px"}});

  // return the panel
  return ui.Panel({widgets:[colorBox,description],layout:ui.Panel.Layout.Flow("horizontal")});
};
 
//  Palette with the colors
var palette = ['blue','green'];
 
// name of the legend
var names = ["Water","vegetation"];
 
// Add color and and names
for (var i = 0; i < 4; i++) legend.add(makeRow(palette[i],names[i]));

// add legend to map (alternatively you can also print the legend to the console)
Map.add(legend);

// Export the image into your drive
// Export.image.toDrive({
//   image: sband,//   scale: 30,//   maxPixels:5000000000,//   fileFormat: 'GeoTIFF',//   folder: '3rd Sem demon on GEE 2021',//   description: 'L8_kmean_classification'});

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)