オープンストリートマップデータのプロット#

これは元々 pyvista/pyvista-support#486 に投稿されました.

osmnx をチェックアウトしてください.

まず,アドレスからグラフを生成します.

import numpy as np
import osmnx as ox

import pyvista as pv

# Alternatively, use the pickeled graph included in our examples.
from pyvista import examples

Open Street Mapサーバーから直接グラフを読み込んでください.

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

graph = examples.download_osmnx_graph()
/home/runner/work/pyvista-docs-dev-ja/pyvista-docs-dev-ja/pyvista-doc-translations/pyvista/pyvista/examples/downloads.py:3932: UserWarning: Unpickling a shapely <2.0 geometry object. Please save the pickle again; shapely 2.1 will not have this compatibility.
  return pickle.load(open(filename, 'rb'))

次に, pyvista.lines_from_points() を使って,エッジを pyvista の線に変換します.

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

# convert each edge into a line
for _, 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)

最後に,ラインをマージしてプロットします

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.647 seconds)

Sphinx-Galleryによるギャラリー