Sunday, 2 September 2018

Calculating areas of different classes in Google Earth Engine?



I am attempting to calculate the area of 5 different classes in my classified map. GEE tutorials showed me a method (https://developers.google.com/earth-engine/tutorial_forest_03#calculating-pixel-areas), however I struggled to apply it because I dont understand how to select a specific class before using .reduceRegion to calculate area because the example is quite different to my map.


Can someone guide me through how to calculate area, whether using the same method, or another?


Here's the link to my map: https://code.earthengine.google.com/1171bdeecd776800b334a49f60d5d443


Here is my code


var image=ee.Image('LANDSAT/LC08/C01/T1_SR/LC08_169062_20180205');

//define parameters

var green = image.select('B3');
var swi = image.select('B6');

var mndwi = green.subtract(swi).divide(green.add(swi)).rename('MNDWI');

var mndwipara = {min: 0, max: 0.7, palette: ['white', 'blue']};


Map.addLayer(mndwi, mndwipara, 'MNDWI');


//apply threshold to select only positive pixels
var lake_mask= mndwi.gt(0);

Map.addLayer(lake_mask);


//classification attempt - scum
var scum=image.expression(
'(nir)*0 + (nir < 0.018)*1', {
'nir': image.select('B5'),
'red': image.select ('B4'),
'green': image.select('B3'),
});


//classification attempt - sediment
var sediment=image.expression(
'(nir)*0 + (nir > 0.018 && red > green && green/nir < 1.3 && red/green>
nir/red)*1', {
'nir': image.select('B5'),
'red': image.select ('B4'),
'green': image.select('B3'),
});


//classification attempt - lowbiomass
var lowbiomass=image.expression(
'(nir)*0 + (nir > 0.018 && red < green && green > 0.065 && nir < red)*1', {
'nir': image.select('B5'),
'red': image.select ('B4'),
'green': image.select('B3'),
});


//classification attempt-scum

var scum=image.expression(
'(nir)*0 + (nir > 0.018 && red < green && green > 0.065 && nir > red && nir/red > 4.07)*1', {
'nir': image.select('B5'),
'red': image.select ('B4'),
'green': image.select('B3'),
});

//classifcation attempt bleached scum
var bleached_scum=image.expression(
'(nir)*0 + (nir > 0.018 && red > green && green/nir > 1.3 && green > 0.065)*1', {

'nir': image.select('B5'),
'red': image.select ('B4'),
'green': image.select('B3'),
});

//classification attempt - microphytobenthos
var microphytobenthos=image.expression(
'(nir)*0 + (nir > 0.018 && red > green && green/nir < 1.3 && red/green< nir/red)*1', {
'nir': image.select('B5'),
'red': image.select ('B4'),

'green': image.select('B3'),
});

//classification attempt - highbiomass
var highbiomass=image.expression(
'(nir)*0 + (nir > 0.018 && red < green && green > 0.065 && nir > red && nir/red < 4.07)*1', {
'nir': image.select('B5'),
'red': image.select ('B4'),
'green': image.select('B3'),
});


/*
lake = 1
scum = 2
bleached_scum = 3
microphytobenthos = 4
highbiomass = 5
sediment

*/



var lake_mask_ = ee.Image(1).mask(lake_mask).toInt();
var scum_ = ee.Image(2).mask(lake_mask.mask(scum)).toInt();
var bleached_scum_ =
ee.Image(3).mask(lake_mask.mask(bleached_scum)).toInt();
var microphytobenthos_ =
ee.Image(4).mask(lake_mask.mask(microphytobenthos)).toInt();
var biomass_ = ee.Image(5).mask(lake_mask.mask(highbiomass)).toInt();


var test = ee.ImageCollection([lake_mask_,scum_,bleached_scum_,microphytobenthos_,biomass_,]);

var test = test.reduce(ee.Reducer.max());

print(test);
Map.addLayer(test,{min: 1, max: 5, palette: ['0000FF',
'1BCFFF','66ff33','F4FF0B','FA0007']},'Classification');

Answer



Assuming all categories are 'inside' the water mask, I'd something like


var area = ee.Image.pixelArea().divide(10000)


lake_mask = lake_mask.updateMask(lake_mask)

var lake_mask_ = lake_mask.multiply(area).select([0],['lake']);
var scum_ = lake_mask.updateMask(scum).multiply(area).select([0],['scum']);
var bleached_scum_ = lake_mask.updateMask(bleached_scum).multiply(area).select([0],['bleached_scum']);
var microphytobenthos_ = lake_mask.updateMask(microphytobenthos).multiply(area).select([0],['microphytobenthos']);
var biomass_ = lake_mask.updateMask(highbiomass).multiply(area).select([0],['highbiomass']);

var area_image = lake_mask_.addBands(scum_)

.addBands(bleached_scum_)
.addBands(microphytobenthos_)
.addBands(biomass_)

var areas = area_image.reduceRegion({
reducer:ee.Reducer.sum(),
geometry: image.geometry(),
scale: 30,
maxPixels:1e13
})

print(areas)

Area is expressed in hectares (dividing pixel area by 10000)


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...