Search code examples
google-earth-engine

Supervised Classification with the Google Earth Engine


For a classification of the Cerrado (a region in Brazil) I have chosen to do a supervised classification using the Google Earth Engine. When running the script an error message pop's up in the console stating: "classified: Layer error: Property 'agriculture' of feature '0_0' is missing." This doesn't seems to be the problem for my other classes (city and forest) while I do exactly the same.

city, agriculture and forest are polygons drawn with the polygon tool. landsat is the image I want to classify. Goias is the region I'm focussing on.

What is the meaning of this feature '0_0'?

var forest = /* color: #0b4a8b */ee.Geometry.MultiPolygon(
        [[[[-49.95376110076904, -15.710967112810327],
           [-49.95560646057129, -15.714189425198422],
           [-49.952216148376465, -15.71480909481255],
           [-49.950928688049316, -15.709603811453348],
           [-49.955735206604004, -15.70952118683543]]],
         [[[-50.87038993835449, -14.062653514869359],
           [-50.854811668395996, -14.071645244690382],
           [-50.84751605987549, -14.064984738032894],
           [-50.8626651763916, -14.058906856438178]]],
         [[[-50.811166763305664, -14.182430235294726],
           [-50.838375091552734, -14.184677018750275],
           [-50.833847522735596, -14.201195329073467],
           [-50.80580234527588, -14.200300844289204]]],
         [[[-50.632081031799316, -14.484754464079412],
           [-50.6339693069458, -14.49414484704292],
           [-50.60023784637451, -14.4989645351375]]],
         [[[-50.56989669799805, -14.577186033177513],
           [-50.56955337524414, -14.552928914937938],
           [-50.57933807373047, -14.566220817061826]]],
         [[[-50.89845657348633, -18.015384864469013],
           [-50.89292049407959, -18.01269129442521],
           [-50.898070335388184, -18.01093637057725]]],
         [[[-50.92931270599365, -18.013793214420286],
           [-50.925493240356445, -18.018690553238297],
           [-50.92193126678467, -18.013017789956983],
           [-50.92467784881592, -18.011058807691924]]],
         [[[-50.90643882751465, -18.011058807691924],
           [-50.90373516082764, -18.01036499625073],
           [-50.90437889099121, -18.006283697189883],
           [-50.90643882751465, -18.006283697189883]]],
         [[[-50.87399482727051, -17.982896032077118],
           [-50.878329277038574, -17.97950803295561],
           [-50.88137626647949, -17.985794148396778],
           [-50.884766578674316, -17.984651234967618],
           [-50.8857536315918, -17.985794148396778],
           [-50.882019996643066, -17.990651447894532]]],
         [[[-50.917725563049316, -17.979059016144365],
           [-50.914249420166016, -17.981671461584988],
           [-50.90897083282471, -17.97697719600834],
           [-50.90939998626709, -17.974936172047833],
           [-50.91085910797119, -17.976650633759945]]],
         [[[-50.90394973754883, -17.97358908331176],
           [-50.899529457092285, -17.97501781345918],
           [-50.89794158935547, -17.971466377179688],
           [-50.902276039123535, -17.96795569182738],
           [-50.90682506561279, -17.96909871332269]]]]),
    goias = /* color: #ffc82d */ee.Geometry.Polygon(
        [[[-53.89773987402128, -17.737335150077946],
          [-53.21656208374657, -18.780701454086767],
          [-50.93127514381945, -19.71426205863078],
          [-46.953989190809466, -18.46836077330379],
          [-45.76744438500839, -14.848185932899527],
          [-45.899312773543386, -12.69278776942265],
          [-48.86573729240354, -12.585603620983632],
          [-50.2225991477078, -12.338855625969925],
          [-51.73879790880113, -15.224867030408598],
          [-52.07001767927636, -15.322314089978098],
          [-52.44591109792566, -15.65224412638864]]]),
    city = /* color: #00ffff */ee.Geometry.MultiPolygon(
        [[[[-47.92356491088867, -15.824128602644778],
           [-47.92081832885742, -15.837010357573558],
           [-47.90245056152344, -15.826275618823361],
           [-47.8809928894043, -15.80563028827748],
           [-47.89266586303711, -15.798692984390442]]],
         [[[-48.13058853149414, -15.823798290439452],
           [-48.11857223510742, -15.823302821119514],
           [-48.108787536621094, -15.831725634384997],
           [-48.12681198120117, -15.841799518532302],
           [-48.11634063720703, -15.851047232007138],
           [-48.100032806396484, -15.836349774735691],
           [-48.07668685913086, -15.830899883899415],
           [-48.09385299682617, -15.800840270519277],
           [-48.11634063720703, -15.799188513979754],
           [-48.12440872192383, -15.786469537412923],
           [-48.140201568603516, -15.790103612200987]]],
         [[[-48.07342529296875, -15.804474087471961],
           [-48.05419921875, -15.826771080856748],
           [-48.060035705566406, -15.798032276385078],
           [-48.073768615722656, -15.803317880062274]]],
         [[[-48.1230354309082, -15.884896851962742],
           [-48.119773864746094, -15.898930392135671],
           [-48.06364059448242, -15.87944827271288],
           [-48.07394027709961, -15.855505799598921]]],
         [[[-48.06621551513672, -15.912632786660408],
           [-48.04939270019531, -15.922702625654379],
           [-48.05574417114258, -15.908340570752602],
           [-48.04269790649414, -15.902892626277593],
           [-48.04664611816406, -15.899425675672019],
           [-48.07119369506836, -15.902232259341346]]],
         [[[-47.78572082519531, -15.89992095798885],
           [-47.77627944946289, -15.907515134108964],
           [-47.769412994384766, -15.903718081898417],
           [-47.77799606323242, -15.891666093512773]]],
         [[[-47.88991928100586, -15.846423428228759],
           [-47.87395477294922, -15.85055182952652],
           [-47.867088317871094, -15.841634376941688],
           [-47.8754997253418, -15.831065034266503]]],
         [[[-48.035831451416016, -15.806786482478387],
           [-48.015403747558594, -15.814714493330174],
           [-48.00870895385742, -15.790433979404416],
           [-48.04664611816406, -15.793902802515186]]],
         [[[-47.849063873291016, -15.745168658461244],
           [-47.837562561035156, -15.758881482187945],
           [-47.8318977355957, -15.768298186245215],
           [-47.83498764038086, -15.749629558126074],
           [-47.87137985229492, -15.724184595030325]]],
         [[[-49.28895950317383, -16.659872383959307],
           [-49.29119110107422, -16.668259437653628],
           [-49.280033111572266, -16.683552530348035],
           [-49.27196502685547, -16.66020129494842],
           [-49.28861618041992, -16.658721191047853]]],
         [[[-49.28947448730469, -16.722519717319],
           [-49.279518127441406, -16.71840962984931],
           [-49.26338195800781, -16.727451705356305],
           [-49.2597770690918, -16.715121496109433],
           [-49.27042007446289, -16.713970635912027],
           [-49.289817810058594, -16.693911672384527]]],
         [[[-49.34560775756836, -16.770847694791637],
           [-49.33479309082031, -16.77528536317065],
           [-49.3260383605957, -16.770354614132874],
           [-49.34080123901367, -16.763944449213486]]],
         [[[-49.33582305908203, -16.655596489686744],
           [-49.33067321777344, -16.666450496373013],
           [-49.321231842041016, -16.6666149462869],
           [-49.32294845581055, -16.65115603689586],
           [-49.3121337890625, -16.641123523891356],
           [-49.323463439941406, -16.64013669096455],
           [-49.34440612792969, -16.652307275281608]]]]),
    image = ee.ImageCollection("LANDSAT/LC8_SR"),
    agriculture = /* color: #d63000 */ee.Geometry.MultiPolygon(
        [[[[-45.955810546875, -14.076641147746095],
           [-45.924224853515625, -14.061988097202269],
           [-45.797882080078125, -13.963388675891327],
           [-45.8734130859375, -13.928069145372694],
           [-45.99220275878906, -13.998036539606128]]],
         [[[-45.874786376953125, -14.12325823495085],
           [-45.874786376953125, -14.152555522222587],
           [-45.87409973144531, -14.18917181904185],
           [-45.8349609375, -14.19383165093469],
           [-45.78620910644531, -14.148560659841655],
           [-45.847320556640625, -14.06065595513735],
           [-45.913238525390625, -14.099284922844143]]],
         [[[-46.13433837890625, -14.138573196735809],
           [-46.13433837890625, -14.09662106589108],
           [-46.175537109375, -14.05266293986954],
           [-46.198883056640625, -14.056659482401876],
           [-46.21124267578125, -14.1325805083437]]],
         [[[-46.201629638671875, -13.81274334991904],
           [-46.193389892578125, -13.91607260401736],
           [-46.12335205078125, -13.872079964542497],
           [-45.962677001953125, -13.82941246714161],
           [-45.935211181640625, -13.749389841111972],
           [-46.1810302734375, -13.736050073136349]]],
         [[[-46.21467590332031, -13.558559225740643],
           [-46.14738464355469, -13.571241563074146],
           [-46.138458251953125, -13.559226734063092],
           [-46.19407653808594, -13.532524939185093]]],
         [[[-45.578155517578125, -14.576938127714245],
           [-45.46073913574219, -14.455293324015502],
           [-45.49919128417969, -14.449973994986367],
           [-45.65574645996094, -14.572286279238748],
           [-45.69419860839844, -14.629430744817604],
           [-45.61180114746094, -14.584912496585007]]],
         [[[-45.59497833251953, -15.266631865983076],
           [-45.533180236816406, -15.270275113711289],
           [-45.51807403564453, -15.289152747755958],
           [-45.518760681152344, -15.275905463068401],
           [-45.517730712890625, -15.23085844143626],
           [-45.487518310546875, -15.227877047992555],
           [-45.52082061767578, -15.197729476629094],
           [-45.56304931640625, -15.205018295784908],
           [-45.552406311035156, -15.252720702239158]]],
         [[[-45.591888427734375, -15.199386048559994],
           [-45.560646057128906, -15.198060792056202],
           [-45.554466247558594, -15.175530157957471],
           [-45.607337951660156, -15.167577595475684]]],
         [[[-45.751190185546875, -15.428529514464561],
           [-45.748443603515625, -15.405361690934004],
           [-45.718231201171875, -15.400065825806683],
           [-45.753936767578125, -15.366301517721318],
           [-45.775909423828125, -15.413305235719099]]],
         [[[-48.28216552734375, -16.494855107721587],
           [-48.25864791870117, -16.497324042366905],
           [-48.26774597167969, -16.520695062112992],
           [-48.23530197143555, -16.51460570242487],
           [-48.23616027832031, -16.485143659165615],
           [-48.278045654296875, -16.479217790217742]]]]);
           
var bands = ['B2','B3','B4','B5','B6','B7'];

var cloudy_water_landsat = ee.ImageCollection('LANDSAT/LC8_SR')
    .filterBounds(goias)
    .filterDate('2016-06-01', '2016-07-10')
    .median()
    .set('SENSOR_ID', 'OLI_TIRS');

var datamask =  cloudy_water_landsat.select('cfmask_conf');
var mask = datamask.eq(1);
var water_landsat = cloudy_water_landsat.updateMask(mask);

var datawater =  water_landsat.select('cfmask');
var not_water = datawater.eq(1);
var water = not_water.not();
var landsat = water_landsat.updateMask(water);

var RGB = {
  bands: ['B4', 'B3', 'B2'],
  min: -2000,
  max: 2000,
  gamma: [1,1,1],
  };

Map.addLayer(landsat, RGB, 'RGB');

var agriculture = ee.Feature (landsat.clip(agriculture));
var city = ee.Feature (landsat.clip(city));
var forest = ee.Feature (landsat.clip(forest));

var polygons = ee.FeatureCollection([agriculture,city,forest]);
  
var training = landsat.sampleRegions(
  {
  collection: polygons,
  properties: ['agriculture','city', 'forest'],
  scale: 500
  });

var classifier = ee.Classifier.svm(
  {
  kernelType: 'RBF',
  gamma: 0.1,
  cost: 10
  });

var trained = classifier.train(training, ('agriculture','city', 'forest'), bands);

var classified = landsat.classify(trained);

Map.addLayer(classified,{},'classified');
Map.setCenter(-49.515, -15.824);


Solution

  • The second argument that you pass to classifier.train() is the name of the band with class property. This should be an integer. You're never adding a class band to the landsat pixels, so it fails on the first feature of the feature collection (the feature with the id 0_0). JavaScript doesn't have tuples like Python (or whichever language you're more used to), so the code ('agriculture','city', 'forest') resolves to the last element in the parens (in this case, forest). So you're telling GEE that your class band is forest, and when it doesn't find a forest band it throws an error. It will be clear that you're not adding a class band if you print the first element of the feature collection using print(training.first());

    What you should be doing instead is adding a band to the pixels under your different polygons, something like landType, which will have a value of 0, 1 or 2 depending on whether it is agriculture, city or forest.