[GRASS-user] Table of polygons bordering each one

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?

Thanks in advance for your help

David Montoya González
Ing. Forestal, Universidad Nacional de Colombia

On Sun, Feb 9, 2014 at 8:13 PM, David Montoya <jdmonto0@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.

Hope this points you into the right direction,

Markus

On 15/02/14 14:35, Markus Neteler wrote:

On Sun, Feb 9, 2014 at 8:13 PM, David Montoya <jdmonto0@gmail.com
<mailto:jdmonto0@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