File size: 3,179 Bytes
c54a043
9ad0e2d
 
 
 
 
 
 
 
 
a22204c
f2cf4fa
9ad0e2d
 
 
 
 
 
81b9b9a
9ad0e2d
 
 
 
d5c9531
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
107
108
109
110
111
112
113
114
115
116
117
118
import json
import os
import leafmap.foliumap as leafmap
import leafmap.colormaps as cm
import streamlit as st

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)
    """
)


@st.cache_data
def load_cog_list():
    print(os.getcwd())
    in_txt = os.path.join(os.getcwd(), "data/cog_files.txt")
    with open(in_txt) as f:
        return [line.strip() for line in f.readlines()[1:]]


@st.cache_data
def get_palettes():
    return list(cm.palettes.keys())
    # palettes = dir(palettable.matplotlib)[:-16]
    # return ["matplotlib." + p for p in palettes]


st.title("Visualize Raster Datasets")
st.markdown(
    """
An interactive web app for visualizing local raster datasets and Cloud Optimized GeoTIFF ([COG](https://www.cogeo.org)). The app was built using [streamlit](https://streamlit.io), [leafmap](https://leafmap.org), and [Titiler](https://developmentseed.org/titiler/).


"""
)


def is_trusted_url(url):
    if url.startswith("https://opendata.digitalglobe.com/events/california-fire-2020/"):
        return True
    else:
        return False


row1_col1, row1_col2 = st.columns([2, 1])

with row1_col1:
    cog_list = load_cog_list()
    cog = st.selectbox("Select a sample Cloud Opitmized GeoTIFF (COG)", cog_list)

with row1_col2:
    empty = st.empty()

    url = empty.text_input(
        "Enter a HTTP URL to a Cloud Optimized GeoTIFF (COG)",
        cog,
    )

    if is_trusted_url(url):
        try:
            options = leafmap.cog_bands(url)
        except Exception as e:
            st.error(e)
        if len(options) > 3:
            default = options[:3]
        else:
            default = options[0]
        bands = st.multiselect("Select bands to display", options, default=options)

        if len(bands) == 1 or len(bands) == 3:
            pass
        else:
            st.error("Please select one or three bands")
    else:
        st.error("Please enter a trusted URL")

    add_params = st.checkbox("Add visualization parameters")
    if add_params:
        vis_params = st.text_area("Enter visualization parameters", "{}")
    else:
        vis_params = {}

    if len(vis_params) > 0:
        try:
            vis_params = json.loads(vis_params.replace("'", '"'))
        except Exception as e:
            st.error(
                f"Invalid visualization parameters. It should be a dictionary. Error: {e}"
            )
            vis_params = {}

    submit = st.button("Submit")

m = leafmap.Map(latlon_control=False)

if submit:
    if url:
        try:
            m.add_cog_layer(url, bands=bands, **vis_params)
        except Exception as e:
            with row1_col2:
                st.error(e)
                st.error("Work in progress. Try it again later.")

with row1_col1:
    m.to_streamlit()