Open In App

Add Compass Bearing Attribute to Graph Edges Using OSMnx Bearing Module

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

Bearing is an important attribute of street network modeling. Bearing helps to measure entropy, which reveals street order and disorder. In this article, we will see how to add a compass-bearing attribute to graph edges Using the OSMnx bearing module in Python.

Syntax of osmnx.bearing.add_edge_bearings()

The vectorized function calculates bearing from the origin node to the destination node for each edge in a directed, unprojected graph. Then the bearings are included as new edge attributes. Below is the syntax:

osmnx.bearing.add_edge_bearings(G, precision=None)

Parameters

  • G (networkx.MultiDiGraph) – unprojected graph
  • precision (int) – deprecated, do not use

Returns : G – graph with edge bearing attributes

Return Type : networkx.MultiDiGraph

Add Compass Bearing Attribute to Graph Edges Using OSMnx Bearing Module

Below, are the example of how to add compass bearing attribute to graph edges using OSMnx bearing Module in Python:

Step 1: Fetching OSM ID

In below code , we are Using Nominatim from geopy, coordinates for Thiruvananthapuram (8.50606, 76.96153) are transformed into a human-readable address. By setting a user agent for Nominatim, the reverse geolocation retrieves location information.

Python3
from geopy.geocoders import Nominatim

# Thiruvananthapuram Coordinates
tvm_lat, tvm_lon = 8.50606, 76.96153

# Nominatim for reverse geolocator
geolocator = Nominatim(user_agent="sample_app")
tvm_osmid = "{lat}, {lon}".format(lat=tvm_lat, lon=tvm_lon)
location = geolocator.reverse(tvm_osmid)

# fetch osm id
location.raw.get('osm_id')

Output

955820326

Step 2: Create NetworkX MultiDiGraph

Now it’s time to create the multidigraph and add the edge bearings for the multidigraph. We will take the coordinates for Thiruvananthapuram, Kollam, and Pathanamthitta. You can get the osmid for each coordinate based on the geolocator.reverse functionality.

Python3
import networkx as nx

# set multidigraph
G = nx.MultiDiGraph(crs="EPSG:4326")

# add each node based on osmid
tvm_osmid, kol_osmid, pat_osmid = 955820326, 281828280, 7351760776
G.add_nodes_from([tvm_osmid, kol_osmid, pat_osmid])

# add coordinates for each node
tvm_lat, tvm_lon = 8.50606, 76.96153
kol_lat, kol_lon = 8.88795, 76.59550
pat_lat, pat_lon = 9.2648, 76.7870
G.nodes[tvm_osmid].update({'osmid': tvm_osmid, 'x': tvm_lon, 'y': tvm_lat})
G.nodes[kol_osmid].update({'osmid': kol_osmid, 'x': kol_lon, 'y': kol_lat})
G.nodes[pat_osmid].update({'osmid': pat_osmid, 'x': pat_lon, 'y': pat_lat})

# add edges
G.add_edges_from([(tvm_osmid, kol_osmid), (kol_osmid, pat_osmid)])

# print nodes
G.nodes
# print edges
G.edges

Output

OutMultiEdgeView([(955820326, 281828280, 0), (281828280, 7351760776, 0)])

Step 3: Add the Edge bearings

Below code add the edge bearings for the created multidigraph. OSMnx integrates edge bearings into the multidigraph. Extracted bearing data for each edge is visualized through a graph plot.

Python3
import osmnx as ox

# add edge bearing
mdigr_bearing = ox.bearing.add_edge_bearings(G, precision=None)
# get added bearing for edges
mdigr_bearing.edges(data="bearing")

Output

OutMultiEdgeDataView([(955820326, 281828280, 316.6), (281828280, 7351760776, 26.6)])

Step 4: Convert Multidigraph Edges to GeoDataFrame

The edges of the multidigraph, enhanced with bearing information, are transformed into a GeoDataFrame. This GeoDataFrame is then visualized on a map.

Python3
# convert the edges of multidigrah to geodataframe
geodf_edge = ox.utils_graph.graph_to_gdfs(
    mdigr_bearing, nodes=False, edges=True,
    node_geometry=False, fill_edge_geometry=True)

# plot in map
geodf_edge.explore()

Output

add_edge_bearing_map

bearing in map



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads