Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Here is one way to export the pixel values of all bands in Google Earth Engine:

  1. Select the image or image collection that you want to export the pixel values from.
  2. Use the .getInfo() method to retrieve the metadata for the image or collection.
  3. Find the number of bands in the image or collection by accessing the "bands" property of the metadata using .get("bands").
  4. Iterate over all the bands using a for loop, and use the .getArray() method to retrieve the pixel values for each band.
  5. Convert the pixel values to a format suitable for export, such as a CSV or JSON file.

Here is an example code snippet:

// Select an image or collection
var collection = ee.ImageCollection("COPERNICUS/S2_SR")
    .filterDate("2021-01-01", "2021-12-31")
    .filterBounds(geometry);

// Get the metadata for the image or collection
var metadata = collection.first().getInfo();

// Find the number of bands in the image or collection
var bandCount = metadata["bands"].length;

// Create an empty array to store the pixel values
var pixelValues = [];

// Iterate over all the bands and retrieve the pixel values
for (var i = 0; i < bandCount; i++) {
  var bandName = metadata["bands"][i]["id"];
  var bandValues = collection.select(bandName).toArray().getInfo();

  // Add the pixel values to the array
  pixelValues.push({
    "band": bandName,
    "values": bandValues
  });
}

// Convert the pixel values to a CSV file and export it
var csv = "Band,Values\n";
pixelValues.forEach(function(data){
  csv += data.band + ",";
  data.values.forEach(function(value){
    csv += value + ",";
  });
  csv += "\n";
});

Export.table.toDrive({
  collection: ee.FeatureCollection([]),
  description: "PixelValues",
  fileFormat: "CSV",
  fileNamePrefix: "PixelValues",
  content: csv
});