Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views11 pages

OSMnx Practical Exercises

The document provides practical exercises for using the OSMnx library to perform various geospatial operations, including retrieving street networks, building footprints, and shortest paths in Windhoek, Namibia. Each example includes step-by-step explanations of the code, demonstrating how to manipulate and visualize geospatial data. The exercises encourage self-learning and exploration of geospatial programming techniques.

Uploaded by

Nande Nangula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views11 pages

OSMnx Practical Exercises

The document provides practical exercises for using the OSMnx library to perform various geospatial operations, including retrieving street networks, building footprints, and shortest paths in Windhoek, Namibia. Each example includes step-by-step explanations of the code, demonstrating how to manipulate and visualize geospatial data. The exercises encourage self-learning and exploration of geospatial programming techniques.

Uploaded by

Nande Nangula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

GIS PROGRAMMING (GIP710S)

OSMnx LIBRARY

PRACTICAL EXERCISES- DIRECTED SELF LEARNING MODE


The following are some examples of how different geospatial operations can be performed
using the OSMnx library. To aid your understanding, attempt has been made to provide a
clear explanation for what every line of code in these examples is meant to do.

I hope you enjoy studying these examples and feel free to attempt the exercises provided
after the examples.

Example 1: Retrieve the street network of Windhoek

# Retrieve the street network of Windhoek

place_name = "Windhoek, Namibia"

G = ox.graph_from_place(place_name, network_type='all')

ox.plot_graph(G)

This code will retrieve the street network of Windhoek and plot it using the ox.plot_graph()
function.

Step wise explanation

1. # Retrieve the street network of Windhoek: This is a comment explaining what the
code does. Comments are lines in a code that are not executed by the Python
interpreter, but serve to provide context and explain the code to other users.

2. place_name = "Windhoek, Namibia": This line assigns a string value to the variable
place_name. The string specifies the name of the location we want to retrieve data
for, in this case "Windhoek, Namibia".

3. G = ox.graph_from_place(place_name, network_type='all'): This line calls the


graph_from_place() function from the OSMnx library, which retrieves the street
network for the specified place name. The function returns a NetworkX graph object,
which is assigned to the variable G. The network_type argument specifies what type
of network we want to retrieve - in this case, 'all' means we want to retrieve all types
of streets (e.g. highways, residential roads, etc.).

4. ox.plot_graph(G): This line calls the plot_graph() function from the OSMnx library,
which creates a plot of the street network represented by the NetworkX graph
object G. The resulting plot shows the nodes (intersections) and edges (streets) of
the street network.

Page 1 of 11
[email protected]
Example 2: Retrieve the building footprints of Windhoek

# Retrieve the building footprints of Windhoek

tags = {'building': True}

G = ox.graph_from_place(place_name, network_type='none', custom_filter=tags)

ox.plot_graph(G, node_size=0, edge_linewidth=0.1)

This code will retrieve the building footprints of Windhoek and plot them using the
ox.plot_graph() function.

Step wise explanation

1. # Retrieve the building footprints of Windhoek: This is a comment explaining what


the code does.

2. tags = {'building': True}: This line assigns a dictionary to the variable tags. The
dictionary specifies a filter that we want to apply to the OpenStreetMap data. In this
case, we only want to retrieve building footprints, so we set the 'building' key to
True.

3. G = ox.graph_from_place(place_name, network_type='none', custom_filter=tags):


This line calls the graph_from_place() function again, but this time we set
network_type to 'none' to retrieve only building footprints, and we specify a custom
filter using the custom_filter argument to retrieve only the features with the
'building' tag.

4. ox.plot_graph(G, node_size=0, edge_linewidth=0.1): This line calls the plot_graph()


function again, but this time we pass in the NetworkX graph object G that contains
only building footprints. We set node_size to 0 so that the nodes (intersections) are
not plotted, and we set edge_linewidth to a small value so that the building
footprints are visible.

Page 2 of 11
[email protected]
Example 3: Retrieve the shortest path between two points in Windhoek

# Retrieve the shortest path between two points in Windhoek

orig = ox.geocode("Jan Jonker Road, Windhoek, Namibia")

dest = ox.geocode("Maerua Mall, Windhoek, Namibia")

route = ox.shortest_path(G, orig, dest, weight='length')

ox.plot_graph_route(G, route)

This code will retrieve the shortest path between two points in Windhoek and plot it using
the ox.plot_graph_route() function.

Step wise explanation

1. # Retrieve the shortest path between two points in Windhoek: This is a comment
explaining what the code does.

2. orig = ox.geocode("Jan Jonker Road, Windhoek, Namibia"): This line calls the
geocode() function from the OSMnx library, which retrieves the latitude and
longitude coordinates of a specified address or place name. In this case, we want to
retrieve the coordinates for "Jan Jonker Road, Windhoek, Namibia", and we assign
the resulting tuple of coordinates to the variable orig.

3. dest = ox.geocode("Maerua Mall, Windhoek, Namibia"): This line is similar to the


previous line, but we retrieve the coordinates for "Maerua Mall, Windhoek,
Namibia" and assign them to the variable dest.

4. route = ox.shortest_path(G, orig, dest, weight='length'): This line calls the


shortest_path() function from the OSMnx library, which calculates the shortest path
between two points in a NetworkX graph object. We pass in the street network
graph object G, as well as the origin and destination coordinates that we retrieved in
the previous lines. We also set weight to 'length' to calculate the shortest path based
on the length of the edges (streets).

5. ox.plot_graph_route(G, route): This line calls the plot_graph_route() function from


the OSMnx library, which creates a plot of the street network with the shortest path

Page 3 of 11
[email protected]
Example 4: Retrieve the street network within a certain distance of a point in Windhoek

# Retrieve the street network within a certain distance of a point in Windhoek

center_point = (-22.570872, 17.080489)

dist = 1000 # meters

G = ox.graph_from_point(center_point, dist=dist, network_type='all')

ox.plot_graph(G)

Step wise explanation

1. # Retrieve the street network within a certain distance of a point in Windhoek: This
is a comment explaining what the code does.

2. center_point = (-22.570872, 17.080489): This line assigns a tuple of latitude and


longitude coordinates to the variable center_point. The coordinates specify the
center point of the area we want to retrieve data for.

3. dist = 1000 # meters: This line assigns a distance value in meters to the variable dist.
The value specifies the radius of the area we want to retrieve data for, measured
from the center_point.

4. G = ox.graph_from_point(center_point, dist=dist, network_type='all'): This line calls


the graph_from_point() function from the OSMnx library, which retrieves the street
network within a certain distance of a specified point. We pass in the center_point
tuple and the dist value, as well as network_type='all' to retrieve all types of streets.

5. ox.plot_graph(G): This line calls the plot_graph() function again, but this time we
pass in the NetworkX graph object G that contains only the street network within the
specified distance from the center_point. The resulting plot shows the nodes
(intersections) and edges (streets) of the street network.

Page 4 of 11
[email protected]
Example 5: Retrieve the street network of Khomasdal and plot it

# Retrieve the street network of Khomasdal and plot it

place_name = "Khomasdal, Windhoek, Namibia"

G = ox.graph_from_place(place_name, network_type="all")

ox.plot_graph(G)

Step wise explanation

1. # Retrieve the street network of Khomasdal and plot it: This is a comment explaining
what the code does.

2. place_name = "Khomasdal, Windhoek, Namibia": This line assigns a string value of


the place name to the variable place_name. The string specifies the location we
want to retrieve data for, which is the Khomasdal region of Windhoek.

3. G = ox.graph_from_place(place_name, network_type="all"): This line calls the


graph_from_place() function from the OSMnx library, which retrieves the street
network for the specified place. We pass in the place_name string and
network_type='all' to retrieve all types of streets.

4. ox.plot_graph(G): This line calls the plot_graph() function again, but this time we
pass in the NetworkX graph object G that contains the street network data for
Khomasdal. The resulting plot shows the nodes (intersections) and edges (streets) of
the street network.

Page 5 of 11
[email protected]
Example 6: Retrieve and save the street network of Khomasdal as a shapefile

# Retrieve and save the street network of Khomasdal as a shapefile

place_name = "Khomasdal, Windhoek, Namibia"

G = ox.graph_from_place(place_name, network_type="all")

ox.save_graph_shapefile(G, filename="khomasdal_network")

Step wise explanation

1. # Retrieve and save the street network of Khomasdal as a shapefile: This is a


comment explaining what the code does.

2. place_name = "Khomasdal, Windhoek, Namibia": This line assigns a string value of


the place name to the variable place_name. The string specifies the location we
want to retrieve data for, which is the Khomasdal region of Windhoek.

3. G = ox.graph_from_place(place_name, network_type="all"): This line calls the


graph_from_place() function from the OSMnx library, which retrieves the street
network for the specified place. We pass in the place_name string and
network_type='all' to retrieve all types of streets.

4. ox.save_graph_shapefile(G, filename="khomasdal_network"): This line calls the


save_graph_shapefile() function from the OSMnx library, which saves the street
network data as a shapefile to the current working directory. We pass in the
NetworkX graph object G and specify a filename of "khomasdal_network". The
resulting shapefile contains data on the nodes (intersections) and edges (streets) of
the street network

Page 6 of 11
[email protected]
Example 7: Retrieve and plot the street network of Khomasdal with only primary and
secondary roads

# Retrieve and plot the street network of Khomasdal with only primary and secondary roads

place_name = "Khomasdal, Windhoek, Namibia"

G = ox.graph_from_place(place_name, network_type="primary_secondary")

ox.plot_graph(G)

Step wise explanation

1. # Retrieve and plot the street network of Khomasdal with only primary and
secondary roads: This is a comment explaining what the code does.

2. `place_name = "Khomasdal, Windhoek, Namibia": This line assigns a string value of the
place name to the variable place_name`. The string specifies the location we want to
retrieve data for, which is the Khomasdal region of Windhoek.

3. G = ox.graph_from_place(place_name, network_type="primary_secondary"): This


line calls the graph_from_place() function from the OSMnx library, which retrieves
the street network for the specified place. We pass in the place_name string and
network_type='primary_secondary' to retrieve only primary and secondary roads.

