0

I've been trying to run this code on Google Earth Engine, however I get the following error and I'm not sure how I can resolve this issue:

enter image description here

//Set map centre
Map.setCenter(159.9737, -9.4336, 12.5)

//Import Sentinel-2 Imagery
var S2 = ee.ImageCollection('COPERNICUS/S2')
    .filterDate('2017-01-01', '2021-01-01')
    .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))

//clip to region of interest
var median = S2.median();
var clipped = median.clip(roi);

//add different visualisations to map
Map.addLayer(clipped, {bands: ['B4', 'B3', 'B2'], min: 301.55555555555554, max: 2162}, 'True Colour')
Map.addLayer(clipped, {bands: ['B8', 'B4', 'B3'], min: 215.9245283018868, max: 4181.5}, 'False Colour')
Map.addLayer(clipped, {bands: ['B12', 'B8', 'B4'], min: 68.14285714285714, max: 3633}, 'SWIR')

//add NDVI to map
var maskcloud1 = function(image) {
var QA60 = image.select(['QA60']);
return image.updateMask(QA60.lt(1));
};

var addNDVI = function(image) {
return image.addBands(image.normalizedDifference(['B8', 'B4']));
};

var S2 = S2.map(addNDVI);

var NDVI = S2.select(['nd']);
var NDVI = NDVI.median();

var ndvi_pal = ['FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901', '66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01', '012E01', '011D01', '011301'];

Map.addLayer(NDVI.clip(roi), {min:-0.5, max:0.9, palette: ndvi_pal}, 'NDVI');

//merge training points
var points = Water.merge(BuiltUp).merge(Road).merge(TreeShrub).merge(GrassBare)
print(points,'points');


var bands = ["B1","B2","B3","B4","B5","B6","B7","B10","B11","B12"];
var label = 'LandCover';

//sample training points to generate training data
var training = clipped.select(bands).sampleRegions({
  collection: points,
  properties: ['LC'],
  scale: 30
});

print(training, 'training');

//train a CART classifier
var classifier = ee.Classifier.smileCart().train({
features: training,
classProperty: "LandCover",
inputProperties: bands});

//run final classification
var classified_image = clipped.select(bands).classify(classifier); 

print(classifier,'classifier')

Map.addLayer(classified_image, {palette: ['blue','green', 'red']},
'Classified');

How can I resolve this?

1 Answer 1

0

We cannot reproduce your error without a functioning script, perhaps include your assets or featuers that you are using. But most likely your error is related to: classProperty: "LandCover" inside the .train() function. You are most likely going to have to change that property to 'LC' or the properties you have in in your featureCollection() called 'training'

3
  • Changing the classProperty to 'LC' seemed to fix the issue. Thanks!
    – Danee
    Commented Mar 31, 2022 at 5:06
  • For future reference it is helpful if you accept the answer, glad it worked
    – Jobbo90
    Commented Mar 31, 2022 at 6:39
  • same line of codes for another image not working 'LC' in the above error. i tried var training = Sentinel_crop.select(bands).sampleRegions({ collection:points, properties: ['LC'], scale: 20 }); var classifier= ee.Classifier.smileRandomForest(10).train({ features: training, classProperty: 'LC', inputProperties: bands}); //print(classifier) //Classifying the input imagery var classification= Sentinel_crop.select(bands).classify(classifier)
    – user28542
    Commented Aug 17, 2023 at 6:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.