Plot Open Street Map Data

This was originally posted to pyvista/pyvista-support#486.

Be sure to check out osmnx

Start by generating a graph from an address.

import osmnx as ox
import numpy as np

import pyvista as pv

Out:

/home/alex/.local/lib/python3.8/site-packages/geopandas/_compat.py:106: UserWarning: The Shapely GEOS version (3.8.0-CAPI-1.13.1 ) is incompatible with the GEOS version PyGEOS was compiled with (3.9.1-CAPI-1.14.2). Conversions between both will be slow.
  warnings.warn(

Read in the graph directly from the Open Street Map server.

# address = 'Holzgerlingen DE'
# graph = ox.graph_from_address(address, dist=500, network_type='drive')
# pickle.dump(graph, open('/tmp/tmp.p', 'wb'))

# Alternatively, use the pickeled graph included in our examples.
from pyvista import examples
graph = examples.download_osmnx_graph()

Next, convert the edges into pyvista lines using pyvista.lines_from_points().

nodes, edges = ox.graph_to_gdfs(graph)
lines = []

# convert each edge into a line
for idx, row in edges.iterrows():
    x_pts = row['geometry'].xy[0]
    y_pts = row['geometry'].xy[1]
    z_pts = np.zeros(len(x_pts))
    pts = np.column_stack((x_pts, y_pts, z_pts))
    line = pv.lines_from_points(pts)
    lines.append(line)

Finally, merge the lines and plot

combined_lines = lines[0].merge(lines[1:])
combined_lines.plot(line_width=3, cpos='xy')
osmnx example

Total running time of the script: ( 0 minutes 0.399 seconds)

Gallery generated by Sphinx-Gallery