Está en la página 1de 14

-- FUNCTION: impresiones.

proceso_imp(text)

-- DROP FUNCTION IF EXISTS impresiones.proceso_imp(text);

CREATE OR REPLACE FUNCTION impresiones.proceso_imp(


depto_ text)
RETURNS text
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
AS $BODY$
BEGIN

IF EXISTS(select * FROM impresiones.manzanos_pun WHERE cod_depto=depto_) AND


EXISTS(select * FROM impresiones.sec_pun WHERE cod_depto=depto_) AND EXISTS(select
* FROM impresiones.puntos_viviend_pun WHERE cod_depto=depto_) THEN

-- INSERT EN manzanos_reducidos_sector (1)


delete from impresiones.manzanos_reducidos_sector where cod_depto = depto_;
INSERT INTO impresiones.manzanos_reducidos_sector
SELECT row_number() OVER (ORDER BY t1.id, t2.id) AS gid,
t1.cod_depto,
t1.depto,
t1.cod_prov,
t1.prov,
t1.cod_mpio,
t1.mpio,
t1.cod_cd_com,
t1.ciu_com,
t1.area_cpv,
t1.con_area,
t1.area_trab,
t1.zona::character varying(3) AS zona,
t1.con_zona::character varying(20) AS con_zona,
t1.sector::character varying(50) AS sector,
t1.con_sec::character varying(50) AS con_sec,
t1.segmento::character varying(50) AS segmento,
t1.con_seg::character varying(80) AS con_seg,
t1.id_manz,
t1.orden_manz,
t1.modi_manz,
t1.obs_manz,
t1.union_manz,
t1.t_hombres::integer AS t_hombres,
t1.t_mujeres::integer AS t_mujeres,
t1.total_pob::integer AS total_pob,
t1.t_viv_ocu::integer AS t_viv_ocu,
t1.t_viv_des::integer AS t_viv_des,
t1.t_viv_col::integer AS t_viv_col,
t1.t_viv_tem::integer AS t_viv_tem,
t1.total_viv::integer AS total_viv,
t1.id_cnpv,
t1.id_amge,
t1.id_ac,
t1.area_cpv12,
t1.geom_key,
t1.fecha_regi::timestamp without time zone AS fecha_regi,
t1.fecha_modi::timestamp without time zone AS fecha_modi,
t1.usuario_re AS usuario_registro,
t1.usuario_mo AS usuario_modificacion,
t1.usuario_ga AS usuario_gabinete,
t1.ciu_com_12,
t1.cod_censo,
t1.usr_act,
t1.fech_inc1,
t1.fech_fin1,
t1.estado1,
t1.usr_rev,
t1.fech_inc2,
t1.fech_fin2,
t1.estado2,
t1.estado3,
t1.union_seg::character varying(50) AS union_seg,
NULL::integer AS area_imp,
t2.area_cpv AS area_cpv2,
t2.con_area AS con_area2,
t2.ara_trab AS ara_trab2,
t2.zona AS zona2,
t2.con_zona AS con_zona2,
t2.sector AS sector2,
t2.con_sec AS con_sec2,
t2.tot_viv AS tot_viv2,
t2.tot_pob AS tot_pob2,
t2.fecha_regi AS fecha_regi2,
t2.usuario_re AS usuario_re2,
t2.geom_key AS geom_key2,
t2.ord_mun AS ord_mun2,
t2.ord_sec AS ord_sec2,
t2.sec_unico AS sec_unico2,
t2.extension AS extension2,
t2.codigo_mpi AS codigo_mpi2,
t2.usr_act AS usr_act2,
t2.fech_inc1 AS fech_inc12,
t2.fech_fin1 AS fech_fin12,
t2.estado1 AS estado12,
t2.usr_rev AS usr_rev2,
t2.fech_inc2 AS fech_inc22,
t2.fech_fin2 AS fech_fin22,
t2.estado2 AS estado22,
t2.estado3 AS estado32,
t2.zona_pl AS zona_pl2,
t2.sector_pl AS sector_pl2,
t2.areacpv_pl AS areacpv_pl2,
t2.areacensal AS areacensal2,
t2.cod_aconur AS cod_aconur2,
t2.nom_conur AS nom_conur2,
t2.cod_zconur AS cod_zconur2,
st_buffer(t1.geom,
CASE
WHEN st_area(t1.geom::geography, true) < 300::double precision THEN '-
0.000002'::numeric
WHEN st_area(t1.geom::geography, true) <= 340::double precision AND
st_area(t1.geom) >= 300::double precision THEN '-0.0000025'::numeric
WHEN st_area(t1.geom::geography, true) <= 380::double precision AND
st_area(t1.geom) >= 340::double precision THEN '-0.0000032'::numeric
WHEN st_area(t1.geom::geography, true) <= 540::double precision AND
st_area(t1.geom) >= 460::double precision THEN '-0.0000040'::numeric
WHEN st_area(t1.geom::geography, true) <= 620::double precision AND
st_area(t1.geom) >= 540::double precision THEN '-0.0000050'::numeric
WHEN st_area(t1.geom::geography, true) <= 700::double precision AND
st_area(t1.geom) >= 620::double precision THEN '-0.0000060'::numeric
WHEN st_area(t1.geom::geography, true) <= 800::double precision AND
st_area(t1.geom) >= 700::double precision THEN '-0.0000070'::numeric
WHEN st_area(t1.geom::geography, true) <= 1000::double precision AND
st_area(t1.geom) >= 800::double precision THEN '-0.0000080'::numeric
WHEN st_area(t1.geom::geography, true) <= 1500::double precision AND
st_area(t1.geom) >= 1000::double precision THEN '-0.000009'::numeric
WHEN st_area(t1.geom::geography, true) <= 3000::double precision AND
st_area(t1.geom) >= 1500::double precision THEN '-0.000010'::numeric
ELSE '-0.000011'::numeric
END::double precision) AS buffer
FROM impresiones.manzanos_pun t1
JOIN impresiones.sec_pun t2 ON st_intersects(t1.geom, t2.geom)
where t1.cod_depto=depto_ and t2.cod_depto=depto_;

