networkx构建delaunay图,并根据节点坐标绘制图


from libpysal import weights, examples
from libpysal.cg import voronoi_frames
from contextily import add_basemap
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
import geopandas
import itertools
# 输入数据
cases = geopandas.read_file(r"E:\code_practice\gitee\Python地理空间分析指南\nx_study\networkx\examples\geospatial\cholera_cases.gpkg")
cases
IdCountgeometry
001POINT (-15539.921 6712903.938)
103POINT (-15356.721 6712858.073)
202POINT (-15336.090 6712866.374)
301POINT (-15353.877 6713017.559)
400POINT (-15282.597 6712895.684)
............
31900POINT (-14951.181 6712211.606)
32000POINT (-14979.678 6712225.369)
32103POINT (-15591.770 6712227.697)
32200POINT (-15525.087 6712152.522)
32300POINT (-14952.725 6712116.692)

324 rows × 3 columns

  • 为了让nx正确绘制图的节点,我们需要为数据集中的每个点构造坐标数组
  • 从geometry列中提取x,y坐标
# x坐标
x = cases.geometry.x
x
0     -15539.921064
1     -15356.720566
2     -15336.089980
3     -15353.877335
4     -15282.596809
           ...     
319   -14951.180762
320   -14979.678278
321   -15591.770010
322   -15525.086590
323   -14952.725267
Length: 324, dtype: float64
# y
y= cases.geometry.y
y
0      6.712904e+06
1      6.712858e+06
2      6.712866e+06
3      6.713018e+06
4      6.712896e+06
           ...     
319    6.712212e+06
320    6.712225e+06
321    6.712228e+06
322    6.712153e+06
323    6.712117e+06
Length: 324, dtype: float64
nodes_coords = np.column_stack((x,y))
#切片展示
nodes_coords[:5]
array([[ -15539.92106438, 6712903.93781305],
       [ -15356.72056559, 6712858.07280407],
       [ -15336.08997978, 6712866.3743291 ],
       [ -15353.877335  , 6713017.55898743],
       [ -15282.5968086 , 6712895.68378701]])
  • 泰森多边形的建立 (具体看libpysal库)
  • 并将
region_df, point_df  = voronoi_frames(nodes_coords, clip="convex hull")
fig, ax = plt.subplots()
region_df.plot(ax=ax, color='blue',edgecolor='black', alpha=0.3)
point_df.plot(ax=ax, color='red',markersize =3)
<AxesSubplot:>

png

  • 利用泰森多边形,可以利用“rook”临接性来构造他们之间的邻接图
  • 如果泰森多边形共享边/面,则它们是相邻的
delaunay = weights.Rook.from_dataframe(region_df)
delaunay_graph = delaunay.to_networkx()
nx.draw(delaunay_graph,node_size =10)

png

# 给节点匹配coords
positions = dict(zip(delaunay_graph.nodes, nodes_coords))
#切片展示
positions_head = dict(itertools.islice(positions.items(), 5))
positions_head
{0: array([ -15539.92106438, 6712903.93781305]),
 1: array([ -15356.72056559, 6712858.07280407]),
 2: array([ -15336.08997978, 6712866.3743291 ]),
 3: array([ -15353.877335  , 6713017.55898743]),
 4: array([ -15282.5968086 , 6712895.68378701])}
  • add_basemap :获取地理底图
  • positions :dict,节点-坐标,用于可视化
# 可视化
# 用nx可视化delaunay_graph,并展示节点正确的地理位置
ax = region_df.plot(facecolor="lightblue", alpha=0.50, edgecolor="cornsilk", linewidth=2)
add_basemap(ax)
ax.axis("off")
nx.draw(
    delaunay_graph,
    positions,   #以节点为键,坐标为值
    ax=ax,
    node_size=2,
    node_color="k",
    edge_color="k",
    alpha=0.8,
)
plt.show()

png

#下面是直接用点、多边形数据可视化的结果,可以与上面对比一下
region_df, point_df  = voronoi_frames(nodes_coords, clip="convex hull")
fig, ax = plt.subplots()
region_df.plot(ax=ax, color='blue',edgecolor='black', alpha=0.3)
point_df.plot(ax=ax, color='red',markersize =3)
<AxesSubplot:>

png


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值