I have problem with performing cropland classification for large areas. I have training data based on ESRIshapefile which represents crop type. Then next step in order to not confuse other landcover with cropland I manually added Non_cropland featurecollection into the Google Eart Engine Platform with settin up property class with integer value. Finally, when I would like to merge them in order to train model. I got an error. Could anybody answer for this question?

Here is my script https://code.earthengine.google.com/?scriptPath=users%2Faidosmakhanov%2Fwork_2021%3ASelected_region_2019

// Import country boundaries feature collection.
var dataset = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017');

// Apply filter where country name equals Kazakhstan.
var kazakhstan = dataset.filter(ee.Filter.eq('country_na', 'Kazakhstan'));

// Print the "kazakhstan" object and explore features and properties.
// There should only be one feature representing Kazakhstan.
print('Kazakhstan feature collection:', kazakhstan);

// Convert the Kazakhstan boundary feature collection to a line for map display.
var kazakhstanBorder =
    ee.Image().byte().paint({featureCollection: kazakhstan, color: 1, width: 3});

// Set map options and add the kazakhstan boundary as a layer to the map.
Map.centerObject(kazakhstan, 5);

Map.addLayer(kazakhstanBorder, null, 'Kazakhstan border');
Map.addLayer(aoi, {color: 'white', strokeWidth: 5}, 'Regions', true, 0.6);

// Import the Kazakhstan's all cropland map.
var all_cropland = ee.FeatureCollection(
//var filtered_kostanay = (all_cropland)
  //                      .filter(ee.Filter.eq("region","Костанайская область"));
// Import the Kazakhstan's all cropland map.
var all_cropland = ee.FeatureCollection(

var selected_region = ee.List(["Костанайская область", 'Акмолинская область', 'Карагандинская область', 'Павлодарская  область','Западно-Казахстанская область' ]); 

var filtered_selected_region = all_cropland.filter(ee.Filter.inList("region",selected_region));
Map.addLayer(filtered_selected_region, {color: 'yellow'}, 'Croplands');

print ('filtered_selected_region', filtered_selected_region.first())

// Display training points

Map.addLayer(trainingPts, {color: 'red'}, 'trainingPts');
print(trainingPts, "trainingPts")

// Function to mask cloud from built-in quality band
// information on cloud
var maskcloud1 = function(image) {
var QA60 = image.select(['QA60']);
return image.updateMask(QA60.lt(1));

// Create image collection of S-2 imagery    
var S2_spring = ee.ImageCollection('COPERNICUS/S2')
               .filterDate('2019-06-01', '2019-08-15')
                          .map(function(image){return image.clip(aoi)})

// Display the mosaic.
    S2_spring, {bands: ['B11', 'B8', 'B3'], min: 225, max: 4000}, 'S2 mosaic');
// Specify and select bands that will be used in the classification.
var bands = [
  'B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B9', 'B10', 'B11',
var imageCl = S2_spring.select(bands);

var trainingPts = traininpoints.merge(Non_cropland);

// Overlay the training points on the imagery to get a training sample; include
// the crop classification property ('class') in the sample feature collection.
var training = imageCl
                     collection: trainingPts,
                     properties: ['class'],
                     scale: 30,
                     tileScale: 8
                       'B1', null)); // Remove null pixels.
// Train a CART classifier with default parameters.
var trainedCart = ee.Classifier.smileCart().train(
    {features: training, classProperty: 'class', inputProperties: bands});

// Train a random forest classifier with default parameters.
var trainedRf = ee.Classifier.smileRandomForest({numberOfTrees: 10}).train({
  features: training,
  classProperty: 'class',
  inputProperties: bands

// Classify the image with the same bands used for training.
var classifiedCart = imageCl.select(bands).classify(trainedCart);
var classifiedRf = imageCl.select(bands).classify(trainedRf);

// Define visualization parameters for classification display.
var classVis = {min: 0, max: 3, palette: ['f2c649', '484848',  '008000', 'ffff99']};

// Add the output of the training classification to the map.
Map.addLayer(classifiedCart.clipToCollection(aoi), classVis, 'Classes (CART)');
    classifiedRf.clipToCollection(aoi), classVis, 'Classes (RF)');

// Calculate the training error matrix and accuracy for both classifiers by
// using the "confusionMatrix" function to generate metrics on the
// resubstitution accuracy.
var trainAccuracyCart = trainedCart.confusionMatrix();
var trainAccuracyRf = trainedRf.confusionMatrix();

// Print model accuracy results.
print('##### TRAINING ACCURACY #####');
print('CART: overall accuracy:', trainAccuracyCart.accuracy());
print('RF: overall accuracy:', trainAccuracyRf.accuracy());
print('CART: error matrix:', trainAccuracyCart);
print('RF: error matrix:', trainAccuracyRf);

// Calculate area of each class (based on RF) in square meters.
var areaImage = ee.Image.pixelArea().addBands(classifiedCart);
var areas = areaImage.reduceRegion({
  reducer: ee.Reducer.sum().group({
    groupField: 1,
    groupName: 'class',
  geometry: aoi.geometry(),
  scale: 500,
  maxPixels: 1e13,
  tileScale: 8 

// Print the area calculations.
print('##### CLASS AREA SQ. METERS #####');

Then I recieved this type of error enter image description here


You get that error message because you try to display an undefined variable on the map: your variable "trainingPts" isn't yet defined on line 40. Try moving it down to below line 64:

var trainingPts = traininpoints.merge(Non_cropland);

// Display training points
Map.addLayer(trainingPts, {color: 'red'}, 'trainingPts');
print(trainingPts, "trainingPts")


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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