wiki:UserWikiVariableBuffer

Variable-Distance Buffer

Source: GIS StackExchange: Varying size buffer along a line with PostGIS

The function below generates variable-distance buffers.

It operates by creating the union of a series of segment buffers . Each segment buffer is the convex hull of the buffers of the segment start and end points, with the buffer distance computed proportionally to the fractional distance along the line.

Example

SELECT ST_VariableBufferFromLine( 'LINESTRING( 0 0, 100 0)' , 1, 10 );

Code

CREATE OR REPLACE FUNCTION ST_VariableBufferFromLine(
    geom GEOMETRY, 
    start_dist NUMERIC,
    end_dist NUMERIC
)
RETURNS GEOMETRY AS
$BODY$

WITH 
    step1 AS 
        (SELECT ST_DumpPoints(geom) AS dump,  
        geom),
    step2 AS
        (SELECT (dump).path[1], 
            ST_Buffer( (dump).geom, 
                start_dist + (end_dist - start_dist) * ST_LineLocatePoint(geom, (dump).geom) 
                ) AS geom 
        FROM step1),
    step3 AS
        (SELECT 
            ST_ConvexHull(ST_Union(geom, LEAD(geom) OVER(ORDER BY path))) AS geom 
        FROM step2)
SELECT ST_Union(geom) AS geom FROM step3

$BODY$
LANGUAGE SQL;
Last modified 3 years ago Last modified on Nov 25, 2019, 11:06:17 AM
Note: See TracWiki for help on using the wiki.