test app
Browse files
app.py
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import folium
|
3 |
+
from streamlit_folium import st_folium
|
4 |
+
import requests
|
5 |
+
from PIL import Image
|
6 |
+
from io import BytesIO
|
7 |
+
import os
|
8 |
+
|
9 |
+
# Securely get the token from environment variables
|
10 |
+
MAPILLARY_ACCESS_TOKEN = os.environ.get('MAPILLARY_ACCESS_TOKEN')
|
11 |
+
|
12 |
+
# Verify token exists
|
13 |
+
if not MAPILLARY_ACCESS_TOKEN:
|
14 |
+
st.error("Mapillary access token not found. Please configure it in the Space secrets.")
|
15 |
+
st.stop()
|
16 |
+
|
17 |
+
def get_nearest_image(lat, lon, radius=50):
|
18 |
+
"""
|
19 |
+
Get the nearest Mapillary image to given coordinates
|
20 |
+
"""
|
21 |
+
params = {
|
22 |
+
'access_token': MAPILLARY_ACCESS_TOKEN,
|
23 |
+
'fields': 'id,thumb_1024_url',
|
24 |
+
'limit': 1
|
25 |
+
}
|
26 |
+
|
27 |
+
query = f"""
|
28 |
+
{{
|
29 |
+
"point": {{"type": "Point", "coordinates": [{lon}, {lat}]}},
|
30 |
+
"radius": {radius}
|
31 |
+
}}
|
32 |
+
"""
|
33 |
+
|
34 |
+
try:
|
35 |
+
response = requests.post(
|
36 |
+
"https://graph.mapillary.com/images/search",
|
37 |
+
json=query,
|
38 |
+
params=params
|
39 |
+
)
|
40 |
+
response.raise_for_status()
|
41 |
+
data = response.json()
|
42 |
+
|
43 |
+
if 'data' in data and len(data['data']) > 0:
|
44 |
+
return data['data'][0]
|
45 |
+
return None
|
46 |
+
|
47 |
+
except requests.exceptions.RequestException as e:
|
48 |
+
st.error(f"Error fetching Mapillary data: {str(e)}")
|
49 |
+
return None
|
50 |
+
|
51 |
+
def main():
|
52 |
+
st.title("Amsterdam Street View Explorer")
|
53 |
+
|
54 |
+
# Initialize the map centered on Amsterdam
|
55 |
+
amsterdam_coords = [52.3676, 4.9041]
|
56 |
+
m = folium.Map(location=amsterdam_coords, zoom_start=13)
|
57 |
+
|
58 |
+
# Add a marker for Amsterdam city center
|
59 |
+
folium.Marker(
|
60 |
+
amsterdam_coords,
|
61 |
+
popup="Amsterdam City Center",
|
62 |
+
icon=folium.Icon(color="red", icon="info-sign")
|
63 |
+
).add_to(m)
|
64 |
+
|
65 |
+
# Display the map and get clicked coordinates
|
66 |
+
map_data = st_folium(m, height=400, width=700)
|
67 |
+
|
68 |
+
# Check if a location was clicked
|
69 |
+
if map_data['last_clicked']:
|
70 |
+
lat = map_data['last_clicked']['lat']
|
71 |
+
lng = map_data['last_clicked']['lng']
|
72 |
+
|
73 |
+
st.write(f"Selected coordinates: {lat:.4f}, {lng:.4f}")
|
74 |
+
|
75 |
+
# Get nearest Mapillary image
|
76 |
+
with st.spinner('Fetching street view image...'):
|
77 |
+
image_data = get_nearest_image(lat, lng)
|
78 |
+
|
79 |
+
if image_data:
|
80 |
+
# Display the image
|
81 |
+
try:
|
82 |
+
response = requests.get(image_data['thumb_1024_url'])
|
83 |
+
image = Image.open(BytesIO(response.content))
|
84 |
+
st.image(image, caption="Street View", use_column_width=True)
|
85 |
+
|
86 |
+
# Add download button
|
87 |
+
st.download_button(
|
88 |
+
label="Download Image",
|
89 |
+
data=response.content,
|
90 |
+
file_name=f"streetview_{lat}_{lng}.jpg",
|
91 |
+
mime="image/jpeg"
|
92 |
+
)
|
93 |
+
|
94 |
+
except Exception as e:
|
95 |
+
st.error(f"Error displaying image: {str(e)}")
|
96 |
+
else:
|
97 |
+
st.warning("No street view images found at this location. Try a different spot.")
|
98 |
+
|
99 |
+
if __name__ == "__main__":
|
100 |
+
main()
|