2

I would like to analyze NDVI in the region of Bangkok.

Here is the code :

{    
    //Define time range
    var startyear = 2006;
    var endyear = 2016;

    //Set date in ee date format
    var startdate = ee.Date.fromYMD(startyear, 1, 1);
    var enddate = ee.Date.fromYMD(endyear, 12, 31);

    ///Import a fusion table of your study area
    // The Ca basin
    var Ca = ee.FeatureCollection(geometry)

    //filter on date and area (bounds)
    var l5images = l5.filterDate(startdate,enddate).filterBounds( Ca);
    var l7images = l7.filterDate(startdate,enddate).filterBounds( Ca);
    var l8images = l8.filterDate(startdate,enddate).filterBounds( Ca);

    ///Include a funtion to remove the clouds 
    //set cloud threshold
    var cloud_threshold = 40;

    // Select the red and NIR bands
    l5images = l5images.select(["B4","B3"]);
    l7images = l7images.select(["B4","B3"]);
    l8images = l8images.select(["B5","B4"]);

    // calculate ndvi from landsat 8
    function l8ndvi(img) {
      var ndvi = img.normalizedDifference(['B5', 'B4']).rename('NDVI');
      return img.addBands(ndvi);
    }

    // calculate ndvi from landsat 5 & 7
    function l57ndvi(img) {
      var ndvi = img.normalizedDifference(['B4', 'B3']).rename('NDVI');
      return img.addBands(ndvi);
    }


    // calculate ndwi for each image in imagecollection
    var l5ndvi = l5images.map(l57ndvi);
    var l7ndvi = l7images.map(l57ndvi);
    var l8ndvi = l8images.map(l8ndvi);

    // combine all data in single image collection
    var allcollection =  ee.ImageCollection((l5ndvi.merge(l7ndvi)).merge(l8ndvi));

    // add map to canvas
    Map.addLayer(allcollection);
    Map.centerObject(allcollection,8);
    print()
}

Google Earth Engine Link that I used

But then there is trouble, but I don't really understand what is it about. enter image description here

So Is there anything I can do about this. I am still very new to programming.

1
  • The message means that Earth Engine is trying to combine B4, B3 and NDVI into a single collection, but that something is different about them which is preventing that. I suspect that the data type of the NDVI band is different to B4 and B3, or that the object type is of NDVI is unknown, so you need to cast NDVI to the right data or object type. See here: developers.google.com/earth-engine/tutorial_js_02#casting – sbphd Feb 13 '20 at 10:33
1

NormalizedDifference() returns floating point values. Your band B3/B4/B5 are probably of some integer value. GEE cannot make composites with difference band types. Easiest solution would be casting the input B3-4-5 bands to floating point numbers. Change the following function of your code, and I think it should work fine (note that your link doesn't work for me).

// calculate ndvi from landsat 8
function l8ndvi(img) {
  var ndvi = img.normalizedDifference(['B5', 'B4']).rename('NDVI');
  return img.toFloat().addBands(ndvi);
}

// calculate ndvi from landsat 5 & 7
function l57ndvi(img) {
  var ndvi = img.normalizedDifference(['B4', 'B3']).rename('NDVI');
  return img.toFloat().addBands(ndvi);
}
0

I'd suggest that you comment out these lines and let us know if it works.

// Select the red and NIR bands
    l5images = l5images.select(["B4","B3"]);
    l7images = l7images.select(["B4","B3"]);
    l8images = l8images.select(["B5","B4"]);

If it works, I'll explain what I have in mind. I'm also a newbie, so maybe my hypothesis is wrong.

2
  • This isn't an answer, it's a comment – nmtoken Feb 14 '20 at 16:44
  • Thx for the reply @nmtoken. I didn't make myself clear, sorry. My answer was "comment out these lines of codes... It should work. Let me know." – VuTien Khang Feb 15 '20 at 12:46

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.