-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path01_π_Main.py
173 lines (136 loc) Β· 6.66 KB
/
01_π_Main.py
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
####################################################################################################
# Author: Ryan Zhang
# Description: Main file for the project
# Date: 2023
####################################################################################################
from datetime import date
from time import sleep
import pandas as pd
import streamlit as st
import streamlit_js_eval as stjs
from geopy.geocoders import Nominatim
import folium
from streamlit_folium import st_folium
from PIL import Image
def get_pos(lat, lng):
return lat, lng
st.set_page_config(
page_title="Map It Forward",
page_icon="π",
initial_sidebar_state="expanded",
menu_items={
'About': "Show where you want community improvements!"
}
)
#Bug report system
st.sidebar.info(f'If you find a bug, please report it [here.](https://github.com/Rhyzhang/map-it-forward/issues)')
# Logo
logo = Image.open('image/logo.jpg')
st.image(logo)
st.markdown("## A participatory community mapping tool")
st.markdown("Pin where you want community improvements!")
# Load data
submissions = pd.read_csv("database/submissions.csv")
# Select Postion Map
with st.expander("Select Postion on Map"):
if 'pos' not in st.session_state:
st.session_state.pos = None
m = folium.Map(location=[40.712772, -74.006058], zoom_start=10, min_zoom=10, min_lat = 40.3, max_lat = 40.98, min_lon = -73.5, max_lon = -74.4, max_bounds = True)
m.add_child(folium.LatLngPopup())
map = st_folium(m, height=350, width=700)
data = None
if map.get("last_clicked"):
data = get_pos(map["last_clicked"]["lat"], map["last_clicked"]["lng"])
if data is not None:
st.session_state.pos = data
st.info("Your location is: " + str(st.session_state.pos))
# Initial Map
map_container = st.empty()
with map_container:
m = folium.Map(location=[40.712772, -74.006058], zoom_start=10, min_zoom=10, min_lat = 40.3, max_lat = 40.98, min_lon = -73.5, max_lon = -74.4, max_bounds = True)
for index,row in submissions.iterrows():
folium.Marker(
[row["lat"], row["lon"]], popup=row["recommendation"], tooltip=row["category"]
).add_to(m)
st_folium(m, height=350, width=725, returned_objects=[])
# If user has selected a location, show it on the map
if st.session_state.pos is not None:
lat, lon = st.session_state.pos
with map_container:
m = folium.Map(location=[40.712772, -74.006058], zoom_start=10, min_zoom=10, min_lat = 40.3, max_lat = 40.98, min_lon = -73.5, max_lon = -74.4, max_bounds = True)
folium.Marker(
[st.session_state.pos[0], st.session_state.pos[1]], popup="Location", tooltip="Your Current Location"
).add_to(m)
st_folium(m, height=350, width=725, returned_objects=[])
# Auto get Location if User wants
loc = False
geolocator = Nominatim(user_agent="community-service")
if st.button("Check my location automatically", help="Need trouble shooting? Go to about page: ..."):
loc = stjs.get_geolocation()
sleep(1)
st.info("The accuracy is: " + str(loc["coords"]["accuracy"]))
lat = loc["coords"]["latitude"]
lon = loc["coords"]["longitude"]
with map_container:
m = folium.Map(location=[lat, lon], zoom_start=12, min_zoom=10, min_lat = 40.3, max_lat = 40.98, min_lon = -73.5, max_lon = -74.4, max_bounds = True)
folium.Marker(
[lat, lon], popup="Location", tooltip="Your Current Location"
).add_to(m)
map = st_folium(m, height=350, width=725, returned_objects=[])
# Get submissions
df = pd.read_csv("database/submissions.csv")
# Create a form to submit a submission
with st.form("Add submission", clear_on_submit=True):
today_date = date.today().strftime("%m/%d/%Y")
age = st.number_input("Age", min_value=0, max_value=100, value=0)
category = st.multiselect("Category", ["Roads", "Parks", "Schools", "Housing", "Other"])
if loc or st.session_state.pos is not None:
# Automatically fill in the location and address
lat = st.number_input("Latitude", min_value=-90.0, max_value=90.0, format="%f", value=float(lat))
lon = st.number_input("Longitude", min_value=-180.0, max_value=180.0, format="%f", value=float(lon))
location = geolocator.reverse(f"{lat}, {lon}")
address = st.text_input("Address", value=location.address)
else:
lat = st.number_input("Latitude", min_value=-90.0, max_value=90.0, format="%f", value=0.0)
lon = st.number_input("Longitude", min_value=-180.0, max_value=180.0, format="%f", value=0.0)
address = st.text_input("Address")
reccomendation = st.text_area("Reccomendation")
severity = st.slider("Severity", min_value=0, max_value=10, value=0)
description = st.text_area("Description")
img_file_upload_buffer = st.file_uploader("Upload a picture")
img_file_picture_buffer = st.camera_input("Take a picture")
if st.form_submit_button("Submit"):
img_file_buffer = None
if img_file_upload_buffer is not None:
img_file_buffer = img_file_upload_buffer
if img_file_picture_buffer is not None:
img_file_buffer = img_file_picture_buffer
# Check if all fields are filled out
if age == 0.0 or category == [] or lat == 0.0 or lon == 0.0 or not address or not reccomendation or not description or img_file_buffer == None:
st.error("Please fill out all fields!")
else:
df.loc[len(df)] = [today_date, age, category, lat, lon, address, reccomendation, severity, description, 0]
df.to_csv("database/submissions.csv", index=False)
# To read image file buffer as a PIL Image:
img = Image.open(img_file_buffer)
img = img.convert('RGB')
st.image(img)
img.save("database/images/" + str(len(df) - 1) + ".jpg")
st.success("Submission submitted!")
sleep(1)
st.experimental_rerun()
# def get_pos(lat, lng):
# return lat, lng
# m = folium.Map(location=[40.712772, -74.006058], zoom_start=12)
# m.add_child(folium.LatLngPopup())
# map = st_folium(m, height=350, width=700)
# data = None
# if map.get("last_clicked"):
# data = get_pos(map["last_clicked"]["lat"], map["last_clicked"]["lng"])
# if data is not None:
# folium.Marker(
# [data[0], data[1]], popup="hi", tooltip="Your Current Location"
# ).add_to(m)
# st_folium(m, width=725, returned_objects=[])
# st.write(data) # Writes to the app
# print(data) # Writes to terminal