I want to extract reflectance values of points for field measurment at different dates. I want to get the median reflectance value for each band for 1 pixel around the point to look for the link with turbidity. Here bellow is my code. Note that I got an issue as my points can be added as a layer but as I imported the csv, the coordinates were not noted in the feature anymore, but I still had the column "geometry" with the coordinates spelled as " c(longitude,latitude)".

 // Function to export a batch as a CSV file
var exportBatch = function(batch, index) {
  var batchCollection = ee.FeatureCollection(batch);
  var batchName = 'batch_' + index.toString();

  // Export the batch as a CSV file to Google Drive
    collection: batchCollection,
    description: batchName,
    fileFormat: 'CSV'

// Get the size of the feature collection
var count = table.size();

// Define the batch size
var batchSize = 100;

// Calculate the number of batches
var numBatches = count.divide(batchSize).ceil();

// Create an empty feature collection to store the results
var extractedFeatureCollection = ee.FeatureCollection([]);

// Iterate over the batches and export each batch as a CSV file
for (var i = 0; i < numBatches.getInfo(); i++) {
  var start = i * batchSize;
  var end = ee.Number(start).add(batchSize);
  var batch = table.toList(batchSize, start);

  // Iterate over the batch
  var extractedData = batch.map(function(feature) {
    feature = ee.Feature(feature); // Convert the feature to an ee.Feature object
    var geometryString = ee.String(feature.get('geometry')); // Get the geometry string

    // Extract the coordinates from the string
    var coordinates = geometryString.split(',').map(function(coord) {
      return ee.Number.parse(coord.trim());
    var longitude = coordinates[0];
    var latitude = coordinates[1];

    var date = ee.Date(feature.get('date_GEE'));

    // Filter the collection based on the date
    var image = collection
      .filterDate(date.advance(-2, 'day'), date.advance(2, 'day'))

    // Apply the QA cloud mask
    var qaMask = image.select('QA60');
    var clearMask = qaMask.bitwiseAnd(1 << 10).eq(0).and(qaMask.bitwiseAnd(1 << 11).eq(0));
    image = image.updateMask(clearMask);

    // Get the DN values for each band
    var bandNames = image.bandNames();
    var reflectanceValues = bandNames.map(function(bandName) {
      var dnValue = ee.Number(image.reduceRegion({
        reducer: ee.Reducer.median(),
        geometry: ee.Geometry.Point([longitude, latitude]).buffer(10),
        scale: 10,
        bestEffort: true
      return dnValue;

    // Create a new feature with the point geometry
    var pointGeometry = ee.Geometry.Point([longitude, latitude]);
    var newFeature = ee.Feature(pointGeometry);

    // Set the existing properties
    newFeature = newFeature.copyProperties(feature);

    // Set the additional properties
    newFeature = newFeature.set('date', date.format('yyyy-MM-dd'));
    newFeature = newFeature.set('Num', feature.get('Num'));
    newFeature = newFeature.set('Turbidity', feature.get('Turbidity'));

    // Set the band values as properties
    if (bandNames.length === reflectanceValues.length) {
      for (var j = 0; j < bandNames.length; j++) {
        var bandName = bandNames[j];
        var reflectanceValue = reflectanceValues[j];
        newFeature = newFeature.set(bandName, reflectanceValue);
    } else {
      print('Warning: Number of bands and values mismatch for point:', feature.get('Num'));

    return newFeature;
  // Merge the extracted data into the feature collection
  extractedFeatureCollection = extractedFeatureCollection.merge(extractedData);

  // Export the batch as a CSV file
  exportBatch(extractedData, i);

// Export the entire feature collection as a CSV file
  collection: extractedFeatureCollection,
  description: 'exported_data',
  fileFormat: 'CSV'
  • Do you want your coordinates to be included to the csv?
    – Padmanabha
    Jun 12, 2023 at 19:59
  • I just want to get the median reflectance values of each band at the buffer zone for each point in my exported csv, with the date of the image, the date of the field measurment , and the turbidity. My main goal is to analyze the link between the reflectance values and the turbidity to eventually create a model of turbidity for the watershed.
    – noob
    Jun 13, 2023 at 4:46


Your Answer

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