File size: 3,063 Bytes
9ad0e2d
c54a043
9ad0e2d
 
 
 
 
 
 
a22204c
f2cf4fa
9ad0e2d
 
 
 
 
 
81b9b9a
9ad0e2d
 
 
c54a043
 
 
 
 
 
9ad0e2d
d5c9531
9ad0e2d
 
 
 
 
c54a043
 
 
 
9ad0e2d
 
 
 
c54a043
 
9ad0e2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c54a043
 
 
 
 
 
 
 
9ad0e2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c54a043
9ad0e2d
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import ast
import json
import streamlit as st
import leafmap.foliumap as leafmap

st.set_page_config(layout="wide")

st.sidebar.info(
    """
    - Web App URL: <https://streamlit.gishub.org>
    - GitHub repository: <https://github.com/giswqs/streamlit-geospatial>
    """
)

st.sidebar.title("Contact")
st.sidebar.info(
    """
    Qiusheng Wu at [wetlands.io](https://wetlands.io) | [GitHub](https://github.com/giswqs) | [Twitter](https://twitter.com/giswqs) | [YouTube](https://www.youtube.com/@giswqs) | [LinkedIn](https://www.linkedin.com/in/giswqs)
    """
)

# Define a whitelist of trusted URLs
trusted_urls = [
    "https://services.terrascope.be/wms/v2",
    # Add more trusted URLs here
]


@st.cache_data
def get_layers(url):
    options = leafmap.get_wms_layers(url)
    return options


def is_trusted_url(url):
    return url in trusted_urls


def app():
    st.title("Web Map Service (WMS)")
    st.markdown(
        """
    This app is a demonstration of loading Web Map Service (WMS) layers. Simply enter the URL of the WMS service
    in the text box below and press Enter to retrieve the layers. Go to https://apps.nationalmap.gov/services to find
    some WMS URLs if needed.
    """
    )

    row1_col1, row1_col2 = st.columns([3, 1.3])
    width = 800
    height = 600
    layers = None

    with row1_col2:

        esa_landcover = "https://services.terrascope.be/wms/v2"
        url = st.text_input(
            "Enter a WMS URL:", value="https://services.terrascope.be/wms/v2"
        )
        empty = st.empty()

        if url:

            if is_trusted_url(url):
                options = get_layers(url)
                # Process options as needed
            else:
                st.error(
                    "The entered URL is not trusted. Please enter a valid WMS URL."
                )

            default = None
            if url == esa_landcover:
                default = "WORLDCOVER_2020_MAP"
            layers = empty.multiselect(
                "Select WMS layers to add to the map:", options, default=default
            )
            add_legend = st.checkbox("Add a legend to the map", value=True)
            if default == "WORLDCOVER_2020_MAP":
                legend = str(leafmap.builtin_legends["ESA_WorldCover"])
            else:
                legend = ""
            if add_legend:
                legend_text = st.text_area(
                    "Enter a legend as a dictionary {label: color}",
                    value=legend,
                    height=200,
                )

        with row1_col1:
            m = leafmap.Map(center=(36.3, 0), zoom=2)

            if layers is not None:
                for layer in layers:
                    m.add_wms_layer(
                        url, layers=layer, name=layer, attribution=" ", transparent=True
                    )
            if add_legend and legend_text:
                legend_dict = json.loads(legend_text.replace("'", '"'))
                m.add_legend(legend_dict=legend_dict)

            m.to_streamlit(height=height)


app()