Error On The Streamlit-"ee.ee_exception.EEException: Image.select: Pattern 'B4' did not match any bands."

1k Views Asked by At

I'm working on estimation chlorophyll-a using a Landsat 8 image and then I’m trying to build an web-app which is using streamlit and geemap library. But my map doesn’t showing when I ran it from my local computer. I've tried to changing it but it's showing the error that tell 'didn't match any band'. My script that didn’t showing the map is on the

import ee
import geemap
import streamlit as st
import numpy as np

def L8_T1():
    
    st.header("Landsat 8 Surface Reflectance Tier 1")
    
    row1_col1, row1_col2 = st.columns([3, 1])
    width = 950
    height = 600
    
    m = geemap.Map()

    study_area = ee.Geometry.Polygon([
        [121.731876,-2.330221], [121.069735, -2.317823], [121.214026,-2.994612], [121.785511,-2.992766]
    ])

    collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR') \
        .filterBounds(study_area)

    def mask_clouds(image):
        # Bits 3 and 5 are cloud shadow and cloud, respectively.
        cloud_shadow_bit_mask = (1 << 3)
        clouds_bit_mask = (1 << 5)
        # Get the pixel QA band.
        qa = image.select('pixel_qa')
        # Both flags should be set to zero, indicating clear conditions.
        mask = qa.bitwiseAnd(cloud_shadow_bit_mask).eq(0) \
            .And(qa.bitwiseAnd(clouds_bit_mask).eq(0))
        return image \
            .divide(10000) \
            .divide(3.141593) \
            .updateMask(mask)

    def calculate_clorophil_a(year) :
        image_collection = collection.filter(ee.Filter.eq("system:index", year))\
            .map(mask_clouds).median()
        ndwi = image_collection.normalizedDifference(['B3', 'B5'])\
            .rename('NDWI')
        clorophil_a = image_collection\
            .expression('10**(-0.9889*((RrsB4)/(RrsB5))+0.3619)', {
                'RrsB4': image_collection.select('B4'),
                'RrsB5': image_collection.select('B5')
                }).updateMask(ndwi)
        return clorophil_a 

    parameter = {'min':0, 'max':1, 'palette':['blue','green']}
    years = ["2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020"]
    
    with row1_col2:
        selected_year = st.multiselect("Select a year", years)
        add_chart = st.checkbox("Show chart")

    if selected_year:
        for year in selected_year:
            clorophil_a_collection = ee.ImageCollection.fromImages([
                calculate_clorophil_a(year)
                for year in years
                ])
            m.addLayer(clorophil_a_collection, parameter, "Clorophyll-a "+year)
            m.add_colorbar(
                parameter,
                label="Clorophyll-a (mg/m3)",
                orientation="horizontal",
                layer_name="Clorophyll-a",
                transparent_bg=True,
                )

        if add_chart:
            m.set_plot_options(plot_type=None, add_marker_cluster=True)
        
        with row1_col1:
            m.to_streamlit(width=width, height=height)

    else:
        with row1_col1:
            m.to_streamlit(width=width, height=height)

