I learned supervised classification through the example of Google Earth Engine, but in this example, I found that I cannot use Map.addLayer () to visualize the training data and test data. When I use this code:

Map.addLayer (trainingPartition, {'color': '# ff0000'}, 'train');
Map.addLayer (testingPartition, {'color': '# 3b8b00'}, 'test');

Nothing appears on the screen. Does anyone know why this is the case? Here are Key codes:

// Optionally, do some accuracy assessment.  Fist, add a column of
// random uniforms to the training dataset.
var withRandom = training.randomColumn('random');
// We want to reserve some of the data for testing, to avoid overfitting the model.
var split = 0.7;  // Roughly 70% training, 30% testing.
var trainingPartition = withRandom.filter(ee.Filter.lt('random', split));
var testingPartition = withRandom.filter(ee.Filter.gte('random', split));

Here is link: https://code.earthengine.google.com/1c2f985baf131e88b3dd96fbbab9b588


If you add a feature collection to the map, and you do not see anything, then either the geometry is not in the visible area (viewport) of the map, or it does not have geometry. In order to check this, I added this line to your code:

print(trainingPartition, testingPartition);

Examining the output shows that indeed, each feature's geometry is null. I then looked back for where these feature collections' data came from, and the answer was this part of your code:

var training = composite.select(bands).sampleRegions({
  collection: newfc,
  properties: [classProperty],
  scale: 30

Reading the documentation for Image.sampleRegions, I find:

geometries (Boolean, default: false):
If true, the results will include a geometry per sampled pixel. Otherwise, geometries will be omitted (saving memory).

This explains why there are no geometries. So, you should make the following modification:

var training = composite.select(bands).sampleRegions({
  collection: newfc,
  properties: [classProperty],
  scale: 30,
  geometries: true,

Then the points will appear on the map.

Note that the problem was not in the part of the code that you posted in the question, only in the link. To help people answer your questions and make them more generally useful, please try in future questions to reduce your code to a mimimal example demonstrating the problem and post all of that code. In this case, you could have tried adding training to the map, and seen that those points also did not appear; then you know that you can remove everything after that (including actually training the classifier) as not relevant.

This not only helps you write a better question, but can also help you find the problem yourself.

  • Kevin.You not only solve my code problem,but give me a best answer about how to find problem myself.It is matters.Thanks! – peng Fang Dec 27 '19 at 0:58

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.