-- INSERT EN t_sector_v_a (2)


delete from impresiones.t_sector_v_a where cod_depto = depto_;
INSERT INTO impresiones.t_sector_v_a
SELECT sec_pun.id,
sec_pun.cod_depto,
sec_pun.depto,
sec_pun.cod_prov,
sec_pun.prov,
sec_pun.cod_mpio,
sec_pun.mpio,
sec_pun.cod_cd_com,
sec_pun.ciu_com,
sec_pun.area_cpv,
sec_pun.con_area,
sec_pun.ara_trab,
sec_pun.zona,
sec_pun.con_zona,
sec_pun.sector,
sec_pun.con_sec,
sec_pun.tot_viv,
sec_pun.tot_pob,
sec_pun.fecha_regi,
sec_pun.usuario_re,
sec_pun.geom_key,
sec_pun.ord_mun,
sec_pun.ord_sec,
sec_pun.sec_unico,
sec_pun.extension,
sec_pun.codigo_mpi,
sec_pun.usr_act,
sec_pun.fech_inc1,
sec_pun.fech_fin1,
sec_pun.estado1,
sec_pun.usr_rev,
sec_pun.fech_inc2,
sec_pun.fech_fin2,
sec_pun.estado2,
sec_pun.estado3,
sec_pun.zona_pl,
sec_pun.sector_pl,
sec_pun.areacpv_pl,
sec_pun.areacensal,
sec_pun.cod_aconur,
sec_pun.nom_conur,
sec_pun.cod_zconur,
split_part(replace(replace(replace(st_astext(st_pointonsurface(sec_pun.geom)),
'POINT'::text, ''::text), '('::text, ''::text), ')'::text, ''::text), ' '::text,
2)::numeric AS latitud,
split_part(replace(replace(replace(st_astext(st_pointonsurface(sec_pun.geom)),
'POINT'::text, ''::text), '('::text, ''::text), ')'::text, ''::text), ' '::text,
1)::numeric AS longitud,

st_setsrid(st_point(split_part(replace(replace(replace(st_astext(st_pointonsurface(
sec_pun.geom)), 'POINT'::text, ''::text), '('::text, ''::text), ')'::text,
''::text), ' '::text, 1)::numeric::double precision,
split_part(replace(replace(replace(st_astext(st_pointonsurface(sec_pun.geom)),
'POINT'::text, ''::text), '('::text, ''::text), ')'::text, ''::text), ' '::text,
2)::numeric::double precision), 4326) AS centroide
FROM impresiones.sec_pun
WHERE sec_pun.cod_depto=depto_;

-- INSERT EN t_dissolve (3)


