{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# How to draw a GeoPandas.GeoDataFrame into folium\n", "\n", "GeoPandas is a project to add support for geographic data to [pandas](http://pandas.pydata.org) objects.\n", "(See https://github.com/geopandas/geopandas)\n", "\n", "It provides (among other cool things) a `GeoDataFrame` object that represents a Feature collection.\n", "When you have one, you may be willing to use it on a folium map. Here's the simplest way to do so." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'../folium/__init__.py'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import geopandas\n", "\n", "import sys\n", "sys.path.insert(0,'..')\n", "\n", "import folium\n", "folium.__file__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this example, we'll use the same file as GeoPandas demo ; it's containing\n", "[the boroughs of New York City](http://www.nyc.gov/html/dcp/download/bytes/nybb_14aav.zip)." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
BoroCodeBoroNameShape_AreaShape_Lenggeometry
05Staten Island1.623847e+09330454.175933(POLYGON ((970217.0223999023 145643.3322143555...
13Brooklyn1.937810e+09741227.337073(POLYGON ((1021176.479003906 151374.7969970703...
24Queens3.045079e+09896875.396449(POLYGON ((1029606.076599121 156073.8142089844...
31Manhattan6.364308e+08358400.912836(POLYGON ((981219.0557861328 188655.3157958984...
42Bronx1.186822e+09464475.145651(POLYGON ((1012821.805786133 229228.2645874023...
\n", "
" ], "text/plain": [ " BoroCode BoroName Shape_Area Shape_Leng \\\n", "0 5 Staten Island 1.623847e+09 330454.175933 \n", "1 3 Brooklyn 1.937810e+09 741227.337073 \n", "2 4 Queens 3.045079e+09 896875.396449 \n", "3 1 Manhattan 6.364308e+08 358400.912836 \n", "4 2 Bronx 1.186822e+09 464475.145651 \n", "\n", " geometry \n", "0 (POLYGON ((970217.0223999023 145643.3322143555... \n", "1 (POLYGON ((1021176.479003906 151374.7969970703... \n", "2 (POLYGON ((1029606.076599121 156073.8142089844... \n", "3 (POLYGON ((981219.0557861328 188655.3157958984... \n", "4 (POLYGON ((1012821.805786133 229228.2645874023... " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "boros = geopandas.GeoDataFrame.from_file('nybb.shp')\n", "boros" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To create a map with these features, simply put them in a `GeoJson`:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map([40.7,-74], zoom_start=10, tiles='cartodbpositron')\n", "\n", "folium.GeoJson(boros).add_to(m)\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Quite easy.\n", "\n", "Well, you can also take advantage of your `GeoDataFrame` structure to set the style of the data. For this, just create a column `style` containing each feature's style in a dictionnary." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "boros['style'] = [\n", " {'fillColor' : '#ff0000', 'weight' : 2, 'color' : 'black'},\n", " {'fillColor' : '#00ff00', 'weight' : 2, 'color' : 'black'},\n", " {'fillColor' : '#0000ff', 'weight' : 2, 'color' : 'black'},\n", " {'fillColor' : '#ffff00', 'weight' : 2, 'color' : 'black'},\n", " {'fillColor' : '#00ffff', 'weight' : 2, 'color' : 'black'},\n", " ]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
BoroCodeBoroNameShape_AreaShape_Lenggeometrystyle
05Staten Island1.623847e+09330454.175933(POLYGON ((970217.0223999023 145643.3322143555...{'weight': 2, 'fillColor': '#ff0000', 'color':...
13Brooklyn1.937810e+09741227.337073(POLYGON ((1021176.479003906 151374.7969970703...{'weight': 2, 'fillColor': '#00ff00', 'color':...
24Queens3.045079e+09896875.396449(POLYGON ((1029606.076599121 156073.8142089844...{'weight': 2, 'fillColor': '#0000ff', 'color':...
31Manhattan6.364308e+08358400.912836(POLYGON ((981219.0557861328 188655.3157958984...{'weight': 2, 'fillColor': '#ffff00', 'color':...
42Bronx1.186822e+09464475.145651(POLYGON ((1012821.805786133 229228.2645874023...{'weight': 2, 'fillColor': '#00ffff', 'color':...
\n", "
" ], "text/plain": [ " BoroCode BoroName Shape_Area Shape_Leng \\\n", "0 5 Staten Island 1.623847e+09 330454.175933 \n", "1 3 Brooklyn 1.937810e+09 741227.337073 \n", "2 4 Queens 3.045079e+09 896875.396449 \n", "3 1 Manhattan 6.364308e+08 358400.912836 \n", "4 2 Bronx 1.186822e+09 464475.145651 \n", "\n", " geometry \\\n", "0 (POLYGON ((970217.0223999023 145643.3322143555... \n", "1 (POLYGON ((1021176.479003906 151374.7969970703... \n", "2 (POLYGON ((1029606.076599121 156073.8142089844... \n", "3 (POLYGON ((981219.0557861328 188655.3157958984... \n", "4 (POLYGON ((1012821.805786133 229228.2645874023... \n", "\n", " style \n", "0 {'weight': 2, 'fillColor': '#ff0000', 'color':... \n", "1 {'weight': 2, 'fillColor': '#00ff00', 'color':... \n", "2 {'weight': 2, 'fillColor': '#0000ff', 'color':... \n", "3 {'weight': 2, 'fillColor': '#ffff00', 'color':... \n", "4 {'weight': 2, 'fillColor': '#00ffff', 'color':... " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "boros" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map([40.7,-74], zoom_start=10, tiles='cartodbpositron')\n", "\n", "folium.GeoJson(boros).add_to(m)\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Conclusion" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That's all folks !\n", "\n", "Hope it'll be useful to you. Don't hesitate to provide a feedback on what can be improved, which method do you prefer, etc." ] } ], "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.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }