GEE- When I mask two years landsat image with one year data, the error: If one image has no bands, the other must also have no bands. Got 0 and 1

by Chenlu Huang   Last Updated April 16, 2019 03:22 AM

I would like to mask the one year Landsat 5 surface reflectance data with two years landsat 5 surface reflectace data with every 15 days. However, I cannot mask successfully if the one year Landsat 5 sr data not include any image. I use this code as following:

// Load a collection of Landsat 5 surface reflectance images.
var L5coll = ee.ImageCollection('LANDSAT/LT05/C01/T1_SR')

// Load BOUND 
var BOUND = ee.Geometry.Rectangle(108.68, 35.61, 108.93, 35.55);
Map.addLayer (BOUND)

// Add NDVI band to image.
var addNDVI = function(image) {
    var ndvi = image.normalizedDifference(['B4', 'B3']).rename('NDVI');
    return image.addBands(ndvi);

  // Remove cloud for Landsat surface reflectance data
function maskL5sr(image) {
  // Bits 3 and 5 are cloud shadow and cloud, respectively.
  var cloudShadowBitMask = (1 << 3);
  var cloudsBitMask = (1 << 5);
  // Get the pixel QA band.
  var qa ='pixel_qa');
  // Both flags should be set to zero, indicating clear conditions.
  var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0)
  return image.updateMask(mask);

// Calculate the basic NDVI (one year)
     var Oneyear =  L5coll
            // return L5coll
print (Oneyear,'Oneyear')
Map.addLayer (Oneyear,{},'Oneyear')

// Calculate the two years ndvi (have same days with 'basicndvi').
      var Twoyears = L5coll
print (Twoyears ,'Twoyears ')
Map.addLayer (Twoyears ,{},'Twoyears')

// Seperating composite into two parts: exist value is origin value, null value is 0
var divide = Oneyear.mask(1).rename('Oneyear')

// Calculate finalndvi through replace the value is 0 of Oneyear into climatology 
var finalndvi = (divide.where(divide.eq(0),Twoyears));

print (finalndvi,'finalndvi')
Map.addLayer (finalndvi,{},'finalndvi')

// Seperating finalndvi into two parts: exist value is 1, null value is 0
var mask = finalndvi.mask().rename('mask');

// Calculate the area through multiply pixelArea by mask
var area = ee.Image.pixelArea().multiply(mask).rename('area');

// Calculate the pixelarea as a proportion of the area of the study area.
var percent = area.divide(300).divide(10000).rename('percent');

// Sum the percent in the study area.
var calculate = percent.reduceRegion({

// Change the calculate into Number.
var percentnumber = ee.Number(calculate.get('percent'))

var ndviwithpercent = finalndvi.set('percentnumber',percentnumber)

print (ndviwithpercent,'ndviwithpercent')
Map.addLayer (ndviwithpercent,{},'ndviwithpercent')

Answers 1

The problem is that there's no imagery during your Oneyear time window. By changing the date filter when assigning that variable to .filterDate('1987-02-01','1987-03-15') the code runs fine.

April 16, 2019 03:16 AM

Related Questions

Updated February 20, 2017 05:22 AM

Updated July 06, 2018 09:22 AM

Updated June 09, 2016 08:09 AM

Updated September 05, 2017 09:22 AM