"""
This code was developed to accompany the paper Rayne L. et al. "*Detecting desertification in the ancient oases of Southern Morocco*" 
"""

## Mounting your Google Drive in the runtime's virtual machine

# Import the drive module from google.colab
from google.colab import drive

# Mount Google Drive to the '/content/drive' directory
drive.mount('/content/drive')

## Install Python Modules via pip

! pip install geemap
! pip install geetools

#### Info about the module GeeMap are available here: https://geemap.org/

## Import Python Modules

import ee
import geemap
import geetools
import os

## Open a folium map object with 'geemap'

Map = geemap.Map()

"""*The* first 6 code's cells pertain to the AOI and to the landcover types' polygons used in the training of the RF algorithm. To skip to the *Desertification detection script*, click --> [**HERE**](https://colab.research.google.com/drive/1SdhjNQsOdXIzhT1JDPZVtkk1Vw6mtkQ9#scrollTo=dfMzRGjz5v8Y&line=4&uniqifier=1))
<--

Otherwise, to modify the script, input your own AOI and training data.
"""

# Default AOI (Area Of Interest)

AOI = ee.Geometry.Polygon(
    [[[-6.645711177723698, 31.149571703840472],
      [-6.645711177723698, 31.007255068271363],
      [-6.529668086903386, 31.007255068271363],
      [-6.529668086903386, 31.149571703840472]]], None, False);

## Define the "Builtup" Feature: here, we are defining a collection of features, each representing a built-up area, with associated geometries and properties.

Builtup = ee.FeatureCollection([
ee.Feature(
    ee.Geometry.Polygon(
      [[[-6.557069573503129, 31.06640297347453],
        [-6.557064209085099, 31.066506360886702],
        [-6.557150039773576, 31.066513253376858],
        [-6.557144675355546, 31.066409865972165]]]),
    {
      "landcover": 150,
      "system:index": "0"
    }),
ee.Feature(
    ee.Geometry.Polygon(
      [[[-6.559350371065804, 31.064892046313716],
        [-6.559345006647774, 31.0650988243106],
        [-6.5594388839632956, 31.06510571690276],
        [-6.559581041041085, 31.065016113165804],
        [-6.55956762999601, 31.064905831527504]]]),
    {
      "landcover": 150,
      "system:index": "1"
    }),
 ee.Feature(
    ee.Geometry.Polygon(
      [[[-6.572758664671154, 31.059410762655013],
       [-6.572656740728588, 31.05959457593278],
       [-6.572747935835094, 31.059638231534073],
       [-6.572844495359631, 31.05944522767163]]]),
    {
      "landcover": 150,
      "system:index": "2"
    }),
ee.Feature(
    ee.Geometry.Polygon(
      [[[-6.572113272167135, 31.060707297214766],
        [-6.572220560527731, 31.06073027358077],
        [-6.5722473826178796, 31.060675130293077]]]),
    {
      "landcover": 150,
      "system:index": "3"
    }),
ee.Feature(
    ee.Geometry.Polygon(
      [[[-6.5775190314938055, 31.098952992025975],
        [-6.577500256030701, 31.099035673643975],
        [-6.577591451137208, 31.099044860485975],
        [-6.577602179973267, 31.098973662437235]]]),
    {
      "landcover": 150,
      "system:index": "4"
    }),
ee.Feature(
    ee.Geometry.Polygon(
      [[[-6.597426862327578, 31.10278431932221],
        [-6.597448319999697, 31.102883073929295],
        [-6.5975341506881735, 31.10287159084073],
        [-6.597518057434084, 31.102782022702197]]]),
    {
      "landcover": 150,
      "system:index": "5"
    }),
ee.Feature(
    ee.Geometry.Polygon(
      [[[-6.601524396432996, 31.10524841955225],
        [-6.60154853631413, 31.105333392248056],
        [-6.60167460013783, 31.10530353698524],
        [-6.601661189092756, 31.10522086082377]]]),
    {
      "landcover": 150,
      "system:index": "6"
    }),
ee.Feature(
    ee.Geometry.Polygon(
      [[[-6.60004649926579, 31.10510373613849],
        [-6.600059910310865, 31.10515426053025],
        [-6.600153787626386, 31.105131294900964],
        [-6.600137694372297, 31.10507158423884]]]),
    {
      "landcover": 150,
      "system:index": "7"
    }),
ee.Feature(
    ee.Geometry.Polygon(
        [[[-6.600508019353537, 31.10431033881222],
          [-6.600537523652701, 31.104372346534625],
          [-6.600609943296103, 31.104356270462333],
          [-6.600588485623984, 31.10430115247954]]]),
    {
      "landcover": 150,
      "system:index": "8"
    }),
ee.Feature(
    ee.Geometry.Polygon(
        [[[-6.599613308292038, 31.104468311972347],
          [-6.599669634681351, 31.104532616169294],
          [-6.5997474187427825, 31.10449816749778],
          [-6.599699138980514, 31.104440753017464]]]),
    {
      "landcover": 150,
      "system:index": "9"
    }),
ee.Feature(
    ee.Geometry.Polygon(
        [[[-6.600416369899431, 31.106193931514273],
          [-6.600384183391252, 31.106255938006694],
          [-6.60045392082564, 31.106281199899414],
          [-6.600480742915789, 31.106216896886547]]]),
    {
      "landcover": 150,
      "system:index": "10"
    }),
ee.Feature(
    ee.Geometry.Polygon(
        [[[-6.599794097407976, 31.106255938006694],
          [-6.599799461826006, 31.10631564792411],
          [-6.599922843440691, 31.10630646178542],
          [-6.5999094323956164, 31.106244455325974]]]),
    {
      "landcover": 150,
      "system:index": "11"
    }),
ee.Feature(
    ee.Geometry.Polygon(
        [[[-6.598233116804479, 31.099063066891958],
          [-6.598235799013494, 31.09913656158188],
          [-6.598316265283941, 31.099131968165423],
          [-6.598310900865911, 31.09905847347194]]]),
    {
      "landcover": 150,
      "system:index": "12"
    }),
ee.Feature(
    ee.Geometry.Polygon(
        [[[-6.597232755676005, 31.107769774460692],
          [-6.597205933585856, 31.107838669417525],
          [-6.597267624393199, 31.10785933789483],
          [-6.597297128692363, 31.107790442953014]]]),
    {
      "landcover": 150,
      "system:index": "13"
    }),
ee.Feature(
  ee.Geometry.Polygon(
    [[[-6.594852931098882, 31.108270392366716],
      [-6.594858295516912, 31.108339286960298],
      [-6.594960219459478, 31.10832780453151],
      [-6.594949490623418, 31.1082635029046]]]),
  {
    "landcover": 150,
    "system:index": "14"
  }),
ee.Feature(
  ee.Geometry.Polygon(
    [[[-6.55819562920769, 31.0742771850581],
      [-6.558176853744586, 31.074350698919517],
      [-6.558278777687152, 31.074366780069113],
      [-6.558289506523211, 31.07430475276279]]]),
  {
    "landcover": 150,
    "system:index": "15"
  }),
ee.Feature(
  ee.Geometry.Polygon(
    [[[-6.600723515349804, 31.058790705477485],
      [-6.600728879767834, 31.058857338266023],
      [-6.6008361681284295, 31.058848147539344],
      [-6.60082543929237, 31.058790705477485]]]),
  {
    "landcover": 150,
    "system:index": "16"
  }),
ee.Feature(
  ee.Geometry.Polygon(
    [[[-6.602727307396174, 31.058187489963213],
      [-6.602657569961787, 31.05821046693779],
      [-6.602684392051936, 31.058277100132724],
      [-6.602748765068293, 31.058256420870332]]]),
  {
    "landcover": 150,
    "system:index": "17"
  }),
ee.Feature(
  ee.Geometry.Polygon(
    [[[-6.602762176113368, 31.057335040284013],
      [-6.602711214142085, 31.05738788779077],
      [-6.602756811695338, 31.057431544404675],
      [-6.60282386692071, 31.057383292356597]]]),
  {
    "landcover": 150,
    "system:index": "18"
  }),
ee.Feature(
  ee.Geometry.Polygon(
    [[[-6.61279066654215, 31.05689425839069],
      [-6.612814806423284, 31.056963190234804],
      [-6.612932823619939, 31.05692872431898],
      [-6.612919412574865, 31.056875876557164]]]),
  {
    "landcover": 150,
    "system:index": "19"
  }),
ee.Feature(
  ee.Geometry.Polygon(
    [[[-6.612598733456925, 31.05639989215384],
      [-6.612609462292984, 31.056455037919672],
      [-6.612676517518357, 31.056441251481193],
      [-6.612663106473282, 31.056397594412903]]]),
  {
    "landcover": 150,
    "system:index": "20"
  }),
ee.Feature(
    ee.Geometry.Polygon(
      [[[-6.608444823324979, 31.050252354608546],
        [-6.608377768099607, 31.050293716608017],
        [-6.608420683443845, 31.050344270138517],
        [-6.608485056460203, 31.050309801825193]]]),
    {
      "landcover": 150,
      "system:index": "21"
    }),
ee.Feature(
    ee.Geometry.Polygon(
      [[[-6.608122595626216, 31.05043834679298],
        [-6.6080045784295605, 31.050479708711613],
        [-6.608031400519709, 31.05052107061226],
        [-6.608144053298335, 31.050477410827703]]]),
    {
      "landcover": 150,
      "system:index": "22"
    }),
ee.Feature(
    ee.Geometry.Polygon(
      [[[-6.606707497402495, 31.04876961004929],
        [-6.606570704742736, 31.04885003739593],
        [-6.60659484462387, 31.048891400004873],
        [-6.606761141582793, 31.048817866465416]]]),
    {
      "landcover": 150,
      "system:index": "23"
    }),
ee.Feature(
    ee.Geometry.Polygon(
      [[[-6.560207470366137, 31.070473578357653],
        [-6.5602021059481075, 31.07053790556271],
        [-6.5602772078005245, 31.070540202962082],
        [-6.560271843382495, 31.070482767961032]]]),
    {
      "landcover": 150,
      "system:index": "24"
    }),
ee.Feature(
    ee.Geometry.Polygon(
      [[[-6.6010952832665755, 31.09368254935266],
        [-6.6011194231477095, 31.09376523555809],
        [-6.601181113955052, 31.09375145452887],
        [-6.601165020700963, 31.093680252512577]]]),
    {
      "landcover": 150,
      "system:index": "25"
    }),
ee.Feature(
    ee.Geometry.Polygon(
      [[[-6.601292039441309, 31.05652620476628],
        [-6.601308132695398, 31.056595136877053],
        [-6.601410056637964, 31.056583648195392],
        [-6.601388598965845, 31.056517013814414]]]),
    {
      "landcover": 150,
      "system:index": "26"
    }),
ee.Feature(
    ee.Geometry.Polygon(
      [[[-6.601251806306085, 31.056680153078215],
        [-6.601251806306085, 31.056776657863132],
        [-6.601388598965845, 31.056783551058327],
        [-6.601388598965845, 31.056680153078215]]]),
    {
      "landcover": 150,
      "system:index": "27"
    }),
ee.Feature(
    ee.Geometry.Polygon(
      [[[-6.554389787002806, 31.104088844789835],
        [-6.554381740375761, 31.104143962895826],
        [-6.554486346527342, 31.104160039004043],
        [-6.554499757572416, 31.10411181067122]]]),
    {
      "landcover": 150,
      "system:index": "28"
    }),
ee.Feature(
    ee.Geometry.Polygon(
      [[[-6.600232729175623, 31.124507781133705],
        [-6.6003480641632635, 31.12454911082513],
        [-6.600404390552576, 31.124438898274633],
        [-6.600294419982966, 31.124402160729357]]]),
    {
      "landcover": 150,
      "system:index": "29"
    }),
ee.Feature(
    ee.Geometry.Polygon(
      [[[-6.634944785209742, 31.122518123068645],
        [-6.634858954521265, 31.123294216098152],
        [-6.6351969128571415, 31.123326361826383],
        [-6.635153997512903, 31.123533012676415],
        [-6.6356207018814946, 31.123546789383745],
        [-6.6356207018814946, 31.122609968728725]]]),
    {
      "landcover": 150,
      "system:index": "30"
    }),
ee.Feature(
    ee.Geometry.Polygon(
      [[[-6.633635867210474, 31.123454944630407],
        [-6.633855808349695, 31.123693740804207],
        [-6.634681928726282, 31.123762624204176],
        [-6.634649742218103, 31.123565158323736],
        [-6.634215224357691, 31.123514643730196],
        [-6.633893359275904, 31.123413614462386]]]),
    {
      "landcover": 150,
      "system:index": "31"
    }),
ee.Feature(
    ee.Geometry.Polygon(
      [[[-6.633555400940027, 31.123229924609017],
        [-6.633603680702295, 31.123344730809066],
        [-6.6338504439316655, 31.12327125485704],
        [-6.633780706497278, 31.12315185631365]]]),
    {
      "landcover": 150,
      "system:index": "32"
    }),
ee.Feature(
    ee.Geometry.Polygon(
      [[[-6.63598548230752, 31.12407948720011],
        [-6.6361464148484135, 31.124074894990354],
        [-6.636151779266443, 31.123914167508165],
        [-6.63599084672555, 31.123927944160155]]]),
    {
      "landcover": 150,
      "system:index": "33"
    }),
ee.Feature(
    ee.Geometry.Polygon(
      [[[-6.636178601356592, 31.124506561737892],
        [-6.636532652946558, 31.124469824218796],
        [-6.636457551094141, 31.12424021440228],
        [-6.636173236938562, 31.124299913008016]]]),
    {
      "landcover": 150,
      "system:index": "34"
    }),
ee.Feature(
    ee.Geometry.Polygon(
      [[[-6.636527288528528, 31.12294979689803],
        [-6.636639941307154, 31.123115118270096],
        [-6.636763322921839, 31.123050826659618],
        [-6.636682856651392, 31.122926835573562]]]),
    {
      "landcover": 150,
      "system:index": "35"
    }),
ee.Feature(
    ee.Geometry.Polygon(
      [[[-6.636039126487818, 31.123220740107016],
        [-6.636135686012354, 31.123454944630407],
        [-6.636366355987635, 31.123422798945736],
        [-6.636291254135218, 31.123096749242965]]]),
    {
      "landcover": 150,
      "system:index": "36"
    }),
ee.Feature(
    ee.Geometry.Polygon(
      [[[-5.726200066912918, 29.788431120924393],
        [-5.726248346675186, 29.788528888368475],
        [-5.72627785097435, 29.7885265605733],
        [-5.726307355273514, 29.788489315843087],
        [-5.726264439929276, 29.788412498543256]]]),
    {
      "landcover": 150,
      "system:index": "37"
    })
]);

