File size: 3,690 Bytes
9a4530c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe114b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a4530c
 
 
fe114b6
9a4530c
 
fe114b6
 
9a4530c
fe114b6
27431a7
9a4530c
fe114b6
 
27431a7
 
9a4530c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import folium
from streamlit_folium import st_folium
import requests
from PIL import Image
from io import BytesIO
import os

# Securely get the token from environment variables
MAPILLARY_ACCESS_TOKEN = os.environ.get('MAPILLARY_ACCESS_TOKEN')

# Verify token exists
if not MAPILLARY_ACCESS_TOKEN:
    st.error("Mapillary access token not found. Please configure it in the Space secrets.")
    st.stop()

def get_bounding_box(lat, lon):
    """
    Create a bounding box around a point that extends roughly 25 meters in each direction
    at Amsterdam's latitude (52.37°N):
    - 0.000224 degrees latitude = 25 meters N/S
    - 0.000368 degrees longitude = 25 meters E/W
    """
    lat_offset = 0.000224  # 25 meters in latitude
    lon_offset = 0.000368  # 25 meters in longitude
    return [
        lon - lon_offset,  # min longitude
        lat - lat_offset,  # min latitude
        lon + lon_offset,  # max longitude
        lat + lat_offset   # max latitude
    ]

def get_nearest_image(lat, lon):
    """
    Get the nearest Mapillary image to given coordinates
    """
    bbox = get_bounding_box(lat, lon)
    params = {
        'fields': 'id,thumb_1024_url',
        'limit': 1,
        'bbox': f'{bbox[0]},{bbox[1]},{bbox[2]},{bbox[3]}'
    }

    header = {'Authorization' : 'OAuth {}'.format(MAPILLARY_ACCESS_TOKEN)}
    try:
        response = requests.get(
            "https://graph.mapillary.com/images",
            params=params,
            headers=header
        )
        response.raise_for_status()
        data = response.json()
        
        if 'data' in data and len(data['data']) > 0:
            return data['data'][0]
        return None
        
    except requests.exceptions.RequestException as e:
        st.error(f"Error fetching Mapillary data: {str(e)}")
        return None

def main():
    st.title("Amsterdam Street View Explorer")
    
    # Initialize the map centered on Amsterdam
    amsterdam_coords = [52.3676, 4.9041]
    m = folium.Map(location=amsterdam_coords, zoom_start=13)
    
    # Add a marker for Amsterdam city center
    folium.Marker(
        amsterdam_coords,
        popup="Amsterdam City Center",
        icon=folium.Icon(color="red", icon="info-sign")
    ).add_to(m)
    
    # Display the map and get clicked coordinates
    map_data = st_folium(m, height=400, width=700)
    
    # Check if a location was clicked
    if map_data['last_clicked']:
        lat = map_data['last_clicked']['lat']
        lng = map_data['last_clicked']['lng']
        
        st.write(f"Selected coordinates: {lat:.4f}, {lng:.4f}")
        
        # Get nearest Mapillary image
        with st.spinner('Fetching street view image...'):
            image_data = get_nearest_image(lat, lng)
            
            if image_data:
                # Display the image
                try:
                    response = requests.get(image_data['thumb_1024_url'])
                    image = Image.open(BytesIO(response.content))
                    st.image(image, caption="Street View", use_column_width=True)
                    
                    # Add download button
                    st.download_button(
                        label="Download Image",
                        data=response.content,
                        file_name=f"streetview_{lat}_{lng}.jpg",
                        mime="image/jpeg"
                    )
                    
                except Exception as e:
                    st.error(f"Error displaying image: {str(e)}")
            else:
                st.warning("No street view images found at this location. Try a different spot.")

if __name__ == "__main__":
    main()