map.py 2.48 KB
Newer Older
Oscar Roisin's avatar
Oscar Roisin committed
1
import folium
2
from cassandra.cluster import Cluster
Oscar Roisin's avatar
Oscar Roisin committed
3

4
5
from kmeans import verifyDateInPeriod
import loading
Oscar Roisin's avatar
Oscar Roisin committed
6

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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

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")
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
        # 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    else:
        return []

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:
        file_name = f"{year}-{month}-{day}.html"
        # Create map
        m = folium.Map(location=[64.2815, 27.6753])
        # Add Marker for each station
        for d in data:
Oscar Roisin's avatar
Oscar Roisin committed
60
61
62
63
            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)
64
65
66
67
68
69
70
71
72
        # 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()
73
    mapIndicator('2010-03-10', numeric_columns)
74
    print()