4. ox.plot_graph(G): This line calls the plot_graph() function again, but this time we
pass in the NetworkX graph object G that contains the street network data for
Khomasdal with only primary and secondary roads. The resulting plot shows the
nodes (intersections) and edges (streets) of the street network with only primary and
secondary roads.

Page 7 of 11
[email protected]
Example 8: Retrieve and plot the street network of Katutura

# Retrieve and plot the street network of Katutura

place_name = "Katutura, Windhoek, Namibia"

G = ox.graph_from_place(place_name, network_type="all")

ox.plot_graph(G)

Step wise explanation

1. # Retrieve and plot the street network of Katutura: This is a comment explaining
what the code does.

2. place_name = "Katutura, Windhoek, Namibia": This line assigns a string value of the
place name to the variable place_name. The string specifies the location we want to
retrieve data for, which is the Katutura region of Windhoek.

3. G = ox.graph_from_place(place_name, network_type="all"): This line calls the


graph_from_place() function from the OSMnx library, which retrieves the street
network for the specified place. We pass in the place_name string and
network_type='all' to retrieve all types of streets.

4. ox.plot_graph(G): This line calls the plot_graph() function again, but this time we
pass in the NetworkX graph object G that contains the street network data for
Katutura. The resulting plot shows the nodes (intersections) and edges (streets) of
the street network.

Page 8 of 11
[email protected]
Example 9: Retrieve and plot the street network of Katutura with only one-way roads

# Retrieve and plot the street network of Katutura with only one-way roads

place_name = "Katutura, Windhoek, Namibia"

G = ox.graph_from_place(place_name, network_type="all",
infrastructure="way['oneway'='yes']")

ox.plot_graph(G)

Step wise explanation

1. # Retrieve and plot the street network of Katutura with only one-way roads: This is
a comment explaining what the code does.

2. place_name = "Katutura, Windhoek, Namibia": This line assigns a string value of the
place name to the variable place_name. The string specifies the location we want to
retrieve data for, which is the Katutura region of Windhoek.

3. G = ox.graph_from_place(place_name, network_type="all",
infrastructure="way['oneway'='yes']"): This line calls the graph_from_place()
function from the OSMnx library, which retrieves the street network for the specified
place. We pass in the place_name string, network_type='all' to retrieve all types of
streets, and infrastructure="way['oneway'='yes']" to retrieve only one-way roads.

4. ox.plot_graph(G): This line calls the plot_graph() function again, but this time we
pass in the NetworkX graph object G that contains the street network data for
Katutura with only one-way roads. The resulting plot shows the nodes (intersections)
and edges (streets) of the street network with only one-way roads.

Page 9 of 11
[email protected]
Example 10: Retrieve and plot the street network of Katutura with a custom boundary

# Retrieve and plot the street network of Katutura with a custom boundary

import geopandas as gpd

# Define a custom boundary

custom_boundary = gpd.read_file("custom_boundary.geojson")

# Retrieve and plot the street network of Katutura within the custom boundary

G = ox.graph_from_polygon(custom_boundary.geometry[0], network_type="all")

ox.plot_graph(G)

Step wise explanation

1. # Retrieve and plot the street network of Katutura with a custom boundary: This is
a comment explaining what the code does.

2. import geopandas as gpd: This line imports the Geopandas library, which we'll use
to read in the custom boundary file.

3. custom_boundary = gpd.read_file("custom_boundary.geojson"): This line reads in a


custom boundary file named custom_boundary.geojson that we'll use to retrieve
only the street network within the boundary. The file should be in GeoJSON format
and contain a single polygon that represents the boundary.

4. G = ox.graph_from_polygon(custom_boundary.geometry[0], network_type="all"):
This line calls the graph_from_polygon() function from the OSMnx library, which
retrieves the street network within the specified polygon boundary. We pass in the
polygon geometry of the custom boundary (custom_boundary.geometry[0]) and
network_type='all' to retrieve all types of streets within the boundary.

5. ox.plot_graph(G): This line calls the plot_graph() function again, but this time we
pass in the NetworkX graph object G that contains the street network data for
Katutura within the custom boundary. The resulting plot shows the nodes
(intersections) and edges (streets) of the street network within the custom
boundary.

Page 10 of 11
[email protected]
Can you try these exercises on your own? Do not bother to submit this!

1. Download and visualize the street network:

 Use OSMNx to download the OPenStreetMap and street network of


Windhoek, Namibia, and visualize the downloaded dataset using networkx or
matplotlib.

2. Calculate the basic street network metrics:

 Concentrating on Khomasdal, Calculate the basic street network metrics,


such as edge and node counts, total and average edge length, and street
density using OSMNx.

3. Plot the shortest path:

 Use OSMNx to plot the shortest path between Mairuwa Mall and Wernhill in
Windhoek.

4. Extract building footprints:

 Use OSMNx to extract building footprints for a selected area of Windhoek.

5. Perform network analysis:

 Use OSMNx to perform network analysis, such as calculating centrality


measures and finding nearest facilities, for Windhoek.

Page 11 of 11
[email protected]

You might also like