## Define the "Bare" Feature: here, we are defining a collection of features, each representing a bare land area, with associated geometries and properties.

Bare = ee.FeatureCollection([
ee.Feature(
    ee.Geometry.Polygon(
      [[[-6.556832125732963, 31.11424083021789],
        [-6.555094054291312, 31.11433268388824],
        [-6.5551155119634315, 31.1160227755592],
        [-6.55689649874932, 31.11587581226064]]]),
    {
      "landcover": 60,
      "system:index": "0"
    }),

 ee.Feature(
    ee.Geometry.Polygon(
      [[[-6.565522482941215, 31.118337417496996],
        [-6.5647285490728065, 31.118374157390136],
        [-6.564792922089164, 31.118906884242264],
        [-6.565586855957572, 31.11885177470657]]]),
    {
        "landcover": 60,
        "system:index": "1"
     }),
ee.Feature(
   ee.Geometry.Polygon(
      [[[-6.570727711052273, 31.134445227098492],
        [-6.5696119121020775, 31.135785996340427],
        [-6.5709852031177025, 31.136649221302207],
        [-6.572122459740017, 31.135822729477557]]]),
    {
        "landcover": 60,
        "system:index": "2"
      }),
ee.Feature(
    ee.Geometry.Polygon(
       [[[-6.637168520843335, 31.072383210748143],
         [-6.635344618713208, 31.073228633563076],
         [-6.636267298614331, 31.074533510543716],
         [-6.6380697430723385, 31.073743235074353]]]),
    {
        "landcover": 60,
        "system:index": "3"
       }),
ee.Feature(
    ee.Geometry.Polygon(
       [[[-6.620516473654747, 31.040674506415698],
         [-6.620108777884483, 31.043450595614363],
         [-6.623778039816856, 31.044020511195434],
         [-6.623992616538048, 31.04137313251235]]]),
    {
         "landcover": 60,
         "system:index": "4"
       }),
ee.Feature(
    ee.Geometry.Polygon(
       [[[-6.539336283781574, 31.044962027507577],
         [-6.541154821493671, 31.046037493429427],
         [-6.541712720968769, 31.045476781943567],
         [-6.540114124395893, 31.044479442695636]]]),
    {
         "landcover": 60,
         "system:index": "5"
       }),
ee.Feature(
    ee.Geometry.Polygon(
        [[[-6.562618238551856, 31.027112973749595],
          [-6.560987455470801, 31.027186524053757],
          [-6.561073286159278, 31.02783008679245],
          [-6.562210542781592, 31.027788715032873]]]),
    {
         "landcover": 60,
         "system:index": "6"
       }),
ee.Feature(
   ee.Geometry.Polygon(
        [[[-6.5720666516596005, 31.017889507415013],
          [-6.57132099755346, 31.01851934102887],
          [-6.5717662442499325, 31.01871242787234],
          [-6.5724153388315365, 31.018146958154386]]]),
    {
         "landcover": 60,
         "system:index": "7"
        }),
ee.Feature(
   ee.Geometry.Polygon(
        [[[-6.619356084893188, 31.026841559704454],
          [-6.615536619255981, 31.026749621482445],
          [-6.615622449944458, 31.02849643252639],
          [-6.619506288598022, 31.02847804520855]]]),
    {
          "landcover": 60,
          "system:index": "8"
         }),
 ee.Feature(
   ee.Geometry.Polygon(
        [[[-6.566202767426079, 31.021839323972088],
          [-6.565521486336296, 31.021811741066482],
          [-6.565526850754326, 31.02232662065417],
          [-6.566261776024406, 31.02226915655242]]]),
    {
           "landcover": 60,
           "system:index": "9"
          }),
ee.Feature(
   ee.Geometry.Polygon(
        [[[-6.564917407959776, 31.023474842492497],
          [-6.563726507157163, 31.023433468841358],
          [-6.563713096112089, 31.024012698321915],
          [-6.564909361332731, 31.02409774362373]]]),
    {
           "landcover": 60,
           "system:index": "10"
          }),
 ee.Feature(
   ee.Geometry.Polygon(
        [[[-6.640545254725405, 31.010841098069196],
          [-6.639882749098726, 31.010098569716842],
          [-6.638469224947878, 31.010487075984926],
          [-6.638697212714144, 31.011110062020478],
          [-6.640057092684694, 31.010962936534316]]]),
     {
           "landcover": 60,
           "system:index": "11"
          }),
 ee.Feature(
   ee.Geometry.Polygon(
        [[[-5.77211727771858, 29.808049945476363],
          [-5.772085091210402, 29.80782186579666],
          [-5.771790048218763, 29.80784513925721],
          [-5.7718115058908825, 29.808077873564667],
          [-5.772026082612074, 29.80806158218076]]]),
      {
          "landcover": 60,
          "system:index": "12"
          }),
 ee.Feature(
   ee.Geometry.Polygon(
        [[[-6.64037207310622, 31.02820516683221],
          [-6.64011994545882, 31.029142918203128],
          [-6.640549098901203, 31.029188886170218],
          [-6.640769040040424, 31.02823274788668]]]),
      {
          "landcover": 60,
          "system:index": "13"
          }),
ee.Feature(
   ee.Geometry.Polygon(
        [[[-6.638279950074604, 31.027258212451795],
          [-6.6382102126402165, 31.02766733467409],
          [-6.637920534066608, 31.027966130692402],
          [-6.63811901753371, 31.028163795235557],
          [-6.638419424943378, 31.027832821815437],
          [-6.638414060525348, 31.02750644412216]]]),
      {
          "landcover": 60,
          "system:index": "14"
          }),
 ee.Feature(
   ee.Geometry.Polygon(
        [[[-6.556736527814062, 31.129160772145486],
          [-6.554397641553075, 31.129151588217557],
          [-6.5542688955203605, 31.132108767060515],
          [-6.556747256650121, 31.13209039977457]]]),
      {
          "landcover": 60,
           "system:index": "15"
         }),
ee.Feature(
   ee.Geometry.Polygon(
        [[[-6.569930346789636, 31.095621032755833],
          [-6.569096187208188, 31.09566237459161],
          [-6.5679294188645265, 31.09556590970367],
          [-6.568422945323267, 31.098735433244226],
          [-6.569898160281458, 31.098657344804497]]]),
      {
          "landcover": 60,
          "system:index": "16"
         }),
ee.Feature(
   ee.Geometry.Polygon(
        [[[-6.6174756425067045, 31.09946223232419],
          [-6.617443455998526, 31.101391440980706],
          [-6.619310273472891, 31.101446560652217],
          [-6.6193746464892484, 31.09947141912494]]]),
      {
          "landcover": 60,
          "system:index": "17"
         }),
ee.Feature(
   ee.Geometry.Polygon(
        [[[-6.618062688092228, 31.106969463980256],
          [-6.614082289914127, 31.10635399496056],
          [-6.6134492885866125, 31.109265954958737],
          [-6.617236567715641, 31.10952315848948]]]),
      {
       "landcover": 60,
        "system:index": "18"
         }),
ee.Feature(
  ee.Geometry.Polygon(
        [[[-6.5646060702606945, 31.104400784846057],
          [-6.564380764703444, 31.104520206976336],
          [-6.564691900949171, 31.104928996977762],
          [-6.56497085068672, 31.104786609648915]]]),
      {
        "landcover": 60,
        "system:index": "19"
         }),
ee.Feature(
  ee.Geometry.Polygon(
        [[[-6.570271380560479, 31.142744670372387],
          [-6.570700534002862, 31.144296519141445],
          [-6.572020180838189, 31.143915446204705],
          [-6.571397908346734, 31.14257020057935]]]),
      {
        "landcover": 60,
        "system:index": "20"
        }),
ee.Feature(
  ee.Geometry.Polygon(
        [[[-6.554866275700824, 31.14461198128278],
          [-6.553927502545611, 31.144469653489292],
          [-6.553809485348956, 31.145002233814107],
          [-6.554801902684466, 31.145071101741134]]]),
       {
         "landcover": 60,
         "system:index": "21"
        }),
ee.Feature(
  ee.Geometry.Polygon(
        [[[-6.537290984442301, 31.090891730379052],
          [-6.537097865393228, 31.093684728856687],
          [-6.54055255060441, 31.093684728856687],
          [-6.540380889227457, 31.09129598524058]]]),
        {
         "landcover": 60,
         "system:index": "22"
        }),
ee.Feature(
  ee.Geometry.Polygon(
        [[[-6.558890513819202, 31.043172455139285],
          [-6.557522587221607, 31.042404899907456],
          [-6.556245855730518, 31.043507971350614],
          [-6.557656697672352, 31.04416061593583]]]),
        {
         "landcover": 60,
         "system:index": "23"
        }),
ee.Feature(
  ee.Geometry.Polygon(
        [[[-6.554995946329578, 31.04131560431423],
          [-6.554480962198719, 31.041752243285064],
          [-6.5558864397225225, 31.042519803779157],
          [-6.556320957582935, 31.042423284536255],
          [-6.5567608398613775, 31.0420326104105]]]),
        {
          "landcover": 60,
          "system:index": "24"
        }),
ee.Feature(
  ee.Geometry.Polygon(
        [[[-6.57438960840544, 31.03606624912055],
          [-6.572807105086653, 31.03606624912055],
          [-6.572817833922713, 31.036760312601928],
          [-6.574582727454512, 31.03678329474987]]]),
        {
          "landcover": 60,
          "system:index": "25"
         }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.567383697436759, 31.021582161647085],
         [-6.566643407748649, 31.02230391345327],
         [-6.5676304606661295, 31.023379635296926],
         [-6.568665793345878, 31.02299807722962],
         [-6.568896463321159, 31.022506186863936]]]),
      {
          "landcover": 60,
          "system:index": "26"
        }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.6135885261045875, 31.02414524260679],
         [-6.613427593563694, 31.02414524260679],
         [-6.613186194752354, 31.02370852290096],
         [-6.612976982449192, 31.023869419867626],
         [-6.613229110096592, 31.024310735866997],
         [-6.613572432850498, 31.024490019907752],
         [-6.613835289333958, 31.02431992992855]]]),
      {
        "landcover": 60,
        "system:index": "27"
       }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.631864683905572, 31.13059373811685],
         [-6.629021542349786, 31.131980480083115],
         [-6.632315295020074, 31.135405921363205],
         [-6.63467563895318, 31.134809003498717]]]),
      {
       "landcover": 60,
       "system:index": "28"
       }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.639077570407125, 31.123694645304468],
         [-6.638913955657217, 31.123864557599052],
         [-6.638932731120321, 31.124339851185596],
         [-6.639302875964376, 31.124431695082453],
         [-6.639552321402761, 31.124112537158638],
         [-6.639477219550344, 31.12382781983142]]]),
      {
       "landcover": 60,
       "system:index": "29"
       }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.58922767902925, 31.0181681315976],
         [-6.588060918107772, 31.018391101715615],
         [-6.588369372144484, 31.01880256066668],
         [-6.589528086438918, 31.018485346660174]]]),
      {
        "landcover": 60,
         "system:index": "30"
       }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.59972685229266, 31.015042615462438],
         [-6.599254783506039, 31.014849521182885],
         [-6.598358925695065, 31.015709247468205],
         [-6.598798807973507, 31.016021873285354],
         [-6.599544462079647, 31.0153598409295]]]),
       {
         "landcover": 60,
         "system:index": "31"
        }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.618534088963246, 31.010451826603294],
         [-6.617579222553944, 31.0092518194423],
         [-6.616956950062489, 31.00995987147175],
         [-6.6176489599883315, 31.011178260378966],
         [-6.618217588299489, 31.010525389769235]]]),
       {
         "landcover": 60,
         "system:index": "32"
       }),
