Google Earth Engine – How to Mask Pixels Inside Polygons

google-earth-enginemasking

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?

Best Answer

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)
Related Question