Skip to content

earthlib.ShadeMask

Functions for shade masking earth engine images.

Landsat457(img, threshold=0.03)

Apply shade masking to a Landsat ⅘/7 image.

Parameters:

Name Type Description Default
img Image

the ee.Image to shade mask.

required
threshold float

the brightness/reflectance value to exclude. pixels below this value are flagged as shade.

0.03

Returns:

Type Description
Image

the same input image with an updated mask.

Source code in earthlib/ShadeMask.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def Landsat457(img: ee.Image, threshold: float = 0.03) -> ee.Image:
    """Apply shade masking to a Landsat 4/5/7 image.

    Args:
        img: the ee.Image to shade mask.
        threshold: the brightness/reflectance value to exclude.
            pixels below this value are flagged as shade.

    Returns:
        the same input image with an updated mask.
    """
    subset = img.select(getBands("Landsat7"))
    shade = shadeMask(subset, threshold)
    return img.updateMask(shade)

Landsat8(img, threshold=0.03)

Apply shade masking to a Landsat 8 image.

Parameters:

Name Type Description Default
img Image

the ee.Image to shade mask.

required
threshold float

the brightness/reflectance value to exclude. pixels below this value are flagged as shade.

0.03

Returns:

Type Description
Image

the same input image with an updated mask.

Source code in earthlib/ShadeMask.py
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def Landsat8(img: ee.Image, threshold: float = 0.03) -> ee.Image:
    """Apply shade masking to a Landsat 8 image.

    Args:
        img: the ee.Image to shade mask.
        threshold: the brightness/reflectance value to exclude.
            pixels below this value are flagged as shade.

    Returns:
        the same input image with an updated mask.
    """
    subset = img.select(getBands("Landsat8"))
    shade = shadeMask(subset, threshold)
    return img.updateMask(shade)

bySensor(sensor)

Returns the appropriate shade mask function to use by sensor type.

Parameters:

Name Type Description Default
sensor str

string with the sensor name to return (e.g. "Landsat8", "Sentinel2").

required

Returns:

Type Description
Callable

the mask function associated with a sensor to pass to an ee .map() call

Source code in earthlib/ShadeMask.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def bySensor(sensor: str) -> Callable:
    """Returns the appropriate shade mask function to use by sensor type.

    Args:
        sensor: string with the sensor name to return (e.g. "Landsat8", "Sentinel2").

    Returns:
        the mask function associated with a sensor to pass to an ee .map() call
    """
    lookup = {
        "Landsat4": Landsat457,
        "Landsat5": Landsat457,
        "Landsat7": Landsat457,
        "Landsat8": Landsat8,
    }
    try:
        function = lookup[sensor]
        return function
    except KeyError:
        supported = ", ".join(lookup.keys())
        raise SensorError(
            f"Shade masking not supported for '{sensor}'. Supported: {supported}"
        )

shadeMask(img, threshold)

Use brightness normalization to identify and remove dark pixels.

Parameters:

Name Type Description Default
img Image

the ee.Image to shade mask.

required
threshold float

the brightness/reflectance value to exclude. pixels below this value are flagged as shade.

required

Returns:

Type Description
Image

a pixel byte map with 0 for shade pixels, 1 for bright pixels.

Source code in earthlib/ShadeMask.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def shadeMask(img: ee.Image, threshold: float) -> ee.Image:
    """Use brightness normalization to identify and remove dark pixels.

    Args:
        img: the ee.Image to shade mask.
        threshold: the brightness/reflectance value to exclude.
            pixels below this value are flagged as shade.

    Returns:
        a pixel byte map with 0 for shade pixels, 1 for bright pixels.
    """
    brightness = img.reduce(ee.Reducer.mean())
    mask = brightness.gt(threshold)
    return mask