ee.Feature(
  ee.Geometry.Polygon(
      [[[-6.646054166001198, 31.02138090886189],
        [-6.643726008576271, 31.02143607491454],
        [-6.643532889527199, 31.022327921666832],
        [-6.646150725525734, 31.022245173350463]]]),
       {
         "landcover": 60,
         "system:index": "33"
       }),
ee.Feature(
 ee.Geometry.Polygon(
      [[[-6.639701273211229, 31.028311167450777],
        [-6.634540703066576, 31.02809971266996],
        [-6.63469090677141, 31.02930407927451],
        [-6.639615442522753, 31.029166175473506]]]),
       {
         "landcover": 60,
         "system:index": "34"
       })
]);

## Define Treecover Feature: Here, we are defining a collection of features, each representing an area with tree cover, with associated geometries and properties.

Treecover = ee.FeatureCollection([
ee.Feature(
    ee.Geometry.Polygon(
      [[[-6.576073102790076, 31.106221678838374],
        [-6.576078467208106, 31.10634569174652],
        [-6.57621794207688, 31.106350284814084],
        [-6.5762125776588505, 31.10623316152186]]]),
    {
      "landcover": 10,
      "system:index": "0"
    }),
ee.Feature(
   ee.Geometry.Polygon(
      [[[-6.575098252819928, 31.107958215443205],
        [-6.5750419264306155, 31.108070743623507],
        [-6.575269914196881, 31.10812356292745],
        [-6.575310147332105, 31.108036296235536]]]),
    {
      "landcover": 10,
      "system:index": "1"
    }),
ee.Feature(
   ee.Geometry.Polygon(
      [[[-6.570771153369097, 31.085519030786415],
        [-6.570470745959429, 31.08550984263644],
        [-6.570454652705339, 31.085610912237172],
        [-6.570545847811846, 31.085721169860857],
        [-6.570808704295305, 31.08564766479261]]]),
    {
      "landcover": 10,
      "system:index": "2"
    }),
ee.Feature(
   ee.Geometry.Polygon(
      [[[-6.555546690693292, 31.089602934377407],
        [-6.555573512783441, 31.08976371994782],
        [-6.5557451741603945, 31.089720078177027],
        [-6.555761267414484, 31.089522541490165]]]),
    {
      "landcover": 10,
      "system:index": "3"
    }),
ee.Feature(
  ee.Geometry.Polygon(
      [[[-6.561545620936588, 31.087537251870167],
        [-6.561658273715214, 31.087799108209392],
        [-6.561899672526554, 31.087725604748417]]]),
    {
      "landcover": 10,
      "system:index": "4"
    }),
ee.Feature(
  ee.Geometry.Polygon(
      [[[-6.5609235140154505, 31.081744821936653],
        [-6.560894009716287, 31.08187346105117],
        [-6.561068353302255, 31.08187346105117],
        [-6.561084446556344, 31.081754010450606]]]),
    {
      "landcover": 10,
      "system:index": "5"
    }),
ee.Feature(
  ee.Geometry.Polygon(
      [[[-6.559043285496011, 31.080155195653553],
        [-6.558962819225564, 31.080332077349247],
        [-6.559113022930398, 31.08037112910784],
        [-6.5591747137377405, 31.080214921977213]]]),
    {
      "landcover": 10,
      "system:index": "6"
   }),
ee.Feature(
  ee.Geometry.Polygon(
      [[[-6.556524963206429, 31.076814122168614],
        [-6.556482047862191, 31.077050738392984],
        [-6.55662956935801, 31.077076008052035],
        [-6.556672484702248, 31.0768485808787]]]),
    {
      "landcover": 10,
      "system:index": "7"
    }),
ee.Feature(
  ee.Geometry.Polygon(
      [[[-6.597749841533937, 31.060539869401893],
        [-6.59766937526349, 31.06055825052748],
        [-6.597701561771669, 31.060647858463884],
        [-6.597765934788026, 31.060629477355604]]]),
    {
      "landcover": 10,
      "system:index": "8"
    }),
ee.Feature(
  ee.Geometry.Polygon(
      [[[-6.6049822400202185, 31.05994738136649],
        [-6.605011744319382, 31.060050775795705],
        [-6.605229003249589, 31.05995197667683],
        [-6.605215592204514, 31.059853177455363]]]),
    {
      "landcover": 10,
      "system:index": "9"
    }),
 ee.Feature(
  ee.Geometry.Polygon(
      [[[-6.6061570475687414, 31.05426213478281],
        [-6.606221420585099, 31.05440689560231],
        [-6.606360895453873, 31.05436553539064],
        [-6.60632066231865, 31.054227667888544]]]),
    {
      "landcover": 10,
      "system:index": "10"
    }),
 ee.Feature(
  ee.Geometry.Polygon(
      [[[-6.574909082656155, 31.089849696108015],
        [-6.575045875315914, 31.08987496236436],
        [-6.575155845885525, 31.08964067500256]]]),
     {
       "landcover": 10,
       "system:index": "11"
    }),
ee.Feature(
  ee.Geometry.Polygon(
      [[[-6.581204003622343, 31.092936532037008],
        [-6.581102079679777, 31.0930835308401],
        [-6.581311291982939, 31.093189186089358],
        [-6.581429309179594, 31.093062859147174]]]),
     {
       "landcover": 10,
       "system:index": "12"
    }),
ee.Feature(
  ee.Geometry.Polygon(
      [[[-6.557764899624889, 31.101432757049277],
        [-6.557759535206859, 31.101549886262063],
        [-6.5580760358706165, 31.101556776211257],
        [-6.558086764706676, 31.101448833616434]]]),
      {
        "landcover": 10,
        "system:index": "13"
    }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.563886485732491, 31.065501226883367],
         [-6.563763104117806, 31.065501226883367],
         [-6.563752375281746, 31.065616102867985],
         [-6.563875756896431, 31.065579342568004]]]),
      {
        "landcover": 10,
        "system:index": "14"
    }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.577712298600105, 31.013110935296186],
         [-6.577594281403449, 31.01332702090584],
         [-6.577682794300941, 31.01341897208052],
         [-6.577822269169715, 31.013253459902245],
         [-6.577803493706611, 31.013133923150296]]]),
      {
         "landcover": 10,
         "system:index": "15"
    }),
