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

2

That is because in line 18 you call s2, which is likely your whole input image collection of the sentinel-2 (but you'll have to provide a link to your code to see those input features).

Besides, you are trying to filter an image on date and location, which is not possible. You can only do that for image collections. By getting the first() of an image collection, you will only get one image.

To improve the code you want it, you should just add this line:

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

A link to a working code with random location would be: https://code.earthengine.google.com/a95b7ec94fa6f9cb0d23fe6ecd5de0d9

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.