GRASS logo

Note: This document is for an older version of GRASS GIS that will be discontinued soon. You should upgrade, and read the current manual page.


r.neighborhoodmatrix - Calculates geometry parameters for raster objects.


raster, statistics, reclass, clumps


r.neighborhoodmatrix --help
r.neighborhoodmatrix [-dlc] input=name [output=name] [separator=character] [--overwrite] [--help] [--verbose] [--quiet] [--ui]


Also take into account diagonal neighbors
Also output length of common border (in pixels)
Include column names in output (meaning will be inversed soon)
Allow output files to overwrite existing files
Print usage summary
Verbose module output
Quiet module output
Force launching GUI dialog


input=name [required]
Raster for which to calculate the neighboorhood matrix
Name for output file
Field separator
Special characters: pipe, comma, space, tab, newline
Default: pipe

Table of contents


r.neighborhoodmatrix identifies all adjacency relations between objects (aka segments or clumps identified by identical integer cell values of adjacent pixels) in a raster map and exports these as a 2xn matrix where n is the number of neighborhood relations with each relation listed in both directions (i.e. if a is neighbor of b, the list will contain a,b and b,a). If a path to an output file is specified, the matrix will be written to that file, otherwise it will be sent to standard output.

Neighborhood relations are determined pixel by pixel, and by default only pixels that share a common pixel boundary are considered neighbors. When the -d flag is set pixels sharing a common corner (i.e. diagonal neighbors) are also taken into account.

When the -l flag is set, the module additionally indicates the length of the common border between two neighbors in number of pixels. As this length is not clearly defined for diagonal neighbors, the -l flag cannot be used in combination with the -d flag.

The -c flag currently adds column headers. Please note that this flag's meaning will be inversed when GRASS 8 comes out in order to harmonize its behaviour with that in other modules.


As neighborhood length is measured in pixels, this length is not in proportion to length in map units if the location is a lat-long location, or if the resolution is not the same in East-West and in North-South direction (rectangular pixels).

The module respects the region settings, so if the raster map is outside the current computational region, the resulting list of neighbors will be empty.



Start by making sure the input map is of type CELL:

r.mapcalc "bc_int = int(boundary_county_500m)"

Send the neighborhood matrix of the counties in the boundary_county map of the North Carolina dataset to standard output:

r.neighborhoodmatrix in=bc_int sep=comma

Idem, but also calculating neighborhood length, sending the output to a file:

r.neighborhoodmatrix -l n=bc_int sep=comma output=county_neighbors.csv




Original Python-Version: Moritz Lennert
C-Version: Markus Metz


Available at: r.neighborhoodmatrix source code (history)

Latest change: Monday Jan 30 19:52:26 2023 in commit: cac8d9d848299297977d1315b7e90cc3f7698730

Note: This document is for an older version of GRASS GIS that will be discontinued soon. You should upgrade, and read the current manual page.

Main index | Raster index | Topics index | Keywords index | Graphical index | Full index

© 2003-2023 GRASS Development Team, GRASS GIS 8.2.2dev Reference Manual