ee.Feature(
 ee.Geometry.Polygon(
       [[[-6.578331064110034, 31.016690398375133],
         [-6.578274737720721, 31.01677315151569],
         [-6.578349839573138, 31.01682832023614],
         [-6.578454445724719, 31.01675935933058]]]),
      {
        "landcover": 10,
        "system:index": "16"
     }),
ee.Feature(
 ee.Geometry.Polygon(
       [[[-6.577120353020152, 31.00929414628942],
         [-6.577088166511973, 31.009521734914276],
         [-6.5774475825199685, 31.009540125890545],
         [-6.5774475825199685, 31.00933782495653]]]),
      {
        "landcover": 10,
        "system:index": "17"
     }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.579103453999282, 31.014473545603757],
         [-6.579098089581252, 31.01459767817524],
         [-6.579229517822982, 31.01462986066737],
         [-6.579245611077071, 31.01446205183896]]]),
      {
        "landcover": 10,
        "system:index": "18"
     }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.5791723299054405, 31.01402459447792],
         [-6.579070405962875, 31.014102752409972],
         [-6.579011397364547, 31.014325732040287],
         [-6.579099910262038, 31.01433722582155],
         [-6.579137461188247, 31.01423837925758],
         [-6.579295711520126, 31.014208495392488],
         [-6.5792474317578575, 31.01400160683866]]]),
      {
        "landcover": 10,
        "system:index": "19"
     }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.580395088663466, 31.011231423434435],
         [-6.58028780030287, 31.01143831800167],
         [-6.580526516905196, 31.011454409782516],
         [-6.580569432249434, 31.01125671057231]]]),
      {
        "landcover": 10,
        "system:index": "20"
     }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.582665589931664, 31.012489216079096],
         [-6.582673636558709, 31.012634040412117],
         [-6.582880166652855, 31.012631741614896],
         [-6.582797018173394, 31.012489216079096]]]),
      {
        "landcover": 10,
        "system:index": "21"
     }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.57835320758247, 31.009886007241146],
         [-6.5782968811931575, 31.01012968645734],
         [-6.578492682451245, 31.01016416931499],
         [-6.578565102094647, 31.009925087912098]]]),
       {
         "landcover": 10,
         "system:index": "22"
      }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.572193678119991, 31.001905723664283],
         [-6.572282191017482, 31.001988489641207],
         [-6.5724753100665545, 31.001882733102402],
         [-6.572394843796108, 31.001795368916692]]]),
       {
         "landcover": 10,
         "system:index": "23"
      }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.577438522457988, 31.00306337621174],
         [-6.577577997326762, 31.003205915841058],
         [-6.5776718746422835, 31.003162234364403],
         [-6.577658463597209, 31.003084067461486],
         [-6.577540446400554, 31.0029967043765]]]),
       {
         "landcover": 10,
         "system:index": "24"
      }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.5959415085091955, 31.043131181555417],
         [-6.596072936750925, 31.04335409323937],
         [-6.596209729410685, 31.043287449594732],
         [-6.59607561895994, 31.043053047439567]]]),
       {
         "landcover": 10,
         "system:index": "25"
      }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.594340988391996, 31.044148468319396],
         [-6.594483145469785, 31.044320820918095],
         [-6.594571658367276, 31.044247283847483],
         [-6.594429501289487, 31.04407493111566]]]),
       {
         "landcover": 10,
         "system:index": "26"
       }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.5913159510259955, 31.04404209194641],
         [-6.591380324042353, 31.044221338842863],
         [-6.591498341239008, 31.0441615899149],
         [-6.591415192759547, 31.043975448783463]]]),
        {
         "landcover": 10,
         "system:index": "27"
       }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.582872797289436, 31.052670999422542],
         [-6.582859386244362, 31.052767508273785],
         [-6.583213437834328, 31.05277210393093],
         [-6.583202708998268, 31.052675595084345]]]),
        {
         "landcover": 10,
         "system:index": "28"
       }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.5889269189605, 31.05511704293663],
         [-6.588916190124441, 31.055277886832894],
         [-6.589036889530111, 31.05529397120755],
         [-6.589036889530111, 31.055140020652736]]]),
        {
         "landcover": 10,
         "system:index": "29"
       })
]);

