Commit 24cbaffa authored by Oscar Roisin's avatar Oscar Roisin

add objective 2

parents 9e3d6cae 91259e12
......@@ -16,13 +16,14 @@ def getDatasForPeriod(startPeriod, endPeriod, indicators):
return datas
def verifyDateInPeriod(startPeriod, endPeriod, year, month, day):
date = datetime.strptime(year + "-" + month + "-" + day, "%Y-%m-%d")
dateStart = datetime.strptime(startPeriod, "%Y-%m-%d")
dateEnd = datetime.strptime(endPeriod, "%Y-%m-%d")
if date < dateStart or date > dateEnd:
return False
return True
isDate = year.isdigit() and month.isdigit() and day.isdigit()
if isDate:
date = datetime.strptime(year + "-" + month + "-" + day, "%Y-%m-%d")
dateStart = datetime.strptime(startPeriod, "%Y-%m-%d")
dateEnd = datetime.strptime(endPeriod, "%Y-%m-%d")
if date >= dateStart and date <= dateEnd:
return True
return False
def getDecileForAllStations(startPeriod, endPeriod, table, nb_indicators, indicators_list):
# map with station and list of maps
......
......@@ -307,7 +307,7 @@ def createTableQueryPartitionningByDate(table):
peak_wind_drct decimal,
peak_wind_time decimal,
metar varchar,
PRIMARY KEY((year), month, day, hour, minute, station)
PRIMARY KEY((year, month), day, station, hour, minute)
)"""
return query
......
import folium
from cassandra.cluster import Cluster
from kmeans import verifyDateInPeriod
import loading
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")
# 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
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:
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)
# 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()
mapIndicator('2010-03-10', numeric_columns)
print()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment