1

I am trying to create a cloud mask for my image, which I successfully did with the following code:

// Masking clouds -> binary array
var maskInside = function(image, geometry) {
  var mask_outside = ee.Image.constant(1).clip(geometry).mask().not();
  return image.updateMask(mask_outside);
};

var tools = require('users/fitoprincipe/geetools:tools');  
var masked = tools.geometry.maskInside(image_august, clouds).clip(cabar_smaller_rectangle);
Map.addLayer(masked);
print(masked);

where geometry is a Polygon that I have created. The mask is then successfully added as a layer: enter image description here

How can I obtain the information of this mask as a list or an array? And then export it out of GEE in a form of a numpy array?

1 Answer 1

1

You have left out details in your question, so I have to make some assumptions:

  • As you have JavaScript code here, you will not run this from Python, even though you want it to turn it into NumPy array. So you will need to export these results to either Cloud Storage or Drive.

  • You don't want the pixel values of the mask exported, but of the masked image.

  • You want to know the coordinate of each pixel

This involves running reduceRegion() with a toList() reducer. You will be quite limited in the number of pixels you can get this way. It's usually better to export this as an image if there's a larger number of pixels.

That said, below is an example how you can get the pixel values and export them as a CSV to Google Drive. Then you'll have to download that file and turn it into a NumPy array.

If you decide to write this in Python instead, you can use the same approach as here, but simply call getInfo() on pixels, and turn that dictionary into a NumPy array.

var masked = image
  .addBands(ee.Image.pixelLonLat()) // Include lat/lng if you want
  .updateMask(mask)

var pixels = masked
  .reduceRegion({
    reducer: ee.Reducer.toList(), 
    geometry: geometry, 
    scale: 100
  })
var keys = ['someBand', 'longitude', 'latitude']  
var features = ee.FeatureCollection(
  pixels
    .toArray(keys)
    .transpose()
    .toList()
    .map(function (values) {
      var properties = ee.Dictionary.fromLists(keys, values)
      // Include the coordinate as GeoJSON, as lat/lon columns, 
      // or leave it out completely from the exported CSV.
      // Here, both are included, which obviously is redundant.
      var point = ee.Geometry.Point([
        properties.get('longitude'), 
        properties.get('latitude')
      ])
      return ee.Feature(point, properties)
    })
)

Export.table.toDrive({
  collection: features,  
  fileNamePrefix: 'pixels', 
  fileFormat: 'CSV',
   // Select which properties to include, and the order of them
  selectors: ['someBand', 'longitude', 'latitude', '.geo']
})

https://code.earthengine.google.com/9e6f714a30e91c236a7c7650beac95a0

1
  • Wow, this is phenomenal, many thanks and much appreciate. Your assumptions were correct indeed.
    – Meki21
    Commented Mar 28, 2022 at 16:49

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.