delete from impresiones.t_dissolve where cod_depto = depto_;
INSERT INTO impresiones.t_dissolve
SELECT manzanos_reducidos_sector.cod_depto,
manzanos_reducidos_sector.depto,
manzanos_reducidos_sector.cod_prov,
manzanos_reducidos_sector.prov,
manzanos_reducidos_sector.cod_mpio,
manzanos_reducidos_sector.mpio,
manzanos_reducidos_sector.cod_cd_com,
manzanos_reducidos_sector.ciu_com,
manzanos_reducidos_sector.area_cpv,
manzanos_reducidos_sector.con_area,
manzanos_reducidos_sector.area_trab,
manzanos_reducidos_sector.zona,
manzanos_reducidos_sector.con_zona,
manzanos_reducidos_sector.sector,
manzanos_reducidos_sector.segmento,
manzanos_reducidos_sector.con_seg,
manzanos_reducidos_sector.area_imp,
manzanos_reducidos_sector.con_sec2 AS con_sec,
manzanos_reducidos_sector.codigo_mpi2,
st_union(manzanos_reducidos_sector.buffer) AS st_union
FROM impresiones.manzanos_reducidos_sector
WHERE manzanos_reducidos_sector.cod_depto = depto_
GROUP BY manzanos_reducidos_sector.cod_depto, manzanos_reducidos_sector.depto,
manzanos_reducidos_sector.cod_prov, manzanos_reducidos_sector.prov,
manzanos_reducidos_sector.cod_mpio, manzanos_reducidos_sector.mpio,
manzanos_reducidos_sector.cod_cd_com, manzanos_reducidos_sector.ciu_com,
manzanos_reducidos_sector.area_cpv, manzanos_reducidos_sector.con_area,
manzanos_reducidos_sector.area_trab, manzanos_reducidos_sector.zona,
manzanos_reducidos_sector.con_zona, manzanos_reducidos_sector.sector,
manzanos_reducidos_sector.segmento, manzanos_reducidos_sector.con_seg,
manzanos_reducidos_sector.area_imp, manzanos_reducidos_sector.con_sec2,
manzanos_reducidos_sector.codigo_mpi2
ORDER BY manzanos_reducidos_sector.con_sec2;

-- INSERT EN sector_vista_atlas (4)


delete from impresiones.sector_vista_atlas where cod_depto = depto_;
INSERT INTO impresiones.sector_vista_atlas
SELECT t1.latitud,
t1.longitud,
t2.cod_depto,
t2.depto,
t2.cod_prov,
t2.prov,
t2.cod_mpio,
t2.mpio,
t2.cod_cd_com,
t2.ciu_com,
t2.area_cpv,
t2.con_area,
t2.area_trab,
t2.zona,
t2.con_zona,
t2.sector,
t2.segmento,
t2.con_seg,
t2.area_imp,
t2.con_sec,
t2.codigo_mpi2,
t2.st_union
FROM impresiones.t_sector_v_a t1
JOIN impresiones.t_dissolve t2 ON t1.con_sec::text = t2.con_sec::text
WHERE t1.cod_depto=depto_ and t2.cod_depto=depto_;

-- INSERT EN etiqueta_manzano (5)


delete from impresiones.etiqueta_manzano where cod_depto = depto_;
INSERT INTO impresiones.etiqueta_manzano
SELECT manzanos_pun.id::integer AS id,
manzanos_pun.cod_depto,
manzanos_pun.depto,
manzanos_pun.cod_prov,
manzanos_pun.prov,
manzanos_pun.cod_mpio,
manzanos_pun.mpio,
manzanos_pun.cod_cd_com,
manzanos_pun.ciu_com,
manzanos_pun.area_cpv,
manzanos_pun.con_area,
manzanos_pun.area_trab,
manzanos_pun.zona::character varying(3) AS zona,
manzanos_pun.con_zona::character varying(20) AS con_zona,
manzanos_pun.sector::character varying(50) AS sector,
manzanos_pun.con_sec::character varying(50) AS con_sec,
manzanos_pun.segmento::character varying(50) AS segmento,
manzanos_pun.con_seg::character varying(80) AS con_seg,
manzanos_pun.id_manz,
manzanos_pun.orden_manz,
manzanos_pun.modi_manz,
manzanos_pun.obs_manz,
manzanos_pun.union_manz,
manzanos_pun.t_hombres::integer AS t_hombres,
manzanos_pun.t_mujeres::integer AS t_mujeres,
manzanos_pun.total_pob::integer AS total_pob,
manzanos_pun.t_viv_ocu::integer AS t_viv_ocu,
manzanos_pun.t_viv_des::integer AS t_viv_des,
manzanos_pun.t_viv_col::integer AS t_viv_col,
manzanos_pun.t_viv_tem::integer AS t_viv_tem,
manzanos_pun.total_viv::integer AS total_viv,
manzanos_pun.id_cnpv,
manzanos_pun.id_amge,
manzanos_pun.id_ac,
manzanos_pun.area_cpv12,
manzanos_pun.geom_key,
manzanos_pun.fecha_regi::timestamp without time zone AS fecha_regi,
manzanos_pun.fecha_modi::timestamp without time zone AS fecha_modi,
manzanos_pun.usuario_re AS usuario_registro,
manzanos_pun.usuario_mo AS usuario_modificacion,
manzanos_pun.usuario_ga AS usuario_gabinete,
manzanos_pun.ciu_com_12,
manzanos_pun.cod_censo,
manzanos_pun.usr_act,
manzanos_pun.fech_inc1,
manzanos_pun.fech_fin1,
manzanos_pun.estado1,
manzanos_pun.usr_rev,
manzanos_pun.fech_inc2,
manzanos_pun.fech_fin2,
manzanos_pun.estado2,
manzanos_pun.estado3,
manzanos_pun.union_seg::character varying(50) AS union_seg,
NULL::integer AS area_imp,

