Bom dia,

Criei uma função para mover pontos(x,y) para dentro de polígonos, usando a extensão PostGIS.
Tem duas coisas que gostaria de perguntar:

1) A função não esta a funcionar devidamente. Suponho que relacionado com esta parte: AND t2.country_id = '||$$'$$||quote_ident(country_code)||$$'$$
Algo esta mal que não consigo identificar.

2) Existe uma maneira de por de parâmetro de entrada na função o nome do schema e da tabela num único parâmetro e não em dois?
fun_move_point_inside_polygon('schema.table_name', 'BR')
em vez de
fun_move_point_inside_polygon('schema_name', 'table_name','BR')

Muito obrigado pela atenção.

Cumprimentos,

Eloi

--------------- inicio código --------------------

CREATE OR REPLACE FUNCTION tmp.fun_move_point_inside_polygon(point_sch text, point_table text, country_code text)
  RETURNS text AS
$BODY$
DECLARE
    row record;
    point_table_gid text;
    point_table_geom text;

BEGIN
    -- point_table_gid
    SELECT t2.column_name INTO point_table_gid
     FROM information_schema.table_constraints t1
      RIGHT JOIN information_schema.constraint_column_usage t2
ON t1.constraint_catalog = t2.constraint_catalog AND t1.constraint_schema = t2.constraint_schema AND t1.constraint_name = t2.constraint_name WHERE t1.constraint_type = 'PRIMARY KEY' AND t1.constraint_schema = quote_ident(point_sch) AND t1.table_name = quote_ident(point_table);

    -- point_table_geom
SELECT f_geometry_column INTO point_table_geom FROM geometry_columns WHERE f_table_schema = quote_ident(point_sch) AND f_table_name = quote_ident(point_table);

    -- new columns
EXECUTE 'ALTER TABLE ' || quote_ident(point_sch) || '.' || quote_ident(point_table) || ' DROP COLUMN IF EXISTS new_geom'; EXECUTE 'ALTER TABLE ' || quote_ident(point_sch) || '.' || quote_ident(point_table) || ' ADD COLUMN new_geom geometry(Point,4326);';

    FOR row IN
     -- identify points outside polygon
     EXECUTE 'SELECT ' || quote_ident(point_table_gid) || ' AS gid
FROM ' || quote_ident(point_sch) || '.' || quote_ident(point_table) || ' AS t1,
                    tmp.country AS t2
WHERE ST_Intersects(t1.' || quote_ident(point_table_geom) || ',t2.geom) = FALSE AND t2.country_id = '||$$'$$||quote_ident(country_code)||$$'$$

    LOOP
        -- update new_geom
EXECUTE 'UPDATE ' || quote_ident(point_sch) || '.' || quote_ident(point_table) || ' AS t1 SET new_geom = ST_ClosestPoint(ST_Buffer(t2.geom,-0.00001), t1.' || quote_ident(point_table_geom) || ')
                 FROM tmp.country AS t2
WHERE t2.country_id = '||$$'$$||quote_ident(country_code)||$$'$$|| ' AND t1.' || quote_ident(point_table_gid) || ' = ' || row.gid || ';';

    END LOOP;
RETURN 'End';
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION tmp.fun_move_point_inside_polygon(text, text, text)
  OWNER TO postgres;
COMMENT ON FUNCTION tmp.fun_move_point_inside_polygon(text, text, text) IS 'Moves outside point into inside a polygon';

--------------- fim código --------------------






_______________________________________________
pgbr-geral mailing list
[email protected]
https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral

Responder a