0

I'm starting with earth engine (ee) coding. Following instructions from https://developers.google.com/earth-engine/tutorial_api_07 I was able to put together some code and get a plot. But why the range of date on the plot goes from 2016 to 2018 while filterDate('2017-01-01', '2017-12-31')?

var image = ee.Image(
  s2.filterBounds(point)
    .filterDate('2017-01-01', '2017-12-31')
    .sort('CLOUD_COVER')
    .first()
);

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

var ndvi = addNDVI(image).select('NDVI');

var withNDVI = s2.map(addNDVI);

var chart = ui.Chart.image.series({
  imageCollection: withNDVI.select('NDVI'),
  region: point,
  reducer: ee.Reducer.first(),
  scale: 30
}).setOptions({title: 'NDVI over time'});

print(chart);

enter image description here

Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97
KcFnMi
  • 5,516
  • 10
  • 62
  • 136

1 Answers1

0

Your code does not produced the expected chart with date range becasue you are setting the result from the temporal filtering to an image (ee.Image(...first()) and then using the original s2 image collection for the NDVI calculations and charting. Your code should look something like this where you set the results from filtering to an image collection variable and use that in the NDVI function and plotting:

var s2 = ee.ImageCollection("COPERNICUS/S2"),
    point = ee.Geometry.Point([-86.54734555291998, 34.74135144079877]);

var filteredIC = s2.filterBounds(point)
    .filterDate('2017-01-01', '2017-12-31')
    .sort('CLOUD_COVER')

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

var withNDVI = filteredIC.map(addNDVI);

var chart = ui.Chart.image.series({
    imageCollection: withNDVI.select('NDVI'),
    region: point,
    reducer: ee.Reducer.first(),
    scale: 30
}).setOptions({title: 'NDVI over time'});

print(chart);

Here is the link to the code: https://code.earthengine.google.com/6e7dba0fbbda1cab133b3dffe31e2e9e

I hope this helps!

Kel Markert
  • 807
  • 4
  • 12