map.py 2.73 KB
Newer Older
1
import folium
2
from cassandra.cluster import Cluster
3

4 5
from kmeans import verifyDateInPeriod
import loading
6

7 8 9 10 11 12 13 14 15
table_name_space = loading.table_name_space
table_name_date = loading.table_name_date
numeric_columns = loading.numeric_columns.copy()
numeric_columns.remove('lon')
numeric_columns.remove('lat')

FIRST_DAY = loading.FIRST_DAY
LAST_DAY = loading.LAST_DAY

Oscar Roisin's avatar
Oscar Roisin committed
16

17 18 19 20 21 22
def getDailyIndicator(year, month, day, indicators):
    # Verify if date exists in the database
    if verifyDateInPeriod(FIRST_DAY, LAST_DAY, year, month, day):
        # Format indicators for request
        ind = list(map(lambda i: f", avg({i}) as {i}", indicators))
        datas = session.execute(f"SELECT station, avg(lat) as lat, avg(lon) as lon {''.join(ind)} FROM {table_name_date} WHERE year = {year} AND month = {month} AND day = {day} GROUP BY year, month, day, station")
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
        # Reset indicators
        ind = indicators.copy()
        ind.insert(0, 'lon')
        ind.insert(0, 'lat')
        ind.insert(0, 'station')
        # Create list of dict
        result = []
        for row in datas:
            rd = dict()
            n = 0
            for elem in row:
                rd[ind[n]]=elem
                n += 1
            result.append(rd)
        return result
38 39 40
    else:
        return []

Oscar Roisin's avatar
Oscar Roisin committed
41 42 43 44 45 46 47
'''
name : mapIndicator
description : Enregistre la carte représentant les indicateurs souhaités pour la date précisée en paramètre
parameters :
    * date : date au format yyyy-mm-dd
    * indicators : liste d'indicateurs
'''
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
def mapIndicator(date, indicators):
    # Split date and filter indicators
    year, month, day = date.split('-')
    indicators = list(filter(lambda i: i in numeric_columns, indicators))
    # Get data for date and filtered indicators
    data = getDailyIndicator(year, month, day, indicators)
    # Verify if date components are integers
    year = int(year)
    month = int(month)
    day = int(day)
    # Test if data for the date selected
    if not(data):
        print(f"Aucune donnée pour pour le jour : {year}-{month}-{day}")
        return
    else:
sim-baz's avatar
sim-baz committed
63
        file_name = f"map_for_{year}-{month}-{day}.html"
64
        # Create map
sim-baz's avatar
sim-baz committed
65
        m = folium.Map(location=[64.2815, 27.6753], zoom_start = 5)
66 67
        # Add Marker for each station
        for d in data:
Oscar Roisin's avatar
Oscar Roisin committed
68 69 70 71
            text = ''
            for key, value in d.items():
                text = text+'<br><b>'+key+'</b> : '+str(value)
            folium.Marker([d['lat'], d['lon']], popup=f"{text}").add_to(m)
72 73 74 75 76 77 78 79 80
        # Save map
        m.save(file_name)
        print(f"La carte a été enregistrée à {file_name}")

if __name__ == '__main__':
    cluster = Cluster()
    session = cluster.connect()
    session.set_keyspace("bazinsim_roisinos_metar")
    print()
81
    mapIndicator('2010-03-10', numeric_columns)
82
    print()