Inam65 commited on
Commit
e6d0365
·
verified ·
1 Parent(s): 8bbda0f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -0
app.py CHANGED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import folium
3
+ from streamlit_folium import st_folium
4
+ from geopy.geocoders import Nominatim
5
+ from datetime import datetime
6
+
7
+ # Initialize Geolocator
8
+ geolocator = Nominatim(user_agent="geoapiExercises")
9
+
10
+ # Streamlit App
11
+ st.set_page_config(page_title="World Map Search", layout="wide")
12
+ st.title("🌍 Interactive World Map Search")
13
+
14
+ # Session states initialization
15
+ if 'latitude' not in st.session_state:
16
+ st.session_state['latitude'] = 20.0
17
+ if 'longitude' not in st.session_state:
18
+ st.session_state['longitude'] = 0.0
19
+ if 'location_name' not in st.session_state:
20
+ st.session_state['location_name'] = ""
21
+
22
+ # Date input (default today)
23
+ default_date = datetime.utcnow().date()
24
+ selected_date = st.text_input("Date (YYYY-MM-DD)", value=str(default_date))
25
+
26
+ # Layout: 2 columns for inputs
27
+ col1, col2, col3 = st.columns(3)
28
+
29
+ with col1:
30
+ latitude = st.text_input("Latitude", value=str(st.session_state['latitude']))
31
+ with col2:
32
+ longitude = st.text_input("Longitude", value=str(st.session_state['longitude']))
33
+ with col3:
34
+ location_search = st.text_input("Search Location", value=st.session_state['location_name'])
35
+
36
+ # Search button
37
+ if st.button("Search"):
38
+ if location_search:
39
+ try:
40
+ location = geolocator.geocode(location_search)
41
+ if location:
42
+ st.session_state['latitude'] = location.latitude
43
+ st.session_state['longitude'] = location.longitude
44
+ st.success(f"Location found: {location.address}")
45
+ else:
46
+ st.error("Location not found. Try another keyword.")
47
+ except:
48
+ st.error("Error while searching the location.")
49
+ else:
50
+ try:
51
+ # If no search but lat/lon entered manually
52
+ lat = float(latitude)
53
+ lon = float(longitude)
54
+ location = geolocator.reverse((lat, lon))
55
+ if location:
56
+ st.session_state['location_name'] = location.address
57
+ st.success(f"Location updated to: {location.address}")
58
+ else:
59
+ st.warning("Coordinates valid, but no address found.")
60
+ st.session_state['latitude'] = lat
61
+ st.session_state['longitude'] = lon
62
+ except ValueError:
63
+ st.error("Invalid latitude or longitude input.")
64
+
65
+ # Map
66
+ st.subheader("Map View:")
67
+
68
+ # Create Map
69
+ m = folium.Map(location=[st.session_state['latitude'], st.session_state['longitude']], zoom_start=5)
70
+ # Add marker
71
+ folium.Marker(
72
+ [st.session_state['latitude'], st.session_state['longitude']],
73
+ popup=st.session_state['location_name'] or "Selected Location",
74
+ tooltip="Click for location",
75
+ ).add_to(m)
76
+
77
+ # Display Map
78
+ st_data = st_folium(m, width=1000, height=600)
79
+
80
+ # Update lat/lon if user clicks on the map
81
+ if st_data and st_data.get("last_clicked"):
82
+ clicked_lat = st_data["last_clicked"]["lat"]
83
+ clicked_lon = st_data["last_clicked"]["lng"]
84
+ st.session_state['latitude'] = clicked_lat
85
+ st.session_state['longitude'] = clicked_lon
86
+ try:
87
+ location = geolocator.reverse((clicked_lat, clicked_lon))
88
+ if location:
89
+ st.session_state['location_name'] = location.address
90
+ else:
91
+ st.session_state['location_name'] = ""
92
+ except:
93
+ st.session_state['location_name'] = ""
94
+ st.experimental_rerun() # Rerun to update UI