在OSMNX图中找到连接的节点到特定节点

发布于 2025-01-20 08:31:15 字数 1362 浏览 1 评论 0原文

我使用 osmnx 创建了一个图表。并检索该图中存在的所有节点。现在我想找到到特定节点的连接节点。我想知道osmnx或networkx中是否有任何方法可以做到这一点?或任何其他方式。我的代码如下。

import pandas as pd
import geopandas as gpd
import osmnx  as ox

#Defining top corners
top =  gpd.GeoDataFrame(columns = ['name', 'geometry'], crs = 4326, geometry = 'geometry')
top.at[0, 'geometry'] = Point(100.49209048590119,13.808722580927133)
top.at[0, 'name'] = 'tl'
top.at[1, 'geometry'] = Point(100.58494841499845, 13.809076204778961)
top.at[1, 'name'] = 'tr'


# Defining Bottom corners
bottom =  gpd.GeoDataFrame(columns = ['name', 'geometry'], crs = 4326, geometry = 'geometry')
bottom.at[0, 'geometry'] = Point(100.49141790672476,13.714053001208732)
bottom.at[0, 'name'] = 'bl'
bottom.at[1, 'geometry'] = Point(100.58476136747744, 13.717826488187361)
bottom.at[1, 'name'] = 'br'

#creating road network
combined = top.append(bottom)
convex = combined.unary_union.convex_hull
graph_extent = convex.buffer(0.02)
graph = ox.graph_from_polygon(graph_extent, network_type= "drive",custom_filter='["highway"!~"|secondary|residential|unclassified|tertiary"]')

#saving projection
graph_proj = ox.project_graph(graph)
edges = ox.graph_to_gdfs(graph_proj, nodes=False)
CRS = edges.crs

nodes = ox.graph_to_gdfs(graph_proj, edges=False)
node_specific = [(100.4923576731243,13.70365525026876),(100.4721408793079,13.72276049159698)]

I have created a graph using osmnx. and retrieved all the nodes that exist in that graph. now I want to find the connecting nodes to a specific node. I wonder if there is any method in osmnx or networkx to do so? or any other way. my code is as below.

import pandas as pd
import geopandas as gpd
import osmnx  as ox

#Defining top corners
top =  gpd.GeoDataFrame(columns = ['name', 'geometry'], crs = 4326, geometry = 'geometry')
top.at[0, 'geometry'] = Point(100.49209048590119,13.808722580927133)
top.at[0, 'name'] = 'tl'
top.at[1, 'geometry'] = Point(100.58494841499845, 13.809076204778961)
top.at[1, 'name'] = 'tr'


# Defining Bottom corners
bottom =  gpd.GeoDataFrame(columns = ['name', 'geometry'], crs = 4326, geometry = 'geometry')
bottom.at[0, 'geometry'] = Point(100.49141790672476,13.714053001208732)
bottom.at[0, 'name'] = 'bl'
bottom.at[1, 'geometry'] = Point(100.58476136747744, 13.717826488187361)
bottom.at[1, 'name'] = 'br'

#creating road network
combined = top.append(bottom)
convex = combined.unary_union.convex_hull
graph_extent = convex.buffer(0.02)
graph = ox.graph_from_polygon(graph_extent, network_type= "drive",custom_filter='["highway"!~"|secondary|residential|unclassified|tertiary"]')

#saving projection
graph_proj = ox.project_graph(graph)
edges = ox.graph_to_gdfs(graph_proj, nodes=False)
CRS = edges.crs

nodes = ox.graph_to_gdfs(graph_proj, edges=False)
node_specific = [(100.4923576731243,13.70365525026876),(100.4721408793079,13.72276049159698)]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小帐篷 2025-01-27 08:31:15

已经简化了您的代码来生成凸形船体。

连接的节点 - 不确定您

  • 在图中定义最近的节点
    • 使用 geopandas sjoin()
    • 使用 OSMNX 最近的_nodes()
    • 显示了

  • osmnx
    • 使用 osmnx Shortest_path()
    • 显示了

  • 所有这些都在分层 folium 可视化
import pandas as pd
import geopandas as gpd
import osmnx as ox
from shapely.geometry import Point, box, MultiPoint
import numpy as np
import folium

# creating road network
convex = MultiPoint(
    [
        Point(100.49209048590119, 13.808722580927133),
        Point(100.58494841499845, 13.809076204778961),
        Point(100.49141790672476, 13.714053001208732),
        Point(100.58476136747744, 13.717826488187361),
    ]
).convex_hull
graph_extent = convex.buffer(0.02)
graph = ox.graph_from_polygon(
    graph_extent,
    network_type="drive",
    custom_filter='["highway"!~"|secondary|residential|unclassified|tertiary"]',
)

# saving projection
graph_proj = ox.project_graph(graph)
edges = ox.graph_to_gdfs(graph_proj, nodes=False)
CRS = edges.crs

nodes = ox.graph_to_gdfs(graph_proj, edges=False)
node_specific = [
    (100.4923576731243, 13.70365525026876),
    (100.4721408793079, 13.72276049159698),
]

# convert specific points to geodataframe and project to same CRS as projected graph
ns = np.array(node_specific)
gdf_node_specific = gpd.GeoDataFrame(
    geometry=gpd.points_from_xy(ns[:, 0], ns[:, 1]), crs="epsg:4386"
).to_crs(CRS)

# use geopandas to find nearest nodes in graph
gdf_connected = gdf_node_specific.sjoin_nearest(nodes)

# use osmnx to find nearest nodes in graph
orig, dest = ox.nearest_nodes(
    graph_proj, gdf_node_specific["geometry"].x, gdf_node_specific["geometry"].y
)

# visualise
m = nodes.explore(name="all nodes", height=300, width=500)
m = gdf_connected.explore(m=m, color="red", name="points")
# nodes on shortest path
nodes.loc[ox.shortest_path(graph_proj, orig, dest, weight="length")].explore(
    m=m, color="green", name="shortest path"
)
folium.LayerControl().add_to(m)
m

Have simplified your code for generating a convex hull of points.

Connected nodes - not sure which you are defining

  • nearest nodes in graph
    • have shown using geopandas sjoin()
    • have shown using osmnx nearest_nodes()
  • shortest path between nearest nodes in graph
    • have shown using osmnx shortest_path()
  • all of these I have visualised on a layered folium visualisation
import pandas as pd
import geopandas as gpd
import osmnx as ox
from shapely.geometry import Point, box, MultiPoint
import numpy as np
import folium

# creating road network
convex = MultiPoint(
    [
        Point(100.49209048590119, 13.808722580927133),
        Point(100.58494841499845, 13.809076204778961),
        Point(100.49141790672476, 13.714053001208732),
        Point(100.58476136747744, 13.717826488187361),
    ]
).convex_hull
graph_extent = convex.buffer(0.02)
graph = ox.graph_from_polygon(
    graph_extent,
    network_type="drive",
    custom_filter='["highway"!~"|secondary|residential|unclassified|tertiary"]',
)

# saving projection
graph_proj = ox.project_graph(graph)
edges = ox.graph_to_gdfs(graph_proj, nodes=False)
CRS = edges.crs

nodes = ox.graph_to_gdfs(graph_proj, edges=False)
node_specific = [
    (100.4923576731243, 13.70365525026876),
    (100.4721408793079, 13.72276049159698),
]

# convert specific points to geodataframe and project to same CRS as projected graph
ns = np.array(node_specific)
gdf_node_specific = gpd.GeoDataFrame(
    geometry=gpd.points_from_xy(ns[:, 0], ns[:, 1]), crs="epsg:4386"
).to_crs(CRS)

# use geopandas to find nearest nodes in graph
gdf_connected = gdf_node_specific.sjoin_nearest(nodes)

# use osmnx to find nearest nodes in graph
orig, dest = ox.nearest_nodes(
    graph_proj, gdf_node_specific["geometry"].x, gdf_node_specific["geometry"].y
)

# visualise
m = nodes.explore(name="all nodes", height=300, width=500)
m = gdf_connected.explore(m=m, color="red", name="points")
# nodes on shortest path
nodes.loc[ox.shortest_path(graph_proj, orig, dest, weight="length")].explore(
    m=m, color="green", name="shortest path"
)
folium.LayerControl().add_to(m)
m

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文