Add Compass Bearing Attribute to Graph Edges Using OSMnx Bearing Module
Last Updated :
21 Mar, 2024
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
bearing in map
Similar Reads
Calculate Compass Bearing Using Python OSMnx Bearing Module
Bearing is a significant angle for Geographic Information System (GIS) applications. In this article, we will see how to calculate the compass Bearing using the OSMnx bearing module based on lat-Ion Points. What is Bearing?Bearing represents the clockwise angle in degrees between the north and the g
3 min read
Convert Node and Edge GeoDataFrames to MultiDiGraph Using OSMnx Utils_graph Module
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
3 min read
Calculate Undirected Graph's Orientation Entropy Using OSMnx Bearing Module
The measure of entropy describes the heterogeneity of data. For street network modeling, the entropy reveals a city's street order and disorder. Cities with high orientation entropy show that the streets are spread out as much as possible. While low orientation entropy means that the streets are nea
4 min read
Difference between Graph Isomorphism and Automorphism using Python NetworkX
Graph isomorphism and automorphism are important concepts in graph theory. Graph isomorphism checks the equivalence of two graphs, while graph automorphism deals with the symmetry within a single graph. Using Python NetworkX, we can easily explore and visualize these concepts, making it a valuable t
3 min read
Convert MultiDiGraph to GeoDataFrame Using OSMnx Utils_graph Module
GeoDataFrame extends the functionality of pandas to deal with spatial data. They have special features and functions that are useful in Geographic Information Systems (GIS). In this article, we will see how we can convert MultiDiGraph to GeoDataFrame using OSMnx module. Syntax of osmnx.utils_graph.g
3 min read
Calculate Graph Total Edge Length Using Python OSMnx Stats Module
In this article, we will see how to calculate graphs' total edge length using the OSMNx stats Module. Explore urban connectivity with precision using the OSMnx stats module, which allows us to effortlessly calculate the total edge length of graphs derived from OpenStreetMap data, providing valuable
2 min read
Find the Nearest Node to a Point Using OSMnx Distance Module
Nearest neighbor search algorithms are crucial for robust navigation. OSMnx makes use of different types of nearest neighbor search algorithms for the projected and unprotected graph. In this article, we will see how we can find the nearest node to a point using OSMnx distance module in Python. Synt
7 min read
Find the Nearest Edge to a Point Using Python OSMnx Distance Module
OSMnx distance module can find the nearest edge to a point using the nearest_edges functionality. In this article, we will see how to find the nearest edge to a point using the OSMnx distance Module in Python. Syntax of osmnx.distance.nearest_edges() FunctionHere, the below function uses an R-tree s
5 min read
Calculate Average Street Circuity Using OSMnx stats Module
Circuity is defined as a lack of straightforwardness and it is a result of a circulation network configuration. It impacts how we use urban space for settlement and travel. Calculating the average circuity of a city provides insights about direct routes in a transportation network. In this article,
2 min read
Ladder Graph Using Networkx Module in Python
In this article, we are going to see the ladder graph using Python. It is a graph that looks like ladders used commonly with every node attached to two other nodes in a specific manner. We can obtain a ladder graph by joining two-path graphs of n nodes each by each node connected with a correspondin
2 min read