我在gis.stackexchange上問過這個問題(但是因爲我的實際問題似乎比GIS更多的數據庫問題,所以我在這裏嘗試我的運氣)。這裏是關於gis.stackexchange的問題:https://gis.stackexchange.com/questions/256535/postgis-2-3-splitting-multiline-by-pointsPostGIS 2.3通過點劃分線
我有一個觸發器,當我插入一個新行時插入一個循環插入我的表中的分離行集,但由於某種原因,我沒有得到想要的結果因爲在這個例子中,我只能得到三條中的兩條。我做錯了什麼?
這裏談到的觸發功能的代碼:
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
@MiguelKVidal它可以是任何的,但總是至少爲2.舉個例子,假設它是3,換句話說,就是我應該在第一行的兩行中分割結尾的情況。 –
嘗試在插入新值之前輸出'compte'(在'while'之前)和'SELECT'的值。這將有助於更好地理解發生的事情。 – MiguelKVidal