## Define Cropland Feature: here we are defining a collection of features, each representing an area of cropland, with associated geometries and properties.

Cropland = ee.FeatureCollection([
ee.Feature(
    ee.Geometry.Polygon(
      [[[-6.588680893774956, 31.109312892304644],
        [-6.588667482729882, 31.109425418879425],
        [-6.588916928168267, 31.109439197634522],
        [-6.588933021422356, 31.109324374614303]]]),
    {
      "landcover": 40,
      "system:index": "0"
    }),
ee.Feature(
    ee.Geometry.Polygon(
      [[[-6.566761766731304, 31.10353752742995],
        [-6.56673762685017, 31.10365695064581],
        [-6.567008529960674, 31.103702882611948],
        [-6.567038034259838, 31.103588052654988]]]),
    {
      "landcover": 40,
      "system:index": "1"
    }),
ee.Feature(
   ee.Geometry.Polygon(
      [[[-6.562543475801725, 31.102864291175788],
        [-6.562519335920591, 31.103077876412403],
        [-6.562696361715574, 31.103121512046812],
        [-6.562851929838438, 31.102891850588133]]]),
    {
      "landcover": 40,
      "system:index": "2"
    }),
ee.Feature(
   ee.Geometry.Polygon(
      [[[-6.552298765858031, 31.094209520826976],
        [-6.552336316784239, 31.09435651766041],
        [-6.552462380607939, 31.09431287799922],
        [-6.552419465263701, 31.09418655255121]]]),
     {
       "landcover": 40,
       "system:index": "3"
     }),
 ee.Feature(
   ee.Geometry.Polygon(
      [[[-6.625068608748079, 30.98892477319254],
        [-6.624993506895662, 30.98906273522375],
        [-6.625181261526705, 30.98916850597923],
        [-6.6253126897684345, 30.989007550435215]]]),
      {
        "landcover": 40,
        "system:index": "4"
     }),
ee.Feature(
   ee.Geometry.Polygon(
      [[[-6.596118955023657, 31.062082406180867],
        [-6.596167234785925, 31.062192691092772],
        [-6.596515921957861, 31.06206172774565],
        [-6.596462277777563, 31.06196982353474]]]),
      {
        "landcover": 40,
        "system:index": "5"
     }),
ee.Feature(
   ee.Geometry.Polygon(
      [[[-6.584100585973758, 31.114326634559138],
        [-6.5840576706295195, 31.114905310675415],
        [-6.584363442457217, 31.114909903328897],
        [-6.584443908727664, 31.114648121725917],
        [-6.584374171293277, 31.114317449195536]]]),
      {
       "landcover": 40,
       "system:index": "6"
      }),
ee.Feature(
  ee.Geometry.Polygon(
      [[[-6.577477829596951, 31.081562145932928],
        [-6.577405409953549, 31.081555254533626],
        [-6.577386634490445, 31.081651734078267],
        [-6.577338354728177, 31.081778076190986],
        [-6.577429549834683, 31.08178956182926],
        [-6.5774697829699065, 31.081667813992848],
        [-6.577475147387936, 31.08157822586267]]]),
      {
        "landcover": 40,
        "system:index": "7"
     }),
ee.Feature(
  ee.Geometry.Polygon(
      [[[-6.567013742846197, 31.08683289886859],
        [-6.56705934039945, 31.08691559103501],
        [-6.567204179686255, 31.086860462932048],
        [-6.5671558999239865, 31.086798443778022]]]),
      {
        "landcover": 40,
        "system:index": "8"
      }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.560500186132763, 31.10241730342834],
         [-6.560516279386852, 31.10251835504352],
         [-6.560891788648937, 31.102479312386755],
         [-6.560870330976818, 31.10238055735981]]]),
      {
        "landcover": 40,
        "system:index": "9"
      }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.5778332680978835, 31.11126821925021],
         [-6.577822539261824, 31.111479489174524],
         [-6.578214141777998, 31.111516231722074],
         [-6.578211459568983, 31.111327926015626]]]),
      {
        "landcover": 40,
        "system:index": "10"
       }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.554230511889527, 31.09953915445754],
         [-6.554217100844452, 31.099640209135007],
         [-6.554445088610718, 31.099658582701203],
         [-6.554450453028748, 31.09955293464717]]]),
      {
        "landcover": 40,
        "system:index": "11"
       }),
ee.Feature(
 ee.Geometry.Polygon(
       [[[-6.561108169595378, 31.104749999661845],
         [-6.561019656697886, 31.104830379662776],
         [-6.561279830972331, 31.105002622292833],
         [-6.561349568406718, 31.104908463027126]]]),
      {
        "landcover": 40,
        "system:index": "12"
       }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.58944754233701, 31.113062834209412],
         [-6.589399262574742, 31.11341417837819],
         [-6.589731856492588, 31.11346240198616],
         [-6.589788182881901, 31.113111057995848]]]),
       {
         "landcover": 40,
         "system:index": "13"
       }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.600038435262445, 31.075612512126863],
         [-6.600113537114862, 31.075754943181206],
         [-6.600188638967279, 31.075727375897007],
         [-6.600118901532892, 31.075580350246327]]]),
       {
         "landcover": 40,
         "system:index": "14"
       }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.5960376743156335, 31.070270315516037],
         [-6.596107411750021, 31.070467892215422],
         [-6.5962120179016015, 31.070440323398277],
         [-6.5961369160491845, 31.070247341454568]]]),
       {
         "landcover": 40,
         "system:index": "15"
       }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.629509220672052, 31.101011080872247],
         [-6.629466305327814, 31.1011052440022],
         [-6.629686246467035, 31.10121778030332],
         [-6.629739890647333, 31.10117873711192]]]),
       {
         "landcover": 40,
         "system:index": "16"
       }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.630489278512215, 31.103224823989127],
         [-6.630314934926247, 31.103580797765233],
         [-6.630411494450783, 31.103612950169648],
         [-6.6306126601269, 31.103263866339407]]]),
       {
         "landcover": 40,
         "system:index": "17"
       }),
ee.Feature(
   ee.Geometry.Polygon(
       [[[-6.575603869752031, 31.112210225506733],
         [-6.57563605626021, 31.112389343912973],
         [-6.575764802292925, 31.11237326919802],
         [-6.575759437874895, 31.11223548581528]]]),
       {
         "landcover": 40,
         "system:index": "18"
       }),
