Skip to content
Snippets Groups Projects
Commit 667fda50 authored by Campos Cordobes, Sergio's avatar Campos Cordobes, Sergio
Browse files

Update src/load_data.py

Deleted src/load_data - copia.py
parent 7db6fa8f
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on: 16/01/2023
@author: Andoni Aranguren Ubierna
"""
import os
import sys
import numpy as np
import pandas as pd
import requests
from mysql import connector
from utm.conversion import to_latlon as utm_to_latlon
import sys
print(sys.path)
from src import constants
def to_latlon(df, x_str, y_str):
x, y = [], []
for index, row in df.iterrows():
coords_portal = utm_to_latlon(row[x_str], row[y_str], zone_number=30, zone_letter="T")
x += [coords_portal[0]]
y += [coords_portal[1]]
df["lon"] = np.round(x, 8)
df["lat"] = np.round(y, 8)
return df
def generate_POIs(cnx):
cursor = cnx.cursor(buffered=True)
path = constants.PATH_POIS
file = path + "/POIS_LugaresTuristicos_Bilbao.csv"
if os.path.isfile(file):
df = pd.read_csv(file, encoding="utf-8", sep=";", decimal=".", encoding_errors='replace')
df = to_latlon(df, "COORDENADA_UTM_X", "COORDENADA_UTM_Y")
df.drop_duplicates(subset=["lon", "lat", "NOMBRE_LUGAR_CAS"], inplace=True)
valores_preferencias = set([i for x in df["TAGS"].unique().tolist() for i in x.split(",")])
df_sql = df[["ID", "lon", "lat", "NOMBRE_LUGAR_CAS"]]
cursor.executemany(constants.sql_insert_poi, df_sql.values.tolist())
cnx.commit()
for tag in valores_preferencias:
cursor.execute(constants.sql_insert_tag, [tag])
cnx.commit()
names_dict = {}
for tag in valores_preferencias:
names_dict[tag] = ""
cursor.execute(constants.sql_get_tag_ids.format(names="','".join(names_dict.keys())), )
cnx.commit()
ret = cursor.fetchall()
for row in ret:
names_dict[row[0]] = row[1]
for index, row in df.iterrows():
for tag in row["TAGS"].split(","):
cursor.execute(constants.sql_insert_poi_tag.format(tag=names_dict[tag]),
row[["lon", "lat", "NOMBRE_LUGAR_CAS"]].values.tolist())
cnx.commit()
def generate_routes(cnx):
cursor = cnx.cursor(buffered=True)
a, b = 'áéíóúüÁÉÍÓÚÜ', '%%%%%%%%%%%%'
trans = str.maketrans(a, b)
path = constants.PATH_ROUTES
for filename in os.listdir(path):
f = os.path.join(path, filename)
if os.path.isfile(f):
df = pd.read_csv(f, sep="\t")
json_itinerary = None
for index, row in df.iterrows():
names = row["poi_names"].replace(";", "','")
dict_id = {}
for name in row["poi_names"].split(";"):
cursor.execute(constants.sql_get_chosen_poi_ids,[name.translate(trans)])
cnx.commit()
ret = cursor.fetchall()
try:
dict_id[ret[0][0]] = str(ret[0][1])
except:
pass
poi_id = ",".join(list(dict_id.values()))
url_route = 'http://localhost:5000/planner/route?'
parameters = "&".join(["fromPlace=" + row["from_place"],
"toPlace=" + row["to_place"],
"timeSlot=" + str(int(row["time_slot"])),
"sports=" + str(row["sports"]),
"culture=" + str(row["culture"]),
"sightseeing=" + str(row["sightseeing"]),
"gastronomy=" + str(row["gastronomy"])])
if poi_id != '':
parameters += "&intermediateNodesIds=" + poi_id
if json_itinerary is None:
json_itinerary = requests.get(url_route+parameters).json()
else:
time_slot = str(int(row["time_slot"]))
json_itinerary["routes"][time_slot] = requests.get(url_route+parameters).json()["routes"][time_slot]
url_save_it = 'http://localhost:5000/database/save/itinerary'
requests.post(url_save_it, json=json_itinerary)
def main(ddbb_config: dict):
cnx = connector.connect(**ddbb_config)
generate_POIs(cnx)
generate_routes(cnx)
cnx.close()
if __name__ == '__main__':
ddbb_config = constants.DDBB_CONFIG
main(ddbb_config)
......@@ -3,6 +3,8 @@
"""
Created on: 16/01/2023
@author: Andoni Aranguren Ubierna
-- Adaptations: 02/2023 @author: Sergio Campos
"""
import os
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment