wiki:UserWikiVariableBuffer

Version 4 (modified by mdavis, 5 years ago) ( diff )

--

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;
Note: See TracWiki for help on using the wiki.