我想要实现的是对来自表的记录数执行st_Intersection(clipper_geom,clipped_geom) .

https://postgis.net/docs/ST_Intersection.html

https://postgis.net/docs/ST_Intersects.html

POSTGIS Intersection不支持本地处理多个几何,与st_intersects()不同,我必须设计一个函数(返回一个表),选择与st_intersects我的限幅器区域相交的所有特征,循环遍历此查询的每个结果并执行每个记录上的交集st_intersection,“geom”字段和“clipped_geom_wkt”是实际记录剪切几何的那些 .

该函数有效,但我需要为我们想要生成剪辑的每个表使用不同的函数 . 我想要实现的是动态读取输入表(列名和类型)并在RETURN语句中定义它们 .

所有字段名称和类型都是相同的,唯一一个将更新的是geom,新的一个将被添加为clipped_geom_wkt .

我试图搜索Stack Overflow,然后我找到了关于如何创建动态表结构的示例,但是我发现的没有一个然后在第一个结果上执行后续LOOP,其中列名必须匹配以插入/更新新数据 .

这是我到目前为止所提出的,但我很清楚如何执行LOOP部分,添加clipped_geom_wkt字段并更新geom字段 . 在这里的一些响应中,我看到如果将更多字段添加到SETOF,建议将RETURN类型设置为TABLE

Easy way to have return type be SETOF table plus additional fields?

但与此同时我还看到,只有在将表定义作为SETOF返回时才支持动态生成的列

Refactor a PL/pgSQL function to return the output of various SELECT queries

PostgreSQL: ERROR: 42601: a column definition list is required for functions returning "record"

CREATE OR REPLACE FUNCTION clip_palin_polygon_complete(clipped_table text,clipper_geom text, age_sequence VARCHAR)
RETURNS TABLE (rec clipped_table, clipped_geom_wkt text)) AS $$ --not sure if this is the right way to do it...
DECLARE var_r record;
BEGIN
    FOR var_r IN (
        SELECT * FROM clipped_table 
        WHERE clipped_table.seq IN (age_sequence)
        AND ST_Intersects(ST_GeomFromText(clipper_geom,4326), clipped_table.geom)
    )
    LOOP
        /* 
        these are the original table fields that I would like to keep and match
        dynamically with any table I have as input (clipped_table)
        objectid := var_r.objectid;
        seq := var_r.seq;
        age := var_r.age;
        primary_lithology := var_r.primary_lithology;
        confidence := var_r.confidence;
        area_calculated := var_r.area_calculated;*/

        --below there are the only two fields that need modifying
        geom := (
            SELECT ST_Intersection(ST_GeomFromText(clipper_geom, 4326), var_r.geom) AS geom);
        clipped_geom_wkt := (
            SELECT
            ST_AsText(ST_Intersection(ST_GeomFromText(clipper_geom,4326), var_r.geom)) AS clipped_geom_wkt);
        RETURN NEXT;
    END LOOP;
END; $$
LANGUAGE 'plpgsql'