GRASS logo


r.neighborhoodmatrix - Calculates a neighborhood matrix for a raster map with regions


raster, neighboorhood, regions


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


Also take into account diagonal neighbors
Also output length of common border (in pixels)
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
output=name [required]
Name for output file (- for standard output)
Default: -
Field separator
Special characters: pipe, comma, space, tab, newline
Default: pipe
Number of processes to run in parallel (for multiple rasters)
Default: 1

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.

In order to speed up calculations, the user can set the parameter processes to the number of desired processes to run in parallel. As the module parallelizes per direction, the maximum number of processes is 4 without and 8 with the -d flag.


The module (without the -d flag) should give the same result as the equivalent call to v.neighborhoodmatrix with the -b flag. Currently it actually seems faster for some maps to transform the raster to vector and then run the latter. More tests are needed, though, to confirm this.

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).



Send the neighborhood matrix of the counties in the boundary_county map of the North Carolina dataset to standard output:
r.neighborhoodmatrix in=boundary_county_500m sep=comma
Idem, but also calculating neighborhood length, sending the output to a file and using 4 parallel processes:
r.neighborhoodmatrix -l n=boundary_county_500m sep=comma \
	output=county_neighbors.csv processes=4
Transforming the raster to vector (without attribute table or topology) and using v.neighborhoodmatrix: -tbv in=boundary_county_500m out=boundary_county_500m type=area
v.neighborhoodmatrix in=boundary_county_500m sep=comma




Moritz Lennert

Last changed: $Date: 2018-04-18 09:19:05 +0200 (Wed, 18 Apr 2018) $


Available at: r.neighborhoodmatrix source code (history)

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

© 2003-2018 GRASS Development Team, GRASS GIS 7.4.2svn Reference Manual