Announcement: All noncommercial projects registered to use Earth Engine before April 15, 2025 must verify noncommercial eligibility to maintain Earth Engine access.
Stay organized with collections
Save and categorize content based on your preferences.
Calculating Yearly Forest Loss
In the previous section you learned how to
calculate total forest area lost in
the given region of interest using the reduceRegion method. Instead of
calculating the total loss, it would be helpful to compute the loss for each year. The way
to achieve this in Earth Engine is using a Grouped Reducer.
To group output of reduceRegion(), you can specify a grouping band that
defines groups by integer pixel values. In the following example, we slightly modify the
previous code and add the lossYear band to the original image. Each pixel in the
lossYear band contain values from 0 to 14 - indicating the year in which the
loss occurred. We also change the reducer to a grouped reducer, specifying the band index of
the grouping band (1) so the pixel areas will be summed and grouped according to the value
in the lossYear band.
Once you run the above code, you will see the yearly forest loss area printed out in a nested
list called groups. We can format the output a little to make the result a
dictionary, with year as the key and loss area as the value. Notice that we are using the
format() method to convert the year values from 0-14 to 2000-2014.
Now that we have yearly loss numbers, we are ready to prepare a chart. We will use the
ui.Chart.array.values() method. This method takes an array (or list) of input
values and an array (or list) of labels for the X-axis.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2023-10-06 UTC."],[[["\u003cp\u003eThis tutorial demonstrates how to calculate yearly forest loss in a specific region using Google Earth Engine and grouped reducers.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003elossYear\u003c/code\u003e band is used to categorize forest loss by year, ranging from 2000 to 2014.\u003c/p\u003e\n"],["\u003cp\u003eThe output is formatted into a dictionary where years are keys and corresponding loss areas are values.\u003c/p\u003e\n"],["\u003cp\u003eA column chart is generated to visualize yearly forest loss trends using the \u003ccode\u003eui.Chart.array.values()\u003c/code\u003e method.\u003c/p\u003e\n"],["\u003cp\u003eThe tutorial further directs users to explore and compare with other deforestation monitoring datasets.\u003c/p\u003e\n"]]],["The content demonstrates how to calculate yearly forest loss using Earth Engine. It involves adding a `lossYear` band to an image, then using a grouped reducer (`reduceRegion`) to sum pixel areas based on the year of loss (0-14). The output is formatted into a dictionary with years (2000-2014) as keys and loss areas as values. Finally, `ui.Chart.array.values()` is used to create a column chart visualizing the yearly forest loss, with years on the x-axis and loss area on the y-axis.\n"],null,["# Charting Yearly Forest Loss\n\nCalculating Yearly Forest Loss\n------------------------------\n\nIn the previous section you learned how to\n[calculate total forest area lost](/earth-engine/tutorials/tutorial_forest_03#calculating-pixel-areas) in\nthe given region of interest using the `reduceRegion` method. Instead of\ncalculating the total loss, it would be helpful to compute the loss for each year. The way\nto achieve this in Earth Engine is using a [Grouped Reducer](/earth-engine/guides/reducers_grouping).\n\n\nTo group output of `reduceRegion()`, you can specify a grouping band that\ndefines groups by integer pixel values. In the following example, we slightly modify the\nprevious code and add the `lossYear` band to the original image. Each pixel in the\n`lossYear` band contain values from 0 to 14 - indicating the year in which the\nloss occurred. We also change the reducer to a grouped reducer, specifying the band index of\nthe grouping band (1) so the pixel areas will be summed and grouped according to the value\nin the `lossYear` band.\n\n### Code Editor (JavaScript)\n\n```javascript\n// Load country boundaries from LSIB.\nvar countries = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017');\n// Get a feature collection with just the Congo feature.\nvar congo = countries.filter(ee.Filter.eq('country_co', 'CF'));\n\n// Get the loss image.\n// This dataset is updated yearly, so we get the latest version.\nvar gfc2017 = ee.Image('UMD/hansen/global_forest_change_2017_v1_5');\nvar lossImage = gfc2017.select(['loss']);\nvar lossAreaImage = lossImage.multiply(ee.Image.pixelArea());\n\nvar lossYear = gfc2017.select(['lossyear']);\nvar lossByYear = lossAreaImage.addBands(lossYear).reduceRegion({\n reducer: ee.Reducer.sum().group({\n groupField: 1\n }),\n geometry: congo,\n scale: 30,\n maxPixels: 1e9\n});\nprint(lossByYear);\n```\n\nOnce you run the above code, you will see the yearly forest loss area printed out in a nested\nlist called `groups`. We can format the output a little to make the result a\ndictionary, with year as the key and loss area as the value. Notice that we are using the\n`format()` method to convert the year values from 0-14 to 2000-2014.\n\n### Code Editor (JavaScript)\n\n```javascript\nvar statsFormatted = ee.List(lossByYear.get('groups'))\n .map(function(el) {\n var d = ee.Dictionary(el);\n return [ee.Number(d.get('group')).format(\"20%02d\"), d.get('sum')];\n });\nvar statsDictionary = ee.Dictionary(statsFormatted.flatten());\nprint(statsDictionary);\n```\n\nMaking a chart\n--------------\n\nNow that we have yearly loss numbers, we are ready to prepare a chart. We will use the\n`ui.Chart.array.values()` method. This method takes an array (or list) of input\nvalues and an array (or list) of labels for the X-axis.\n\n\n### Code Editor (JavaScript)\n\n```javascript\nvar chart = ui.Chart.array.values({\n array: statsDictionary.values(),\n axis: 0,\n xLabels: statsDictionary.keys()\n}).setChartType('ColumnChart')\n .setOptions({\n title: 'Yearly Forest Loss',\n hAxis: {title: 'Year', format: '####'},\n vAxis: {title: 'Area (square meters)'},\n legend: { position: \"none\" },\n lineWidth: 1,\n pointSize: 3\n });\nprint(chart);\n```\n\nThe result should look like the chart below.\nFigure 1. Chart of Forest Loss by Year\n\nIn the [next section](/earth-engine/tutorials/tutorial_forest_04),\nyou'll learn about another deforestation monitoring dataset,\n[FORMA](https://www.cgdev.org/sites/default/files/1423248_file_Hammer_Kraft_Wheeler_FORMA_FINAL.pdf),\nand compare it to the Hansen et al. data."]]