5

I am working on a classification problem on agricultural fields. To make things easier, I first identified road, cities, water, fields, and etc. I am planning to eliminate road, cities, and water out of the image to improve my model. I got all these areas as polygons in a .shp file and I want to mask out these areas.

Wherever I check, all applications are for clipping a polygon.

Is there a method to mask the polygons I provided in GEE code editor?

1 Answer 1

8

There may be many ways to do that, but when I had to do it I did:

var maskInside = function(image, geometry) {
  var mask = ee.Image.constant(1).clip(geometry).mask().not()
  return image.updateMask(mask)
}

And put that function in a package (geetools for the code editor) that you can use in this way:

var tools = require('users/fitoprincipe/geetools:tools')  
var i = ee.Image.random(1)
var masked = tools.geometry.maskInside(i, geometry)
Map.addLayer(masked)
3
  • Thanks! I can't try now since it's quite late here in Turkey and I'm out of office. It looks this solves the problem. It seems an easy task but I couldn't find anyone does it. Thanks a lot Commented Jun 7, 2019 at 23:29
  • What about if I need to mask the area outside the polygon?
    – FraNut
    Commented Nov 24, 2023 at 10:14
  • 1
    @FraNut use image.clip(geometry) Commented Nov 26, 2023 at 14:59

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.