0

I want to extract for each point of my shapefile the NDVI and the related time-series charts but I cannot understand how to extract values.

This is the code

// load the S2 dataset
var S2 = ee.ImageCollection('COPERNICUS/S2')
           .filterDate("2015-01-01", "2021-12-31")
           .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 10));

// S2.limit(10).aside(print)

// load S2_CLOUD_PROBABILITY dataset
var S2_CLOUD_PROBABILITY = ee.ImageCollection('COPERNICUS/S2_CLOUD_PROBABILITY');

// Join S2_summer2018 and S2_CLOUD_PROBABILITY
var S2withCloudMask = ee.Join.saveFirst('cloud_mask').apply({
primary: S2,
secondary: S2_CLOUD_PROBABILITY,
condition: ee.Filter.equals({leftField: 'system:index', rightField: 'system:index'})
});
S2withCloudMask = ee.ImageCollection(S2withCloudMask);

// S2withCloudMask.first().aside(print)

// define a function to mask-out clouds from each image
var maskClouds = function(img) {
var clouds = ee.Image(img.get('cloud_mask')).select('probability');
var isNotCloud = clouds.lt(20);
return img.mask(isNotCloud);
};

// // use the maskClouds function  
var S2_cloudsMasked = S2withCloudMask.map(maskClouds);
 
// add NDVI
// var addNDVI = function(image) {
function addNDVI(image) {
 var ndvi = image.normalizedDifference(['B8', 'B4']).rename('ndvi');
 return image.addBands(ndvi);
}
 
// Map the function over the collection
var S2_cloudsMasked_ndvi = S2_cloudsMasked.map(addNDVI);

// S2_cloudsMasked_ndvi.limit(5).aside(print);

// visualize the time series
var chart = ui.Chart.image.series({
 imageCollection: S2_cloudsMasked_ndvi.select('ndvi'),
 region: geometry,
 reducer: ee.Reducer.mean(),
 scale: 20
}).setOptions({
     lineWidth: 1,
     title: 'NDVI Time Series',
     interpolateNulls: true,
     vAxis: {title: 'NDVI'},
     hAxis: {title: '', format: 'YYYY-MMM'}// ,
     // explorer: {}
     // explorer: {axis: 'horizontal'}  // or 'vertical'
   });
print(chart);

// // Map.addLayer(geometry);
// Map.centerObject(geometry, 15);
// ```
0

You didn't specify what format you want the time-series in. Below you have one approach that gives you a single ee.FeatureCollection with NDVI for all points. If this isn't exactly what you're looking for, it could at least be a starting point.

var points = ee.FeatureCollection([
  ee.Feature(ee.Geometry.Point([12.487749926662959, 41.88485067969689])),
  ee.Feature(ee.Geometry.Point([12.492396084115729, 41.890305914470154]))
])

var timeSeries = points
  .map(timeSeriesForPoint)
  .flatten()
  
var chart = ui.Chart.feature.groups({
  features: timeSeries, 
  xProperty: 'date', 
  yProperty: 'ndvi', 
  seriesProperty: 'point'
}) 

print(timeSeries)
print(chart)
Map.addLayer(points, null, 'points')


function timeSeriesForPoint(point) {
  return S2_cloudsMasked_ndvi
    .filterBounds(point.geometry())
    .map(function (image) {
      return extractNdvi(image, point)
    })
}

function extractNdvi(image, point) {
    var ndvi = image
      .select('ndvi')
      .reduceRegion({
        reducer: ee.Reducer.first(), 
        geometry: point.geometry(), 
        scale: 10
      })
      .getNumber('ndvi')
    return ee.Feature(null, {
      point: point.geometry().coordinates().join(', '),
      ndvi: ndvi,
      date: image.date()
    })
}

https://code.earthengine.google.com/33e7e519f0d509266c1ad7c160a9e41e

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.