st_setsrid(st_point(split_part(replace(replace(replace(st_astext(st_pointonsurface(
manzanos_pun.geom)), 'POINT'::text, ''::text), '('::text, ''::text), ')'::text,
''::text), ' '::text, 1)::numeric::double precision,
split_part(replace(replace(replace(st_astext(st_pointonsurface(manzanos_pun.geom)),
'POINT'::text, ''::text), '('::text, ''::text), ')'::text, ''::text), ' '::text,
2)::numeric::double precision), 4326) AS centroide
FROM impresiones.manzanos_pun
WHERE manzanos_pun.cod_depto=depto_;

-- INSERT EN t_viv_ajustadas_buffer_r_a (6)


delete from impresiones.t_viv_ajustadas_buffer_r_a where cod_depto = depto_;
INSERT INTO impresiones.t_viv_ajustadas_buffer_r_a
SELECT DISTINCT p.gid,
COALESCE(st_closestpoint(st_boundary(pol.buffer), p.geom), p.geom) AS
geom_ajustado,
p.id AS id2,
p.cod_depto,
p.depto,
p.cod_prov,
p.prov,
p.cod_mpio,
p.mpio,
p.cod_cd_com,
p.ciu_com,
p.area_cpv,
p.con_area,
p.area_trab,
p.zona,
p.con_zona,
p.sector,
p.con_sec,
p.segmento,
p.con_seg,
p.id_manz,
p.orden_manz,
p.udns_des,
p.udns_cod,
p.manz_cod_u,
p.ord_manz_u,
p.lado_manz,
p.orden_pred,
p.ord_pre_co,
p.nro_puerta,
p.aforo,
p.t_hombres,
p.t_mujeres,
p.total_pob,
p.t_viv_ocu,
p.t_viv_des,
p.t_viv_col,
p.t_viv_tem,
p.total_viv,
p.tipo_edif,
p.uso_edif,
p.tipo_via,
p.nombre_via,
p.jefe_hogar,
p.es_edifici,
p.nom_edif,
p.nom_esta,
p.act_eco,
p.latitud,
p.longitud,
p.seg_viv_po,
p.viv_seg,
p.pob_seg,
p.seg_manz,
p.estado,
p.segmento_m,
p.ord_mun,
p.ord_seg,
p.seg_unico,
p.extension,
p.ord_pre_se,
p.ord_sec,
p.sec_unico,
p.mayor_viv,
p.ord_aux,
p.estado_1,
p.estado_2,
p.estado_3,
p.estado_4,
p.estado_5,
p.estado_6,
p.estado_7,
p.estado_8,
p.segui_avc,
p.migrado_f3,
p.omitido,
p.codigo_mpi,
p.codigo_dis,
p.fecha_regi,
p.fecha_modi,
p.usuario_re,
p.usuario_mo,
p.usuario_ga,
p.ciu_com_12,
p.area_cpv12,
p.union_manz,
p.tipo_area,
p.union_seg,
p.condominio,
p.areacpv_pl,
p.zona_pl,
p.sector_pl,
p.segmento_p,
p.areacensal,
p.tipo_ac,
p.cod_aconur,
p.nom_conur,
p.cod_zconur
FROM impresiones.puntos_viviend_pun p
JOIN impresiones.manzanos_reducidos_sector pol ON st_dwithin(p.geom,
pol.buffer, 0.00004::double precision)
WHERE p.cod_depto=depto_ and pol.cod_depto=depto_;

-- INSERT EN ruta_ajustada (7)


delete from impresiones.ruta_ajustada where cod_depto = depto_;
INSERT INTO impresiones.ruta_ajustada
SELECT row_number() OVER (ORDER BY t_viv_ajustadas_buffer_r_a.con_seg) AS gid,
st_makeline(t_viv_ajustadas_buffer_r_a.geom_ajustado ORDER BY
(t_viv_ajustadas_buffer_r_a.ord_pre_co::integer)) AS geom,
t_viv_ajustadas_buffer_r_a.cod_depto,
t_viv_ajustadas_buffer_r_a.depto,
t_viv_ajustadas_buffer_r_a.cod_prov,
t_viv_ajustadas_buffer_r_a.prov,
t_viv_ajustadas_buffer_r_a.cod_mpio,
t_viv_ajustadas_buffer_r_a.mpio,
t_viv_ajustadas_buffer_r_a.ciu_com,
t_viv_ajustadas_buffer_r_a.con_seg,
t_viv_ajustadas_buffer_r_a.id_manz
FROM impresiones.t_viv_ajustadas_buffer_r_a
WHERE t_viv_ajustadas_buffer_r_a.cod_depto=depto_
GROUP BY t_viv_ajustadas_buffer_r_a.cod_depto, t_viv_ajustadas_buffer_r_a.depto,
t_viv_ajustadas_buffer_r_a.cod_prov, t_viv_ajustadas_buffer_r_a.prov,
t_viv_ajustadas_buffer_r_a.cod_mpio, t_viv_ajustadas_buffer_r_a.mpio,
t_viv_ajustadas_buffer_r_a.ciu_com, t_viv_ajustadas_buffer_r_a.con_seg,
t_viv_ajustadas_buffer_r_a.id_manz
ORDER BY t_viv_ajustadas_buffer_r_a.con_seg;

