Google Earth Engine – Extracting Raster Values for Points Using JavaScript API

google-earth-enginegoogle-earth-engine-javascript-api

I want Extract pixel values by points from NDVI and convert it to a table in Google Earth Engine, but I don’t know what should I do.


var dataset = ee.ImageCollection('MODIS/061/MOD13Q1')
                  .filter(ee.Filter.date('2021-01-01', '2021-05-01'))
                  .mean();
var ndvi = dataset.select('NDVI');
var ndviVis = {
  min: 0.0,
  max: 8000.0,
  palette: [
    'FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901',
    '66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01',
    '012E01', '011D01', '011301'
  ],
};

var pts = ee.FeatureCollection([
  ee.Feature(ee.Geometry.Point([-118.6010, 37.0777]), {plot_id: 1}),
  ee.Feature(ee.Geometry.Point([-118.5896, 37.0778]), {plot_id: 2}),
  ee.Feature(ee.Geometry.Point([-118.5842, 37.0805]), {plot_id: 3}),
  ee.Feature(ee.Geometry.Point([-118.5994, 37.0936]), {plot_id: 4}),
  ee.Feature(ee.Geometry.Point([-118.5861, 37.0567]), {plot_id: 5})
]);

Map.addLayer(pts);

Map.centerObject(pts);
Map.addLayer(ndvi, ndviVis, 'NDVI');

Best Answer

You can use reduceRegions to apply a reducer over the pixels that intersect with your points. As you are working with a point layer, I suggest using first reducer.

// Applying first reducer
var results = ndvi.reduceRegions({
  collection: pts, 
  reducer: ee.Reducer.first(), 
  scale: 250, // Native resolution
});

// Print in console (the NDVI value will be named “first”).  
print(results);

//Export to Google Drive as csv file
Export.table.toDrive({
  collection: results,
  description:'RasterValues',
  fileFormat: 'csv'
});
Related Question