113 lines
3.7 KiB
Python
113 lines
3.7 KiB
Python
# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2025)
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
from urllib.error import URLError
|
|
|
|
import pandas as pd
|
|
import pydeck as pdk
|
|
|
|
import streamlit as st
|
|
from streamlit.hello.utils import show_code
|
|
|
|
|
|
def mapping_demo() -> None:
|
|
@st.cache_data
|
|
def from_data_file(filename: str) -> pd.DataFrame:
|
|
url = (
|
|
"https://raw.githubusercontent.com/streamlit/"
|
|
f"example-data/master/hello/v1/{filename}"
|
|
)
|
|
return pd.read_json(url)
|
|
|
|
try:
|
|
all_layers = {
|
|
"Bike rentals": pdk.Layer(
|
|
"HexagonLayer",
|
|
data=from_data_file("bike_rental_stats.json"),
|
|
get_position=["lon", "lat"],
|
|
radius=200,
|
|
elevation_scale=4,
|
|
elevation_range=[0, 1000],
|
|
extruded=True,
|
|
),
|
|
"Bart stop exits": pdk.Layer(
|
|
"ScatterplotLayer",
|
|
data=from_data_file("bart_stop_stats.json"),
|
|
get_position=["lon", "lat"],
|
|
get_color=[200, 30, 0, 160],
|
|
get_radius="[exits]",
|
|
radius_scale=0.05,
|
|
),
|
|
"Bart stop names": pdk.Layer(
|
|
"TextLayer",
|
|
data=from_data_file("bart_stop_stats.json"),
|
|
get_position=["lon", "lat"],
|
|
get_text="name",
|
|
get_color=[0, 0, 0, 200],
|
|
get_size=10,
|
|
get_alignment_baseline="'bottom'",
|
|
),
|
|
"Outbound flow": pdk.Layer(
|
|
"ArcLayer",
|
|
data=from_data_file("bart_path_stats.json"),
|
|
get_source_position=["lon", "lat"],
|
|
get_target_position=["lon2", "lat2"],
|
|
get_source_color=[200, 30, 0, 160],
|
|
get_target_color=[200, 30, 0, 160],
|
|
auto_highlight=True,
|
|
width_scale=0.0001,
|
|
get_width="outbound",
|
|
width_min_pixels=3,
|
|
width_max_pixels=30,
|
|
),
|
|
}
|
|
st.sidebar.subheader("Map layers")
|
|
selected_layers = [
|
|
layer
|
|
for layer_name, layer in all_layers.items()
|
|
if st.sidebar.checkbox(layer_name, True)
|
|
]
|
|
if selected_layers:
|
|
st.pydeck_chart(
|
|
pdk.Deck(
|
|
map_style=None,
|
|
initial_view_state={
|
|
"latitude": 37.76,
|
|
"longitude": -122.4,
|
|
"zoom": 11,
|
|
"pitch": 50,
|
|
},
|
|
layers=selected_layers,
|
|
)
|
|
)
|
|
else:
|
|
st.error("Please choose at least one layer above.")
|
|
except URLError as e:
|
|
st.error(
|
|
f"""
|
|
**This demo requires internet access.**
|
|
Connection error: {e.reason}
|
|
"""
|
|
)
|
|
|
|
|
|
st.set_page_config(page_title="Mapping demo", page_icon=":material/public:")
|
|
st.title("Mapping demo")
|
|
st.write(
|
|
"""
|
|
This demo shows how to use `st.pydeck_chart` to display geospatial data.
|
|
"""
|
|
)
|
|
mapping_demo()
|
|
show_code(mapping_demo)
|