Пример #1
0
from database_connection import DatabaseConnection
from services_utils import *

# Settings:
user = "******"
password = "******"
host = "127.0.0.1"
port = "5432"
database = "da3t_db"
google_key = ""
datatourisme_url = "http://*****:*****@application.route("/get-weather", methods = ["POST","GET"])
def get_weather():
	if request.method == "GET":
		datetime = request.args.get("datetime")
		station_id = request.args.get("station-id")
		
		datetime = datetime.split('-')[0] + "-" + datetime.split('-')[1] + "-" + datetime.split('-')[2] + " " + datetime.split('-')[3] + ":" + datetime.split('-')[4] + ":" + datetime.split('-')[5]
Пример #2
0
def visualise_orchestration(trajectory, stops_durations, entering_time,
                            trajectory_weather, weathers):
    fig, gnt = plt.subplots()
    gnt.set_ylim(0, 150)
    trajectory_duration = trajectory_to_hour(trajectory)

    start = trajectory_duration["start"]
    end = trajectory_duration["end"]

    gnt.set_xlim(start, end)
    gnt.set_xticks(np.arange(start, end, 1))

    plt.xticks(rotation=90)

    gnt.set_xlabel('Trajectory duration (hour)')
    gnt.set_ylabel('Orchestration')
    gnt.set_yticks([10, 40])
    gnt.set_yticklabels(['stops/moves', 'weather'])
    gnt.grid(True)

    # - Stops-moves visualization start -

    # Stops visualization:
    for stop_duration in stops_durations:
        time = stop_to_hour(stop_duration)
        start_stop = time["start"]
        end_stop = time["end"]
        gnt.broken_barh([(start_stop, end_stop - start_stop)], (5, 15),
                        facecolors=('tab:red'))
        time = None
        start = None
        end = None

    length = len(stops_durations)

    # Moves visualization:
    for i in range(length - 1):
        stop1 = stops_durations[i]
        time1 = stop_to_hour(stop1)
        stop2 = stops_durations[i + 1]
        time2 = stop_to_hour(stop2)

        if time2["start"] > time1["end"]:
            gnt.broken_barh([(time1["end"], time2["start"] - time1["end"])],
                            (5, 15),
                            facecolors=('tab:orange'))

    first_stop = stops_durations[0]
    split_first_stop = stop_to_hour(first_stop)
    if trajectory_duration["start"] < split_first_stop["start"]:
        gnt.broken_barh(
            [(trajectory_duration["start"],
              split_first_stop["start"] - trajectory_duration["start"])],
            (5, 15),
            facecolors=('tab:orange'))

    last_stop = stops_durations[-1]
    split_last_stop = stop_to_hour(last_stop)

    if trajectory_duration['end'] > split_last_stop["end"]:
        gnt.broken_barh([(split_last_stop["end"], trajectory_duration["end"] -
                          split_last_stop["end"])], (5, 15),
                        facecolors=('tab:orange'))

    # - Stops-moves visualization end -

    # - Weather visualization start -

    database_connection = DatabaseConnection(user, password, host, database)
    connection = database_connection.get_connection()
    cursor = connection.cursor()

    trajectory_date = str(trajectory_weather[0]['time']).split(' ')[0]
    trajectory_date_start = trajectory_date + ' 00:00:00'
    trajectory_date_end = trajectory_date + ' 23:00:00'

    query = "SELECT datetime, description FROM weather WHERE datetime BETWEEN %s AND %s"

    cursor.execute(query, (trajectory_date_start, trajectory_date_end))
    result = cursor.fetchall()

    weather_values = []

    if result:
        for obj in result:
            weather_values.append({'datetime': obj[0], 'description': obj[1]})

    start_end_trajectory = trajectory_to_hour(trajectory_weather)
    start_trajectory = int(str(start_end_trajectory['start']).split('.')[0])
    end_trajectory = int(str(start_end_trajectory['end']).split('.')[0])

    weather_values_to_hour = []
    i = 0

    for obj in weather_values:
        time = time_to_hour(obj['datetime'])
        weather_values_to_hour.append({
            "time": int(time),
            "description": obj["description"]
        })
        i = i + 1

    colors = [
        'tab:blue', 'gold', 'tab:green', 'indigo', 'tab:purple', 'tab:brown',
        'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan', 'teal'
    ]
    i = start_trajectory
    j = end_trajectory
    k = 0

    current_hour = start_trajectory
    end_hour = end_trajectory
    color = 10

    previous_weather = "no_weather"
    current_weather = "no_weather"

    patches = []
    patches_map = {}

    while current_hour <= end_hour:
        if current_hour in weather_values_to_hour:
            current_weather = str(
                weather_values_to_hour[current_hour]['description'])
            if current_weather != previous_weather:
                color = 0 if (color == 10) else color + 1
                patches_map[colors[color]] = current_weather

        gnt.broken_barh([(current_hour, 1)], (30, 25),
                        facecolors=(colors[color]))
        previous_weather = current_weather
        current_hour = current_hour + 1

    for key, value in patches_map.items():
        patch = mpatches.Patch(color=key, label=value)
        patches.append(patch)

    plt.legend(handles=patches)
    plt.grid(False)
    plt.tight_layout()

    plt.savefig("static/images/chart.png")