1

I am new to GEE. I'm trying to do a crop mapping for my ROI using Random Forest.

I am using Landsat8, computed index (NDVI, NDWI). Stacking all images of interest of Landsat with the needed bands, create training points for my crop, however, most of the bands in my stacked layer are now masked and classification can not be done over them.

I do not understand what the problem is! And how can I unmasked those layers!

Here is the whole script with the problem highlighted:

https://code.earthengine.google.com/d520c057fa80bb809f6654aac127ee91

// Import L8 Collection
var L8ATOM = ee.ImageCollection("LANDSAT/LC08/C02/T1_TOA");

// Filter L8 collection
var L820 = L8ATOM.filterBounds(ROI20)
            .filterDate('2020-05-01','2020-09-30') 
            .filterMetadata('CLOUD_COVER','less_than',1)
            .select(['B2','B3','B4','B5','B6','B7']);

// Visualize L8 collection for reference
// Map.addLayer(imagecollection,vis,'True color')
Map.centerObject(ROI20,10);
Map.addLayer(L820,{},'L8 Collection',false);
print(L820,'Original');

/////////////////////////// Calculate NDVI /////////////////////////// 
// NDVI function

function addNDVI(image){
  var ndvi =image.normalizedDifference (['B5','B4']).rename('NDVI');
  return image.addBands(ndvi);
}

// Map NDVI function to L8 collection 
var with_ndvi = L820.map(addNDVI);

// Visualize NDVI function for reference
var rgb_vis = {min:-1, max:1, bands:['B4', 'B3', 'B2']}; // NDVI viz parameters
Map.addLayer(with_ndvi,rgb_vis, 'NDVI',false);
print(with_ndvi,'NDVI');

/////////////////////////// Calculate NDWI /////////////////////////// 
// Calculate NDWI
function addNDWI(image){
  var ndwi =image.normalizedDifference (['B3','B5']).rename('NDWI');
  return image.addBands(ndwi);
}

// Map NDWI function to NDVI collection
var with_ndwi = with_ndvi.map(addNDWI);

// Visualize NDWI function for reference
Map.addLayer(with_ndwi ,rgb_vis, 'NDWI',false);
print(with_ndwi,'NDWI');

/////////////////////////// Create Stacked Layer  /////////////////////////// 
// Convert image collection to image for sampling
var stacked_img = with_ndwi.toBands();

print('Collection to Bands Stack', stacked_img);

// Visualize reduced image collection
Map.addLayer(stacked_img,{},'Collection to Bands Stack',false);
Map.addLayer(rice,{},'RICE shp')


// Visualize first band only to preserve memory
//Map.addLayer(stacked_img.select('LC08_174043_20200520_B2'),{},'valid band from stacked img');
Map.addLayer(stacked_img.select('LC08_177039_20200525_B2'),{},'valid band from stacked img');

///////// Visualize all unmasked bands/////////////
//Map.addLayer(stacked_img.select('LC08_177039_20200525_B2','LC08_176039_20200907_B2','LC08_176040_20200822_B2','LC08_176040_20200907_B2'),{},'valid Bands');

// Visualize reduced image collection with clip
var clipped_stack = stacked_img.clip(ROI20);
Map.addLayer(clipped_stack,{},'Clipped Stack',false);

//visualize maize///
Map.addLayer(maize,{},'maize')

/////////////////////////// Attempt Sampling  /////////////////////////// 
// Rice and maizel samples on selected unmasked band (B2)from the stacked image
var sample = RicS.merge(Maize);
var band = 'LC08_177039_20200525_B2'    // it works for my training sample only if i choose this band, however i want the classifier to work on all bands in my stacked image.
var train_img = stacked_img
              .select(band)
              .sampleRegions({
    collection: sample,
    properties: ["class"],
    scale: 30,
});
var classifier = ee.Classifier.smileRandomForest(100)
                  .train({
                    features:train_img , 
                    classProperty: 'class'
                    
                  });

var classified = stacked_img.classify(classifier, 'Classified');
//var classified1 = classified.updateMask(classified.eq(1))
Map.addLayer(classified,{palette:['beige','green'],min:0,max:1},'Classified');

print(classified)
4
  • Could you share a link to the code containing the trainSample object (and set it as public)? Commented Nov 20, 2022 at 20:47
  • This is an incomplete script and parts needed to help you are missing. Include a link to the script using the Get Link button in the EE Code Editor. Also make sure that ll assets used are shared. Commented Nov 21, 2022 at 10:41
  • Please set your assests as "anyone can read". Assets -> asset -> share -> anyone can read. Commented Nov 21, 2022 at 22:47
  • The link you provided does not match the script of the question above. Please revise your question above to match your code, or provide an updated code to match your question. Commented Nov 22, 2022 at 14:19

1 Answer 1

0

The issue with your script is caused by the with_ndwi.ToBands() operation. You can see from the image below that flattening the image collection to bands reduces the image to the extent of the first image in the collection. Your subsequent clip is then applied only to this small extent.

Flattened Extent Example

To fix this reduction of extent, you will need to reduce your collection into meaningful composites (i.e. monthly or yearly composites) so that the extent of each image matches your area of interest.

As a result of the extent of your image being limited, there are no bands from which your features can sample from, thus causing you to have 0 features in your training sample. You can see from the example below how sampling within an area that has bands returns a feature collection with elements.

Sampling Example

For reference, here is a link to your script that I modified to reproduce the examples I have shown. https://code.earthengine.google.com/98447e6535907774e3823f9aeaf5d844

3
  • Dear Andrew, thank you for your time and information. I followed your advice trying to change some filters (even work for the whole year), but the problem is still there. I even looked through the stacked image I have created and there are some bands there that cover my training area points but do not know why they are not shown or taken into consideration when I build the script. it always worked over the first bands. Commented Nov 22, 2022 at 16:28
  • Can you please link the code you are working with so I can take a look? Commented Nov 24, 2022 at 7:56
  • Dear Andrew, here is the link: code.earthengine.google.com/d520c057fa80bb809f6654aac127ee91 Commented Nov 24, 2022 at 21:40

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.