{ "metadata": { "name": "", "signature": "sha256:26f9c5115f67d82016ff64d0bb66b8f16ab725c0df35c7c424618e520210cbef" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "A promising route for using leaflet.js maps in the IPython notebook -- use [Folium: Python Data. Leaflet.js Maps. \u2014 Folium 0.1.2 documentation](https://folium.readthedocs.org/en/latest/). Easiest way to install Folium:\n", "\n", " pip install folium\n", "\n", "This notebook is a tiny modification of http://nbviewer.ipython.org/gist/bburky/7763555/folium-ipython.ipynb. (See https://gist.github.com/bburky/7763555) Specifically, I host the us_counties_20m_topo.json file on my server. I confirm that this notebook works in IPython 2.0 " ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import HTML\n", "import folium" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "def inline_map(map):\n", " \"\"\"\n", " Embeds the HTML source of the map directly into the IPython notebook.\n", " \n", " This method will not work if the map depends on any files (json data). Also this uses\n", " the HTML5 srcdoc attribute, which may not be supported in all browsers.\n", " \"\"\"\n", " map._build_map()\n", " return HTML(''.format(srcdoc=map.HTML.replace('\"', '"')))\n", "\n", "def embed_map(map, path=\"map.html\"):\n", " \"\"\"\n", " Embeds a linked iframe to the map into the IPython notebook.\n", " \n", " Note: this method will not capture the source of the map into the notebook.\n", " This method should work for all maps (as long as they use relative urls).\n", " \"\"\"\n", " map.create_map(path=path)\n", " return HTML(''.format(path=path))" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "map = folium.Map(location=[40, -99], zoom_start=4)\n", "map.simple_marker([40.67, -73.94], popup='Add popup text here.')\n", "inline_map(map)" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "" ], "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "import pandas as pd\n", "\n", "#Grab the geojson from github\n", "#county_geo = r'us_counties_20m_topo.json'\n", "# https://gist.githubusercontent.com/wrobstory/5609959/raw/17e222ecd9e26348f50a04fa484485a0e0f54a58/us_counties_20m_topo.json\n", "county_geo = 'http://mashupguide.net/wwod14/us_counties_20m_topo.json'\n", "county_data = 'https://raw.github.com/wrobstory/folium/master/examples/data/us_county_data.csv'\n", "\n", "df = pd.read_csv(county_data, na_values=[' '])\n", "df['FIPS_Code'] = df['FIPS_Code'].astype(str)\n", "\n", "def set_id(fips):\n", " '''Modify FIPS code to match GeoJSON property'''\n", " if fips == '0':\n", " return None\n", " elif len(fips) <= 4:\n", " return ''.join(['0500000US0', fips])\n", " else:\n", " return ''.join(['0500000US', fips])\n", "\n", "#Apply set_id, drop NaN\n", "df['GEO_ID'] = df['FIPS_Code'].apply(set_id)\n", "df = df.dropna()\n", "\n", "map = folium.Map(location=[40, -99], zoom_start=4)\n", "map.geo_json(geo_path=county_geo, data_out='data2.json', data=df,\n", " columns=['GEO_ID', 'Unemployment_rate_2011'],\n", " key_on='feature.id',\n", " threshold_scale=[0, 5, 7, 9, 11, 13],\n", " fill_color='YlGnBu', line_opacity=0.3,\n", " legend_name='Unemployment Rate 2011 (%)',\n", " topojson='objects.us_counties_20m')\n", "\n", "embed_map(map)" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "" ], "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Blending folium with interact" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.html import widgets\n", "from IPython.display import display, Image, HTML, clear_output" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "# not the most interesting demo --> but a proof of concept on how we can control map using interact\n", "\n", "def plot_map(lat, long, zoom):\n", " map = folium.Map(location=[lat, long], zoom_start=zoom)\n", " map.simple_marker([lat, long], popup='lat:{lat} long:{long}'.format(lat=lat,long=long))\n", " display(inline_map(map))\n", " \n", "widgets.interact(plot_map, \n", " lat=widgets.FloatSliderWidget(min=-90,max=90,step=0.1,value=0),\n", " long=widgets.FloatSliderWidget(min=-180,max=180,step=0.1,value=0),\n", " zoom=widgets.IntSliderWidget(min=0,max=20,step=1,value=2))" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }