{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Custom filters and other infrastructure types\n", "\n", "Author: [Geoff Boeing](https://geoffboeing.com/)\n", "\n", "Use OSMnx to download and visualize a power line network and a subway system.\n", "\n", " - [Overview of OSMnx](http://geoffboeing.com/2016/11/osmnx-python-street-networks/)\n", " - [GitHub repo](https://github.com/gboeing/osmnx)\n", " - [Examples, demos, tutorials](https://github.com/gboeing/osmnx-examples)\n", " - [Documentation](https://osmnx.readthedocs.io/en/stable/)\n", " - [Journal article/citation](http://geoffboeing.com/publications/osmnx-complex-street-networks/)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import osmnx as ox\n", "%matplotlib inline\n", "ox.config(use_cache=True, log_console=True)\n", "ox.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use custom filters to fine-tune your network. OSMnx uses `network_type` presets to query for streets that allow walking, biking, driving, etc. You can override this by passing a `custom_filter` to specify specific OSM ways you want in your graph." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "place = 'Berkeley, California, USA'\n", "\n", "# only get motorway ways\n", "cf = '[\"highway\"~\"motorway\"]'\n", "G = ox.graph_from_place(place, network_type='drive', custom_filter=cf)\n", "print(len(G), 'motorway')\n", "\n", "# only get primary ways\n", "cf = '[\"highway\"~\"primary\"]'\n", "G = ox.graph_from_place(place, network_type='drive', custom_filter=cf)\n", "print(len(G), 'primary')\n", "\n", "# use the pipe (|) as 'or' operator\n", "cf = '[\"highway\"~\"motorway|primary\"]'\n", "G = ox.graph_from_place(place, network_type='drive', custom_filter=cf)\n", "print(len(G), 'motorway + primary')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# network of the canals of amsterdam\n", "place = 'Amsterdam, Netherlands'\n", "G = ox.graph_from_place(place, custom_filter='[\"waterway\"~\"canal\"]')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To download the road network for an entire country, you often need to limit your query to something like motorways only, to fit in your computer's RAM. For large queries, such as the entire nation of Belgium, OSMnx will subdivide your query into multiple server requests to download all the data, then assemble the graph." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%time\n", "# get only motorways, trunks, and their links in all of Belgium\n", "# takes a couple minutes to do all the downloading and processing\n", "# OSMnx automatically divides up the query into multiple requests to not overload server\n", "cf = '[\"highway\"~\"motorway|motorway_link|trunk|trunk_link\"]'\n", "G = ox.graph_from_place('Belgium', network_type='drive', custom_filter=cf)\n", "fig, ax = ox.plot_graph(G, node_size=0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# get NY subway rail network\n", "# note this is rail *infrastructure* and thus includes crossovers, sidings, spurs, yards, etc\n", "# for station-based rail network, you should download a station adjacency matrix elsewhere\n", "ox.settings.useful_tags_way += ['railway']\n", "G = ox.graph_from_place('New York City, New York', retain_all=False, truncate_by_edge=True,\n", " simplify=True, custom_filter='[\"railway\"~\"subway\"]')\n", "\n", "fig, ax = ox.plot_graph(G, node_size=0, edge_color='w', edge_linewidth=0.2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python (ox)", "language": "python", "name": "ox" }, "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.9.1" } }, "nbformat": 4, "nbformat_minor": 4 }