In a function I'm looping over the all rows a select statement returns. I want to skip the fist and the last row. How could I do this?
...
FOR arow IN
SELECT (dp).path[1] As index,
ST_AsText((dp).geom) As wktnode,
(dp).geom As vertex
FROM (SELECT
ST_DumpPoints(
ST_Segmentize(NEW.the_geom,
-- the max length of the segment
ST_Length(
NEW.the_geom
)/NEW.dist_calc_pnts
)
) AS dp
) As foo
LOOP
test_me := arow.index;
IF NOT (ST_Equals(arow.vertex, ST_StartPoint(NEW.the_geom))
OR
ST_Equals(arow.vertex, ST_EndPoint(NEW.the_geom))) THEN
RAISE NOTICE 'Calling update_pntzzz(%)', test_me;
INSERT INTO
pntzzz(onedee_id, the_geom)
VALUES(NEW.id, arow.vertex);
END IF;
END LOOP;
...
As I said, I do not need the result from first and the last iteration but I'm struggling on how to implement this. Right now the if
statement checks whether the geometry of the current row is identical to the start or endpoint of the original line geometry - a rather expensive check.
This is some sample code to show how to skip the first and the last element in a loop.
It should be simple to adapt it to your use case.
DO $$DECLARE
counter integer := 0;
i integer;
old_i integer;
BEGIN
FOR i IN 1..10 LOOP
counter := counter + 1;
IF counter > 2 THEN
-- processing of old_i goes here
RAISE WARNING 'Processing row %', old_i;
END IF;
old_i := i;
END LOOP;
END;$$;