Announcement: All noncommercial projects registered to use Earth Engine beforeApril 15, 2025 mustverify noncommercial eligibility to maintain access. If you have not verified by September 26, 2025, your access may be on hold.

ee.Image.selfMask

  • TheselfMask() method updates an image's mask using the image's own pixel values.

  • The mask is updated at positions where the existing mask is not zero.

  • The output image retains the original metadata and footprint.

  • TheselfMask() method is called on anImage object and returns a maskedImage.

Updates an image's mask at all positions where the existing mask is not zero using the value of the image as the new mask value. The output image retains the metadata and footprint of the input image.

UsageReturns
Image.selfMask()Image
ArgumentTypeDetails
this:imageImageThe image to mask with itself.

Examples

Code Editor (JavaScript)

// A Sentinel-2 surface reflectance image.varimg=ee.Image('COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG');vartrueColorViz={bands:['B4','B3','B2'],min:0,max:2700,gamma:1.3};print('Sentinel-2 image',img);Map.setCenter(-122.36,37.47,10);Map.addLayer(img,trueColorViz,'Sentinel-2 image');// Create a Boolean land mask from the SWIR1 band; water is value 0, land is 1.varlandMask=img.select('B11').gt(100);print('Land mask',landMask);Map.addLayer(landMask,{palette:['blue','lightgreen']},'Land mask');// Mask the land mask by itself; pixel values equal to 0 (water) become invalid.varlandMaskMasked=landMask.selfMask();print('Land mask, masked',landMaskMasked);Map.addLayer(landMaskMasked,{palette:['gold']},'Land mask, masked');

Python setup

See the Python Environment page for information on the Python API and usinggeemap for interactive development.

importeeimportgeemap.coreasgeemap

Colab (Python)

# A Sentinel-2 surface reflectance image.img=ee.Image('COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG')true_color_viz={'bands':['B4','B3','B2'],'min':0,'max':2700,'gamma':1.3,}display('Sentinel-2 image',img)m=geemap.Map()m.set_center(-122.36,37.47,10)m.add_layer(img,true_color_viz,'Sentinel-2 image')# Create a Boolean land mask from the SWIR1 band water is value 0, land is 1.land_mask=img.select('B11').gt(100)display('Land mask',land_mask)m.add_layer(land_mask,{'palette':['blue','lightgreen']},'Land mask')# Mask the land mask by itself pixel values equal to 0 (water) become invalid.land_mask_masked=land_mask.selfMask()display('Land mask, masked',land_mask_masked)m.add_layer(land_mask_masked,{'palette':['gold']},'Land mask, masked')m

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2023-10-06 UTC.