Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
import folium
|
4 |
+
from streamlit_folium import folium_static
|
5 |
+
import pandas as pd
|
6 |
+
from streamlit.runtime.state import SessionStateProxy
|
7 |
+
|
8 |
+
@st.cache_resource
|
9 |
+
def load_states():
|
10 |
+
return pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv")
|
11 |
+
|
12 |
+
@st.cache_resource
|
13 |
+
def load_cities():
|
14 |
+
return pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/2014_us_cities.csv")
|
15 |
+
|
16 |
+
def get_lat_lon(city, state):
|
17 |
+
cities_df = load_cities()
|
18 |
+
city_row = cities_df[(cities_df["City"] == city) & (cities_df["State"] == state)]
|
19 |
+
if not city_row.empty:
|
20 |
+
return city_row["lat"].values[0], city_row["lon"].values[0]
|
21 |
+
return None
|
22 |
+
|
23 |
+
# Set the page configuration
|
24 |
+
st.set_page_config(page_title="Google Maps 3D", layout="wide")
|
25 |
+
|
26 |
+
# Set your Google Maps API key
|
27 |
+
api_key = os.getenv("GM_TOKEN")
|
28 |
+
|
29 |
+
# Load states data
|
30 |
+
states_df = load_states()
|
31 |
+
state_codes = sorted(states_df["code"].unique())
|
32 |
+
|
33 |
+
# Initialize session state
|
34 |
+
if "city" not in st.session_state:
|
35 |
+
st.session_state["city"] = "Mound"
|
36 |
+
if "state" not in st.session_state:
|
37 |
+
st.session_state["state"] = "MN"
|
38 |
+
|
39 |
+
# Create Streamlit UI elements
|
40 |
+
st.title("π Google Maps 3D")
|
41 |
+
city = st.text_input("Enter a city ποΈ", value=st.session_state["city"])
|
42 |
+
state = st.selectbox("Select a state π", options=state_codes, index=state_codes.index(st.session_state["state"]))
|
43 |
+
search_button = st.button("Search π")
|
44 |
+
|
45 |
+
# Update session state
|
46 |
+
st.session_state["city"] = city
|
47 |
+
st.session_state["state"] = state
|
48 |
+
|
49 |
+
# Get the latitude and longitude of the selected city and state
|
50 |
+
if search_button:
|
51 |
+
lat_lon = get_lat_lon(city, state)
|
52 |
+
if lat_lon:
|
53 |
+
map_center = list(lat_lon)
|
54 |
+
map_zoom = 12
|
55 |
+
else:
|
56 |
+
st.warning(f"Could not find the city '{city}' in the state '{state}'.")
|
57 |
+
map_center = [37.7749, -122.4194] # Example coordinates for San Francisco
|
58 |
+
map_zoom = 12
|
59 |
+
else:
|
60 |
+
map_center = get_lat_lon("Mound", "MN")
|
61 |
+
map_zoom = 12
|
62 |
+
|
63 |
+
# Create a Folium map instance
|
64 |
+
m = folium.Map(location=map_center, zoom_start=map_zoom)
|
65 |
+
|
66 |
+
# Add the Google Maps 3D tile layer
|
67 |
+
google_3d_layer = folium.TileLayer(
|
68 |
+
tiles=f"https://tile.googleapis.com/v1/3dtiles/root.json?key={api_key}",
|
69 |
+
attr="Google",
|
70 |
+
name="Google Maps 3D",
|
71 |
+
overlay=True,
|
72 |
+
control=True,
|
73 |
+
)
|
74 |
+
google_3d_layer.add_to(m)
|
75 |
+
|
76 |
+
# Display the Folium map using streamlit-folium
|
77 |
+
folium_static(m, width=800, height=600)
|