question2.py 1.88 KB
Newer Older
Unknown's avatar
Unknown committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
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)
        mean_lat += each[1]
        mean_lon += each[2]
        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 31
    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():
        # print(each)
Unknown's avatar
Unknown committed
32
        # Here we choose not to display the "nan" values and the METAR ID
Tianyang's avatar
Tianyang committed
33
        l = [attributes[i] + ":" + str(each[i + 4]) for i in range(len(attributes)) if str(each[i + 4]) != 'null'
Unknown's avatar
Unknown committed
34 35 36
             and attributes[i] != "metar"]
        string='\n'.join(l)
        folium.Marker([each[2],each[1]],
Unknown's avatar
Unknown committed
37 38
                      popup=string,
                      icon=folium.Icon(color='red')).add_to(m)
Tianyang's avatar
Tianyang committed
39
    m.save("Projet-NF26/map2.html")
Unknown's avatar
Unknown committed
40 41 42 43


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