[GRASS-user] Table of polygons bordering each one

Moritz Lennert mlennert at club.worldonline.be
Wed Feb 19 01:18:49 PST 2014


On 15/02/14 14:35, Markus Neteler wrote:
> On Sun, Feb 9, 2014 at 8:13 PM, David Montoya <jdmonto0 at gmail.com
> <mailto:jdmonto0 at gmail.com>> wrote:
>
>     Hi, I have a question. I need to generate a table that says which
>     polygons bordering to each one, for example:
>
>                    ID1 	               ID2
>     1 	3
>     1 	2
>     2 	1
>     2 	4
>     3 	1
>     3 	4
>     3 	5
>     4 	2
>     4 	3
>     4 	6
>     5 	3
>     5 	6
>     6 	4
>     6 	5
>
>
>     In this case the polygon 1 borders with the polygons 3 and 2, and so
>     on. How can I do that in Grass?
>
>
> The module v.to.db offers a "sides" option, perhaps that's useful here.
>
> For an (un)related example, see
> http://grasswiki.osgeo.org/wiki/Vector_length_of_common_boundaries
>
> Rather the length you could then just retrieve the IDs.

A simple two-liner gets you a csv-file with the neighborhood matrix. 
Example with the NC-dataset:

v.category boundary_county op=add layer=2 out=counties
v.to.db -p counties op=sides layer=2 qlayer=1 type=boundary > neighbors.csv

This matrix contains the -1 for "no neighbor" and some repetition of 
pairs. You can use some awk, sort and uniq magic to clean that up:

v.to.db -p counties op=sides layer=2 qlayer=1 type=boundary | awk -F '|' 
'{if($2!=-1 && $3 !=-1) printf"%i;%i\n", $2,$3}' | sort -n | uniq > 
neigbhors.csv

Moritz


More information about the grass-user mailing list