148 lines
4.7 KiB
Python
148 lines
4.7 KiB
Python
|
|
import os
|
||
|
|
import io
|
||
|
|
import csv
|
||
|
|
import traceback
|
||
|
|
from datetime import datetime
|
||
|
|
|
||
|
|
def parse_time(strinput):
|
||
|
|
try:
|
||
|
|
h, m, s = strinput.split(":")
|
||
|
|
h = int(h)
|
||
|
|
d = 0
|
||
|
|
while h > 23:
|
||
|
|
d += 1
|
||
|
|
h -= 24
|
||
|
|
|
||
|
|
return datetime.strptime("{}:{}:{}".format(h,m,s), '%H:%M:%S').time(), d
|
||
|
|
except:
|
||
|
|
current_app.logger.debug('Traceback {}'.format(traceback.format_exc()))
|
||
|
|
current_app.logger.warning("Hora incorrecta => {}".format(strinput))
|
||
|
|
return datetime.strptime("01:01:01", '%H:%M:%S').time(), 0
|
||
|
|
|
||
|
|
|
||
|
|
def parse_paraderoid(strinput):
|
||
|
|
primerodenero2022 = 44562
|
||
|
|
|
||
|
|
if strinput.find("-") > 0:
|
||
|
|
current_app.logger.error("Error en el dato de la parada: {}".format(strinput))
|
||
|
|
try:
|
||
|
|
whatdate= datetime.strptime('{}-{}'.format(strinput, 2022), '%d-%b-%Y').date()
|
||
|
|
idparadero = primerodenero2022 + whatdate.timetuple().tm_yday
|
||
|
|
current_app.logger.warning("Calculado el Identificador en base al año 2022 => {}".format(idparadero))
|
||
|
|
return idparadero
|
||
|
|
except:
|
||
|
|
return 0
|
||
|
|
else:
|
||
|
|
return int(strinput)
|
||
|
|
|
||
|
|
|
||
|
|
def completa_dataset():
|
||
|
|
iparaderos = 0
|
||
|
|
for paradero in Paradero.query.all():
|
||
|
|
iparaderos += 1
|
||
|
|
cparadero = 0
|
||
|
|
lparadas = 0
|
||
|
|
sparadas = 0
|
||
|
|
dparadas = 0
|
||
|
|
lineas = []
|
||
|
|
lparada = []
|
||
|
|
|
||
|
|
for parada in Parada.query.filter(Parada.paraderoid==paradero.id).all():
|
||
|
|
cparadero += 1
|
||
|
|
if cparadero % 1000 == 0:
|
||
|
|
current_app.logger.debug("Paradero {}, {} detenciones. {} lineas".format(iparaderos, cparadero, len(lineas)))
|
||
|
|
|
||
|
|
elemento = (parada.servicio.lineaid, parada.servicio.direccionid)
|
||
|
|
|
||
|
|
if parada.servicio.dotw == 'S':
|
||
|
|
sparadas += 1
|
||
|
|
elif parada.servicio.dotw == 'D':
|
||
|
|
dparadas += 1
|
||
|
|
else:
|
||
|
|
lparadas += 1
|
||
|
|
|
||
|
|
if elemento in lineas:
|
||
|
|
continue
|
||
|
|
|
||
|
|
lineas.append(elemento)
|
||
|
|
if parada.servicio.direccionid:
|
||
|
|
lparada.append("{} ({} ida)".format(parada.servicio.linea.codigo, parada.secuencia))
|
||
|
|
else:
|
||
|
|
lparada.append("{} ({} regreso)".format(parada.servicio.linea.codigo, parada.secuencia))
|
||
|
|
|
||
|
|
lparada.sort()
|
||
|
|
paradero.recorridos = " - ".join(lparada)
|
||
|
|
paradero.paradas = 5*lparadas+sparadas+dparadas
|
||
|
|
paradero.resumen = "{} paradas a la semana, {} lineas".format(paradero.paradas, len(lineas))
|
||
|
|
paradero.paradasl = lparadas
|
||
|
|
paradero.paradass = sparadas
|
||
|
|
paradero.paradasd = dparadas
|
||
|
|
paradero.lineas = len(lineas)
|
||
|
|
current_app.logger.info("Paradero {}, {} detenciones a la semana en {} lineas".format(iparaderos, paradero.paradas, len(lineas)))
|
||
|
|
if iparaderos % 10 == 0:
|
||
|
|
db.session.commit()
|
||
|
|
|
||
|
|
db.session.commit()
|
||
|
|
|
||
|
|
def agrega_paraderos():
|
||
|
|
from bs4 import BeautifulSoup
|
||
|
|
from zipfile import ZipFile
|
||
|
|
|
||
|
|
# print(os.getcwd())
|
||
|
|
|
||
|
|
current_app.logger.info("Archivo de paraderos, kmz")
|
||
|
|
with ZipFile('webinterface/models/datos/PARADEROS_FORMALES.kmz', 'r') as kmz:
|
||
|
|
kml = kmz.open(kmz.filelist[0].filename, 'r').read()
|
||
|
|
|
||
|
|
soup = BeautifulSoup(kml, 'xml')
|
||
|
|
existen = 0
|
||
|
|
nuevo = 0
|
||
|
|
|
||
|
|
|
||
|
|
for paradero in soup.find_all("kml:Placemark"):
|
||
|
|
if (existen+nuevo) % 1000 == 0:
|
||
|
|
current_app.logger.debug("Leidos {} paraderos existentes y {} nuevos".format(existen, nuevo))
|
||
|
|
|
||
|
|
|
||
|
|
lat = int(paradero.find("kml:SimpleData", {"name": "LATITUDE"}).text)/1000000
|
||
|
|
lng = int(paradero.find("kml:SimpleData", {"name": "LONGITUDE"}).text)/1000000
|
||
|
|
|
||
|
|
codigo = paradero.find("kml:SimpleData", {"name": "CODIGO_INT"}).text
|
||
|
|
comuna = paradero.find("kml:SimpleData", {"name": "COMUNA"}).text
|
||
|
|
recorridos = paradero.find("kml:SimpleData", {"name": "RECORRIDOS"}).text
|
||
|
|
tamano = paradero.find("kml:SimpleData", {"name": "SUPERFICIE"}).text
|
||
|
|
capacidad = paradero.find("kml:SimpleData", {"name": "CAPACIDAD"}).text
|
||
|
|
materialpiso = paradero.find("kml:SimpleData", {"name": "RADIER_MAT"}).text
|
||
|
|
materialpared = paradero.find("kml:SimpleData", {"name": "PARED_POS2"}).text
|
||
|
|
materialtecho = paradero.find("kml:SimpleData", {"name": "TECHUMBRE2"}).text
|
||
|
|
|
||
|
|
|
||
|
|
match = None
|
||
|
|
|
||
|
|
for radio in [0.00000001, 0.00000002, 0.00000003, 0.00000004, 0.00000005, 0.000000075, 0.0000001, 0.0000002]:
|
||
|
|
match = Paradero.query.filter(((Paradero.latitud-lat)*(Paradero.latitud-lat)+(Paradero.longitud-lng)*(Paradero.longitud-lng)) < radio).first()
|
||
|
|
|
||
|
|
if match is not None:
|
||
|
|
break
|
||
|
|
|
||
|
|
if match is None:
|
||
|
|
nuevo += 1
|
||
|
|
nombre = "{} cerca {}".format(paradero.find("kml:SimpleData", {"name": "NOMBRE_DE_"}).text, paradero.find("kml:SimpleData", {"name": "CALLE_CERC"}).text)
|
||
|
|
match = Paradero(id=codigo, codigo=codigo, nombre=nombre, latitud=lat, longitud=lng)
|
||
|
|
db.session.add(match)
|
||
|
|
db.session.commit()
|
||
|
|
else:
|
||
|
|
existen += 1
|
||
|
|
|
||
|
|
match.codigo = codigo
|
||
|
|
match.comuna = comuna
|
||
|
|
match.recorridos2 = recorridos
|
||
|
|
match.tamano = tamano
|
||
|
|
match.capacidad = capacidad
|
||
|
|
match.techo = materialtecho
|
||
|
|
match.pared = materialpared
|
||
|
|
match.piso = materialpiso
|
||
|
|
db.session.commit()
|
||
|
|
|
||
|
|
current_app.logger.info("Cargados {} paraderos existentes y {} nuevos".format(existen, nuevo))
|