question2.py 1.9 KB
Newer Older
Unknown's avatar
Unknown committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
import numpy as np
import matplotlib.pyplot as plt
from functools import reduce
from database_pre2 import connection
import re
import folium


def createMap(data):
    mean_lat = 0
    mean_lon = 0
    count = 0
    for each in data.result():
        #print(each)
Tianyang's avatar
Tianyang committed
15 16
        mean_lat += each[2]
        mean_lon += each[3]
Unknown's avatar
Unknown committed
17 18 19 20 21 22 23
        count += 1
    if count == 0:
        print('No data available at this timestamp !')
        return
    mean_lat = mean_lat/count
    mean_lon = mean_lon/count

Unknown's avatar
Unknown committed
24
    m = folium.Map(location=[mean_lon,mean_lat], zoom_start=6)
Unknown's avatar
Unknown committed
25

Unknown's avatar
Unknown committed
26
    # Attributes names which will be displayed on the map
Unknown's avatar
Unknown committed
27 28 29 30
    attributes = ["alti", "drct", "dwpf", "feel", "gust", "ice_accretion_1hr", "ice_accretion_3hr", "ice_accretion_6hr",
                  "metar", "mslp", "p01i", "peak_wind_drct", "peak_wind_gust", "peak_wind_time", "relh", "sknt",
                  "skyc1", "skyc2", "skyc3", "skyc4", "skyl1", "skyl2", "skyl3", "skyl4", "tmpf", "vsby", "wxcodes"]
    for each in data.result():
Tianyang's avatar
Tianyang committed
31
        # Here we choose not to display the None values and the METAR ID
Tianyang's avatar
Tianyang committed
32 33
        ls = ["station:"+each[4]+"\n"]
        l = [attributes[i] + ":" + str(each[i + 5]) for i in range(len(attributes)) if each[i + 5] != None
Unknown's avatar
Unknown committed
34
             and attributes[i] != "metar"]
Tianyang's avatar
Tianyang committed
35
        l = ls + l
Unknown's avatar
Unknown committed
36
        string='\n'.join(l)
Tianyang's avatar
Tianyang committed
37
        folium.Marker([each[3],each[2]],
Unknown's avatar
Unknown committed
38 39
                      popup=string,
                      icon=folium.Icon(color='red')).add_to(m)
Tianyang's avatar
Tianyang committed
40
    m.save("Projet-NF26/map2.html")
Tianyang's avatar
Tianyang committed
41
    print("Generated successfully!")
Unknown's avatar
Unknown committed
42 43 44 45


if __name__ == "__main__":
    session = connection()
Unknown's avatar
Unknown committed
46 47
    # Timestamp user wants to search
    date = input("Please enter the date you want to search (format 'yyyy-MM-dd'):")
Tianyang's avatar
Tianyang committed
48
    time = input("Please enter the time you want to search (format 'hh:MM'):")
Unknown's avatar
Unknown committed
49
    #timestamp = '2017-12-02 00:30:00'
Tianyang's avatar
Tianyang committed
50
    data = session.execute_async("select * from caitiany.database_time where date = '%s' AND time = '%s'"%(date,time))
Unknown's avatar
Unknown committed
51
    createMap(data)