-- INSERT EN tipo_edif_ajustadas (8)


delete from impresiones.tipo_edif_ajustadas where cod_depto = depto_;
INSERT INTO impresiones.tipo_edif_ajustadas
SELECT t_viv_ajustadas_buffer_r_a.gid,
t_viv_ajustadas_buffer_r_a.geom_ajustado,
t_viv_ajustadas_buffer_r_a.id2,
t_viv_ajustadas_buffer_r_a.cod_depto,
t_viv_ajustadas_buffer_r_a.depto,
t_viv_ajustadas_buffer_r_a.cod_prov,
t_viv_ajustadas_buffer_r_a.prov,
t_viv_ajustadas_buffer_r_a.cod_mpio,
t_viv_ajustadas_buffer_r_a.mpio,
t_viv_ajustadas_buffer_r_a.cod_cd_com,
t_viv_ajustadas_buffer_r_a.ciu_com,
t_viv_ajustadas_buffer_r_a.area_cpv,
t_viv_ajustadas_buffer_r_a.con_area,
t_viv_ajustadas_buffer_r_a.area_trab,
t_viv_ajustadas_buffer_r_a.zona,
t_viv_ajustadas_buffer_r_a.con_zona,
t_viv_ajustadas_buffer_r_a.sector,
t_viv_ajustadas_buffer_r_a.con_sec,
t_viv_ajustadas_buffer_r_a.segmento,
t_viv_ajustadas_buffer_r_a.con_seg,
t_viv_ajustadas_buffer_r_a.id_manz,
t_viv_ajustadas_buffer_r_a.orden_manz,
t_viv_ajustadas_buffer_r_a.udns_des,
t_viv_ajustadas_buffer_r_a.udns_cod,
t_viv_ajustadas_buffer_r_a.manz_cod_u,
t_viv_ajustadas_buffer_r_a.ord_manz_u,
t_viv_ajustadas_buffer_r_a.lado_manz,
t_viv_ajustadas_buffer_r_a.orden_pred,
t_viv_ajustadas_buffer_r_a.ord_pre_co,
t_viv_ajustadas_buffer_r_a.nro_puerta,
t_viv_ajustadas_buffer_r_a.aforo,
t_viv_ajustadas_buffer_r_a.t_hombres,
t_viv_ajustadas_buffer_r_a.t_mujeres,
t_viv_ajustadas_buffer_r_a.total_pob,
t_viv_ajustadas_buffer_r_a.t_viv_ocu,
t_viv_ajustadas_buffer_r_a.t_viv_des,
t_viv_ajustadas_buffer_r_a.t_viv_col,
t_viv_ajustadas_buffer_r_a.t_viv_tem,
t_viv_ajustadas_buffer_r_a.total_viv,
t_viv_ajustadas_buffer_r_a.tipo_edif,
t_viv_ajustadas_buffer_r_a.uso_edif,
t_viv_ajustadas_buffer_r_a.tipo_via,
t_viv_ajustadas_buffer_r_a.nombre_via,
t_viv_ajustadas_buffer_r_a.jefe_hogar,
t_viv_ajustadas_buffer_r_a.es_edifici,
t_viv_ajustadas_buffer_r_a.nom_edif,
t_viv_ajustadas_buffer_r_a.nom_esta,
t_viv_ajustadas_buffer_r_a.act_eco,
t_viv_ajustadas_buffer_r_a.latitud,
t_viv_ajustadas_buffer_r_a.longitud,
t_viv_ajustadas_buffer_r_a.seg_viv_po,
t_viv_ajustadas_buffer_r_a.viv_seg,
t_viv_ajustadas_buffer_r_a.pob_seg,
t_viv_ajustadas_buffer_r_a.seg_manz,
t_viv_ajustadas_buffer_r_a.estado,
t_viv_ajustadas_buffer_r_a.segmento_m,
t_viv_ajustadas_buffer_r_a.ord_mun,
t_viv_ajustadas_buffer_r_a.ord_seg,
t_viv_ajustadas_buffer_r_a.seg_unico,
t_viv_ajustadas_buffer_r_a.extension,
t_viv_ajustadas_buffer_r_a.ord_pre_se,
t_viv_ajustadas_buffer_r_a.ord_sec,
t_viv_ajustadas_buffer_r_a.sec_unico,
t_viv_ajustadas_buffer_r_a.mayor_viv,
t_viv_ajustadas_buffer_r_a.ord_aux,
t_viv_ajustadas_buffer_r_a.estado_1,
t_viv_ajustadas_buffer_r_a.estado_2,
t_viv_ajustadas_buffer_r_a.estado_3,
t_viv_ajustadas_buffer_r_a.estado_4,
t_viv_ajustadas_buffer_r_a.estado_5,
t_viv_ajustadas_buffer_r_a.estado_6,
t_viv_ajustadas_buffer_r_a.estado_7,
t_viv_ajustadas_buffer_r_a.estado_8,
t_viv_ajustadas_buffer_r_a.segui_avc,
t_viv_ajustadas_buffer_r_a.migrado_f3,
t_viv_ajustadas_buffer_r_a.omitido,
t_viv_ajustadas_buffer_r_a.codigo_mpi,
t_viv_ajustadas_buffer_r_a.codigo_dis,
t_viv_ajustadas_buffer_r_a.fecha_regi,
t_viv_ajustadas_buffer_r_a.fecha_modi,
t_viv_ajustadas_buffer_r_a.usuario_re,
t_viv_ajustadas_buffer_r_a.usuario_mo,
t_viv_ajustadas_buffer_r_a.usuario_ga,
t_viv_ajustadas_buffer_r_a.ciu_com_12,
t_viv_ajustadas_buffer_r_a.area_cpv12,
t_viv_ajustadas_buffer_r_a.union_manz,
t_viv_ajustadas_buffer_r_a.tipo_area,
t_viv_ajustadas_buffer_r_a.union_seg,
t_viv_ajustadas_buffer_r_a.condominio,
t_viv_ajustadas_buffer_r_a.areacpv_pl,
t_viv_ajustadas_buffer_r_a.zona_pl,
t_viv_ajustadas_buffer_r_a.sector_pl,
t_viv_ajustadas_buffer_r_a.segmento_p,
t_viv_ajustadas_buffer_r_a.areacensal,
t_viv_ajustadas_buffer_r_a.tipo_ac,
t_viv_ajustadas_buffer_r_a.cod_aconur,
t_viv_ajustadas_buffer_r_a.nom_conur,
t_viv_ajustadas_buffer_r_a.cod_zconur
FROM impresiones.t_viv_ajustadas_buffer_r_a
WHERE (t_viv_ajustadas_buffer_r_a.tipo_edif::text = 'EN CONSTRUCCIÓN'::text OR
t_viv_ajustadas_buffer_r_a.tipo_edif::text = 'PREDIO BALDÍO / AMURALLADO /
EDIFICACIÓN EN RUINAS'::text OR t_viv_ajustadas_buffer_r_a.es_edifici::text =
'1'::text OR t_viv_ajustadas_buffer_r_a.condominio = 1::numeric) AND
t_viv_ajustadas_buffer_r_a.cod_depto=depto_;

