1

I try crop classification for wheat crops in my study area. My code very well works for the 2016-2019 years. But for the 2020 year, my code did not work.

Here is my code:

var Polygon = 
    /* color: #d63000 */
    /* displayProperties: [
      {
        "type": "rectangle"
      }
    ] */
    ee.Geometry.Polygon(
        [[[9.671764962717377, 50.72618024130225],
          [9.671764962717377, 49.65178588495313],
          [11.182385079904877, 49.65178588495313],
          [11.182385079904877, 50.72618024130225]]], null, false);




//Import S2 image collection, // Define a cloud masking function and Selected Boundary // Selected period  
var collection4 = ee.ImageCollection('COPERNICUS/S2').filterMetadata('CLOUDY_PIXEL_PERCENTAGE', 'less_than', 20).filterBounds(Polygon).filterDate('2020-05-01', '2020-05-15');
var image4 = collection4.mean();


// Define an index function (selected NDVI value).
var ndvi4 = image4.normalizedDifference(['B8', 'B4']).gt(0.4);

//Import S2 image collection, // Define a cloud masking function and Selected Boundary // Selected period  
var collection5 = ee.ImageCollection('COPERNICUS/S2').filterMetadata('CLOUDY_PIXEL_PERCENTAGE', 'less_than', 20).filterBounds(Polygon).filterDate('2020-06-15', '2020-06-30');
var image5 = collection5.mean();

// Define an index function (selected NDVI value).
var ndvi5 = image5.normalizedDifference(['B8', 'B4']).lt(0.3);

//Import S2 image collection, // Define a cloud masking function and Selected Boundary // Selected period  
var collection6 = ee.ImageCollection('COPERNICUS/S2').filterMetadata('CLOUDY_PIXEL_PERCENTAGE', 'less_than', 20).filterBounds(Polygon).filterDate('2020-07-01', '2020-07-15');
var image6 = collection6.mean();

// Define an index function (selected NDVI value).
var ndvi6 = image6.normalizedDifference(['B8', 'B4']).lt(0.3);

//NDVI collection 
var wheat = (ndvi4).and(ndvi5).and(ndvi6)



var wheatViz = {min: 0, max: 1, palette: ['#FFFFFF00', 'blue']}
//var wheatMask = wheat.eq(1)
var ndviMasked = wheat.updateMask(wheat.gte(0.5))
Map.addLayer(ndviMasked.clip(Polygon), wheatViz, 'wheat')

1 Answer 1

1

Your filter ee.ImageCollection('COPERNICUS/S2').filterMetadata('CLOUDY_PIXEL_PERCENTAGE', 'less_than', 20).filterBounds(Polygon).filterDate('2020-07-01', '2020-07-15') produces no images. Then, when you take the .mean() of that, you get an image with no bands, so the attempt to compute the normalized difference produces the error you see.

I found this out by modifying your wheat expression to see which of its component images produced the error, then confirmed it by adding print(collection6); which reported the image collection had 0 images.

Next, I took a look at the collection without the CLOUDY_PIXEL_PERCENTAGE filter. There are only 2 images in that date range and region, and both of them have CLOUDY_PIXEL_PERCENTAGE greater than 75%.

You will have to skip analyzing that date range.

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.