ee.Feature(
 ee.Geometry.Polygon(
       [[[-6.652692796855557, 31.01305691827863],
         [-6.6526981612735865, 31.01344081493286],
         [-6.652928831248867, 31.013431619821493],
         [-6.652939560084927, 31.013059217065567]]]),
       {
        "landcover": 40,
        "system:index": "19"
       }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.647521217855696, 31.014945058722724],
         [-6.647529264482741, 31.015138152808696],
         [-6.647958417925124, 31.015156542701238],
         [-6.647963782343154, 31.01498183857879]]]),
       {
         "landcover": 40,
         "system:index": "20"
       }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.654375101085619, 31.018066698738185],
         [-6.65396472310634, 31.017947168023593],
         [-6.653945947643236, 31.01804601074062],
         [-6.65434827899547, 31.018218410583103]]]),
       {
         "landcover": 40,
         "system:index": "21"
       }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.608484856313304, 31.008685520886356],
         [-6.608484856313304, 31.008867133214633],
         [-6.608680657571391, 31.00887632876634],
         [-6.608680657571391, 31.008701613131972]]]),
       {
         "landcover": 40,
         "system:index": "22"
       }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.594897778787525, 31.008812039654103],
         [-6.595034571447284, 31.008968364003078],
         [-6.595284016885669, 31.00883043076729],
         [-6.595109673299701, 31.008738475165888]]]),
       {
         "landcover": 40,
         "system:index": "23"
       }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.596904544703365, 31.008500060223117],
         [-6.596893815867306, 31.00870925961088],
         [-6.597124485842587, 31.00871615628619],
         [-6.597124485842587, 31.0085115547069]]]),
       {
         "landcover": 40,
         "system:index": "24"
       }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.601022779830161, 31.00677476176671],
         [-6.601017415412131, 31.006901203300433],
         [-6.601350009329978, 31.00690580117122],
         [-6.601352691538993, 31.006777060705193]]]),
       {
         "landcover": 40,
         "system:index": "25"
       }),
ee.Feature(
  ee.Geometry.Polygon(
       [[[-6.576600577033839, 31.00698470479762],
         [-6.576219703353725, 31.007141032143245],
         [-6.576300169624171, 31.00724448392218],
         [-6.576659585632167, 31.00706056957089]]]),
        {
         "landcover": 40,
         "system:index": "26"
        }),
ee.Feature(
   ee.Geometry.Polygon(
       [[[-6.575303062907238, 31.004726621848686],
         [-6.575311109534283, 31.004788694501524],
         [-6.575796589365979, 31.004731219824325],
         [-6.575772449484845, 31.004664549155414]]]),
       {
        "landcover": 40,
        "system:index": "27"
        }),
ee.Feature(
   ee.Geometry.Polygon(
       [[[-6.579764549636755, 31.11019227257493],
         [-6.579759185218725, 31.110396655586246],
         [-6.58000863065711, 31.1103989520221],
         [-6.580000584030065, 31.1101991618971]]]),
        {
          "landcover": 40,
          "system:index": "28"
        }),
ee.Feature(
    ee.Geometry.Polygon(
       [[[-6.580764734020669, 31.112738058535754],
         [-6.5807620518116545, 31.11293325056895],
         [-6.580877386799295, 31.112930954194425],
         [-6.58088543342634, 31.112735762156518]]]),
        {
          "landcover": 40,
          "system:index": "29"
        })
]);

## Define Desertified Feature: here we are defining a collection of features, each representing an area of desertification, with associated geometries and properties.

