1

I would like to have a table or a matrix with the coordinates and value of each pixel on a given polygon. I am working with the Global Forest Change data specifically with the 'loss' layer. In this layer, each pixel has a value of 1 if the forest has been cleared or 0 if it was not.

What I have done so far makes no sense as I am a beginner. The code below is the one offered by the Google Earth Engine Tutorial.

var gfc2018 = ee.Image("UMD/hansen/global_forest_change_2018_v1_6");

Map.addLayer(gfc2018.mask(gfc2018), {
  bands: ['treecover2000'],
  palette: ['000000', '00FF00'],
  max: 100
}, 'forest cover masked');

var treeCover = gfc2018.select(['treecover2000']);
var lossImage = gfc2018.select(['loss']);
var gainImage = gfc2018.select(['gain']);

// Use the and() method to create the lossAndGain image.
var gainAndLoss = gainImage.and(lossImage);

// Add the tree cover layer in green.
Map.addLayer(treeCover.updateMask(treeCover),
    {palette: ['000000', '00FF00'], max: 100}, 'Forest Cover');

// Add the loss layer in red.
Map.addLayer(lossImage.updateMask(lossImage),
            {palette: ['FF0000']}, 'Loss');

// Add the gain layer in blue.
Map.addLayer(gainImage.updateMask(gainImage),
            {palette: ['0000FF']}, 'Gain');

// Show the loss and gain image.
Map.addLayer(gainAndLoss.updateMask(gainAndLoss),
    {palette: 'FF00FF'}, 'Gain and Loss');
    
    
// Create a polygon
var region = ee.Geometry.Rectangle(-75.2806, 3.1209, -75.0554, 3.2413);

// Extract pixel data from polygon
...?```

1 Answer 1

1

First - exporting pixel values as a CSV is not very efficient, and chances are that you will run out of memory for larger areas. But if it is what you need to do, these are the steps I would take:

  • You include the ee.Image.pixelLonLat() bands in your image
  • Call reduceRegion(), specifying the scale to the resolution of your imagery
  • Massage the result so you get an ee.FeatureCollection
  • Export the ee.FeatureCollection to Google Drive

Here's how you could implement it:

// Dictionary with bands for keys and list of pixels values for value
var pixelsDict = gfc2018
  .select(['gain', 'loss', 'treecover2000'])
  .addBands(ee.Image.pixelLonLat()).reduceRegion({
  reducer: ee.Reducer.toList(), 
  geometry: region, 
  scale: 30
})

var bandNames = pixelsDict.keys()

// List of pixels, containing a list of band values
var pixels = ee.Array(pixelsDict.values()).transpose().toList() 
var table = ee.FeatureCollection(
  pixels.map(function (bandValues) {
    // Dictionary with bands for keys and valueband value for value
    var properties = ee.Dictionary.fromLists(
      bandNames, ee.Array(bandValues).toList()
    )
    // Set to null if you just want longitude and latitude as columns
    var geometry = ee.Geometry.Point(
      properties.getNumber('longitude'),
      properties.getNumber('latitude')
    )
    return ee.Feature(geometry, properties)
  })  
)
print(table.limit(100))

Export.table.toDrive({
  collection: table, 
  description: 'name-of-the-file', 
  // Columns to include. Add longitude and latitude if you want
  selectors: ['gain', 'loss', 'treecover2000'] 
})

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

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.