{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Plot Open Street Map Data {#open_street_map_example}\n\nThis was originally posted to\n[pyvista/pyvista-support#486](https://github.com/pyvista/pyvista-support/issues/486).\n\nBe sure to check out [osmnx](https://github.com/gboeing/osmnx)\n\nStart by generating a graph from an address.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport osmnx as ox\n\nimport pyvista as pv\n\n# Alternatively, use the pickeled graph included in our examples.\nfrom pyvista import examples"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Read in the graph directly from the Open Street Map server.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# address = 'Holzgerlingen DE'\n# graph = ox.graph_from_address(address, dist=500, network_type='drive')\n# pickle.dump(graph, open('/tmp/tmp.p', 'wb'))\n\ngraph = examples.download_osmnx_graph()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Next, convert the edges into pyvista lines using\n`pyvista.lines_from_points`{.interpreted-text role=\"func\"}.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nodes, edges = ox.graph_to_gdfs(graph)\nlines = []\n\n# convert each edge into a line\nfor _, row in edges.iterrows():\n    x_pts = row['geometry'].xy[0]\n    y_pts = row['geometry'].xy[1]\n    z_pts = np.zeros(len(x_pts))\n    pts = np.column_stack((x_pts, y_pts, z_pts))\n    line = pv.lines_from_points(pts)\n    lines.append(line)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Finally, merge the lines and plot\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "combined_lines = lines[0].merge(lines[1:])\ncombined_lines.plot(line_width=3, cpos='xy')"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.12.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}