Desertified = ee.FeatureCollection([
ee.Feature(
    ee.Geometry.Polygon(
      [[[-6.582921740235892, 31.11879353038119],
        [-6.582841273965445, 31.1190231533764],
        [-6.5833562580963045, 31.11911959486871],
        [-6.583447453202811, 31.118899157027975]]]),
    {
      "landcover": 100,
      "system:index": "0"
    }),
ee.Feature(
    ee.Geometry.Polygon(
      [[[-6.587623063339169, 31.116086233381818],
        [-6.58759624124902, 31.116214825995435],
        [-6.587647203220303, 31.11640312200819],
        [-6.588011983646329, 31.116444455229292],
        [-6.588250700248654, 31.116366381352098],
        [-6.588317755474026, 31.116191863041504]]]),
     {
        "landcover": 110,
        "system:index": "1"
     }),
ee.Feature(
   ee.Geometry.Polygon(
       [[[-6.584531751966147, 31.11310258948388],
         [-6.5845263875481175, 31.113527417058023],
         [-6.584743646478324, 31.113525120697872],
         [-6.584749010896354, 31.113123256815268]]]),
     {
        "landcover": 110,
        "system:index": "2"
     }),
ee.Feature(
   ee.Geometry.Polygon(
       [[[-6.580760816074163, 31.113769922089485],
         [-6.58070448968485, 31.114284304050337],
         [-6.581415275073796, 31.114334823556913],
         [-6.581426003909856, 31.11381584916359]]]),
      {
        "landcover": 110,
        "system:index": "3"
      }),
ee.Feature(
   ee.Geometry.Polygon(
       [[[-6.582635203965941, 31.12298474801238],
         [-6.582436720498839, 31.123616182025128],
         [-6.582895378240385, 31.12369884216667],
         [-6.583101908334532, 31.123078889349888]]]),
      {
        "landcover": 110,
        "system:index": "4"
      }),
ee.Feature(
   ee.Geometry.Polygon(
       [[[-6.581511975542869, 31.125397463020484],
         [-6.5814154160183325, 31.126191900931985],
         [-6.582102061526145, 31.126320479852936],
         [-6.582402468935813, 31.12541123945711]]]),
      {
        "landcover": 110,
        "system:index": "5"
      }),
ee.Feature(
    ee.Geometry.Polygon(
       [[[-6.580492736117209, 31.126825609641276],
         [-6.580363990084495, 31.127744020548455],
         [-6.580943347231711, 31.127766980707214],
         [-6.581098915354575, 31.126825609641276]]]),
      {
        "landcover": 110,
        "system:index": "6"
      }),
ee.Feature(
    ee.Geometry.Polygon(
       [[[-6.579470657083242, 31.13032227692087],
         [-6.579298995706289, 31.13074013958472],
         [-6.5797013270585225, 31.130809017869083],
         [-6.579813979837148, 31.130400339316562]]]),
       {
        "landcover": 110,
        "system:index": "7"
      }),
ee.Feature(
    ee.Geometry.Polygon(
        [[[-6.584172085165925, 31.12450559428097],
          [-6.5841023477315375, 31.125029102387792],
          [-6.584488585829682, 31.125061247528283],
          [-6.584665611624665, 31.12458825364773]]]),
       {
         "landcover": 110,
         "system:index": "8"
      }),
ee.Feature(
    ee.Geometry.Polygon(
        [[[-6.588246359583387, 31.07748346504897],
          [-6.588240995165357, 31.0780945266856],
          [-6.588616504427442, 31.07808993225185],
          [-6.588621868845472, 31.077506437362004]]]),
        {
              "landcover": 110,
              "system:index": "9"
        }),
ee.Feature(
    ee.Geometry.Polygon(
        [[[-6.5901292703118415, 31.074579719985383],
          [-6.589952244516859, 31.074800260697295],
          [-6.590247287508497, 31.075011611732815],
          [-6.590558423754224, 31.074814044474795]]]),
        {
          "landcover": 110,
          "system:index": "10"
         }),
ee.Feature(
    ee.Geometry.Polygon(
         [[[-6.593731304264951, 31.078617500054012],
           [-6.59363742694943, 31.078957485664183],
           [-6.594055851555753, 31.07899883518247],
           [-6.59413095340817, 31.07867263293826]]]),
        {
          "landcover": 110,
          "system:index": "11"
         }),
ee.Feature(
    ee.Geometry.Polygon(
         [[[-6.58530799006007, 31.12136832932378],
           [-6.585270439133861, 31.121754085172544],
           [-6.585653995022991, 31.121882670107006],
           [-6.585640583977916, 31.121423437398178]]]),
        {
         "landcover": 110,
         "system:index": "12"
         }),
ee.Feature(
    ee.Geometry.Polygon(
         [[[-6.5922802788413755, 31.077360190114902],
           [-6.592376838365912, 31.077645046773572],
           [-6.592832813898443, 31.077603696666397],
           [-6.592602143923163, 31.07730505646948]]]),
        {
          "landcover": 110,
          "system:index": "13"
          }),
ee.Feature(
    ee.Geometry.Polygon(
         [[[-6.583096562965005, 31.06219038388161],
           [-6.583128749473183, 31.062714235479714],
           [-6.583670555694192, 31.06273261618516],
           [-6.5837081066204, 31.062130646235435]]]),
        {
          "landcover": 110,
          "system:index": "14"
          }),
ee.Feature(
    ee.Geometry.Polygon(
         [[[-6.5787360747246, 31.021965343418753],
           [-6.578446396150992, 31.022415862483797],
           [-6.578776307859823, 31.022443445214495],
           [-6.579044528761313, 31.02199982199392]]]),
        {
          "landcover": 110,
          "system:index": "15"
          }),
ee.Feature(
    ee.Geometry.Polygon(
         [[[-6.579754168535656, 31.119958770087923],
           [-6.579703206564373, 31.12056266995996],
           [-6.580135042215771, 31.12054430043877],
           [-6.5801645465149345, 31.119970251109912]]]),
        {
          "landcover": 110,
           "system:index": "16"
          }),
ee.Feature(
    ee.Geometry.Polygon(
         [[[-6.580940295077702, 31.11577357942498],
           [-6.5809483417047465, 31.116010098626735],
           [-6.58148210129871, 31.11598943192387],
           [-6.5814740546716655, 31.11576209789532]]]),
        {
          "landcover": 110,
          "system:index": "17"
          }),
ee.Feature(
    ee.Geometry.Polygon(
         [[[-6.578740124246957, 31.132138884917058],
           [-6.578705255529764, 31.132790921057122],
           [-6.579139773390176, 31.132832247140318],
           [-6.5793060703491, 31.132536076479592],
           [-6.579523329279306, 31.132127405367406]]]),
        {
          "landcover": 110,
          "system:index": "18"
         }),
ee.Feature(
    ee.Geometry.Polygon(
         [[[-6.5796343449547985, 31.134169751119913],
           [-6.579532421012233, 31.134495762770204],
           [-6.580117142577479, 31.134449845704154],
           [-6.58010641374142, 31.134144496649537]]]),
        {
          "landcover": 110,
          "system:index": "19"
         }),
ee.Feature(
    ee.Geometry.Polygon(
         [[[-6.582071838726162, 31.064017531683515],
           [-6.58213084732449, 31.06494574015033],
           [-6.58311790024197, 31.064895194377915],
           [-6.583080349315762, 31.06398996099645]]]),
        {
          "landcover": 110,
          "system:index": "20"
         }),
ee.Feature(
    ee.Geometry.Polygon(
         [[[-6.567389471915594, 31.05863891671205],
           [-6.567437751677862, 31.05912602507377],
           [-6.567955418017736, 31.059103048320274],
           [-6.567912502673498, 31.058632023651306]]]),
         {
          "landcover": 110,
          "system:index": "21"
         }),
ee.Feature(
    ee.Geometry.Polygon(
         [[[-6.584146038239589, 31.12028614663869],
           [-6.582568899338832, 31.120274665654918],
           [-6.582542077248683, 31.121177066744217],
           [-6.584097758477321, 31.1211977323175]]]),
         {
          "landcover": 110,
          "system:index": "22"
         }),
ee.Feature(
    ee.Geometry.Polygon(
         [[[-6.577885740997829, 31.12948656045129],
           [-6.577483409645595, 31.128917157194376],
           [-6.576877230408229, 31.12894470904354],
           [-6.5768665015721695, 31.129954937987936],
           [-6.577268832924403, 31.13050596742342],
           [-6.577783817055263, 31.130533518811184]]]),
         {
           "landcover": 110,
           "system:index": "23"
         }),
ee.Feature(
    ee.Geometry.Polygon(
         [[[-6.559883947955416, 31.10914661515409],
           [-6.5597122865784625, 31.10944056235896],
           [-6.559824939357088, 31.110230540962675],
           [-6.56027018605356, 31.11023513384231],
           [-6.560409660922335, 31.10932573934037]]]),
         {
           "landcover": 110,
           "system:index": "24"
          }),
ee.Feature(
    ee.Geometry.Polygon(
         [[[-6.783571605162595, 31.101314228690104],
           [-6.783362392859433, 31.10130963537896],
           [-6.7833570284414035, 31.101429061396388],
           [-6.783576969580625, 31.101424468090787]]]),
         {
           "landcover": 110,
           "system:index": "25"
          }),
ee.Feature(
    ee.Geometry.Polygon(
         [[[-6.783475045638059, 31.100896236466763],
           [-6.783469681220029, 31.101061596247593],
           [-6.78368962235925, 31.101061596247593],
           [-6.78369498677728, 31.100873269807767]]]),
         {
           "landcover": 110,
           "system:index": "26"
          }),
ee.Feature(
    ee.Geometry.Polygon(
         [[[-6.6111326097688705, 31.054156545932134],
           [-6.610301124974254, 31.054073825273154],
           [-6.610263574048045, 31.054262244447266],
           [-6.6111326097688705, 31.054308200286794]]]),
         {
           "landcover": 110,
           "system:index": "27"
         }),
ee.Feature(
    ee.Geometry.Polygon(
         [[[-6.610365497990611, 31.05363724282617],
           [-6.609947073384288, 31.053715368253307],
           [-6.610000717564586, 31.05400489133575],
           [-6.61040304891682, 31.05396353094933]]]),
         {
          "landcover": 110,
          "system:index": "28"
         }),
ee.Feature(
   ee.Geometry.Polygon(
         [[[-6.610091912671092, 31.052736497974337],
           [-6.61014555685139, 31.05322823219005],
           [-6.610440599843028, 31.0532190409196],
           [-6.610542523785594, 31.052764071922837]]]),
         {
          "landcover": 110,
          "system:index": "29"
         }),
ee.Feature(
   ee.Geometry.Polygon(
         [[[-6.598722483205042, 31.057808333851895],
           [-6.598717118787012, 31.05731892102478],
           [-6.59839793591424, 31.057309730149502],
           [-6.598368431615076, 31.057815226972334],
           [-6.59863397030755, 31.057810631558766]]]),
         {
           "landcover": 110,
           "system:index": "30"
          }),
ee.Feature(
   ee.Geometry.Polygon(
         [[[-6.597941960381708, 31.05775318887047],
           [-6.597936595963678, 31.057900242083257],
           [-6.598092164086542, 31.057900242083257],
           [-6.598100210713587, 31.057760081994903]]]),
         {
           "landcover": 110,
           "system:index": "31"
          }),
ee.Feature(
   ee.Geometry.Polygon(
          [[[-6.553510022445921, 31.10385688907646],
            [-6.552415681167845, 31.103776508251535],
            [-6.552353990360502, 31.104256495596253],
            [-6.553472471519712, 31.10431161360495]]]),
          {
           "landcover": 110,
           "system:index": "32"
          }),
ee.Feature(
    ee.Geometry.Polygon(
           [[[-6.584714325214347, 31.118494515375577],
             [-6.584472926403007, 31.11891013389905],
             [-6.584703596378287, 31.119011167961258],
             [-6.584944995189628, 31.11862540096327]]]),
           {
            "landcover": 110,
            "system:index": "33"
           }),
ee.Feature(
    ee.Geometry.Polygon(
           [[[-6.582624780904546, 31.122319742722272],
             [-6.582557725679174, 31.12290755463942],
             [-6.583947109948888, 31.123105021888286],
             [-6.584067809354559, 31.122508026623322],
             [-6.58328996874024, 31.122420773154648]]]),
          {
           "landcover": 110,
           "system:index": "34"
          }),
ee.Feature(
    ee.Geometry.Polygon(
           [[[-6.5855447683545405, 31.122485065191984],
             [-6.5844209227773005, 31.12248965747871],
             [-6.584375325224047, 31.122939700498637],
             [-6.585609141370898, 31.122962661819976]]]),
          {
            "landcover": 110,
            "system:index": "35"
          }),
ee.Feature(
   ee.Geometry.Polygon(
           [[[-6.567418277535843, 31.113040224314936],
             [-6.566978395257401, 31.112787623068478],
             [-6.566677987847733, 31.1131780065297],
             [-6.566991806302475, 31.113639575492094],
             [-6.567812562261032, 31.113515572110725]]]),
          {
            "landcover": 110,
            "system:index": "36"
          }),
ee.Feature(
  ee.Geometry.Polygon(
           [[[-6.580573023668476, 31.127874430756346],
             [-6.580508650652119, 31.1284530242679],
             [-6.5817666066801035, 31.128547160180062],
             [-6.581924857011982, 31.127656309270666]]]),
          {
            "landcover": 110,
            "system:index": "37"
          }),
ee.Feature(
  ee.Geometry.Polygon(
           [[[-6.556770067943695, 31.146195506693623],
             [-6.556625228656891, 31.14730655582555],
             [-6.5569149072304995, 31.147347875583463],
             [-6.557279687656525, 31.14699436151703],
             [-6.557800036205414, 31.145910855647205]]]),
          {
            "landcover": 110,
            "system:index": "38"
          }),
ee.Feature(
  ee.Geometry.Polygon(
           [[[-6.577155728025597, 31.123945316399368],
             [-6.577225465459984, 31.124429793967266],
             [-6.57757951704995, 31.12437927983399],
             [-6.577525872869652, 31.12395679693906]]]),
          {
            "landcover": 110,
            "system:index": "39"
          }),
ee.Feature(
   ee.Geometry.Polygon(
           [[[-6.609281381963161, 31.0096494613939],
             [-6.60904534756985, 31.010164406697825],
             [-6.612339100240138, 31.01090923337611],
             [-6.613004288075832, 31.010467855306388],
             [-6.6101396888479265, 31.009805784369515]]]),
          {
            "landcover": 110,
            "system:index": "40"
           })
]);

