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