PostGIS 2.3 Split a line by points

Viewed 1406

I asked this question on gis.stackexchange ( but since my actual problem seems to be more a DB problem than GIS I am trying my luck here). Here is the question on gis.stackexchange : https://gis.stackexchange.com/questions/256535/postgis-2-3-splitting-multiline-by-points

I have a trigger in which I a looping when inserting a new line to INSERT the set of splitted lines in my table, but for some reason I do not get the wanted result since in the example I only get two lines out of three. What a I doing wrong ?

Here comes the code of the trigger function :

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;
1 Answers

This is a relatively complex query and has a lot of moving parts. my recommendation for debugging the query involves multiple ideas:

  1. Consider splitting the function into smaller functions, that are easier to test, and then compose the function from a set of parts you know for sure work as you need them to.

  2. export a set of intermediate results to an intermediate table, you you can visualise the intermediate result-sets easily using a graphical tool and can better assess where the data went wrong.

  3. is is possible that the combination of ST_ functions you are using don't create the geometries you think they create, one way to rule this out is by visualising the results of geographical function combinations, like st_dump(st_split(...))) or st_dump(st_split(...)) for example.

  4. perhaps this check: st_startpoint(geom) = (SELECT geom FROM brs WHERE brs.row_number = i)) could be made by checking "points near" and not "exact point", maybe the points are very near, as in centimeters near, making them essentially "the same point", but not actually be the exact point. this is just an assumption though.

  5. Consider sharing more data with StackOverflow! like a small dataset or example so we can actually run the code! :)

Related