File size: 2,843 Bytes
31801e9
aa6995b
31801e9
 
 
 
 
 
 
 
 
 
2aba678
31801e9
 
 
 
c247940
31801e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2aba678
31801e9
 
660bd96
31801e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import folium
from folium.plugins import HeatMap
from ingest import load_data
from process import get_lat_lon
from gradio_folium import Folium
from folium import Map


def update_header(file_info):
    if file_info is not None:
        filename = file_info.split('/')[-1]  # Access the filename from the file_info dictionary
        header = f"<h1>UK Postcodes Map: {filename}</h1>"  # Update the Markdown content
    return header  # Continue to pass the file_info to the next function if necessary

def generate_map(file_path):
    # Load the postcodes
    postcode_mapping = load_data('data/ukpostcodes.csv')
    # Load the data (this needs to be adapted to work outside Flask)
    postcodes = load_data(file_path)

    # Get latitude, longitude, and count data for the specified postcodes
    lat_lon_data = get_lat_lon(postcodes, postcode_mapping)

    # Prepare data for different frequency bands
    low_freq_data = [
        [data['latitude'], data['longitude']] 
        for data in lat_lon_data 
        if data['count'] == 1 and data['latitude'] and data['longitude']
    ]
    med_freq_data = [
        [data['latitude'], data['longitude']] 
        for data in lat_lon_data 
        if 2 <= data['count'] <= 5 and data['latitude'] and data['longitude']
    ]
    high_freq_data = [
        [data['latitude'], data['longitude']] 
        for data in lat_lon_data 
        if data['count'] > 5 and data['latitude'] and data['longitude']
    ]

    # Create your map here using Folium
    map = Map(location=[51.505303, -0.13902], zoom_start=10)

    # Adding different heatmaps for different frequencies
    if low_freq_data:
        HeatMap(low_freq_data, radius=10, blur=10, gradient={0.8: 'blue', 1: 'lime'}).add_to(map)
    if med_freq_data:
        HeatMap(med_freq_data, radius=15, blur=10, gradient={0.8: 'orange', 1: 'lime'}).add_to(map)
    if high_freq_data:
        HeatMap(high_freq_data, radius=20, blur=10, gradient={0.8: 'red', 1: 'lime'}).add_to(map)
    
    return map 

# Define a Gradio interface
with gr.Blocks() as demo:

    with gr.Row(): 
        header = gr.Markdown(("<h1>UK Postcodes Map</h1>"))
    
    with gr.Row():
        map = Folium(value = Map(location=[51.505303, -0.13902], zoom_start=10), height=700)


    with gr.Row():
        file_uploader = gr.UploadButton(
            label=("Upload"),
            file_count="single",
            file_types=[".csv", ".xlsx", '.xls'],
            interactive=True,
            scale=1,
        )

    file_uploader.upload(fn = generate_map, 
                        inputs= file_uploader, 
                        outputs=map)
    
    file_uploader.upload(fn=update_header, 
                         inputs=file_uploader, 
                         outputs=header)

                    
if __name__ == "__main__":
    demo.launch()