0

I'd like to copy the time:start property from dataset2021 collection to ndvi2021 and further to difference which is 2021-2020. How can I do it?

//point

var point = ee.Geometry.Point(-122.082, 37.42).buffer(50);

//Dataset
var dataset2021 = ee.ImageCollection('COPERNICUS/S2_SR')
                  .filterDate('2021-04-15', '2021-09-30')
                  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 5))
                  .filterBounds(point) 
                  .select(['B4', 'B8']);
                  
var dataset2020 = ee.ImageCollection('COPERNICUS/S2_SR')
                  .filterDate('2020-04-15', '2020-09-30')
                  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 5))
                  .filterBounds(point)
                  .select(['B4, 'B8']);

Map.centerObject(point, 14);

 // NDVI 2021
var med2021 = dataset2021.median();
var result2021 = med2021.select('B4', 'B8');
var ndvi2021 = result2021.normalizedDifference(['B8', 'B4']).rename('ndvi');

 // NDVI 2020
var med2020 = dataset2020.median();
var result2020 = med2020.select('B4', 'B8');
var ndvi2020 = result2020.normalizedDifference(['B8', 'B4']).rename('ndvi');

// difference
var diff2019 = ndvi2021.subtract(ndvi2020);

// CHART with properties

var chart = ui.Chart.image.series({
  imageCollection: ndvi2021.select('ndvi'),
  region: point,
  reducer: ee.Reducer.median(),
  scale: 10
}).setOptions({
  title: 'NDVI over time',
  hAxis: {title: 'Date'},
  vAxis: {title: 'NDVI'} 
  });

print(chart);


var chart = ui.Chart.image.series({
  imageCollection: diff2019.select('ndvi'),
  region: point,
  reducer: ee.Reducer.median(),
  scale: 10
}).setOptions({
  title: 'ZNDVI over time',
  hAxis: {title: 'Date'},
  vAxis: {title: 'difference'} 
  });

print(chart);

1 Answer 1

1

The value passed for parameter imageCollection is an image (ndvi2021 & diff2019) for the method ui.Chart.image.series(). Your imageCollection is reduced to a single image after applying median() function. It is not possible to show 'NDVI over time' for a single image.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.