capturar valores de campos en variables usando consulta espacial

From: "jvenegasperu (dot)" <jvenegasperu(at)gmail(dot)com>
To: Ayuda <pgsql-es-ayuda(at)postgresql(dot)org>
Subject: capturar valores de campos en variables usando consulta espacial
Date: 2015-09-22 17:42:42
Message-ID: CA+KjtGeUN6Q_ezhvcDENPgKts48ORMFTi519m1n-5QPrD4pR3w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda

Hola buen dia a todos

tengo este problema actualmente utilizo qgis para dibujar redes agua y
alcantarillado que se graban en una BD postgres con postgis.

basicamente los accesorios de tuberia como Codos, tes, tapones, valvulas
etc los almaceno como punto en la BD pero para imprimir planos se requiere
el simbolo del objeto el cual debe tener un agulo de inclinacion como la
imagen que adjunto

bueno ya estoy logrando que el usuario primero dibuje las lineas de las
redes de agua y grabe despues inserte los accesorios como punto en las
interesecciones o uniones al hacer clic en grabar se dispara un triger que
evalua los angulos de rotacion de las lineas involucradas y en funcion de
eso obtiene el valor para el angulo de inclinacion que tendra el simbolo
para el plano que se imprimira hasta ahi todo bien sin embargo la manera
como obtengo los datos no me agrada porque pienso que se puede hacer en una
sola consulta

aqui adjunto como lo estoy haciendo creo una serie de variables y luego
hago un select para obtener cada valor sin embargo creo que se podria hacer
hacer en un solo select obtener cada grupo de valores que necesito.

ademas en el segundo y tercer grupo uso el id encontrado en la primera
consulta y en el tercero el id de la primera y segunda consulta

espero puedan ayudarme a mejorar esta parte ya que son 24 consultas que
pienso porian ser solo 3 una por cada grupo de datos que necesito

gidagua es identificardor del tramo
iniagua es el punto inicial del tramo
finagua es el punto final del tramo
xiagua es la coordenada x del punto inicial
yiagua es la coordenada y del punto inicial
xfagua es la coordenada x del punto final
yfagua es la coordenada y del punto final

de igual manera para las siguientes consultas

CREATE OR REPLACE FUNCTION public.rota_accesorio()
RETURNS trigger AS
$BODY$
DECLARE

-- Primer grupo

gidagua INTEGER;
iniagua geometry;
finagua geometry;
xiagua numeric;
xfagua numeric;
yiagua numeric;
yfagua numeric;
rotagua numeric;

-- Segundo grupo

gidagua2 INTEGER;
iniagua2 geometry;
finagua2 geometry;
xiagua2 numeric;
xfagua2 numeric;
yiagua2 numeric;
yfagua2 numeric;
rotagua2 numeric;

-- Tercer grupo

gidagua3 INTEGER;
iniagua3 geometry;
finagua3 geometry;
xiagua3 numeric;
xfagua3 numeric;
yiagua3 numeric;
yfagua3 numeric;
rotagua3 numeric;

mayor numeric;
menor numeric;

BEGIN

-- aqui hago un select por cada variable por favor ayudenme como podria
poner cada campo en una variable

gidagua = (select gid from ap_dist_agua b where
st_intersects(st_buffer(new.the_geom,0.5),b.the_geom) and new.the_geom &&
b.the_geom limit 1);
iniagua = (select st_startpoint(b.the_geom) from ap_dist_agua b where
st_intersects(st_buffer(new.the_geom,0.5),b.the_geom) and new.the_geom &&
b.the_geom limit 1);
finagua = (select st_endpoint(b.the_geom) from ap_dist_agua b where
st_intersects(st_buffer(new.the_geom,0.5),b.the_geom) and new.the_geom &&
b.the_geom limit 1);
xiagua = (select st_x(st_startpoint(b.the_geom)) from ap_dist_agua b
where st_intersects(st_buffer(new.the_geom,0.5),b.the_geom) and
new.the_geom && b.the_geom limit 1);
xfagua = (select st_x(st_endpoint(b.the_geom)) from ap_dist_agua b where
st_intersects(st_buffer(new.the_geom,0.5),b.the_geom) and new.the_geom &&
b.the_geom limit 1);
yiagua = (select st_y(st_startpoint(b.the_geom)) from ap_dist_agua b
where st_intersects(st_buffer(new.the_geom,0.5),b.the_geom) and
new.the_geom && b.the_geom limit 1);
yfagua = (select st_y(st_endpoint(b.the_geom)) from ap_dist_agua b where
st_intersects(st_buffer(new.the_geom,0.5),b.the_geom) and new.the_geom &&
b.the_geom limit 1);
rotagua = (select rotacion from ap_dist_agua b where
st_intersects(st_buffer(new.the_geom,0.5),b.the_geom) and new.the_geom &&
b.the_geom limit 1);

-- aqui uso el gid del agua anterior

