我有 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()
# 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)
当我用29 POINTS建一个LineString,用momepy.gdf_to_nx函数建图时,图中的节点数变成了2,这是怎么回事?
# data
s
line_code bus_direct geometry
0 323 0 LINESTRING (12949768.231 4862490.823, 12950200...
# draw
position = {n:[n[0], n[1]] for n in list(graph.nodes)}
f, ax = plt.subplots(1, 2, figsize=(10, 6), sharex=True, sharey=True)
for i, facet in enumerate(ax):
facet.set_title(("gpd line", "nx graph")[i])
facet.axis("off")
s.plot(color='orange',ax = ax[0])
graph = momepy.gdf_to_nx(s,approach='primal')
nx.draw(graph,position,ax=ax[1],node_size=15)
我希望当我绘制图表时,它会与 GeoDataframes 的实际位置一致。