{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " \n", " \n", " \n", " \n", "
\n", " \n", " \n", " \n", " \n", "

Bokeh Tutorial

\n", "
\n", "\n", "

09. Geographic Plots

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from bokeh.io import output_notebook, show\n", "output_notebook()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is often useful to be able to relate datasets with their real-world context. You can plot geographic data just like any other type of data, as in the [Texas Unemployment example](https://docs.bokeh.org/en/latest/docs/gallery/texas.html), but Bokeh also provides several specialized mechanisms for plotting data in geographic coordinates:\n", "\n", "* [GMapPlot](https://docs.bokeh.org/en/latest/docs/user_guide/geo.html#google-maps-support): Bokeh Plots on top of Google Maps\n", "\n", "* [TileSource](https://docs.bokeh.org/en/latest/docs/user_guide/geo.html#tile-providers), especially WMTSTileSource: allows data to be overlaid on data from any map tile server, including \n", "[Google Maps](http://maps.google.com), \n", "[Stamen](http://maps.stamen.com), \n", "[MapQuest](https://www.mapquest.com/), \n", "[OpenStreetMap](https://www.openstreetmap.org), \n", "[ESRI](http://www.esri.com), \n", "and custom servers.\n", "\n", "* [GeoJSONDataSource](https://docs.bokeh.org/en/latest/docs/user_guide/geo.html#geojson-datasource): Allows reading data in [GeoJSON](http://geojson.org/) format for use with Bokeh plots and glyphs, similar to `ColumnDataSource`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Google Maps plots\n", "\n", "Bokeh can render its own interactive glyphs on top of a Google Maps underlay. To use this functionality, call the `gmap` method on a standard figure as shown below. You will need to supply a valid Google API key for the plot to function, see: https://developers.google.com/maps/documentation/javascript/get-api-key" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "from bokeh.models import GMapOptions\n", "from bokeh.plotting import gmap\n", "\n", "map_options = GMapOptions(lat=30.2861, lng=-97.7394, map_type=\"roadmap\", zoom=11)\n", "\n", "# Replace the value below with your personal API key:\n", "api_key = os.environ[\"GOOGLE_API_KEY\"]\n", "\n", "p = gmap(api_key, map_options, title=\"Austin\")\n", "\n", "data = dict(lat=[ 30.29, 30.20, 30.29],\n", " lon=[-97.70, -97.74, -97.78])\n", "\n", "p.circle(x=\"lon\", y=\"lat\", size=15, fill_color=\"blue\", fill_alpha=0.8, source=data)\n", "\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## WMTS Tile Source\n", "\n", "WTMS is the most common web standard for tiled map data, i.e. maps supplied as standard-sized image patches from which the overall map can be constructed at a given zoom level. WTMS uses Web Mercator format, measuring distances from Greenwich, England as meters north and meters west, which is easy to compute but does distort the global shape. \n", "\n", "First let's create an empty Bokeh plot covering the USA, with bounds specified in meters:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from bokeh.plotting import figure\n", "from bokeh.models import WMTSTileSource\n", "\n", "# web mercator coordinates\n", "USA = x_range,y_range = ((-13884029,-7453304), (2698291,6455972))\n", "\n", "p = figure(tools='pan, wheel_zoom', x_range=x_range, y_range=y_range, \n", " x_axis_type=\"mercator\", y_axis_type=\"mercator\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A few WTMS tile sources are already defined in `bokeh.tile_providers`, but here we'll show how to specify the interface using a format string showing Bokeh how to request a tile with the required zoom, x, and y values from a given tile provider:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "url = 'http://a.basemaps.cartocdn.com/rastertiles/voyager/{Z}/{X}/{Y}.png'\n", "attribution = \"Tiles by Carto, under CC BY 3.0. Data by OSM, under ODbL\"\n", "\n", "p.add_tile(WMTSTileSource(url=url, attribution=attribution))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you show the figure, you can then use the wheel zoom and pan tools to navigate over any zoom level, and Bokeh will request the appropriate tiles from the server and insert them at the correct locations in the plot:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That's all it takes to put map data into your plot! Of course, you'll usually want to show other data as well, or you could just use the tile server's own web address. You can now add anything you would normally use in a Bokeh plot, as long as you can obtain coordinates for it in Web Mercator format. For example:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "\n", "def wgs84_to_web_mercator(df, lon=\"lon\", lat=\"lat\"):\n", " \"\"\"Converts decimal longitude/latitude to Web Mercator format\"\"\"\n", " k = 6378137\n", " df[\"x\"] = df[lon] * (k * np.pi/180.0)\n", " df[\"y\"] = np.log(np.tan((90 + df[lat]) * np.pi/360.0)) * k\n", " return df\n", "\n", "df = pd.DataFrame(dict(name=[\"Austin\", \"NYC\"], lon=[-97.7431,-74.0059], lat=[30.2672,40.7128]))\n", "wgs84_to_web_mercator(df)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p = figure(tools='pan, wheel_zoom', x_range=x_range, y_range=y_range, \n", " x_axis_type=\"mercator\", y_axis_type=\"mercator\")\n", "\n", "p.add_tile(WMTSTileSource(url=url, attribution=attribution))\n", "\n", "p.circle(x=df['x'], y=df['y'], fill_color='orange', size=10)\n", "show(p)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# EXERCISE: find some data in lat, lon (e.g. at http://data.gov), \n", "# import it into a dataframe or data source, and add it on the map above.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Next Section" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Click on this link to go to the next notebook: [10 - Exporting and Embedding](10%20-%20Exporting%20and%20Embedding.ipynb).\n", "\n", "To go back to the overview, click [here](00%20-%20Introduction%20and%20Setup.ipynb)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.13" } }, "nbformat": 4, "nbformat_minor": 4 }