【发布时间】:2018-03-08 17:19:18
【问题描述】:
我在 gis.stackexchange 上问了这个问题(但由于我的实际问题似乎比 GIS 更像是一个数据库问题,所以我在这里试试运气)。这是关于 gis.stackexchange 的问题:https://gis.stackexchange.com/questions/256535/postgis-2-3-splitting-multiline-by-points
我有一个触发器,当插入新行以插入表中的一组拆分行时,我会循环,但由于某种原因,我没有得到想要的结果,因为在示例中我只得到了两行三。我做错了什么?
下面是触发函数的代码:
CREATE OR REPLACE FUNCTION public.split_cable()
RETURNS trigger AS
$BODY$
DECLARE compte integer;
DECLARE i integer := 2;
BEGIN
compte = (SELECT count(*) FROM boite WHERE st_intersects(boite.geom, new.geom));
WHILE i < compte LOOP
WITH brs AS (SELECT row_number() over(), boite.geom FROM boite, cable2
WHERE st_intersects(boite.geom, new.geom)
-- here the ORDER BY serve to get the "boite" objects in a specific order
ORDER BY st_linelocatepoint(st_linemerge(new.geom),boite.geom)),
brs2 AS (SELECT st_union(geom) AS geom FROM brs),
cables AS (SELECT (st_dump(st_split(new.geom, brs2.geom))).geom FROM brs2)
INSERT INTO cable2 (geom) VALUES (
SELECT st_multi(cables.geom) FROM cables WHERE st_startpoint(geom) = (SELECT geom FROM brs WHERE brs.row_number = i));
i = i + 1;
END LOOP;
new.geom = (WITH brs AS (SELECT row_number() over(), boite.geom FROM boite, cable2
WHERE st_intersects(boite.geom, new.geom)
ORDER BY st_linelocatepoint(st_linemerge(new.geom),boite.geom)),
brs2 AS (SELECT st_union(geom) as geom from brs),
cables AS (SELECT (st_dump(st_split(new.geom, brs2.geom))).geom FROM brs2)
SELECT st_multi(cables.geom) FROM cables WHERE st_startpoint(geom) = (SELECT geom FROM brs WHERE brs.row_number = 1));
RETURN new;
END
$BODY$
LANGUAGE plpgsql;
【问题讨论】:
-
你的例子中
compte的初始值是多少? -
@MiguelKVidal 它可以是任何值,但始终至少为 2。例如,假设它是 3,换句话说,在这种情况下,我应该以两行拆分结束首行。
-
在插入新值之前,尝试输出
compte(while之前)和SELECT的值。这将有助于更好地了解正在发生的事情。 -
你能提供一个带有数据集的数据表来试试吗? (如sqlfiddle.com/#!17)
标签: sql postgresql loops triggers postgis