gidagua2 = (select gid from ap_dist_agua b where
st_intersects(st_buffer(new.the_geom,0.5),b.the_geom) and new.the_geom &&
b.the_geom and b.gid != gidagua limit 1);
iniagua2 = (select st_startpoint(b.the_geom) from ap_dist_agua b where
st_intersects(st_buffer(new.the_geom,0.5),b.the_geom) and new.the_geom &&
b.the_geom and b.gid != gidagua limit 1);
finagua2 = (select st_endpoint(b.the_geom) from ap_dist_agua b where
st_intersects(st_buffer(new.the_geom,0.5),b.the_geom) and new.the_geom &&
b.the_geom and b.gid != gidagua limit 1);
xiagua2 = (select st_x(st_startpoint(b.the_geom)) from ap_dist_agua b
where st_intersects(st_buffer(new.the_geom,0.5),b.the_geom) and
new.the_geom && b.the_geom and b.gid != gidagua limit 1);
xfagua2 = (select st_x(st_endpoint(b.the_geom)) from ap_dist_agua b where
st_intersects(st_buffer(new.the_geom,0.5),b.the_geom) and new.the_geom &&
b.the_geom and b.gid != gidagua limit 1);
yiagua2 = (select st_y(st_startpoint(b.the_geom)) from ap_dist_agua b
where st_intersects(st_buffer(new.the_geom,0.5),b.the_geom) and
new.the_geom && b.the_geom and b.gid != gidagua limit 1);
yfagua2 = (select st_y(st_endpoint(b.the_geom)) from ap_dist_agua b where
st_intersects(st_buffer(new.the_geom,0.5),b.the_geom) and new.the_geom &&
b.the_geom and b.gid != gidagua limit 1);
rotagua2 = (select rotacion from ap_dist_agua b where
st_intersects(st_buffer(new.the_geom,0.5),b.the_geom) and new.the_geom &&
b.the_geom and b.gid != gidagua limit 1);

-- aqui uso los gid del agua 1 y 2
gidagua3 = (select gid from ap_dist_agua b where
st_intersects(st_buffer(new.the_geom,0.5),b.the_geom) and new.the_geom &&
b.the_geom and b.gid != gidagua and b.gid != gidagua2 limit 1);
iniagua3 = (select st_startpoint(b.the_geom) from ap_dist_agua b where
st_intersects(st_buffer(new.the_geom,0.5),b.the_geom) and new.the_geom &&
b.the_geom and b.gid != gidagua and b.gid != gidagua2 limit 1);
finagua3 = (select st_endpoint(b.the_geom) from ap_dist_agua b where
st_intersects(st_buffer(new.the_geom,0.5),b.the_geom) and new.the_geom &&
b.the_geom and b.gid != gidagua and b.gid != gidagua2 limit 1);
xiagua3 = (select st_x(st_startpoint(b.the_geom)) from ap_dist_agua b
where st_intersects(st_buffer(new.the_geom,0.5),b.the_geom) and
new.the_geom && b.the_geom and b.gid != gidagua and b.gid != gidagua2 limit
1);
xfagua3 = (select st_x(st_endpoint(b.the_geom)) from ap_dist_agua b where
st_intersects(st_buffer(new.the_geom,0.5),b.the_geom) and new.the_geom &&
b.the_geom and b.gid != gidagua and b.gid != gidagua2 limit 1);
yiagua3 = (select st_y(st_startpoint(b.the_geom)) from ap_dist_agua b
where st_intersects(st_buffer(new.the_geom,0.5),b.the_geom) and
new.the_geom && b.the_geom and b.gid != gidagua and b.gid != gidagua2 limit
1);
yfagua3 = (select st_y(st_endpoint(b.the_geom)) from ap_dist_agua b where
st_intersects(st_buffer(new.the_geom,0.5),b.the_geom) and new.the_geom &&
b.the_geom and b.gid != gidagua and b.gid != gidagua2 limit 1);
rotagua3 = (select st_y(st_endpoint(b.the_geom)) from ap_dist_agua b
where st_intersects(st_buffer(new.the_geom,0.5),b.the_geom) and
new.the_geom && b.the_geom and b.gid != gidagua and b.gid != gidagua2 limit
1);

raise notice 'gidagua % xiagua % xfagua % yiagua % yfagua % gidagua2 %
xiagua2 % xfagua2 % yiagua2 % yfagua2 % gidagua3 % xiagua3 % xfagua3 %
yiagua3 % yfagua3 % mayor % menor
%',gidagua,xiagua,xfagua,yiagua,yfagua,gidagua2,xiagua2,xfagua2,yiagua2,yfagua2,gidagua3,xiagua3,xfagua3,yiagua3,yfagua3,mayor,menor;
RETURN NEW;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION public.rota_accesorio()
OWNER TO postgres;

--
José Mercedes Venegas Acevedo
cel Mov RPM #955853768

mails: jvenegasperu(at)gmail(dot)com

Attachment Content-Type Size
pgayuda.png image/png 102.1 KB
unknown_filename text/plain 157 bytes

Responses

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message Alvaro Herrera 2015-09-22 18:04:34 Re: capturar valores de campos en variables usando consulta espacial
Previous Message Guillermo E. Villanueva 2015-09-21 23:44:17 Re: plpython