-- INSERT EN t_manzanos_buffer (9)


delete from impresiones.t_manzanos_buffer where cod_depto = depto_;
INSERT INTO impresiones.t_manzanos_buffer
SELECT manzanos_pun.id,
manzanos_pun.con_seg,
manzanos_pun.id_manz,
manzanos_pun.orden_manz,
manzanos_pun.union_manz,
st_buffer(manzanos_pun.geom, '-0.000010'::numeric::double precision) AS
buffer1,
st_buffer(manzanos_pun.geom,
CASE
WHEN st_area(manzanos_pun.geom::geography, true) < 300::double
precision THEN '-0.000002'::numeric
WHEN st_area(manzanos_pun.geom::geography, true) <= 340::double
precision AND st_area(manzanos_pun.geom) >= 300::double precision THEN '-
0.0000025'::numeric
WHEN st_area(manzanos_pun.geom::geography, true) <= 380::double
precision AND st_area(manzanos_pun.geom) >= 340::double precision THEN '-
0.0000032'::numeric
WHEN st_area(manzanos_pun.geom::geography, true) <= 540::double
precision AND st_area(manzanos_pun.geom) >= 460::double precision THEN '-
0.0000040'::numeric
WHEN st_area(manzanos_pun.geom::geography, true) <= 620::double
precision AND st_area(manzanos_pun.geom) >= 540::double precision THEN '-
0.0000050'::numeric
WHEN st_area(manzanos_pun.geom::geography, true) <= 700::double
precision AND st_area(manzanos_pun.geom) >= 620::double precision THEN '-
0.0000060'::numeric
WHEN st_area(manzanos_pun.geom::geography, true) <= 800::double
precision AND st_area(manzanos_pun.geom) >= 700::double precision THEN '-
0.0000070'::numeric
WHEN st_area(manzanos_pun.geom::geography, true) <= 1000::double
precision AND st_area(manzanos_pun.geom) >= 800::double precision THEN '-
0.0000080'::numeric
WHEN st_area(manzanos_pun.geom::geography, true) <= 1500::double
precision AND st_area(manzanos_pun.geom) >= 1000::double precision THEN '-
0.000009'::numeric
WHEN st_area(manzanos_pun.geom::geography, true) <= 3000::double
precision AND st_area(manzanos_pun.geom) >= 1500::double precision THEN '-
0.000010'::numeric
ELSE '-0.000011'::numeric
END::double precision) AS buffer2,
manzanos_pun.cod_depto
FROM impresiones.manzanos_pun
WHERE manzanos_pun.cod_depto=depto_;

