|
import streamlit as st |
|
import folium |
|
from streamlit_folium import folium_static |
|
from folium.plugins import MarkerCluster |
|
import time |
|
import math |
|
|
|
|
|
california_med_centers = [ |
|
('UCSF Medical Center', 37.7631, -122.4576, 'General medical and surgical', 'San Francisco'), |
|
('Cedars-Sinai Medical Center', 34.0762, -118.3790, 'Heart specialty', 'Los Angeles'), |
|
('Stanford Health Care', 37.4331, -122.1750, 'Teaching hospital', 'Stanford'), |
|
('UCLA Medical Center', 34.0659, -118.4466, 'Research and teaching', 'Los Angeles'), |
|
('Scripps La Jolla Hospitals', 32.8851, -117.2255, 'Multiple specialties', 'La Jolla'), |
|
('Sharp Memorial Hospital', 32.7992, -117.1542, 'Trauma center', 'San Diego'), |
|
('Huntington Hospital', 34.1330, -118.1475, 'Non-profit hospital', 'Pasadena'), |
|
('Hoag Memorial Hospital', 33.6045, -117.8664, 'Community hospital', 'Newport Beach'), |
|
('UCSD Medical Center', 32.7554, -117.1682, 'Academic health center', 'San Diego'), |
|
('UC Davis Medical Center', 38.5539, -121.4554, 'Public academic health', 'Sacramento'), |
|
('John Muir Medical Center', 37.9192, -122.0426, 'Heart care', 'Walnut Creek'), |
|
('Santa Clara Valley Medical Center', 37.3121, -121.9769, 'County hospital', 'San Jose'), |
|
('Kaiser Permanente San Francisco', 37.7741, -122.4179, 'Health maintenance organization', 'San Francisco'), |
|
('City of Hope', 34.1285, -117.9665, 'Cancer center', 'Duarte'), |
|
('UCI Medical Center', 33.7886, -117.8572, 'University hospital', 'Orange'), |
|
('Good Samaritan Hospital', 34.0506, -118.2831, 'Private hospital', 'Los Angeles'), |
|
('Los Angeles County General', 34.0581, -118.2917, 'Public hospital', 'Los Angeles'), |
|
('California Pacific Medical Center', 37.7864, -122.4357, 'Private non-profit', 'San Francisco'), |
|
('Sutter Roseville Medical Center', 38.7521, -121.2760, 'General medical and surgical', 'Roseville'), |
|
('St. Joseph Hospital', 33.7821, -117.9188, 'Faith-based care', 'Orange') |
|
] |
|
|
|
|
|
if 'car_position' not in st.session_state: |
|
st.session_state.car_position = [37.7631, -122.4576] |
|
if 'is_moving' not in st.session_state: |
|
st.session_state.is_moving = False |
|
if 'start' not in st.session_state: |
|
st.session_state.start = california_med_centers[0] |
|
if 'destination' not in st.session_state: |
|
st.session_state.destination = california_med_centers[1] |
|
|
|
|
|
def calculate_distance(lat1, lon1, lat2, lon2): |
|
R = 3958.8 |
|
lat1_rad, lon1_rad = math.radians(lat1), math.radians(lon1) |
|
lat2_rad, lon2_rad = math.radians(lat2), math.radians(lon2) |
|
dlat = lat2_rad - lat1_rad |
|
dlon = lon2_rad - lon1_rad |
|
a = math.sin(dlat / 2)**2 + math.cos(lat1_rad) * math.cos(lat2_rad) * math.sin(dlon / 2)**2 |
|
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a)) |
|
return R * c |
|
|
|
|
|
def create_map(): |
|
m = folium.Map(location=st.session_state.car_position, zoom_start=13, tiles="OpenStreetMap") |
|
|
|
|
|
marker_cluster = MarkerCluster().add_to(m) |
|
for center in california_med_centers: |
|
folium.Marker( |
|
location=[center[1], center[2]], |
|
popup=f'<b>{center[0]}</b><br>Description: {center[3]}<br>City: {center[4]}', |
|
icon=folium.Icon(color='red') |
|
).add_to(marker_cluster) |
|
|
|
|
|
folium.Marker( |
|
location=st.session_state.car_position, |
|
popup='Car', |
|
icon=folium.Icon(color='blue', icon='car', prefix='fa') |
|
).add_to(m) |
|
|
|
return m |
|
|
|
|
|
def move_car(): |
|
target = [st.session_state.destination[1], st.session_state.destination[2]] |
|
lat_step = (target[0] - st.session_state.car_position[0]) / 50 |
|
lon_step = (target[1] - st.session_state.car_position[1]) / 50 |
|
st.session_state.car_position[0] += lat_step |
|
st.session_state.car_position[1] += lon_step |
|
|
|
|
|
st.markdown(""" |
|
<style> |
|
.full-width-map {width: 100%; height: 600px;} |
|
</style> |
|
""", unsafe_allow_html=True) |
|
|
|
with st.container(): |
|
m = create_map() |
|
folium_static(m, width=1200, height=600) |
|
|
|
st.markdown(""" |
|
# π₯ California Medical Centers π |
|
Select a start and destination, then drive the car across the map! The map stays centered on the car. |
|
""") |
|
|
|
|
|
distance = calculate_distance( |
|
st.session_state.start[1], st.session_state.start[2], |
|
st.session_state.destination[1], st.session_state.destination[2] |
|
) |
|
remaining_distance = calculate_distance( |
|
st.session_state.car_position[0], st.session_state.car_position[1], |
|
st.session_state.destination[1], st.session_state.destination[2] |
|
) |
|
st.write(f"**Start**: {st.session_state.start[0]} ({st.session_state.start[1]}, {st.session_state.start[2]})") |
|
st.write(f"**Destination**: {st.session_state.destination[0]} ({st.session_state.destination[1]}, {st.session_state.destination[2]})") |
|
st.write(f"**Total Distance**: {distance:.1f} miles") |
|
if st.session_state.is_moving: |
|
st.write(f"**Remaining Distance**: {remaining_distance:.1f} miles") |
|
|
|
|
|
col1, col2, col3 = st.columns(3) |
|
with col1: |
|
if st.button("Start Driving"): |
|
st.session_state.is_moving = True |
|
with col2: |
|
if st.button("Stop Driving"): |
|
st.session_state.is_moving = False |
|
with col3: |
|
if st.button("Reset Car to Start"): |
|
st.session_state.car_position = [st.session_state.start[1], st.session_state.start[2]] |
|
st.session_state.is_moving = False |
|
st.rerun() |
|
|
|
|
|
st.subheader("Set Start and Destination") |
|
start_col, dest_col = st.columns(2) |
|
with start_col: |
|
st.write("Pick Start Location:") |
|
for i in range(0, len(california_med_centers), 2): |
|
col1, col2 = st.columns(2) |
|
with col1: |
|
if st.button(f"Start: {california_med_centers[i][0]}", key=f"start_{i}"): |
|
st.session_state.start = california_med_centers[i] |
|
st.session_state.car_position = [california_med_centers[i][1], california_med_centers[i][2]] |
|
st.rerun() |
|
if i + 1 < len(california_med_centers): |
|
with col2: |
|
if st.button(f"Start: {california_med_centers[i+1][0]}", key=f"start_{i+1}"): |
|
st.session_state.start = california_med_centers[i+1] |
|
st.session_state.car_position = [california_med_centers[i+1][1], california_med_centers[i+1][2]] |
|
st.rerun() |
|
|
|
with dest_col: |
|
st.write("Pick Destination Location:") |
|
for i in range(0, len(california_med_centers), 2): |
|
col1, col2 = st.columns(2) |
|
with col1: |
|
if st.button(f"Dest: {california_med_centers[i][0]}", key=f"dest_{i}"): |
|
st.session_state.destination = california_med_centers[i] |
|
st.rerun() |
|
if i + 1 < len(california_med_centers): |
|
with col2: |
|
if st.button(f"Dest: {california_med_centers[i+1][0]}", key=f"dest_{i+1}"): |
|
st.session_state.destination = california_med_centers[i+1] |
|
st.rerun() |
|
|
|
|
|
if st.session_state.is_moving: |
|
move_car() |
|
time.sleep(0.5) |
|
st.rerun() |