0

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: 'folder',
  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?

1 Answer 1

0

If I understand correctly, you got less pixels in the FeatureCollection resulting from ee.Image().sample than you expected based on viewing the image in the Map. You are looking at the NDVI image at a Map Scale of 15 (approx 5m/px). The Sentinel2 image bands you are computing NDVI on have a native scale of 10m/px see here. It is important to specify the scale parameter in your ee.Image().sample() method, as this affects the resolution of the image you are interacting with upon run-time, and consequently in your case, how many available pixels there are in the image to sample from. See here on how Earth Engine handles scale and pyramiding.

Here is a code editor link: https://code.earthengine.google.com/8abc705104b2e9ce842bc85d97e07e54.

Change the number in the scale parameter on line 54 to see how this is working.

4
  • Great. I could change the scale and the number of values changed. But I'm trying to understand the correct relation to set in scale parameter. For instance, the geometry region in this case has 351x452 pixels but I can't get 158.652 (351x452) ndvi values.
    – D3usM4tos
    Commented Dec 16, 2022 at 12:19
  • The reason to use sample() is to get a FeatureCollection by a image, so the export() function works. Is there another way to do this rather than sample()?
    – D3usM4tos
    Commented Dec 16, 2022 at 12:19
  • I'm not sure I've explained the scale/pyramiding concept well enough. Depending on what scale you are zoomed in at, the number of lines and columns in the image changes because Earth Engine is dynamically changing the resolution of the image.
    – kwgeo
    Commented Dec 16, 2022 at 14:47
  • Your geometry region is a vector polygon so it has no native scale in terms of pixels/meter. You must decide for yourself what the appropriate scale would be for retrieving NDVI values from the image. In this case as I had mentioned, using a scale higher than 10m/px (say 5m/px as you had done originally), gives you no more information because the Sentinel2 data you are creating the NDVI image from is natively created at 10m/px.
    – kwgeo
    Commented Dec 16, 2022 at 14:47

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.