{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Geojson in python\n", "- https://pypi.python.org/pypi/geojson/\n", "\n", "# Bokeh with geojson\n", "- http://bokeh.pydata.org/en/0.11.1/docs/user_guide/geo.html" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Bokeh with geojson (sample)\n", "- requires bokeh > 0.11.1\n", "- iPython > 4.0.0\n", "- jupyter\n", " - [ref](https://oyama999.wordpress.com/2015/09/12/snow-leopard-ipython-jupyter/)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from bokeh.io import output_notebook" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", " \n", "
\n", " \n", " BokehJS successfully loaded.\n", "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "output_notebook()" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from bokeh.io import output_file, show\n", "from bokeh.models import (\n", " GMapPlot, GMapOptions, ColumnDataSource, Circle, DataRange1d, PanTool, WheelZoomTool, BoxSelectTool\n", ")\n", "\n", "map_options = GMapOptions(lat=30.29, lng=-97.73, map_type=\"roadmap\", zoom=11)\n", "\n", "plot = GMapPlot(\n", " x_range=DataRange1d(), y_range=DataRange1d(), map_options=map_options, title=\"Austin\"\n", ")\n", "\n", "source = ColumnDataSource(\n", " data=dict(\n", " lat=[30.29, 30.20, 30.29],\n", " lon=[-97.70, -97.74, -97.78],\n", " )\n", ")\n", "\n", "circle = Circle(x=\"lon\", y=\"lat\", size=15, fill_color=\"blue\", fill_alpha=0.8, line_color=None)\n", "plot.add_glyph(source, circle)\n", "\n", "plot.add_tools(PanTool(), WheelZoomTool(), BoxSelectTool())\n", "show(plot)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from bokeh.io import show\n", "from bokeh.models import GeoJSONDataSource\n", "from bokeh.plotting import figure\n", "from bokeh.sampledata.sample_geojson import geojson\n", "\n", "geo_source = GeoJSONDataSource(geojson=geojson)\n", "\n", "p = figure()\n", "p.circle(x='x', y='y', alpha=0.9, source=geo_source)\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "## Geojson" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from geojson import Feature, Point, GeometryCollection, LineString\n", "\n", "my_feature = Feature(geometry=Point((1.6432, -19.123)))\n", "my_other_feature = Feature(geometry=Point((-80.234, -22.532)))\n", "my_line = LineString([(1.6432, -19.123), (-80.234, -22.532)])\n", "gc = GeometryCollection([my_feature, my_other_feature, my_line]) # doctest: +ELLIPSIS" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\"type\": \"GeometryCollection\", \"geometries\": [{\"geometry\": {\"type\": \"Point\", \"coordinates\": [1.6432, -19.123]}, \"type\": \"Feature\", \"properties\": {}}, {\"geometry\": {\"type\": \"Point\", \"coordinates\": [-80.234, -22.532]}, \"type\": \"Feature\", \"properties\": {}}, {\"type\": \"LineString\", \"coordinates\": [[1.6432, -19.123], [-80.234, -22.532]]}]}\n" ] } ], "source": [ "import geojson as gj\n", "\n", "dump = gj.dumps(gc)\n", "print(dump)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "geo_source = GeoJSONDataSource(geojson=dump)\n", "p = figure()\n", "p.circle(x='x', y='y', alpha=0.9, source=geo_source, size=10)\n", "p.line(x='x', y='y', source=geo_source, line_width=2)\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "- bokehがちょっと挙動おかしい\n", " - グラフが表示されないことがままある\n", "- 特に、GoogleMapは1回目の実行はうまくいくけど2回目以降は表示されない\n", " - なんかこの辺はAPIとかと関係してそうで修正難しそう" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 }