[postgis-devel] New function ST_DumpSegments?? (was : utility function : linestring -> setof segment)

Rémi Cura remi.cura at gmail.com
Wed Oct 30 06:34:05 PDT 2013


Hey,
here is the function, safe for geometry collection and multilinestring as
well.
It mimics ST_DumpPoints.

It doesn't work on polygon as we would first need a method STDumpLines.


Origine of segments is preserved inside the path when multi.
Could it be added to core ?
Shall I add a ticket?

Cheers,
Rémi-C

/////////////////////////////////

DROP FUNCTION IF EXISTS public.DumpSegments(line geometry ) ;
CREATE OR REPLACE FUNCTION public.DumpSegments(_line geometry)
RETURNS SETOF geometry_dump
AS
$BODY$
--this function breaks a line into minimal segments and return it
 DECLARE
_r record;
BEGIN

FOR _r in SELECT ST_Dump(ST_CollectionExtract (_line,2)) AS dp
LOOP
RETURN QUERY WITH line AS(
SELECT --ST_GeomFromText('LINESTRING(12 1, 13 1, 14 2, 15 4)') AS line
(_r.dp).path AS gpath, (_r.dp).geom AS line
),
dump AS(
SELECT gpath[1] AS gpath, (ST_DumpPoints(line)) as dp
FROM line
),
segments AS (
SELECT CASE WHEN gpath IS NULL THEN ARRAY[(dp).path[1]-1] ELSE ARRAY[gpath,
(dp).path[1]-1] END AS path,ST_MakeLine( lag((dp).geom , 1, NULL) OVER
(ORDER BY  (dp).path),(dp).geom) AS geom
FROM dump
)
SELECT path,geom
FROM segments
WHEre geom  IS NOT NULL;
END LOOP;--loop if multi linestring
 RETURN;
END;
$BODY$
  LANGUAGE plpgsql  IMMUTABLE STRICT;


 SELECT public.DumpSegments(line2)
FROM ST_GeomFromText('MULTILINESTRING((12 1, 13 1, 14 2, 15 4),(1 1, 2 2,3
3))') AS line1,ST_GeomFromText( 'LINESTRING(12 1, 13 1, 14 2, 15 4)') AS
line2




//////////////////////////////////////




2013/10/30 Rémi Cura <remi.cura at gmail.com>

> Thanks,
> wanted to do it the clean way (geos accelerated maybe?),
>
> but not so much a big deal, I'll go you way.
>
> Cheers,
> Rémi-C
>
>
> 2013/10/30 Nicolas Ribot <nicolas.ribot at gmail.com>
>
>> Hi,
>>
>> I don't think such a function exists in postgis.
>> (To achieve that, I usually dump line points and create new
>> linestrings with vertices n, n+1)
>>
>> Nicolas
>>
>> On 30 October 2013 10:59, Rémi Cura <remi.cura at gmail.com> wrote:
>> > Hey,
>> > is somebodyers aware of a function breaking a linestring into a set of
>> > segments (mathematical meaning),
>> > without introducing new points?
>> >
>> > Cheers,
>> >
>> >
>> > Rémi-C
>> >
>> > _______________________________________________
>> > postgis-users mailing list
>> > postgis-users at lists.osgeo.org
>> > http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users
>> _______________________________________________
>> postgis-users mailing list
>> postgis-users at lists.osgeo.org
>> http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-devel/attachments/20131030/29610b69/attachment.html>


More information about the postgis-devel mailing list