-- INSERT EN t_viviendas_ajustada_buffer_multipunto (10)


delete from impresiones.t_viviendas_ajustada_buffer_multipunto where cod_depto =
depto_;
INSERT INTO impresiones.t_viviendas_ajustada_buffer_multipunto
SELECT t_viv_ajustadas_buffer_r_a.con_seg,
st_collect(t_viv_ajustadas_buffer_r_a.geom_ajustado) AS st_collect,
t_viv_ajustadas_buffer_r_a.cod_depto
FROM impresiones.t_viv_ajustadas_buffer_r_a
WHERE t_viv_ajustadas_buffer_r_a.cod_depto=depto_
GROUP BY t_viv_ajustadas_buffer_r_a.con_seg,t_viv_ajustadas_buffer_r_a.cod_depto;

-- INSERT EN manzanos_segmentos_impresion (11)


delete from impresiones.manzanos_segmentos_impresion where cod_depto = depto_;
INSERT INTO impresiones.manzanos_segmentos_impresion
SELECT row_number() OVER (ORDER BY t1.id) AS id,
t1.buffer1 AS geom,
t1.id AS id2,
t1.con_seg AS con_seg_mnz,
t1.id_manz,
t1.orden_manz,
t1.union_manz,
t2.con_seg,
st_intersection(t1.buffer1, t2.st_collect) AS geom_pto,
t1.cod_depto
FROM impresiones.t_manzanos_buffer t1
LEFT JOIN impresiones.t_viviendas_ajustada_buffer_multipunto t2 ON
st_intersects(t1.buffer1, t2.st_collect)
WHERE t1.cod_depto=depto_;

-- INSERT EN codigo_segmento (12)


delete from impresiones.codigo_segmento where cod_depto = depto_;
INSERT INTO impresiones.codigo_segmento
SELECT row_number() OVER (ORDER BY m.id) AS id,
t.gid,
t.con_seg,
st_setsrid(st_astext(st_pointonsurface(st_intersection(t.geom,
m.buffer1)))::geometry, 4326) AS geom,
"right"(t.con_seg::text, 2) AS segmento,
t.cod_depto
FROM impresiones.ruta_ajustada t,
impresiones.t_manzanos_buffer m
WHERE st_intersects(t.geom, m.buffer1) and (t.cod_depto=depto_ and
m.cod_depto=depto_);

-- INSERT EN viviendas_ajustadas (13)


delete from impresiones.viviendas_ajustadas where cod_depto = depto_;
INSERT INTO impresiones.viviendas_ajustadas
WITH rankeddata AS (
SELECT t.geom_ajustado,
t.gid,
t.con_seg,
t.ord_pre_se,
row_number() OVER (PARTITION BY t.con_seg ORDER BY
(t.ord_pre_se::numeric)) AS rowasc,
row_number() OVER (PARTITION BY t.con_seg ORDER BY
(t.ord_pre_se::numeric) DESC) AS rowdesc,
t.cod_depto
FROM impresiones.t_viv_ajustadas_buffer_r_a t
WHERE t.cod_depto=depto_
)
SELECT row_number() OVER (ORDER BY rankeddata.gid) AS id,
rankeddata.geom_ajustado,
rankeddata.gid,
rankeddata.con_seg,
rankeddata.ord_pre_se,
CASE
WHEN rankeddata.rowasc = 1 THEN 1
WHEN rankeddata.rowdesc = 1 THEN 2
ELSE 0
END AS cod_if,
rankeddata.cod_depto
FROM rankeddata
ORDER BY rankeddata.con_seg, (rankeddata.ord_pre_se::numeric);

-- INSERT EN vivienda_ajustada_tabla (14)


