wiki:UserWikiVariableBuffer

Version 2 (modified by mdavis, 4 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.

Note: currently only the end distance can be given. The start distance is approximately 0.

Example

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

Code

CREATE OR REPLACE FUNCTION ST_VariableBufferFromLine(
    geom GEOMETRY, 
    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, 
                GREATEST(end_dist * ST_LineLocatePoint(geom, (dump).geom), 0.001)) 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.