Google Earth Engine – Calculate Landsat 7 NDVI Time Series and Get Maximum NDVI Value

google-earth-engine

I want to to calculate NDVI time series from 2015 to 2018 using Landsat 7 and get the maximum NAVI value in this period and it's corresponding image index. That is, the horizontal and vertical axes value of this picture, I want to get the Image index in Google Earth Engine, not the time.

code

var landsat7_SR = ee.ImageCollection("LANDSAT/LE07/C01/T1_SR")
                    .filterBounds(geometry); 
var ndvi_list = landsat7_SR.filterDate("1999-01-01", "2018-04-30")
               .map(function(image) { 
                 var ndvi=image.expression('float(b("B4") - b("B3")) / (b("B4") + b("B3"))').rename('ndvi');
                 return image.addBands(ndvi); 
                });

Best Answer

To get the maximum ndvi you could use qualityMosaic function. To be able to know which image ID the pixel comes from is more complicated because as it is not a numerical property, you have to encode it some how. This is how I'd do it, but it's just one way, there could be other ways (may be easier)

l7 = ee.ImageCollection("LANDSAT/LE07/C01/T1_SR")
l7 = l7.filterDate("1999-01-01", "2018-04-30").filterBounds(geometry)
       .map(function(image) { 
          var ndvi=image.expression('float(b("B4") - b("B3")) / (b("B4") + b("B3"))').rename('ndvi');
          ndvi = ndvi.multiply(100)
          return image.addBands(ndvi); 
        })
        // add date band
        .map(function(img){
          var millis = img.date().millis()
          var days = millis.divide(1000).divide(60).divide(60).divide(24).toInt() // 86400000
          var dateband = ee.Image.constant(days).rename('date')
          return img.addBands(dateband)
        })
        // add PATH ROW band
        .map(function(img){
          var path = ee.Number(img.get('WRS_PATH')).toInt()
          var row = ee.Number(img.get('WRS_ROW')).toInt()
          var pathi = ee.Image.constant(path).rename('path')
          var rowi = ee.Image.constant(row).rename('row')
          return img.addBands(pathi).addBands(rowi)
        })
        // cast to Int
        .map(function(img){
          return img.toUint16()
        })

// function to get the image ID from date, path and row
var get_id = function(date, path, row) {
  var d = ee.Date(ee.Number(date).multiply(86400000))
  var i = l7.filterDate(d, d.advance(1, 'day'))
            .filterMetadata('WRS_PATH', 'equals', path)
            .filterMetadata('WRS_ROW', 'equals', row)

  return ee.Image(i.first()).id()
}

// apply qualityMosaic
var mosaic = l7.qualityMosaic('ndvi')
Map.addLayer(mosaic, {bands:['ndvi'], min:0, max:80})

// function to be able to print the image ID when clicking on the Map over the image
var click = function(coords) {
  var lon = coords.lon
  var lat = coords.lat
  var p = ee.Geometry.Point([lon, lat])
  var values = mosaic.reduceRegion(ee.Reducer.first(), p, 30)
  var date = get_id(values.get('date'), values.get('path'), values.get('row'))
  print('ID on point', p, date)
}
Map.onClick(click)