File size: 3,882 Bytes
32fdadc
4497247
32fdadc
 
 
 
 
 
 
29ef836
dec2e4d
32fdadc
 
 
 
 
 
e189598
32fdadc
 
 
 
c4f81d9
 
 
 
 
 
 
 
 
 
 
32fdadc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4f81d9
32fdadc
 
 
 
 
 
 
 
 
4497247
32fdadc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
119
120
121
122
123
124
125
126
127
128
129
import os
import fiona
import geopandas as gpd
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/c/QiushengWu) | [LinkedIn](https://www.linkedin.com/in/qiushengwu)
    """
)


# Define a whitelist of trusted URLs
trusted_urls = [
    "https://github.com/giswqs/streamlit-geospatial/raw/master/data/us_states.geojson",
    # Add more trusted URLs here
]


def is_trusted_url(url):
    return url in trusted_urls


def save_uploaded_file(file_content, file_name):
    """
    Save the uploaded file to a temporary directory
    """
    import tempfile
    import os
    import uuid

    _, file_extension = os.path.splitext(file_name)
    file_id = str(uuid.uuid4())
    file_path = os.path.join(tempfile.gettempdir(), f"{file_id}{file_extension}")

    with open(file_path, "wb") as file:
        file.write(file_content.getbuffer())

    return file_path


def app():

    st.title("Upload Vector Data")

    row1_col1, row1_col2 = st.columns([2, 1])
    width = 950
    height = 600

    with row1_col2:

        backend = st.selectbox(
            "Select a plotting backend", ["folium", "kepler.gl", "pydeck"], index=2
        )

        if backend == "folium":
            import leafmap.foliumap as leafmap
        elif backend == "kepler.gl":
            import leafmap.kepler as leafmap
        elif backend == "pydeck":
            import leafmap.deck as leafmap

        url = st.text_input(
            "Enter a URL to a vector dataset",
            "https://github.com/giswqs/streamlit-geospatial/raw/master/data/us_states.geojson",
        )

        data = st.file_uploader(
            "Upload a vector dataset", type=["geojson", "kml", "zip", "tab"]
        )

        container = st.container()

        if data or is_trusted_url(url):
            if data:
                file_path = save_uploaded_file(data, data.name)
                layer_name = os.path.splitext(data.name)[0]
            elif url:
                file_path = url
                layer_name = url.split("/")[-1].split(".")[0]

            with row1_col1:
                if file_path.lower().endswith(".kml"):
                    fiona.drvsupport.supported_drivers["KML"] = "rw"
                    gdf = gpd.read_file(file_path, driver="KML")
                else:
                    gdf = gpd.read_file(file_path)
                lon, lat = leafmap.gdf_centroid(gdf)
                if backend == "pydeck":

                    column_names = gdf.columns.values.tolist()
                    random_column = None
                    with container:
                        random_color = st.checkbox("Apply random colors", True)
                        if random_color:
                            random_column = st.selectbox(
                                "Select a column to apply random colors", column_names
                            )

                    m = leafmap.Map(center=(lat, lon))
                    m.add_gdf(gdf, random_color_column=random_column)
                    st.pydeck_chart(m)

                else:
                    m = leafmap.Map(center=(lat, lon), draw_export=True)
                    m.add_gdf(gdf, layer_name=layer_name)
                    # m.add_vector(file_path, layer_name=layer_name)
                    if backend == "folium":
                        m.zoom_to_gdf(gdf)
                    m.to_streamlit(width=width, height=height)

        else:
            with row1_col1:
                m = leafmap.Map()
                st.pydeck_chart(m)


app()