import streamlit as st import folium from folium.plugins import Draw from streamlit_folium import st_folium import psycopg2 import os # Set the page layout st.set_page_config(layout="wide") # Function to connect to the database # @st.cache_resource def connect_to_db(): try: conn = psycopg2.connect( dbname=st.secrets["dbname"], user=st.secrets["user"], password=st.secrets["password"], host=st.secrets["host"], port=st.secrets["port"], sslmode=st.secrets["ssl"] ) return conn except Exception as e: st.error(f"An error occurred while connecting to the database: {e}") return None # conn = connect_to_db() # Perform query. # Uses st.cache_data to only rerun when the query changes or after 10 min. # @st.cache_data(ttl=600) # Function to fetch previously saved data points def fetch_saved_data(): conn = connect_to_db() if conn: cursor = conn.cursor() cursor.execute('SELECT "X", "Y", description, gl_certainty FROM public.gettinglost_tracking') data = cursor.fetchall() cursor.close() conn.close() return data return [] # Perform query. # Uses st.cache_data to only rerun when the query changes or after 10 min. # Function to save data to the database # @st.cache_data(ttl=600) def save_data(lat, lon, description, rating): conn = connect_to_db() if conn: cursor = conn.cursor() cursor.execute('INSERT INTO public.gettinglost_tracking ("X", "Y", geom, description, gl_certainty) VALUES (%s, %s, ST_SetSRID(ST_MakePoint(%s, %s), 4326), %s, %s)', (lat, lon, lon, lat, description, rating)) conn.commit() cursor.close() conn.close() st.sidebar.success("Data recorded successfully!") else: st.sidebar.error("Failed to save data.") st.title('Getting Lost Mapping') # Create the Folium map with Draw plugin for point markers m = folium.Map(location=[51.505, -0.09], zoom_start=13, width='100%', height='100%') draw = Draw(draw_options={'polyline': False, "marker": True, 'rectangle': False, 'polygon': False, 'circle': False, 'circlemarker': False}) m.add_child(draw) # Display previously saved data points on the map # conn = connect_to_db() for point in fetch_saved_data(): lat, lon, desc, rating = point folium.Marker([lat, lon], popup=f"{desc} (Certainty: {rating})").add_to(m) # Render the map map_output = st_folium(m, width='100%', height=600) try: # Extract coordinates from drawn point coords = map_output["all_drawings"][-1]["geometry"]["coordinates"] st.session_state.coord = coords except: # Default coordinates if no point has been drawn yet coords = [0,0] # Data Entry Form in the sidebar st.sidebar.write("Select a location on the map using the location button, then fill out the form.") description = st.sidebar.text_area("Please describe the location in a few words:") rating = st.sidebar.selectbox('Certainty (low(1)-High(5)', ['1', '2', '3', '4', '5']) # Clicked coordinates clicked_lat = coords[1] # Default value, should ideally be updated on map click clicked_lon = coords[0] # Default value, should ideally be updated on map click if st.sidebar.button('Save'): save_data(clicked_lat, clicked_lon, description, rating)