Open In App

Calculate Undirected Graph’s Orientation Entropy Using OSMnx Bearing Module

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

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:

chicago_tvm_layout

Comparison Chicago-Thiruvananthapuram

Step 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
len(ch_bearings)

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:

chicago_tvm_polar_hist

Polar histogram

From 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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads