0

I want to define a dictionary with landcover type and their coverage in my study area. I wrote the code as below, but it is not working as I want.

How can I define a dictionary or another datatype to get thes statistics for each class? When I change the class codes and percentages the dictionary object can not construct.

The whole code is as follows:

//var CLC= ee.Image('COPERNICUS/CORINE/V20/100m/
//var CLC2018= ee.Image('COPERNICUS/CORINE/V20/100m/2018');
print(CLC);
var landCover2018 = CLC2018.select('landcover').clip(Rectangle);
print('landCover2018',landCover2018);
print('Dataset covers ',CLC.size(),'Years');

print whole area coverage

var LC_Area=CLC2018.geometry().area()
print('LC_Area m2',LC_Area);
var LC_AreaSqKm = ee.Number(LC_Area).divide(1e6).round()
print('LC_Area Km2',LC_AreaSqKm)
/////////
var LC2018_Area=landCover2018.geometry().area();
print('LC2018_Area',LC2018_Area);

calculate coverage of each landcover classes

var counts=landCover2018.reduceRegion({
  reducer: ee.Reducer.frequencyHistogram(),
  geometry: Rectangle,
  scale: 10
});

print('counts:',counts);
var LC_Counts=counts.get('landcover');
print ('LC_Counts',LC_Counts)
//

create an dictiınary object containing landcover types and their coverages

var LC_Count_Dict=ee.Dictionary(LC_Counts)
print('Landcover dictionary:', LC_Count_Dict)
print('Landcover dictionary classes:', LC_Count_Dict.keys())
print('Landcover dictionary percentages:', LC_Count_Dict.values())

second way for calculations:

// Define a region of interest (ROI).
var roi = ee.Geometry.Rectangle(30.94712, 37.9215, 30.73357, 37.77457);

Load a categorical image to use for defining classes.

var LC_New = ee.Image('COPERNICUS/CORINE/V20/100m/2018').select('landcover');
print('LC_New',LC_New)

Create an image that contains one band with the pixel's area, and a second band with the class information. Sum all the pixel areas within each class, using a grouped reducer.

var LC_New_Areas = ee.Image.pixelArea().addBands(LC_New)
  .reduceRegion({
    reducer: ee.Reducer.sum().group({
      groupField: 1,
      groupName: 'code',
    }),
    geometry: roi,
    scale: 1,  // sample the geometry at 1m intervals
    maxPixels: 1e10
  }).get('groups');

Print the list of dictionaries.

print('LC_New_Areas',LC_New_Areas);
Map.centerObject(Rectangle, 12) 

Display the classified image and region of interest.

Map.addLayer(LC_New.randomVisualizer(), {}, 'LC_New',false);
Map.addLayer(roi, {}, 'roi');

Reduce the region. The region parameter is the Feature geometry.

var LC_Hist=counts;   

print(LC_Hist.getInfo())
print('LC Hist:', LC_Hist.get('landcover').getInfo())
// not worked so in second line modified
var LCHist=ee.Dictionary(LC_Hist.get('landcover').getInfo())
print('LCHist',LCHist)
print(LCHist.values())
print(LCHist.keys())

Reduce the region. The region parameter is the Feature geometry.

var landCover2018ClassDefs = landCover2018.get('landcover_class_names');
var landCover2018ClassVals = landCover2018.get('landcover_class_values');
var LandCoverTypes=landCover2018.get('landcover_class_names').getInfo()
var LandCoverValues=landCover2018.get('landcover_class_values').getInfo()
print('landCover2018ClassDefs',landCover2018ClassDefs)
print('landCover2018ClassVals',landCover2018ClassVals)
print('LandCoverTypes',LandCoverTypes)
print('LandCoverValues',LandCoverValues)

define dictionary objects in two ways:

var LCDict=ee.Dictionary.fromLists(landCover2018ClassDefs,landCover2018ClassVals)
var LCDict2=ee.Dictionary(landCover2018ClassVals,landCover2018ClassDefs)
print(LCDict.keys()) 
print(LCDict.values())

print('LCDict',LCDict);
print('LCDict2',LCDict2);

the LCDict2 does not created by this error massage:

Dictionary (Error)
Dictionary: Element at position 0 is not a string.

I try another method like this to create object for whole landcover (44 class) and landcover in my study area (17 class):

var LC_Keys44=LCDict.keys()
var LC_Values44=LCDict.values()

var LC_Keys17=LCHist.keys()
var LC_Percentages17=LCHist.values()

print('LC_Keys44',LC_Keys44)
print('LC_Values44',LC_Values44)
print('LC_Keys17',LC_Keys17)
print('LC_Percentages17',LC_Percentages17)
print(LC_Values44.get(1))
print(LC_Keys44.get(1))

now i want to compare the landcover class codes in study area with whole dataset and get the class descriptions. for example var a=LC_Values44.get(21)// answer is 112 var b=LC_Keys17.get(0)// answer is 112 print('a',a) print('b',b) var cc =ee.Algorithms.IsEqual(a,b) print('cc',cc)

a and b are equal but cc get value of false

when I try this

var aa=ee.Array(a)
var bb=ee.Array(ee.Number.parse(b))

print('a:',aa,'b:',bb)

the result is

a:
112
b:
112

but when try this

print(aa+bb)

the result is like this:

ee.Array({
  "type": "Invocation",
  "arguments": {
    "values": {
      "type": "Invocation",
      "arguments": {
        "list": {
          "type": "Invocation",
          "arguments": {
            "dictionary": {
              "type": "Invocation",
              "arguments": {

How can I get the landcover image for my study area with all of the properties and get statistics for area coverage of each class in study area?

Here is the whole code

https://code.earthengine.google.com/?scriptPath=users%2FSolmaz%2FBurdur_Sentinel_CloudFree%3ALandCoverClear

0

1 Answer 1

2

I followed your code line by line and you don't need to create an image that contains one band with the pixel's area and a second band with the class information. You only need to retrieve both landCover2018ClassVals and landCover2018ClassDefs (in this order) for building your dictionary. However, in this order, you need previously define a function to map landCover2018ClassVals and convert them in strings. Complete code looks as follows:

var CLC = ee.ImageCollection("COPERNICUS/CORINE/V20/100m");

print(CLC);

var CLC2018 = ee.Image('COPERNICUS/CORINE/V20/100m/2018');

print(CLC2018);

var Rectangle = ee.Geometry.Rectangle(30.94712, 37.9215, 30.73357, 37.77457);

var landCover2018 = CLC2018.select('landcover').clip(Rectangle);

Map.centerObject(Rectangle);
Map.addLayer(landCover2018);

print('landCover2018', landCover2018);
print('Dataset covers (years) ', CLC.size());

var LC_Area=CLC2018.geometry().area();
print('LC_Area m2',LC_Area);

var LC_AreaSqKm = ee.Number(LC_Area).divide(1e6).round();
print('LC_Area Km2', LC_AreaSqKm);

var LC2018_Area=landCover2018.geometry().area();
print('LC2018_Area', LC2018_Area);

//calculate coverage of each landcover classes

var counts=landCover2018.reduceRegion({
  reducer: ee.Reducer.frequencyHistogram(),
  geometry: Rectangle,
  scale: 10
});

print('counts:',counts);

var LC_Counts = counts.get('landcover');
print ('LC_Counts',LC_Counts);

//create an dictionary object containing landcover types and their coverages

var LC_Count_Dict = ee.Dictionary(LC_Counts);

print('Landcover dictionary:', LC_Count_Dict);
print('Landcover dictionary classes:', LC_Count_Dict.keys());
print('Landcover dictionary percentages:', LC_Count_Dict.values());

// Define a region of interest (ROI).
var roi = ee.Geometry.Rectangle(30.94712, 37.9215, 30.73357, 37.77457);

//Load a categorical image to use for defining classes.

var LC_New = ee.Image('COPERNICUS/CORINE/V20/100m/2018').select('landcover');
print('LC_New',LC_New);

var landCover2018ClassVals = landCover2018.get('landcover_class_values');
var landCover2018ClassDefs = landCover2018.get('landcover_class_names');

print("landCover2018ClassVals", landCover2018ClassVals);

var landCover2018ClassVals = ee.List(landCover2018ClassVals).map(function (ele) {
  
  return ee.String(ee.Number(ele).int());
  
});

var LCDict = ee.Dictionary.fromLists(landCover2018ClassVals, landCover2018ClassDefs);

print(LCDict);

var count_def = ee.Dictionary(LC_Counts).keys();
var count_val = ee.Dictionary(LC_Counts).values();

var count_paired = count_def.map(function (ele) {
  
  var idx = count_def.indexOf(ele);
  
  return [LCDict.get(ele), count_val.get(idx)];
  
}).flatten();

print(count_paired);

After running above code in GEE code editor, following paired values (land cover definitions and areas in ROI) were printed in Tab Console.

0: 
Artificial surfaces; urban fabric; discontinuous urban fabric
1: 
56622.06274509805
2: 
Artificial surfaces; industrial, commercial, and transport units; industrial or commercial units
3: 
4600
4: 
Artificial surfaces; mine, dump, and construction sites; mineral extraction sites
5: 
46180.580392156866
6: 
Agricultural areas; arable land; non-irrigated arable land
7: 
71138.8039215686
8: 
Agricultural areas; permanent crops; fruit trees and berry plantations
9: 
229689.431372549
10: 
Agricultural areas; pastures; pastures
11: 
35600
12: 
Agricultural areas; heterogeneous agricultural areas; complex cultivation patterns
13: 
272709.1254901961
14: 
Agricultural areas; heterogeneous agricultural areas; land principally occupied by agriculture, with significant areas of natural vegetation
15: 
212199.70980392172
16: 
Forest and semi natural areas; forests; broad-leaved forest
17: 
126898.7725490196
18: 
Forest and semi natural areas; forests; coniferous forest
19: 
194545.3960784314
20: 
Forest and semi natural areas; forests; mixed forest
21: 
67137.4431372549
22: 
Forest and semi natural areas; scrub and/or herbaceous vegetation associations; natural grasslands
23: 
56173.43529411765
24: 
Forest and semi natural areas; scrub and/or herbaceous vegetation associations; sclerophyllous vegetation
25: 
349720.8274509803
26: 
Forest and semi natural areas; scrub and/or herbaceous vegetation associations; transitional woodland-shrub
27: 
544931.5098039213
28: 
Forest and semi natural areas; open spaces with little or no vegetation; bare rocks
29: 
50560.37254901962
30: 
Forest and semi natural areas; open spaces with little or no vegetation; sparsely vegetated areas
31: 
207349.9725490196
32: 
Water bodies; inland waters; water bodies
33: 
538855.3607843138

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.