momepy.neighbors

momepy.neighbors(geometry, graph, weighted=False)[source]

Calculate the number of neighbours captured by graph.

If weighted=True, the number of neighbours will be divided by the perimeter of the object to return a relative value (neighbors per meter).

Adapted from [Hermosilla et al., 2012].

Notes

The index of geometry must match the index along which the graph is built.

Parameters:
geometry : GeoDataFrame | GeoSeries

GeoDataFrame containing geometries to analyse.

graph : libpysal.graph.Graph

Graph representing spatial relationships between elements.

weighted : bool

If True, the number of neighbours will be divided by the perimeter of the object to return a relative value (neighbors per meter).

Return type:

Series

Examples

>>> from libpysal import graph
>>> path = momepy.datasets.get_path("bubenec")
>>> buildings = geopandas.read_file(path, layer="buildings")
>>> tessellation = momepy.morphological_tessellation(buildings)

Define a spatial graph denoting adjacency:

>>> contig = graph.Graph.build_contiguity(tessellation)
>>> contig
<Graph of 144 nodes and 768 nonzero edges (1 component, 0 isolates) indexed by
 [0, 1, 2, 3, 4, ...]>

Number of neighbors of each tessellation cell:

>>> momepy.neighbors(tessellation, contig)
focal
0       4
1       9
2       3
3       3
4       7
    ..
139     3
140     6
141    12
142     2
143     5
Name: neighbors, Length: 144, dtype: int64

Weighted by the tessellation perimeter:

>>> momepy.neighbors(tessellation, contig, weighted=True)
focal
0      0.012732
1      0.010126
2      0.013391
3      0.010180
4      0.038930
         ...
139    0.020039
140    0.036771
141    0.045290
142    0.044329
143    0.051833
Name: neighbors, Length: 144, dtype: float64