TariqShah
TariqShah

Reputation: 25

Finding the connected nodes to a specific node in osmnx graph

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)]

Upvotes: 0

Views: 787

Answers (1)

Rob Raymond
Rob Raymond

Reputation: 31226

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

Upvotes: 0

Related Questions