def L8_T2():
    
    st.header("Landsat 8 Surface Reflectance Tier 2")
    
    row1_col1, row1_col2 = st.columns([3, 1])
    width = 950
    height = 600
    
    m = geemap.Map()

    study_area = ee.Geometry.Polygon([
        [121.731876,-2.330221], [121.069735, -2.317823], [121.214026,-2.994612], [121.785511,-2.992766]
    ])

    collection = ee.ImageCollection('LANDSAT/LC08/C01/T2_SR') \
                .filterBounds(study_area)

    def mask_clouds(image):
        # Bits 3 and 5 are cloud shadow and cloud, respectively.
        cloud_shadow_bit_mask = (1 << 20)
        clouds_bit_mask = (1 << 25)
        # Get the pixel QA band.
        qa = image.select('pixel_qa')
        # Both flags should be set to zero, indicating clear conditions.
        mask = qa.bitwiseAnd(cloud_shadow_bit_mask).eq(0) \
            .And(qa.bitwiseAnd(clouds_bit_mask).eq(0))
        return image\
            .divide(10000)\
            .divide(3.141593)\
            .updateMask(mask)

    def calculate_clorophil_a(year) :
        image_collection = collection.filter(ee.Filter.eq("system:index", year))\
            .map(mask_clouds)\
                .median() 
        ndwi = image_collection.normalizedDifference(['B3', 'B5']).rename('NDWI')
        clorophil_a = image_collection.expression(
            '10**(-0.9889*((RrsB4)/(RrsB5))+0.3619)', {
                'RrsB4': image_collection.select('B4'),
                'RrsB5': image_collection.select('B5')
            }).updateMask(ndwi)
        return clorophil_a 

    parameter = {'min':0, 'max':1, 'palette':['blue','green']}
    years = ["2016", "2017", "2018", "2019", "2020"]
    
    with row1_col2:
        selected_year = st.multiselect("Select a year", years)
        add_chart = st.checkbox("Show chart")

    if selected_year:
        for year in selected_year:
            clorophil_a_collection = ee.ImageCollection.fromImages([
                calculate_clorophil_a(year)
                for year in years
                ])
            m.addLayer(clorophil_a_collection, parameter, "Clorophyll-a "+year)
            m.add_colorbar(
                parameter,
                label="Clorophyll-a (mg/m3)",
                orientation="horizontal",
                layer_name="Clorophyll-a",
                transparent_bg=True,
                )

        if add_chart:
            m.set_plot_options(plot_type=None, add_marker_cluster=True)

        with row1_col1:
            m.to_streamlit(width=width, height=height)

    else:
        with row1_col1:
            m.to_streamlit(width=width, height=height)            
    
def app():
    st.title("Chlorophyll-a")

    st.markdown("""

    Aplikasi Web ini dibuat dengan menggunakan Streamlit untuk menampilkan nilai 
    estimasi besar klorofil-a pada Danau Matano dan Danau Towuti menggunakan 
    algoritma Jaelani 2015 berdasarkan jurnal [Pemetaan Distribusi Spasial Konsentrasi Klorofil-A dengan Landsat 8 di Danau Matano dan Danau Towuti, Sulawesi Selatan](http://lipi.go.id/publikasi/pemetaan-distribusi-spasial-konsentrasi-klorofil-a-dengan-landsat-8-di-danau-matano-dan-danau-towuti-sulawesi-selatan/2062)

    """)
    
    apps = ["Landsat 8 Surface Reflectance Tier 1", "Landsat 8 Surface Reflectance Tier 2"]
    
    selected_app = st.selectbox("Select an Image", apps)
    
    if selected_app == "Landsat 8 Surface Reflectance Tier 1":
        L8_T1()
    elif selected_app == "Landsat 8 Surface Reflectance Tier 2":
        L8_T2()

This is the error that they say:

Traceback (most recent call last):
  File "C:\Users\...\scriptrunner\script_runner.py", line 443, in _run_script
    exec(code, module.__dict__)
  File "C:\Users\...\streamlit_app.py", line 49, in <module>
    app["func"]()
  File "C:\Users\...\geemap_script.py", line 172, in app
    L8_T1()
  File "C:\Users\...\geemap_script.py", line 62, in L8_T1
    m.addLayer(clorophil_a_collection, parameter, "Clorophyll-a "+year)
  File "C:\Users\...\geemap.py", line 1411, in add_ee_layer
    map_id_dict = ee.Image(image).getMapId(vis_params)
  File "C:\Users\...\ee\image.py", line 130, in getMapId
    response = data.getMapId(request)
  File "C:\Users\...\ee\data.py", line 569, in getMapId
    result = _execute_cloud_call(
  File "C:\Users\...\ee\data.py", line 332, in _execute_cloud_call
    raise _translate_cloud_exception(e)
ee.ee_exception.EEException: Image.select: Pattern 'B4' did not match any bands.

What should I do to solve this problem? Because the name of each band is in accordance with what is in the metadata which says B1, B2, B3, B4, B5, B6, B7

0

There are 0 best solutions below