delete from impresiones.vivienda_ajustada_tabla where cod_depto = depto_;
INSERT INTO impresiones.vivienda_ajustada_tabla
WITH rankeddata AS (
SELECT t.geom_ajustado,
t.gid,
t.con_seg,
t.orden_manz,
t.nro_puerta,
t.jefe_hogar,
t.ord_pre_se,
row_number() OVER (PARTITION BY t.con_seg ORDER BY
(t.ord_pre_se::numeric)) AS rowasc,
row_number() OVER (PARTITION BY t.con_seg ORDER BY
(t.ord_pre_se::numeric) DESC) AS rowdesc,
t.cod_depto
FROM impresiones.t_viv_ajustadas_buffer_r_a t
WHERE t.cod_depto=depto_
)
SELECT row_number() OVER (ORDER BY rankeddata.gid) AS id,
rankeddata.geom_ajustado,
rankeddata.gid,
rankeddata.con_seg,
rankeddata.orden_manz,
rankeddata.nro_puerta,
CASE
WHEN rankeddata.jefe_hogar IS NULL THEN 'S/N'::text::character varying
ELSE rankeddata.jefe_hogar
END AS jefe_hogar,
rankeddata.ord_pre_se,
CASE
WHEN rankeddata.rowasc = 1 THEN 1
WHEN rankeddata.rowdesc = 1 THEN 2
ELSE 0
END AS cod_if,
rankeddata.cod_depto
FROM rankeddata
ORDER BY rankeddata.con_seg, (rankeddata.ord_pre_se::numeric);

-- INSERT EN manzanos_reducidos_segmentos (15)


delete from impresiones.manzanos_reducidos_segmentos where cod_depto = depto_;
INSERT INTO impresiones.manzanos_reducidos_segmentos
SELECT row_number() OVER (ORDER BY t1.id) AS id,
t1.geom,
t1.id2,
t1.con_seg_mnz,
t1.id_manz,
t1.orden_manz,
t1.union_manz,
t1.con_seg,
t3.full_manz,
t4.fill_manz,
t1.geom_pto,
t1.cod_depto
FROM impresiones.manzanos_segmentos_impresion t1
LEFT JOIN ( SELECT manzanos_segmentos_impresion.con_seg,
string_agg(manzanos_segmentos_impresion.orden_manz::text, ','::text) AS
full_manz
FROM impresiones.manzanos_segmentos_impresion
WHERE manzanos_segmentos_impresion.cod_depto=depto_
GROUP BY manzanos_segmentos_impresion.con_seg) t3 ON t1.con_seg::text =
t3.con_seg::text
LEFT JOIN ( SELECT
CASE
WHEN char_length(manzanos_segmentos_impresion.union_manz::text)
< 10 OR manzanos_segmentos_impresion.union_manz IS NULL OR
manzanos_segmentos_impresion.union_manz::text = ''::text THEN '0'::character
varying
ELSE manzanos_segmentos_impresion.union_manz
END AS union_manz,
string_agg(DISTINCT
CASE
WHEN char_length(manzanos_segmentos_impresion.union_manz::text)
< 10 OR manzanos_segmentos_impresion.union_manz IS NULL OR
manzanos_segmentos_impresion.union_manz::text = ''::text THEN '0'::character
varying
ELSE manzanos_segmentos_impresion.orden_manz
END::text, ','::text) AS fill_manz
FROM impresiones.manzanos_segmentos_impresion
WHERE manzanos_segmentos_impresion.cod_depto=depto_
GROUP BY manzanos_segmentos_impresion.union_manz) t4 ON
t4.union_manz::text =
CASE
WHEN char_length(t1.union_manz::text) < 10 OR t1.union_manz IS NULL OR
t1.union_manz::text = ''::text THEN '0'::character varying
ELSE t1.union_manz
END::text
WHERE t1.cod_depto = depto_
ORDER BY t1.con_seg;

-- INSERT EN manzano_segmento_tablas (16)


delete from impresiones.manzano_segmento_tablas where cod_depto = depto_;
INSERT INTO impresiones.manzano_segmento_tablas
SELECT vivienda_ajustada_tabla.id,
vivienda_ajustada_tabla.geom_ajustado,
vivienda_ajustada_tabla.gid,
vivienda_ajustada_tabla.con_seg,
vivienda_ajustada_tabla.orden_manz,
vivienda_ajustada_tabla.nro_puerta,
vivienda_ajustada_tabla.jefe_hogar,
vivienda_ajustada_tabla.ord_pre_se,
vivienda_ajustada_tabla.cod_if,
vivienda_ajustada_tabla.cod_depto
FROM impresiones.vivienda_ajustada_tabla
WHERE vivienda_ajustada_tabla.cod_if = ANY (ARRAY[1, 2]) and
vivienda_ajustada_tabla.cod_depto=depto_;

return 'Proceso finalizado';

ELSE
return 'No existe datos, verifique codigo';
END IF;

END;
$BODY$;

ALTER FUNCTION impresiones.proceso_imp(text)


OWNER TO mpaucara;

También podría gustarte