diff --git a/.env.develop b/.env.develop index 6b7d8e1..3e0b511 100644 --- a/.env.develop +++ b/.env.develop @@ -26,4 +26,7 @@ SMTP_FROM='"Sistema Transporte" ' # PATH UPLOAD GTFS_UPLOADS=/uploads/gtfs -PHOTOS_UPLOADS=/uploads/photos \ No newline at end of file +PHOTOS_UPLOADS=/uploads/photos + +# URL INFO PARADERO +URL_PARADERO='http://localhost:3001/rutaParadero/?id=' \ No newline at end of file diff --git a/project/api/management/commands/actualiza_GTFS.sql b/project/api/management/commands/actualiza_GTFS.sql index 3a81a71..193571c 100644 --- a/project/api/management/commands/actualiza_GTFS.sql +++ b/project/api/management/commands/actualiza_GTFS.sql @@ -1,222 +1,358 @@ +DO $$ +DECLARE + inicio_vigencia DATE; + v_error TEXT := ''; +BEGIN -ALTER TABLE linea_paradero drop CONSTRAINT IF EXISTS linea_paradero_id_linea_fkey; + -- Verificar agency_id único + IF (SELECT COUNT(*) FROM (SELECT agency_id FROM z_agency GROUP BY agency_id HAVING COUNT(*) > 1) AS duplicated) > 0 THEN + update gtfs_archivo + set status = 'Error - Agency_id duplicados' + where trim(upper(status))='PROCESANDO' ; + v_error:= '1 La validacion de archivos detecto errores en los archivos GTFS'; + END IF; + + -- Verificar route_id único + IF (SELECT COUNT(*) FROM (SELECT route_id FROM z_routes GROUP BY route_id HAVING COUNT(*) > 1) AS duplicated) > 0 THEN + update gtfs_archivo + set status = 'Error - Route_id duplicados' + where trim(upper(status))='PROCESANDO' ; + v_error:= '2 La validacion de archivos detecto errores en los archivos GTFS'; + END IF; ------ + -- Verificar service_id único en calendar + IF (SELECT COUNT(*) FROM (SELECT service_id FROM z_calendar GROUP BY service_id HAVING COUNT(*) > 1) AS duplicated) > 0 THEN + update gtfs_archivo + set status = 'Error - service_id duplicados en calendar' + where trim(upper(status))='PROCESANDO' ; + v_error:= '3 La validacion de archivos detecto errores en los archivos GTFS'; + END IF; -ALTER TABLE gtfs_stop_times drop CONSTRAINT IF EXISTS fk_gtfs_sto_reference_gtfs_tri; + -- Verificar trip_id único + IF (SELECT COUNT(*) FROM (SELECT trip_id FROM z_trips GROUP BY trip_id HAVING COUNT(*) > 1) AS duplicated) > 0 THEN + update gtfs_archivo + set status = 'Error - Trip_id duplicados ' + where trim(upper(status))='PROCESANDO' ; + v_error:= '4 La validacion de archivos detecto errores en los archivos GTFS'; + END IF; ------ + -- Verificar stop_id único + IF (SELECT COUNT(*) FROM (SELECT stop_id FROM z_stops GROUP BY stop_id HAVING COUNT(*) > 1) AS duplicated) > 0 THEN + update gtfs_archivo + set status = 'Error - Stop_id duplicados' + where trim(upper(status))='PROCESANDO' ; + v_error:= '5 La validacion de archivos detecto errores en los archivos GTFS'; + END IF; -ALTER TABLE gtfs_frequencie drop CONSTRAINT IF EXISTS fk_gtfs_fre_reference_gtfs_tri; + -- Verificar referencias cruzadas entre trips y routes + IF (SELECT COUNT(*) FROM z_trips WHERE route_id NOT IN (SELECT route_id FROM z_routes)) > 0 THEN + update gtfs_archivo + set status = 'Error - Trips_id con route_id que no existen' + where trim(upper(status))='PROCESANDO' ; + v_error:= '6 La validacion de archivos detecto errores en los archivos GTFS'; + END IF; ------ + -- Verificar referencias cruzadas entre stop_times y trips + IF (SELECT COUNT(*) FROM z_stop_times WHERE trip_id NOT IN (SELECT trip_id FROM z_trips)) > 0 THEN + update gtfs_archivo + set status = 'Error - Stop_times con trips_id que no existen' + where trim(upper(status))='PROCESANDO' ; + v_error:= '7 La validacion de archivos detecto errores en los archivos GTFS'; + END IF; -DELETE FROM gtfs_calendar; + -- Verificar referencias cruzadas entre stop_times y stops + IF (SELECT COUNT(*) FROM z_stop_times WHERE stop_id NOT IN (SELECT stop_id FROM z_stops)) > 0 THEN + update gtfs_archivo + set status = 'Error - stop_time con stop_id que no existen' + where trim(upper(status))='PROCESANDO' ; + v_error:= '8 La validacion de archivos detecto errores en los archivos GTFS'; + END IF; ------ + /*WITH inicio_calendar AS ( + SELECT MIN(start_date) AS inicio FROM z_calendar + ), inicio_calendar_dates AS ( + SELECT MIN(date) AS inicio FROM z_calendar_dates WHERE exception_type = 1 + ) + SELECT LEAST( + COALESCE((SELECT inicio FROM inicio_calendar), CURRENT_DATE), + COALESCE((SELECT inicio FROM inicio_calendar_dates), CURRENT_DATE) + ) INTO inicio_vigencia;*/ -insert into gtfs_calendar -select service_id, monday::bool , tuesday::bool , wednesday::bool , thursday::bool ,friday::bool ,saturday::bool ,sunday::bool -from z_calendar zc; + SELECT MIN(start_date) INTO inicio_vigencia FROM z_calendar; ------ + + IF CURRENT_DATE < inicio_vigencia THEN + update gtfs_archivo + set status = 'PENDIENTE' , valid_from= inicio_vigencia + where trim(upper(status))='PROCESANDO' ; + v_error:= '9 EL ARCHIVO AUN NO ESTA VIGENTE POR FECHA'; + END IF; + +if v_error = '' THEN -update paradero -set vigente = true, -stop_name= (select stop_name from z_stops where stop_id=paradero.id_paradero::text limit 1 ), -stop_desc=(select stop_desc from z_stops where stop_id=paradero.id_paradero::text limit 1 ), -stop_lat =(select stop_lat from z_stops where stop_id=paradero.id_paradero::text limit 1)::float(8), -stop_lon =(select stop_lon from z_stops where stop_id=paradero.id_paradero::text limit 1)::float(8), -stop_code=(select stop_code from z_stops where stop_id=paradero.id_paradero::text limit 1) -where id_paradero::text in (select stop_id from z_stops ); + update gtfs_archivo + set vigente = False + where vigente = true and id_red in + (select id_red from gtfs_archivo where trim(upper(status))='PROCESANDO' ); ------ -insert into paradero -select -stop_id, null as id_comuna, null as id_tipo_paradero, true, -stop_code , stop_name , stop_desc , stop_lat::float(8) , stop_lon::float(8) , zone_id , -stop_url , location_type , -parent_station ,null as stop_time_zone , wheelchair_boarding::numeric -from z_stops zs -where stop_id not in (select id_paradero::text from paradero); + ALTER TABLE linea_paradero drop CONSTRAINT IF EXISTS linea_paradero_id_linea_fkey; ------ + ----- -delete from linea_paradero; + ALTER TABLE gtfs_stop_times drop CONSTRAINT IF EXISTS fk_gtfs_sto_reference_gtfs_tri; ------ + ----- -delete from linea; + ALTER TABLE gtfs_frequencie drop CONSTRAINT IF EXISTS fk_gtfs_fre_reference_gtfs_tri; ------ + ----- -update linea -set vigente = false -where id_red in (select id_red from gtfs_archivo where trim(upper(status))='PROCESANDO' ) -and id_linea not in ( - select trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) - from z_routes zr - inner join z_trips zt on zr.route_id =zt.route_id -); + DELETE FROM gtfs_calendar; ------ + ----- -update linea -set vigente = true -where id_red in (select id_red from gtfs_archivo where trim(upper(status))='PROCESANDO' ) -and id_linea in ( - select trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) - from z_routes zr - inner join z_trips zt on zr.route_id =zt.route_id -); + insert into gtfs_calendar + select service_id, monday::bool , tuesday::bool , wednesday::bool , thursday::bool ,friday::bool ,saturday::bool ,sunday::bool + from z_calendar zc; ------ + ----- -insert into linea -select distinct - trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) as route_id, - null,--zr.agency_id , - substring(zt.trip_headsign FROM '(\S+) -') as route_short_name , - zr.route_desc , - zr.route_type::numeric , - zr.route_url, - zr.route_color , - zr.route_text_color , - zt.trip_headsign as route_long_name , true , - (select id_red from gtfs_archivo where trim(upper(status))='PROCESANDO' limit 1) as id_red - from z_routes zr - inner join z_trips zt - on zr.route_id =zt.route_id - and replace (zt.trip_headsign,' ','') =replace ((SPLIT_PART(route_long_name, '-', 1)||'-'||SPLIT_PART(route_long_name, '-', 2)),' ','') -where route_long_name not like '%'||route_short_name ||'%' -and (trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) ) not in (select id_linea from linea ) -union -select distinct -trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) as route_id, - null,--zr.agency_id , - substring(zt.trip_headsign FROM '(\S+) -') as route_short_name , - zr.route_desc , - zr.route_type::numeric , - zr.route_url, - zr.route_color , - zr.route_text_color , - zt.trip_headsign as route_long_name , true , - (select id_red from gtfs_archivo where trim(upper(status))='PROCESANDO' limit 1) as id_red - from z_routes zr - inner join z_trips zt - on zr.route_id =zt.route_id - and replace (zt.trip_headsign,' ','') =replace ((SPLIT_PART(route_long_name, '-', 3)||'-'||SPLIT_PART(route_long_name, '-', 4)),' ','') -where route_long_name not like '%'||route_short_name ||'%' -and (trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) ) not in (select id_linea from linea ) -union -select distinct - trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) as route_id, - null,--zr.agency_id , - route_short_name , - zr.route_desc , - zr.route_type::numeric , - zr.route_url, - zr.route_color , - zr.route_text_color , - zt.trip_headsign as route_long_name , true , - (select id_red from gtfs_archivo where trim(upper(status))='PROCESANDO' limit 1) as id_red - from z_routes zr - inner join z_trips zt - on zr.route_id =zt.route_id - and - ( position('-' in zt.trip_headsign) = 0 - or - route_long_name like '%'||route_short_name ||'%' - ) -where (trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) ) not in (select id_linea from linea ) -order by 1; + update paradero + set vigente = true, + stop_name= (select stop_name from z_stops where stop_id=paradero.id_paradero::text limit 1 ), + stop_desc=(select stop_desc from z_stops where stop_id=paradero.id_paradero::text limit 1 ), + stop_lat =(select stop_lat from z_stops where stop_id=paradero.id_paradero::text limit 1)::float(8), + stop_lon =(select stop_lon from z_stops where stop_id=paradero.id_paradero::text limit 1)::float(8), + stop_code=(select stop_code from z_stops where stop_id=paradero.id_paradero::text limit 1) + where id_paradero::text in (select stop_id from z_stops ); ------ + ----- -truncate table gtfs_shape; + insert into paradero + select + stop_id, null as id_comuna, null as id_tipo_paradero, true, + stop_code , stop_name , stop_desc , stop_lat::float(8) , stop_lon::float(8) , zone_id , + stop_url , location_type , + parent_station ,null as stop_time_zone , wheelchair_boarding::numeric + from z_stops zs + where stop_id not in (select id_paradero::text from paradero); ------ + ----- -insert into gtfs_shape -select shape_id::numeric,shape_pt_lat::float8,shape_pt_lon::float8, -shape_pt_sequence::numeric,shape_dist_traveled::float8 -from z_shapes zs; + delete from linea_paradero; ------ - -truncate table gtfs_stop_times; + ----- ------ -truncate table gtfs_frequencie; + CREATE TABLE IF NOT EXISTS z_verifica_route ( + route_id text NULL, + route_short_name text NULL + ); ------ -truncate table gtfs_trips; + delete from linea l + where id_linea in + ( + select route_id||'-0' from z_verifica_route as v + where coalesce((select count(*) from z_routes as r where r.route_id=v.route_id and r.route_short_name = v.route_short_name),0)=0 + union + select route_id||'-1' from z_verifica_route as v + where coalesce((select count(*) from z_routes as r where r.route_id=v.route_id and r.route_short_name = v.route_short_name),0)=0 + union + select route_id from z_verifica_route as v + where coalesce((select count(*) from z_routes as r where r.route_id=v.route_id and r.route_short_name = v.route_short_name),0)=0 + ) ; ------ + delete from z_verifica_route ; -insert into gtfs_trips -select -trip_id, -trim(route_id)||'-'||trim(direction_id::varchar) as route_id, -shape_id::numeric ,null as regreso, trip_headsign,trip_short_name,direction_id::numeric,service_id::varchar,block_id -from z_trips zt; + insert into z_verifica_route + select route_id, route_short_name from z_routes ; ------ + ----- -insert into gtfs_stop_times -select stop_id,trip_id,arrival_time,stop_sequence::numeric,stop_headsign,departure_time,drop_off_type::numeric,null as shape_dist_traveled , -timepoint::numeric , pickup_type::numeric -from z_stop_times zst ; + update linea + set vigente = false + where id_red in (select id_red from gtfs_archivo where trim(upper(status))='PROCESANDO' ) + and id_linea not in ( + select trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) + from z_routes zr + inner join z_trips zt on zr.route_id =zt.route_id + ); ------ - -insert into linea_paradero (id_linea , id_paradero ) -SELECT DISTINCT -l.id_linea , p.id_paradero -FROM linea l -JOIN gtfs_trips t ON l.id_linea =t.id_linea -JOIN gtfs_stop_times st ON t.id_trip = st.id_trip -JOIN paradero p ON st.id_paradero = p.id_paradero; - ------ + ----- -UPDATE paradero -SET stop_name = REGEXP_REPLACE( - REGEXP_REPLACE( - stop_name, - '\yentre\y', - '-entre', - 'gi' - ), - '\yesq\y', - '-esq', - 'gi' -); + update linea + set vigente = true + where id_red in (select id_red from gtfs_archivo where trim(upper(status))='PROCESANDO' ) + and id_linea in ( + select trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) + from z_routes zr + inner join z_trips zt on zr.route_id =zt.route_id + ); ------ -update paradero -set id_comuna = (select id_comuna from comuna_georeferencia as c where ST_Contains(c.geom, ST_SetSRID(ST_MakePoint(paradero.stop_lon, paradero.stop_lat), 4326)) limit 1) -where id_comuna is not null; ------ + ----- -insert into dispositivo -select 'QRCode-'||id_paradero , id_paradero , true, null, 3 -from paradero as p where id_paradero -not in (select id_paradero from dispositivo as d where id_tipo_dispositivo=3 and d.id_paradero = p.id_paradero); + insert into linea + select distinct + trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) as route_id, + null,--zr.agency_id , + substring(zt.trip_headsign FROM '(\S+) -') as route_short_name , + zr.route_desc , + zr.route_type::numeric , + zr.route_url, + zr.route_color , + zr.route_text_color , + zt.trip_headsign as route_long_name , true , + (select id_red from gtfs_archivo where trim(upper(status))='PROCESANDO' limit 1) as id_red + from z_routes zr + inner join z_trips zt + on zr.route_id =zt.route_id + and replace (zt.trip_headsign,' ','') =replace ((SPLIT_PART(route_long_name, '-', 1)||'-'||SPLIT_PART(route_long_name, '-', 2)),' ','') + where route_long_name not like '%'||route_short_name ||'%' + and (trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) ) not in (select id_linea from linea ) + union + select distinct + trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) as route_id, + null,--zr.agency_id , + substring(zt.trip_headsign FROM '(\S+) -') as route_short_name , + zr.route_desc , + zr.route_type::numeric , + zr.route_url, + zr.route_color , + zr.route_text_color , + zt.trip_headsign as route_long_name , true , + (select id_red from gtfs_archivo where trim(upper(status))='PROCESANDO' limit 1) as id_red + from z_routes zr + inner join z_trips zt + on zr.route_id =zt.route_id + and replace (zt.trip_headsign,' ','') =replace ((SPLIT_PART(route_long_name, '-', 3)||'-'||SPLIT_PART(route_long_name, '-', 4)),' ','') + where route_long_name not like '%'||route_short_name ||'%' + and (trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) ) not in (select id_linea from linea ) + union + select distinct + trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) as route_id, + null,--zr.agency_id , + route_short_name , + zr.route_desc , + zr.route_type::numeric , + zr.route_url, + zr.route_color , + zr.route_text_color , + zt.trip_headsign as route_long_name , true , + (select id_red from gtfs_archivo where trim(upper(status))='PROCESANDO' limit 1) as id_red + from z_routes zr + inner join z_trips zt + on zr.route_id =zt.route_id + and + ( position('-' in zt.trip_headsign) = 0 + or + route_long_name like '%'||route_short_name ||'%' + ) + where (trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) ) not in (select id_linea from linea ) + order by 1; ------ + ----- -ALTER TABLE linea_paradero ADD CONSTRAINT linea_paradero_id_linea_fkey FOREIGN KEY (id_linea) REFERENCES linea(id_linea); + truncate table gtfs_shape; ------ + ----- -ALTER TABLE gtfs_stop_times ADD CONSTRAINT fk_gtfs_sto_reference_gtfs_tri FOREIGN KEY (id_trip) REFERENCES gtfs_trips(id_trip) ON DELETE RESTRICT ON UPDATE RESTRICT; + insert into gtfs_shape + select shape_id::numeric,shape_pt_lat::float8,shape_pt_lon::float8, + shape_pt_sequence::numeric,shape_dist_traveled::float8 + from z_shapes zs; ------ + ----- + + truncate table gtfs_stop_times; -ALTER TABLE gtfs_frequencie ADD CONSTRAINT fk_gtfs_fre_reference_gtfs_tri FOREIGN KEY (id_trip) REFERENCES gtfs_trips(id_trip) ON DELETE RESTRICT ON UPDATE RESTRICT; + ----- ------ \ No newline at end of file + truncate table gtfs_frequencie; + + ----- + + truncate table gtfs_trips; + + ----- + + insert into gtfs_trips + select + trip_id, + trim(route_id)||'-'||trim(direction_id::varchar) as route_id, + shape_id::numeric ,null as regreso, trip_headsign,trip_short_name,direction_id::numeric,service_id::varchar,block_id + from z_trips zt; + + ----- + + insert into gtfs_stop_times + select stop_id,trip_id,arrival_time,stop_sequence::numeric,stop_headsign,departure_time,drop_off_type::numeric,null as shape_dist_traveled , + timepoint::numeric , pickup_type::numeric + from z_stop_times zst ; + + ----- + + insert into linea_paradero (id_linea , id_paradero ) + SELECT DISTINCT + l.id_linea , p.id_paradero + FROM linea l + JOIN gtfs_trips t ON l.id_linea =t.id_linea + JOIN gtfs_stop_times st ON t.id_trip = st.id_trip + JOIN paradero p ON st.id_paradero = p.id_paradero; + + ----- + + UPDATE paradero + SET stop_name = REGEXP_REPLACE( + REGEXP_REPLACE( + stop_name, + '\yentre\y', + '-entre', + 'gi' + ), + '\yesq\y', + '-esq', + 'gi' + ); + + ----- + update paradero + set id_comuna = (select id_comuna from comuna_georeferencia as c where ST_Contains(c.geom, ST_SetSRID(ST_MakePoint(paradero.stop_lon, paradero.stop_lat), 4326)) limit 1) + where id_comuna is not null; + ----- + + insert into dispositivo + select 'QRCode-'||id_paradero , id_paradero , true, null, 3 + from paradero as p where id_paradero + not in (select id_paradero from dispositivo as d where id_tipo_dispositivo=3 and d.id_paradero = p.id_paradero); + + ----- + + ALTER TABLE linea_paradero ADD CONSTRAINT linea_paradero_id_linea_fkey FOREIGN KEY (id_linea) REFERENCES linea(id_linea); + + ----- + + ALTER TABLE gtfs_stop_times ADD CONSTRAINT fk_gtfs_sto_reference_gtfs_tri FOREIGN KEY (id_trip) REFERENCES gtfs_trips(id_trip) ON DELETE RESTRICT ON UPDATE RESTRICT; + + ----- + + ALTER TABLE gtfs_frequencie ADD CONSTRAINT fk_gtfs_fre_reference_gtfs_tri FOREIGN KEY (id_trip) REFERENCES gtfs_trips(id_trip) ON DELETE RESTRICT ON UPDATE RESTRICT; + + ----- + + + update paradero p set id_comuna = ( + select id_comuna from comuna_georeferencia cg + where st_contains(cg.geom, st_setsrid(st_makepoint(p.stop_lon, p.stop_lat) ,4326)) + limit 1) + where id_comuna is null; + + + update gtfs_archivo + set vigente = true , status = 'GTFS CARGADO' + where trim(upper(status))='PROCESANDO' ; + +END IF; + +END$$; \ No newline at end of file diff --git a/project/api/management/commands/actualiza_GTFS.sql.old b/project/api/management/commands/actualiza_GTFS.sql.old new file mode 100644 index 0000000..be6dd0c --- /dev/null +++ b/project/api/management/commands/actualiza_GTFS.sql.old @@ -0,0 +1,252 @@ + +ALTER TABLE linea_paradero drop CONSTRAINT IF EXISTS linea_paradero_id_linea_fkey; + +----- + +ALTER TABLE gtfs_stop_times drop CONSTRAINT IF EXISTS fk_gtfs_sto_reference_gtfs_tri; + +----- + +ALTER TABLE gtfs_frequencie drop CONSTRAINT IF EXISTS fk_gtfs_fre_reference_gtfs_tri; + +----- + +DELETE FROM gtfs_calendar; + +----- + +insert into gtfs_calendar +select service_id, monday::bool , tuesday::bool , wednesday::bool , thursday::bool ,friday::bool ,saturday::bool ,sunday::bool +from z_calendar zc; + +----- + +update paradero +set vigente = true, +stop_name= (select stop_name from z_stops where stop_id=paradero.id_paradero::text limit 1 ), +stop_desc=(select stop_desc from z_stops where stop_id=paradero.id_paradero::text limit 1 ), +stop_lat =(select stop_lat from z_stops where stop_id=paradero.id_paradero::text limit 1)::float(8), +stop_lon =(select stop_lon from z_stops where stop_id=paradero.id_paradero::text limit 1)::float(8), +stop_code=(select stop_code from z_stops where stop_id=paradero.id_paradero::text limit 1) +where id_paradero::text in (select stop_id from z_stops ); + +----- + +insert into paradero +select +stop_id, null as id_comuna, null as id_tipo_paradero, true, +stop_code , stop_name , stop_desc , stop_lat::float(8) , stop_lon::float(8) , zone_id , +stop_url , location_type , +parent_station ,null as stop_time_zone , wheelchair_boarding::numeric +from z_stops zs +where stop_id not in (select id_paradero::text from paradero); + +----- + +delete from linea_paradero; + +----- + + +CREATE TABLE IF NOT EXISTS z_verifica_route ( + route_id text NULL, + route_short_name text NULL +); + + +delete from linea l +where id_linea in +( +select route_id||'-0' from z_verifica_route as v +where coalesce((select count(*) from z_routes as r where r.route_id=v.route_id and r.route_short_name = v.route_short_name),0)=0 +union +select route_id||'-1' from z_verifica_route as v +where coalesce((select count(*) from z_routes as r where r.route_id=v.route_id and r.route_short_name = v.route_short_name),0)=0 +union +select route_id from z_verifica_route as v +where coalesce((select count(*) from z_routes as r where r.route_id=v.route_id and r.route_short_name = v.route_short_name),0)=0 +) ; + +delete from z_verifica_route ; + +insert into z_verifica_route +select route_id, route_short_name from z_routes ; + +----- + +update linea +set vigente = false +where id_red in (select id_red from gtfs_archivo where trim(upper(status))='PROCESANDO' ) +and id_linea not in ( + select trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) + from z_routes zr + inner join z_trips zt on zr.route_id =zt.route_id +); + +----- + +update linea +set vigente = true +where id_red in (select id_red from gtfs_archivo where trim(upper(status))='PROCESANDO' ) +and id_linea in ( + select trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) + from z_routes zr + inner join z_trips zt on zr.route_id =zt.route_id +); + +----- + +insert into linea +select distinct + trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) as route_id, + null,--zr.agency_id , + substring(zt.trip_headsign FROM '(\S+) -') as route_short_name , + zr.route_desc , + zr.route_type::numeric , + zr.route_url, + zr.route_color , + zr.route_text_color , + zt.trip_headsign as route_long_name , true , + (select id_red from gtfs_archivo where trim(upper(status))='PROCESANDO' limit 1) as id_red + from z_routes zr + inner join z_trips zt + on zr.route_id =zt.route_id + and replace (zt.trip_headsign,' ','') =replace ((SPLIT_PART(route_long_name, '-', 1)||'-'||SPLIT_PART(route_long_name, '-', 2)),' ','') +where route_long_name not like '%'||route_short_name ||'%' +and (trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) ) not in (select id_linea from linea ) +union +select distinct +trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) as route_id, + null,--zr.agency_id , + substring(zt.trip_headsign FROM '(\S+) -') as route_short_name , + zr.route_desc , + zr.route_type::numeric , + zr.route_url, + zr.route_color , + zr.route_text_color , + zt.trip_headsign as route_long_name , true , + (select id_red from gtfs_archivo where trim(upper(status))='PROCESANDO' limit 1) as id_red + from z_routes zr + inner join z_trips zt + on zr.route_id =zt.route_id + and replace (zt.trip_headsign,' ','') =replace ((SPLIT_PART(route_long_name, '-', 3)||'-'||SPLIT_PART(route_long_name, '-', 4)),' ','') +where route_long_name not like '%'||route_short_name ||'%' +and (trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) ) not in (select id_linea from linea ) +union +select distinct + trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) as route_id, + null,--zr.agency_id , + route_short_name , + zr.route_desc , + zr.route_type::numeric , + zr.route_url, + zr.route_color , + zr.route_text_color , + zt.trip_headsign as route_long_name , true , + (select id_red from gtfs_archivo where trim(upper(status))='PROCESANDO' limit 1) as id_red + from z_routes zr + inner join z_trips zt + on zr.route_id =zt.route_id + and + ( position('-' in zt.trip_headsign) = 0 + or + route_long_name like '%'||route_short_name ||'%' + ) +where (trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) ) not in (select id_linea from linea ) +order by 1; + +----- + +truncate table gtfs_shape; + +----- + +insert into gtfs_shape +select shape_id::numeric,shape_pt_lat::float8,shape_pt_lon::float8, +shape_pt_sequence::numeric,shape_dist_traveled::float8 +from z_shapes zs; + +----- + +truncate table gtfs_stop_times; + +----- + +truncate table gtfs_frequencie; + +----- + +truncate table gtfs_trips; + +----- + +insert into gtfs_trips +select +trip_id, +trim(route_id)||'-'||trim(direction_id::varchar) as route_id, +shape_id::numeric ,null as regreso, trip_headsign,trip_short_name,direction_id::numeric,service_id::varchar,block_id +from z_trips zt; + +----- + +insert into gtfs_stop_times +select stop_id,trip_id,arrival_time,stop_sequence::numeric,stop_headsign,departure_time,drop_off_type::numeric,null as shape_dist_traveled , +timepoint::numeric , pickup_type::numeric +from z_stop_times zst ; + +----- + +insert into linea_paradero (id_linea , id_paradero ) +SELECT DISTINCT +l.id_linea , p.id_paradero +FROM linea l +JOIN gtfs_trips t ON l.id_linea =t.id_linea +JOIN gtfs_stop_times st ON t.id_trip = st.id_trip +JOIN paradero p ON st.id_paradero = p.id_paradero; + +----- + +UPDATE paradero +SET stop_name = REGEXP_REPLACE( + REGEXP_REPLACE( + stop_name, + '\yentre\y', + '-entre', + 'gi' + ), + '\yesq\y', + '-esq', + 'gi' +); + +----- +update paradero +set id_comuna = (select id_comuna from comuna_georeferencia as c where ST_Contains(c.geom, ST_SetSRID(ST_MakePoint(paradero.stop_lon, paradero.stop_lat), 4326)) limit 1) +where id_comuna is not null; +----- + +insert into dispositivo +select 'QRCode-'||id_paradero , id_paradero , true, null, 3 +from paradero as p where id_paradero +not in (select id_paradero from dispositivo as d where id_tipo_dispositivo=3 and d.id_paradero = p.id_paradero); + +----- + +ALTER TABLE linea_paradero ADD CONSTRAINT linea_paradero_id_linea_fkey FOREIGN KEY (id_linea) REFERENCES linea(id_linea); + +----- + +ALTER TABLE gtfs_stop_times ADD CONSTRAINT fk_gtfs_sto_reference_gtfs_tri FOREIGN KEY (id_trip) REFERENCES gtfs_trips(id_trip) ON DELETE RESTRICT ON UPDATE RESTRICT; + +----- + +ALTER TABLE gtfs_frequencie ADD CONSTRAINT fk_gtfs_fre_reference_gtfs_tri FOREIGN KEY (id_trip) REFERENCES gtfs_trips(id_trip) ON DELETE RESTRICT ON UPDATE RESTRICT; + +----- + + +update paradero p set id_comuna = ( +select id_comuna from comuna_georeferencia cg +where st_contains(cg.geom, st_setsrid(st_makepoint(p.stop_lon, p.stop_lat) ,4326)) +limit 1) +where id_comuna is null; \ No newline at end of file diff --git a/project/api/management/commands/procesa_zip.py b/project/api/management/commands/procesa_zip.py index 278c0f6..4428958 100644 --- a/project/api/management/commands/procesa_zip.py +++ b/project/api/management/commands/procesa_zip.py @@ -33,14 +33,14 @@ class Command(BaseCommand): print(f'procesa: {filepath}') procesa_zip(filepath) - registro_anterior = GtfsArchivo.objects.filter(vigente=True, id_red = red.id_red).first() - if registro_anterior: - registro_anterior.vigente = False - registro_anterior.save() + #registro_anterior = GtfsArchivo.objects.filter(vigente=True, id_red = red.id_red).first() + #if registro_anterior: + # registro_anterior.vigente = False + # registro_anterior.save() - gtfs_archivo.status = 'GTFS CARGADO' - gtfs_archivo.vigente = True - gtfs_archivo.save() + #gtfs_archivo.status = 'GTFS CARGADO' + #gtfs_archivo.vigente = True + #gtfs_archivo.save() self.stdout.write(self.style.SUCCESS('¡Comando ejecutado con éxito!')) @@ -129,7 +129,10 @@ def procesa_tablas_z(cursor): current_folder = os.path.dirname(os.path.abspath(__file__)) with open(os.path.join(current_folder, 'actualiza_GTFS.sql'),'r') as file: - content = ''.join(file.readlines()) + sqlFile = file.read() + cursor.execute(sqlFile) + cursor.commit() + """ content = ''.join(file.readlines()) arr_sql = content.split('-----') for sql in arr_sql: @@ -137,4 +140,4 @@ def procesa_tablas_z(cursor): if sql > ' ': print(f'SQL> {sql}', flush=True) cursor.execute(sql) - print('', flush=True) + print('', flush=True) """ diff --git a/project/api/models.py b/project/api/models.py index 9a35e36..57dda27 100755 --- a/project/api/models.py +++ b/project/api/models.py @@ -288,7 +288,7 @@ class Region(models.Model): class RolAplicacion(models.Model): id_rol_app = models.AutoField(primary_key=True) id_aplicacion = models.ForeignKey(Aplicacion, models.DO_NOTHING, db_column='id_aplicacion', blank=False, null=False) - id_rol = models.ForeignKey(Rol, models.DO_NOTHING, db_column='id_rol', blank=False, null=False) + id_rol = models.ForeignKey(Rol, on_delete=models.CASCADE, db_column='id_rol', blank=False, null=False) solo_visualizar = models.BooleanField(blank=True, null=True) class Meta: @@ -298,8 +298,9 @@ class RolAplicacion(models.Model): class RolOperador(models.Model): id_rol_operador = models.AutoField(primary_key=True) - id_rol = models.ForeignKey(Rol, models.DO_NOTHING, db_column='id_rol') - id_operador = models.ForeignKey(Operador, models.DO_NOTHING, db_column='id_operador') + id_rol = models.ForeignKey(Rol, models.DO_NOTHING, db_column='id_rol') + id_operador = models.ForeignKey(Operador, on_delete=models.CASCADE, db_column='id_operador') + class Meta: managed = True diff --git a/project/api/urls.py b/project/api/urls.py index 7f25e5a..93c859e 100755 --- a/project/api/urls.py +++ b/project/api/urls.py @@ -41,5 +41,5 @@ urlpatterns = [ path('auth/nueva-contrasena/', auth.nueva_contrasena, name='auth_contrasena'), path('mapas/paraderos/', mapa.paraderos, name='mapa-paraderos'), path('mapas/rutas/', mapa.rutas, name='mapa-rutas'), - path('upload/zip/', upload.upload_zip, name='upload_zip'), + path('upload/zip/', upload.upload_zip, name='upload_zip'), ] diff --git a/project/api/views/dispositivo.py b/project/api/views/dispositivo.py index dd28fda..fab02a8 100755 --- a/project/api/views/dispositivo.py +++ b/project/api/views/dispositivo.py @@ -105,6 +105,7 @@ class DispositivoViewSet(viewsets.ModelViewSet): if id_linea == linea.id_linea: hora_llegada = trayecto['hora_llegada'] distancia_km = None + texto_llegada= None # si no trae latitud ni longitud: buscar en trayectos_none por patente # y sobreescribir latitud y longitud @@ -122,18 +123,19 @@ class DispositivoViewSet(viewsets.ModelViewSet): p_lon = trayecto['longitude'] p_lat = trayecto['latitude'] - sql = "select hora_llegada, distancia_km from fn_gtfs_calcula_distancia_tiempo_llegada(%s,%s,%s,%s,%s)" + sql = "select hora_llegada, distancia_km, texto_llegada from fn_gtfs_calcula_distancia_tiempo_llegada(%s,%s,%s,%s,%s)" cursor.execute(sql, [trip_id, stop_id, velocidad_promedio, p_lon, p_lat]) row = cursor.fetchone() - if patente == 'FDCB32': + """ if patente == 'FDCB32': print(f'SQL: {sql}', flush=True) print(f'params: {[trip_id, stop_id, velocidad_promedio, p_lon, p_lat]}', flush=True) - print(f'row: {row}', flush=True) + print(f'row: {row}', flush=True) """ if row != None: hora_llegada = row[0].strftime('%H:%M:%S') distancia_km = row[1] + texto_llegada= row[2] llegadas.append({ 'patente': trayecto['vehicle_license_plate'], @@ -143,6 +145,7 @@ class DispositivoViewSet(viewsets.ModelViewSet): 'stop_id_alterno': trayecto['stop_id_alterno'], 'EstimadaGPS': hora_llegada, 'DistanciaGPS': distancia_km, + 'textoLlegada' : texto_llegada, 'Mensajelinea': None, }) diff --git a/project/api/views/paradero.py b/project/api/views/paradero.py index 8db6b48..a8ff2a5 100755 --- a/project/api/views/paradero.py +++ b/project/api/views/paradero.py @@ -5,6 +5,7 @@ from api.serializers import ParaderoSerializer from api.models import Paradero, Dispositivo from decouple import config import logging +from django.db.models import Count class ParaderoViewSet(viewsets.ModelViewSet): queryset = Paradero.objects.all() @@ -25,7 +26,17 @@ class ParaderoViewSet(viewsets.ModelViewSet): pass return JsonResponse({ 'count': queryset.count() }) + + @action(detail=False, methods=['get'], url_path='count_by_comuna') + def count_by_comuna(self, request, pk=None): + # Solo paraderos vigentes + queryset = Paradero.objects.filter(vigente=True) + # Conteo agrupado por comuna + count_by_comuna = queryset.values('id_comuna__nombre_comuna').annotate(total=Count('id_paradero')).order_by('id_comuna') + + return JsonResponse({'count_by_comuna': list(count_by_comuna)}) + @action(detail=False, methods=['get'], url_path='info-public/(?P\S+)') def info_public(self, request, pk=None): if 'HTTP_REFERER' in request.META: