I want to create an array in Earth Engine that is composed of the NDVI data of each pixel in the image. Below is the code that calculates the NDVI of each pixel in the ROI (a region). I can store the data for a specified pixel (check the code below the "Extract the data" comment) in a variable(dataN), but I am unable to store the data for each pixel in the region in an array.

var L8 = ee.ImageCollection("LANDSAT/LC8_L1T_TOA"),
roi = /* color: #d63000 */ee.Geometry.Point([74.47821320074036, 31.556778358715253]);

var rgb_vis = {min: 0, max: 0.3, bands: ['B4', 'B3', 'B2']};
var filtered = L8.filterDate('2015-09-01', '2015-10-30').filterBounds(roi);
var image = ee.Image(filtered.first());
var red = image.select('B4');var nir = image.select('B5');

var ndvi = nir.subtract(red).divide(nir.add(red)); //layer from which I want to access the individual pixel data from

Map.addLayer(image, rgb_vis, 'RGB');
Map.addLayer(ndvi, {min: 0, max: 1}, 'NDVI');

// Extract the data
var data = ndvi
.reduceRegion(ee.Reducer.first(), roi, 10)

// Convert to Number for further use
var dataN = ee.Number(data)

// Show data

1 Answer 1


your roi object is a point but let's say it was a polygon or a set of points stored as a featureCollection object then you could do it as

var ndvi_array = ndvi.sampleRegions({
  scale:30 //the scale of image 'ndvi' should go here. i put 30 since its landsat
}).aggregate_array("B5"); //'B5' since that is the name of your band

This will give you an array of pixels sampled at the same same scale as the image within the region roi which should include all the pixels. However, I do suggest against this practice if you are going to sample over a polygon or polygons. That would be too resource intensive and also remember you can not display an array of more than 5000 elements on the console.

  • Thank you, Nishanta! This works well for a specified region that is not too big. Also yes, my ROI was a point, I changed that to a pixel, thanks for pointing that out. Jun 11, 2019 at 11:45
  • I agree with the part where you said that this gets resource-intensive when one tries to sample it over a large region. Is there any other approach for storing the NDVI for each pixel in an image? I have to do this because I have to implement Dynamic Time Warping in Earth Engine for a city. Any help will be appreciated. Again, thank you for your initial answer! Jun 11, 2019 at 11:49

Your Answer

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

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