Calculate Undirected Graph's Orientation Entropy Using OSMnx Bearing Module
Last Updated :
18 Mar, 2024
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 nearly concentrated to a point. In this article, we will calculate the Undirected graph's Orientation entropy using OSMnx bearing Module in Python.
Syntax of osmnx.bearing.orientation_entropy() Function
Orientation entropy is the entropy of its edges’ bidirectional bearings across evenly spaced bins. Ignores self-loop edges as their bearings are undefined.
osmnx.bearing.orientation_entropy(Gu, num_bins=36, min_length=0, weight=None)
Parameters:
- Gu (networkx.MultiGraph) – undirected, unprojected graph with bearing attributes on each edge
- num_bins (int) – number of bins; for example, if num_bins=36 is provided, then each bin will represent 10 degrees around the compass
- min_length (float) – ignore edges with length attributes less than min_length; useful to ignore the noise of many very short edges
- weight (string) – if not None, weight edges’ bearings by this (non-null) edge attribute.
Returns: entropy – the graph’s orientation entropy
Return Type: Float
Calculate Undirected Graph's Orientation Entropy Using OSMnx Bearing Module
Below, are the step-by-step explanation of how to calculate undirected graph's orientation entrophy using OSMnx bearing module in Python:
Step 1: Visualize Street Network
To better understand city street orientation entropy, we can consider two popular cities - Thiruvananthapuram and Chicago. Let's plot the layout of Thiruvananthapuram and Chicago. Sample code shown below:
Python3
import osmnx as ox
# plot chicago
chicago_mdigraph = ox.graph_from_place('Chicago')
ox.plot_graph(chicago_mdigraph)
Output:
Comparison Chicago-ThiruvananthapuramStep 2: Convert to Undirected Graph and Add Edge Bearings
As a next step, let's add all the bearings (and reciprocals) for all the edges in a city and divide them into 36 equal sized bins (each bin represents 10°).
bearing reciprocal: if the bearing from u to v is 90°, then we additionally add a bearing of 270° since the one-dimensional street centerline points in both directions.
Let's take the multidigraph of Chicago. Convert the directed graph to the undirected graph and add edge bearing. Sample code as shown below:
Python3
# convert directed to undirected graph
chicago_undirected_graph = ox.utils_graph.get_undirected(chicago_mdigraph)
# add edge bearing
chicago_mdigr_bearing = ox.bearing.add_edge_bearings(
chicago_undirected_graph, precision=None)
# get bearing
ch_bearings = chicago_mdigr_bearing.edges(data="bearing")
Output:
MultiEdgeDataView([(701660, 1223297118, 89.0), (701660, 8693882986, 82.7),
(702090, 261263104, 286.8), (702090, 1223297118, 280.8), (702090, 266800212, 102.5),
(20217442, 6423774545, 76.0), (25779161, 739968328, 161.2), (25779161, 26703891, 357.3),
(25779161, 9233026648, 347.1), (25779173, 709393016, 116.5), (25779173, 25779174, 130.2),
(25779173, 309816217, 327.1), ...])
Let check the number of bearings:
Python3
Output:
428965
Our entropy calculation function avoid self loop. So only bearings without self loop (u!=v) are considered. If min_length is provided, then the length of edges should be greater than the minimum length. If weight parameter is set, then the bearing data extends based on the weight for each edges (the edges must be weighted).
Python3
bearings = []
min_length = 0
weight = None
for u, v, data in chicago_mdigr_bearing.edges(data=True):
# ignore self-loops and checks minimum length
if u != v and data["length"] >= min_length:
if weight:
# weight edges' bearings by some edge attribute value
bearings.extend([data["bearing"]] * int(data[weight]))
else:
# don't weight bearings, just take one value per edge
bearings.append(data["bearing"])
len(bearings)
Output:
428066
Step 3: Orientation Distribution in Street Network
We can plot polar histogram (36 bins) to compare Chicago and Thiruvananthapuram. below code generates a polar histogram to visualize the orientation distribution of edges in the Chicago street network using OSMnx, with parameters specifying the number of bins, figure size, color, and other aesthetic properties.
Python3
# polar histogram
ox.plot.plot_orientation(chicago_mdigr_bearing, num_bins=36, min_length=0,
weight=None, ax=None, figsize=(5, 5), area=True,
color='#003366', edgecolor='k', linewidth=0.5,
alpha=0.7, title=None, title_y=1.05, title_font=None,
xtick_font=None)
Output:
Polar histogramFrom the above polar histogram, we can conclude that the majority of streets in Chicago falls into just 4 bins centered on 0°, 90°, 180°, and 270°. Meanwhile, Thiruvananthapuram has nearly uniform distributions of street orientations around the compass.
Step 4: Calculate Orientation Entropy
Orientation Entropy for Chicago
Now we are Calculating orientation entropy for the Chicago street network using OSMnx with specified parameters.
Python
# orientation entropy for chicago
chicaho_entropy = ox.bearing.orientation_entropy(
chicago_mdigr_bearing, num_bins=36, min_length=0, weight=None)
Output:
2.24709579954578
Orientation Entropy for Thiruvananthapuram
Now we are Calculating orientation entropy for the Thiruvananthapuram street network using OSMnx with specified parameters.
Python3
# orientation entropy for Thiruvananthapuram
tvm_entropy = ox.bearing.orientation_entropy(
tvm_mdigr_bearing, num_bins=36, min_length=0, weight=None)
Output:
3.565979382949194
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
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
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
Operations on Graph and Special Graphs using Networkx module | Python This article shows how to create an undirected Graph. This article continues for other types of Graphs and visualization techniques. The basic Graph operations are as follows: Getting Subgraph from a Graph : Given a Graph, if we are given a subset of its set of nodes, we can create a Subgraph by sel
8 min read
Operations on Graph and Special Graphs using Networkx module | Python This article shows how to create an undirected Graph. This article continues for other types of Graphs and visualization techniques. The basic Graph operations are as follows: Getting Subgraph from a Graph : Given a Graph, if we are given a subset of its set of nodes, we can create a Subgraph by sel
8 min read
Add Compass Bearing Attribute to Graph Edges Using OSMnx Bearing Module 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_beari
3 min read