Open In App

Convert Node and Edge GeoDataFrames to MultiDiGraph Using OSMnx Utils_graph Module

Last Updated : 19 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Geodataframe can handle spatial data. Available node/edge shape files or geo-package layers can be loaded as GeoDataFrame. Converting them to multidigraph helps in graph analysis. In this article, we will see how to convert node and edge GeoDataFrames to a MultiDiGraph using OSMnx utils_graph Module in Python.

Syntax of osmnx.utils_graph.graph_from_gdfs() Function

This function is the inverse of graph_to_gdfs and is designed to work in conjunction with it. The function syntax is as follows.

osmnx.utils_graph.graph_from_gdfs(gdf_nodes, gdf_edges, graph_attrs=None)

Parameters:

  • gdf_nodes (geopandas.GeoDataFrame) – GeoDataFrame of graph nodes uniquely indexed by osmid. It should contains x and y coordinate columns representing node geometries.
  • gdf_edges (geopandas.GeoDataFrame) – GeoDataFrame of graph edges uniquely multi-indexed by u, v, key
  • graph_attrs (dict) – the new G.graph attribute dict. if None, use crs from gdf_edges as the only graph-level attribute (gdf_edges must have crs attribute set)

Returns: G

Return Type: networkx.MultiDiGraph

Convert Node and Edge GeoDataFrames to MultiDiGraph Using OSMnx Utils_graph Module

Below, are the explanation of how to convert node and edge GeoDataFrames to MultiDiGraph Using OSMNx utils_graph Module in Python:

Step 1: Create Node and Edge Geodataframe

Let’s create a node Geodataframe . Please make sure the node should be uniquely indexed by osmid and It should contains x and y coordinate columns representing node geometries. As Below code imports GeoPandas and sets up coordinates for Thiruvananthapuram, Kolkata, and Patna. It organizes these coordinates into a dictionary and converts it into a GeoDataFrame, setting the ‘osmid’ as the index for reference.

Python3
import geopandas as gd

# Coordinates
tvm_lat, tvm_lon = 8.50606, 76.96153
kol_lat, kol_lon = 8.88795, 76.59550
pat_lat, pat_lon = 9.2648, 76.7870

# set the node dictionary for multidigraph
node_dict = {'osmid': [955820326, 281828280, 7351760776],
             'x': [tvm_lon, kol_lon, pat_lon],
             'y': [tvm_lat, kol_lat, pat_lat]
             }
# convert node dictionary to GeoDataFrame
node_gdf = gd.GeoDataFrame(node_dict)

# set osmid as index
node_gdf = node_gdf.set_index('osmid')
print(node_gdf)

Output

                   x        y
osmid                        
955820326   76.96153  8.50606
281828280   76.59550  8.88795
7351760776  76.78700  9.26480

Step 2: Setting Up Edges in a MultiDiGraph

Now we can set an edge geodataframe. It is must that graph edges should be uniquely multi-indexed by u, v, key. As below code defines edges between nodes specified by their IDs, organizes them into a dictionary, converts them into a GeoDataFrame, and sets a multi-index.

Python3
# set the edge for multidigraph
edge_dict = {'u': [955820326, 281828280],
             'v': [281828280, 7351760776],
             'key': [0, 0]}
# convert edge dict to geodataframe
edge_gdf = gd.GeoDataFrame(edge_dict, crs=None)
# set multi index
edge_gdf = edge_gdf.set_index(['u', 'v', 'key'])

print(edge_gdf)

Output

Empty GeoDataFrame
Columns: []
Index: [(955820326, 281828280, 0), (281828280, 7351760776, 0)]

Step 3: Fetching OSM ID Using Geopy

You can get the osm id using Nominatim reverse functionality. As below code imports Nominatim from Geopy, initiate a geolocator, retrieves the OSM ID for a given latitude and longitude pair (here for Thiruvananthapuram), and extracts the OSM ID.

Python3
from geopy.geocoders import Nominatim
# fetch osm id
geolocator = Nominatim(user_agent="sample_app")
tvm_osmid = "{lat}, {lon}".format(lat=tvm_lat, lon=tvm_lon)
location = geolocator.reverse(tvm_osmid)
location.raw.get('osm_id')

Output

955820326

Step 4: Convert Geodataframe to MultiDiGraph

Below, code explain how OSMnx to convert GeoDataFrames representing nodes and edges into a MultiDiGraph, showing the coordinate reference system (CRS) attribute as EPSG:4326. It doesn’t print the MultiDiGraph object but allows access to its nodes and edges.

Python3
import osmnx as ox

# set crs attribute
graph_attrs = {"crs": "EPSG:4326"}

# convert geodataframe to multidigraph
multi_digraph = ox.utils_graph.graph_from_gdfs(
    node_gdf, edge_gdf, graph_attrs=graph_attrs)

print(multi_digraph)
multi_digraph.nodes
multi_digraph.edges

Output

MultiDiGraph with 3 nodes and 2 edges
OutMultiEdgeView([(955820326, 281828280, 0), (281828280, 7351760776, 0)])


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads