Python: 如何检测geopandas dataframe中一个点的相邻节点?

Python: how to detect the adjacent nodes of a point in a geopandas dataframe?

我有一个如下所示的 geoepandas 数据框:

df = pd.DataFrame()
df['x']=[0,1,3,0,1,3,0,1,3]
df['y']=[0,0,0,1.5,1.5,1.5,2.5,2.5,2.5]
points=[]
for i in df.index:
    points.append(Point(df['x'][i],df['y'][i]))

gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.x, df.y))
f,ax=plt.subplots()
gdf.plot(color='red',ax=ax)

其中gdf如下

    x     y     geometry
0   0   0.0     POINT (0.00000 0.00000)
1   1   0.0     POINT (1.00000 0.00000)
2   3   0.0     POINT (3.00000 0.00000)
3   0   1.5     POINT (0.00000 1.50000)
4   1   1.5     POINT (1.00000 1.50000)
5   3   1.5     POINT (3.00000 1.50000)
6   0   2.5     POINT (0.00000 2.50000)
7   1   2.5     POINT (1.00000 2.50000)
8   3   2.5     POINT (3.00000 2.50000)

我想检测相邻节点(仅在垂直和水平方向上)并具有如下数据框:

    x     y     geometry                   adjacent
0   0   0.0     POINT (0.00000 0.00000)    [1,3]
1   1   0.0     POINT (1.00000 0.00000)    [0,2,4]
2   3   0.0     POINT (3.00000 0.00000)    [1,5]
3   0   1.5     POINT (0.00000 1.50000)    [0,4,6]
4   1   1.5     POINT (1.00000 1.50000)    [1,3,5,7]
5   3   1.5     POINT (3.00000 1.50000)    [2,4,8]
6   0   2.5     POINT (0.00000 2.50000)    [3,7]
7   1   2.5     POINT (1.00000 2.50000)    [4,6]
8   3   2.5     POINT (3.00000 2.50000)    [5,7]

pysal 的 Voronoi 权重中的 rook 标准可以做到这一点。

import os
import pandas as pd
import geopandas as gpd
import shapely
from shapely.geometry import Point
import matplotlib.pyplot as plt
import pysal
import numpy as np
from libpysal.weights import Voronoi


df = pd.DataFrame()
df['x']=[0,1,3,0,1,3,0,1,3]
df['y']=[0,0,0,1.5,1.5,1.5,2.5,2.5,2.5]
points=[]
for i in df.index:
    points.append(Point(df['x'][i],df['y'][i]))

gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.x, df.y))
f, ax=plt.subplots()
gdf.plot(color='red',ax=ax)


for idx, row in df.iterrows():
    ax.annotate(str(idx), (df['x'][idx], df['y'][idx])
    
    
points = df[['x', 'y']].to_numpy()
w = Voronoi(points, criterion='rook')
neighbors_dict = w.neighbors

df['adjacent'] = df.index.map(neighbors_dict)

print(df)

    x   y   geometry                adjacent
0   0   0.0 POINT (0.00000 0.00000) [1, 3]
1   1   0.0 POINT (1.00000 0.00000) [0, 2, 4]
2   3   0.0 POINT (3.00000 0.00000) [1, 5]
3   0   1.5 POINT (0.00000 1.50000) [0, 4, 6]
4   1   1.5 POINT (1.00000 1.50000) [1, 3, 5, 7]
5   3   1.5 POINT (3.00000 1.50000) [8, 2, 4]
6   0   2.5 POINT (0.00000 2.50000) [3, 7]
7   1   2.5 POINT (1.00000 2.50000) [8, 4, 6]
8   3   2.5 POINT (3.00000 2.50000) [5, 7]