0

I have a shapefile that has 1500 points. I am trying to extract 1-year time-series NDVI values from Sentinel 2_SR for each point and export it to CSV with NDVI values date-wise and existing UID column from the shapefile. This is what my code looks like:

// Load the shapefile asset
var shapefile = ee.FeatureCollection('projects/ee-tilok-research/assets/CI_1500');

// Load Sentinel-2 SR image collection
var sentinel = ee.ImageCollection('COPERNICUS/S2_SR');

// Filter by date and region
var filtered = sentinel.filterDate('2022-05-01', '2023-05-31')
                       .filterBounds(shapefile);

// Define a cloud mask function
var cloudMask = function(image) {
  var qa = image.select('QA60');
  var cloudBitMask = 1 << 10;
  var cirrusBitMask = 1 << 11;
  var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
               .and(qa.bitwiseAnd(cirrusBitMask).eq(0));
  return image.updateMask(mask).divide(10000);
};

map.addLayer(shapefile);

// Apply the cloud mask function
var masked = filtered.map(cloudMask);

// Define a NDVI function
var ndvi = function(image) {
  return image.normalizedDifference(['B8', 'B4']).rename('NDVI');
};

// Apply the NDVI function
var withNDVI = masked.map(ndvi);

// Reduce the collection to a single image or a time series
// For example, you can use mean, median, max, min, etc.
var reduced = withNDVI.mean();

// Export the NDVI values as a CSV file
// Join the NDVI values with the existing attributes of the shapefile
var joined = ee.Join.saveAll('ndvi').apply({
  primary: shapefile,
  secondary: reduced.reduceRegions({
    collection: shapefile,
    reducer: ee.Reducer.first(),
    scale: 10 // change this according to your needs
  }),
  condition: ee.Filter.equals({
    leftField: '.geo',
    rightField: '.geo'
  })
});

// Export the joined feature collection to Google Drive
Export.table.toDrive({
  collection: joined,
  description: 'ndvi_values', // change this according to your needs
  folder: 'your_folder_name', // change this according to your needs
  // selectors: ['your_attribute_1', 'your_attribute_2', ..., 'ndvi'] // change this according to your needs
});

I get an error at the end Error: Internal error. (Error code: 13) Please help me out

3
  • Internal error doesn't occur for a particular reason. What you can try var reduced = withNDVI.mean().clip(shapefile).
    – Padmanabha
    Commented Aug 9, 2023 at 15:19
  • Probably, your issue is in your shapefile (not available). I ran the code (there is a little error in map.addLayer statement) with three arbitrary points and it was produced a result (features have NDVI values) without any internal error.
    – xunilk
    Commented Aug 9, 2023 at 16:17
  • @Padmanabha I think I should remove the mean() because I want the multiple date values. so that I can plot the graph with NDVI values in a time period of 1 year. Commented Aug 17, 2023 at 6:39

0

Your Answer

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

Browse other questions tagged or ask your own question.