如何使用 GeoDataframe 点和线串来构建 Networkx 图形?

问题描述 投票:0回答:0

我有 2 个 GeoDataframes,一个是点对象,另一个是线串。点在线串上。我如何使用这 2 个 gdfs 来构建 networkx 图?

#  Import
import osmnx as ox
import networkx as nx
import os
os.environ['USE_PYGEOS'] = '0'
import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt



# Data

gdf1_line.head()
index line_code line_direc  geometry
0   77  0   LINESTRING (12948661.614 4856347.606, 12948661...
1   335 0   LINESTRING (12945808.586 4844253.556, 12945806...
2   323 0   LINESTRING (12949768.231 4862490.823, 12949825...
3   349 0   LINESTRING (12949059.428 4850398.713, 12949059...
4   349 1   LINESTRING (12941150.014 4842236.071, 12941144...


gdf2_line.head()
index staion_id line_code   order_inLine    geometry
4563    657844  323 1   POINT (12949768.231 4862490.823)
4564    657845  323 2   POINT (12950200.654 4862400.385)
4565    657846  323 3   POINT (12950221.658 4861898.833)
4566    657847  323 4   POINT (12950051.957 4861245.409)
4567    657848  323 5   POINT (12949078.265 4861197.891)  

 

# Plot
fig,ax = plt.subplots(figsize=(20,10)) 
gdf1_line.plot(ax=ax,color='grey',label='bus line',zorder=1,alpha=0.8)
gdf2_points.plot(ax=ax,label='bus on station',zorder=2,marker='^',markersize=5,color='blue',alpha=0.4)   
plt.axis('off')
plt.legend()
plt.show() 

enter image description here

# Try

import momepy
graph = momepy.gdf_to_nx(gdf1_line,approach='primal')
nx.draw(graph,{n:[n[0], n[1]] for n in list(graph.nodes)},node_size=15)

enter image description here

我希望当我绘制图表时,它会与 GeoDataframes 的实际位置一致。

python networkx geopandas osmnx
© www.soinside.com 2019 - 2024. All rights reserved.