This article complete in PDF format as presented to the GRASS GIS 2002 users conference, Trento, September 2002 here.
-------------------------

April 4, 2002  --just fixed some bugs in geo_addfuncs.c and added a few new calls, as return_buffer to make the buffer lines and path_to_file for writing
postgres lines to format suitable for v.in.arc input. --alex

Here is an example of vector line buffer constructed from a GRASS line map (rivers). It consists of polygons from another map (kuruma_id).
Both maps were exported to PostgreSQL with a new module v.to.pg (see some other simple examples here).
 
----------------------------------------
For spatial buffer query, a simple C code was used to make a shared library with functions that can be loaded by Postgres backend by commands like:

CREATE FUNCTION pt_in_path_buffer(path,point,float8) RETURNS boolean AS 'geo_addfuncs.so','path_buffer_contain_pt' LANGUAGE 'c';

(syntax is for 7.1.3 version - different in 7.2)

Now we can query polygons (and sites) in different ways:

a) center of polygon boundary box is within buffer (picture)
b) overlap (any point within buffer)
c) contained (all points within buffer),

etc.

To draw the selected (falling in 1000 m buffer) polygons on screen:

d.vect.pg -f -s map=kuruma_id sql=test_buffer1000.sql color=grey
 

test_buffer<i>.sql:
select rec_id from info_kuruma_bnd, riv_names_arc where (
(pt_in_path_buffer(riv_names_arc.segment, center(boundary::box),<i>) = 't')
and (riv_names_arc.riv_id = 205001 or riv_names_arc.riv_id = 104004 or
riv_names_arc.riv_id = 204007))

 

----------------------------------------------------
Make buffers in psql with command:

select path_to_file(return_buffer(path_reduce(segment,2),100)) 
from riv_names_arc where riv_id = 204007;

Each time it writes a single result buffer line (in picture in violet - 100 m) in the
file '/tmp/boundary.pol'. This name can be changed to whatever you like. The line is further imported to grass by v.in.arc module.

Functions 'path_to_file', 'return_buffer' and 'path_reduce' are from the same library, and are created by Postgres superuser using CREATE FUNCTION. Check the code for correct parameter definitions before you create these SQL functions in psql.

Note: 'path_reduce' second parameter is number of times the input line is generalized before buffering. This parameter is 1 if radius of buffering is less or equal to segments lengths. However, when radius is much greater than distances between vertices, the true UNION of circles is intrapolated by 'thinning" the number of vertices. Thus we can produce, e.g., 
1000 m buffer (in red in picture) with minimal (or none by adding more to this parameter) 'swallow tails' that are resulting from algorithm based on only 2 neighboring segments analysis. This algorithm is correct to the limit of radius mentioned above. For large buffers, increase the parameter in 'path_reduce' until the needed smoothness of buffer. For example, the river 205001 with 1000 buffer (top of picture) needed 24-times thinning to produce the shown line.
 

 

Finally, draw in yellow rivers themselves:
d.vect.pg -s map=rivers sql=show_rivs.sql color=yellow

show_rivs.sql:
select riv_id from riv_names_arc where (
riv_names_arc.riv_id = 205001 or riv_names_arc.riv_id = 104004 or
riv_names_arc.riv_id = 204007)

Alex Shevlakov, Motivation Free Software consulting,
Moscow, Russia
sixote@yahoo.com