Skip to main content
2 of 3
naming
Vince
  • 20.3k
  • 16
  • 48
  • 65

Export all pixel NDVI values from a region using Earth Engine

I need to export a CSV file with all pixel NDVI values from a clipped region but with my code below I just got part of pixel values, not all pixel values.

var sent2 = ee.ImageCollection("COPERNICUS/S2"),
    trueColour = {"bands":["B4","B3","B2"],"min":0,"max":3000},
    local = 
    /* color: #98ff00 */
    /* locked: true */
    ee.Geometry.Point([-38.67714056850234, -11.07113127584459]),
    geometry = 
    /* color: #98ff00 */
    /* locked: true */
    /* displayProperties: [
    {
      "type": "rectangle"
    }
    ] */
    ee.Geometry.Polygon(
     [[[-38.67902798420933, -11.068997041766817],
      [-38.67902798420933, -11.07215579800511],
      [-38.67490811116245, -11.07215579800511],
      [-38.67490811116245, -11.068997041766817]]], null, false);


var start_date = '2022-04-01';
var finish_date = '2022-04-10';

var image = ee.Image(sent2
 .filterDate(start_date, finish_date)
 .filterBounds(local)
 .sort('CLOUD_COVER',true)
 .first());

var true_image = sent2.median().clip(geometry);
Map.addLayer(true_image, trueColour,'Clipped True Image');

var palette = ['f4e1e1', 'ff0000', 'fa0000', 'F1B555', 'FCD163', '99B718',
           '74A901', '66A000', '529400', '3E8601', '207401', '056201',
           '004C00', '023B01', '012E01', '011D01', '011301'];

var ndvi = image.normalizedDifference(['B8', 'B4']);

var clippedNDVI = ndvi.clip(geometry);

var clippedImage = image.clip(geometry); //ClipGeometry can any geometry or feature or shapfile
var clippedImagendvi = clippedImage.normalizedDifference(['B8', 'B4']);

Map.addLayer(clippedImagendvi, {min: 0, max: 1, palette: palette},'Clipped Image ndvi');

Map.centerObject(local,15);

// Changing image to FeatureCollection
var coll = clippedNDVI.sample({
  region: geometry,
  geometries: true,  // This specifies that you want the lat-long, rather
  // than image samples without any position information.
});

// Export a .csv table of date, mean NDVI for watershed
Export.table.toDrive({
  collection: coll,
  folder: 'doutorado',
  description: 'ndviMandassaia',
  fileFormat: 'CSV',
  selectors: ['nd']
});

To export .csv from region I did need to change the clipped image to FeatureCollection by .sample() line, setting maximum number of picture (lines x columns) but it didn't work. Is there other manner to solve this problem?