"""**Desertification detection script** developed by dr Louise Rayne* and edited in Colab by dr Filippo Brandolini^ (School of History, Classics and Archaeology, Newcastle University - Newcastle upon Tyne, UK)

*louiserayne@googlemail.com (or louise.rayne@ncl.ac.uk)

^filippo.brandolini@newcastle.ac.uk
"""

## # Function to mask clouds in Sentinel-2 imagery and extract desired bands
## Load, filter, and process Sentinel-2 imagery for a specific time period and area of interest (AOI)

# Sentinel-2 Cloud Masking Function
def maskS2clouds(image):
  # Select the QA60 band
  qa = image.select('QA60')

  # Define cloud and cirrus bit masks
  cloudBitMask = ee.Number(2).pow(10).int()
  cirrusBitMask = ee.Number(2).pow(11).int()

  # Create a mask to exclude cloudy and cirrus pixels
  mask = qa.bitwiseAnd(cloudBitMask).eq(0).And(
             qa.bitwiseAnd(cirrusBitMask).eq(0))

  # Apply the mask, scale the image, and select specific bands
  return image.updateMask(mask).divide(10000) \
      .select("B.*") \
      .copyProperties(image, ["system:time_start"]);

# Load Sentinel-2 imagery
S2 = ee.ImageCollection("COPERNICUS/S2") \
  .filterDate('2021-06-01', '2021-8-31') \
  .filter(ee.Filter.lt("CLOUDY_PIXEL_PERCENTAGE", 5)) \
  .filterBounds(AOI) \
  .map(maskS2clouds) \
  .select(['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B9', 'B10', 'B11', 'B12' ,'B8A']) \
  .median() \
  .clip(AOI);

# Calculate Tasseled Cap components and set visualization parameters

# Tasseled Cap coefficients for brightness, greenness, and wetness
S2_coeff = ee.Array([
 [0.0356, 0.0822, 0.1360, 0.2611, 0.2964, 0.3338, 0.3877, 0.3895, 0.949, 0.0009, 0.3882, 0.1366, 0.4750],
 [-0.0635, -0.1128, -0.1680, -0.3480, -0.3303, 0.0852, 0.3302, 0.3165, 0.0467 ,-0.0009, -0.4578, -0.4064, 0.3625],
 [0.0649, 0.1363, 0.2802, 0.3072, 0.5288, 0.1379, -0.0001, -0.0807, -0.0302, 0.0003, -0.4064, -0.5602, -0.1389],
]);

# Convert the Sentinel-2 image to a 1D array

arrayImage1D = S2.toArray();

## Uncomment (i.e. remove the # symbol) the line below to print the 1D array data
#print(arrayImage1D)

# Convert the 1D array to a 2D array
arrayImage2D = arrayImage1D.toArray(1);

# Perform matrix multiplication with Tasseled Cap coefficients
componentsImage = ee.Image(S2_coeff) \
  .matrixMultiply(arrayImage2D) \
  .arrayProject([0]) \
  .arrayFlatten(
    [['brightness', 'greenness', 'wetness']]);

# Visualization parameters for the Tasseled Cap components
vizParams = {
  'bands': ['brightness', 'greenness', 'wetness'],
  'min': [0.3754, -0.3038, -0.1134],
  'max': [1.5125, 0.1667, 0.0968]
};

# Merge and prepare input classes, define color map, and combine Sentinel-1 and Sentinel-2 data

# Input your classes here

# Merge different land cover classes (Desertified, Treecover, Cropland, Builtup, Bare)
landcover_train = Desertified.merge(Treecover).merge(Cropland).merge(Builtup).merge(Bare)

# Define a color map for visualizing land cover classes
sld_intervals = '<RasterSymbolizer>' + \
  '<ColorMap type="intervals" extended="False">' + \
    '<ColorMapEntry color="#ff7043" quantity="150" label="Builtup"/>' + \
    '<ColorMapEntry color="#F2F4F4" quantity="60" label="Bare"/>' + \
    '<ColorMapEntry color="#fff176" quantity="10" label="Treecover"/>' + \
    '<ColorMapEntry color="#a5d6a7" quantity="40" label="Cropland"/>' + \
    '<ColorMapEntry color="#9fa8da" quantity="110" label="Desertified"/>' + \
  '</ColorMap>' + \
'</RasterSymbolizer>'

# Sentinel-1 data collection
sentinel1 = ee.ImageCollection('COPERNICUS/S1_GRD') \
                    .filterDate('2021-06-01', '2021-8-31')

# Filter Sentinel-1 data for VV and VH polarizations in IW (Interferometric Wide) mode
vvIw = sentinel1 \
  .filter(ee.Filter.listContains('transmitterReceiverPolarisation', 'VV')) \
    .filter(ee.Filter.eq('instrumentMode', 'IW'))

# Separate Sentinel-1 data into ascending and descending passes
vvIwAsc = vvIw.filter(
  ee.Filter.eq('orbitProperties_pass', 'ASCENDING'))
vvIwDesc = vvIw.filter(
  ee.Filter.eq('orbitProperties_pass', 'DESCENDING'))

# Calculate the mean of VV polarization for both ascending and descending passes
vvIwAscDescMean = vvIwAsc.merge(vvIwDesc).select('VV').mean()

# Combine Tasseled Cap components from Sentinel-2, Sentinel-2 bands, and mean VV polarization from Sentinel-1 merged_image
merged_image = ee.Image(componentsImage.addBands(S2.addBands(vvIwAscDescMean)))

# # Train a Random Forest classifier and classify the input imagery to identify desertified areas

# Sample regions from the merged image based on the provided land cover classes
training = merged_image.sampleRegions(**{
  'collection': landcover_train,
  'properties': ['landcover'],
  'scale': 10
});

## Uncomment (i.e. remove the # symbol) the line below to print the sampled training data

# print(training)

# Create a Random Forest classifier with 10 trees and train it using the training data
classifier = ee.Classifier.smileRandomForest(10).train(**{
    'features': training,
    'classProperty': 'landcover',
    'inputProperties': ['brightness', 'greenness', 'wetness', 'B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B9', 'B10', 'B11', 'B12' ,'B8A', 'VV']
});

# Classify the input imagery using the trained classifier
classified = merged_image.classify(classifier);

# Create a binary image where desertified areas are represented as '1'
desert = classified.eq(110);

# Add and visualize Sentinel-2 imagery, Tasseled Cap components, Random Forest classification, and desertified areas on the map

# Add Sentinel-2 imagery with specific bands and visualization parameters
Map.addLayer(S2, {'bands': ['B8', 'B4', 'B3'], 'min': 0, 'max': 0.5}, 'S2', False)

# Add Tasseled Cap components image with predefined visualization parameters
Map.addLayer(componentsImage, vizParams, 'components', False)

# Add the Random Forest classification results with the defined color map
Map.addLayer(classified.sldStyle(sld_intervals), {}, 'RF_class2', False)

# Add a binary layer to visualize desertified areas with a specific color palette
Map.addLayer(desert.mask(desert), {'palette': 'F87A2D'}, 'desert', True)

# Center the map view on the Area of Interest (AOI) with a specified zoom level
Map.centerObject(AOI, 14)

# Display the map
Map

## Set Coordinates Reference Systems (CRS), For example: Morocco --> 'EPSG:32628';  WGS 84 --> 'EPSG:4326'.

pjcrs = 'EPSG:32628'

## Generate image download URL. If your ara running the code in Google Colab, the outputs are saved automatically in the sample_data folder in your Drive

geemap.ee_export_image(ee_object= componentsImage, filename='componentsImage.tif',scale=200,crs= pjcrs,region= AOI, file_per_band= False);
geemap.ee_export_image(ee_object= desert, filename='desert.tif',scale=20,crs= pjcrs,region= AOI, file_per_band= False);
geemap.ee_export_image(ee_object= S2, filename='S2.tif',scale=20,crs= pjcrs,region= AOI, file_per_band= False);