| 1 | = Variable-Distance Buffer = |
| 2 | |
| 3 | Source: [https://gis.stackexchange.com/questions/340968/varying-size-buffer-along-a-line-with-postgis GIS StackExchange: Varying size buffer along a line with PostGIS] |
| 4 | |
| 5 | Using the function given below, variable-distance buffers can be generated. |
| 6 | |
| 7 | === Example === |
| 8 | {{{ |
| 9 | SELECT gid, ST_VariableBufferFromLine(geom,10.0) AS geom FROM mylines |
| 10 | }}} |
| 11 | |
| 12 | === Code === |
| 13 | {{{ |
| 14 | CREATE OR REPLACE FUNCTION ST_VariableBufferFromLine( |
| 15 | geom GEOMETRY, |
| 16 | Length_BufferSize_Ratio NUMERIC |
| 17 | ) |
| 18 | |
| 19 | RETURNS GEOMETRY AS |
| 20 | |
| 21 | $BODY$ |
| 22 | |
| 23 | WITH |
| 24 | step1 AS |
| 25 | (SELECT ST_DumpPoints(geom) AS dump, |
| 26 | ST_Length(geom) AS len, |
| 27 | geom), |
| 28 | step2 AS |
| 29 | (SELECT (dump).path[1], |
| 30 | ST_Buffer((dump).geom, GREATEST(ST_LineLocatePoint(geom, (dump).geom)*len/Length_BufferSize_Ratio,0.001)) AS geom |
| 31 | FROM step1), |
| 32 | step3 AS |
| 33 | (SELECT |
| 34 | ST_ConvexHull(ST_Union(geom, LEAD(geom) OVER(ORDER BY path))) AS geom |
| 35 | FROM step2) |
| 36 | SELECT ST_Union(geom) AS geom FROM step3 |
| 37 | |
| 38 | $BODY$ |
| 39 | |
| 40 | LANGUAGE SQL; |
| 41 | }}} |