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

Parameters:
gdfGeoDataFrame | GeoSeries

GeoDataFrame containing geometries to analyse.

graphlibpysal.graph.Graph

Graph representing spatial relationships between elements.

weightedbool

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

Returns:
Series

Notes

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

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 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 area:

>>> momepy.neighbors(tessellation, contig, weighted=True)
focal
0      0.012732
1      0.010116
2      0.013350
3      0.010172
4      0.038916
        ...
139    0.020037
140    0.036766
141    0.045287
142    0.044147
143    0.051799
Name: neighbors, Length: 144, dtype: float64