0

I can export NDVI values to CSV file. But I couldn't export RGB values(b2,,b3,b4) to csv file and RGB values must be in range 0-255.

var geometry = 
    /* color: #d63000 */
    /* displayProperties: [
      {
        "type": "rectangle"
      }
    ] */
    ee.Geometry.Polygon(
        [[[26.9559366037084, 39.42848463948233],
          [26.9559366037084, 39.41562180312237],
          [26.971214466257226, 39.41562180312237],
          [26.971214466257226, 39.42848463948233]]], null, false);

var image = ee.Image(ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
    .filterBounds(geometry)
    .filterDate('2020-06-01', '2020-8-31')
    .filterMetadata('CLOUD_COVER','less_than',5)
    .sort('CLOUD_COVER')
    .first());

var palette = [
  'FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718',
  '74A901', '66A000', '529400', '3E8601', '207401', '056201',
  '004C00', '023B01', '012E01', '011D01', '011301']
  
function addNdvi(img){
    var ndvi = img.normalizedDifference(['B5','B4']).rename('ndvi');
    return img.addBands(ndvi);
  }
  
var ndvi= addNdvi(image.clip(geometry));
  
Map.addLayer(ndvi,{bands: ['ndvi'], palette: palette},'ndvi');
Map.addLayer(image.clip(geometry),{bands: ['B4', 'B3', 'B2'], max:0.3}, 'rgb');


var ndviFeatures = ee.FeatureCollection(ee.List(ndvi
  .reduceRegion({
    reducer: ee.Reducer.toList(),
    geometry: geometry,
    scale: 10
  })
  .get('ndvi'))
  .map(function (ndvi) {
    return ee.Feature(null, {ndvi: ndvi})``
  })
)


var rgbFeatures = ee.FeatureCollection(ee.List(image
  .reduceRegion({
    reducer: ee.Reducer.toList(),
    geometry: geometry,
    scale: 10
  })
  .get('image'))
  .map(function (rgb) {
    return ee.Feature(null, {rgb: rgb})
  })
)


print('# rgb of pixels',rgbFeatures.size())
print('# ndvi of pixels',ndviFeatures.size())


Export.table.toDrive({
  collection: rgbFeatures,
  
  description: 'dene',
  fileFormat: 'CSV',
  selectors: ['B4', 'B3', 'B2']
});


Export.table.toDrive({
  collection: ndviFeatures,
  description: 'ndvi',
  selectors: ['ndvi']
})

1 Answer 1

1

You didn't specify how you wanted your CSV file to be formatted, so here's one approach simply creating a column for each of the three bands:

var bands = ['B2', 'B3', 'B4']
var rgbFeatures = ee.FeatureCollection(
  image
  .select(bands)
  .multiply(255) // 0-1 to 0-255
  .reduceRegion({
    reducer: ee.Reducer.toList(),
    geometry: geometry,
    scale: 30
  })
  .toArray()
  .transpose()
  .toList()
  .map(function (values) {
    return ee.Feature(null, ee.Dictionary.fromLists(bands, values))
  })
)

https://code.earthengine.google.com/fdaa4f571afe0ee931c854b72974f93e

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.