{ "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": "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": 1, "metadata": {}, "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", "
BoroCodeBoroNameShape_LengShape_Areageometry
05Staten Island330454.1759331.623847e+09MULTIPOLYGON (((970217.022 145643.332, 970227....
13Brooklyn741227.3370731.937810e+09MULTIPOLYGON (((1021176.479 151374.797, 102100...
24Queens896875.3964493.045079e+09MULTIPOLYGON (((1029606.077 156073.814, 102957...
31Manhattan358400.9128366.364308e+08MULTIPOLYGON (((981219.056 188655.316, 980940....
42Bronx464475.1456511.186822e+09MULTIPOLYGON (((1012821.806 229228.265, 101278...
\n", "
" ], "text/plain": [ " BoroCode BoroName Shape_Leng Shape_Area \\\n", "0 5 Staten Island 330454.175933 1.623847e+09 \n", "1 3 Brooklyn 741227.337073 1.937810e+09 \n", "2 4 Queens 896875.396449 3.045079e+09 \n", "3 1 Manhattan 358400.912836 6.364308e+08 \n", "4 2 Bronx 464475.145651 1.186822e+09 \n", "\n", " geometry \n", "0 MULTIPOLYGON (((970217.022 145643.332, 970227.... \n", "1 MULTIPOLYGON (((1021176.479 151374.797, 102100... \n", "2 MULTIPOLYGON (((1029606.077 156073.814, 102957... \n", "3 MULTIPOLYGON (((981219.056 188655.316, 980940.... \n", "4 MULTIPOLYGON (((1012821.806 229228.265, 101278... " ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import geopandas\n", "\n", "url = (\n", " \"https://raw.githubusercontent.com/python-visualization/folium/master/examples/data\"\n", ")\n", "nybb = f\"{url}/nybb.zip\"\n", "boros = geopandas.read_file(nybb)\n", "\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": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import folium\n", "\n", "\n", "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": 3, "metadata": {}, "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", " \n", "
BoroCodeBoroNameShape_LengShape_Areageometrystyle
05Staten Island330454.1759331.623847e+09MULTIPOLYGON (((970217.022 145643.332, 970227....{'fillColor': '#ff0000', 'weight': 2, 'color':...
13Brooklyn741227.3370731.937810e+09MULTIPOLYGON (((1021176.479 151374.797, 102100...{'fillColor': '#00ff00', 'weight': 2, 'color':...
24Queens896875.3964493.045079e+09MULTIPOLYGON (((1029606.077 156073.814, 102957...{'fillColor': '#0000ff', 'weight': 2, 'color':...
31Manhattan358400.9128366.364308e+08MULTIPOLYGON (((981219.056 188655.316, 980940....{'fillColor': '#ffff00', 'weight': 2, 'color':...
42Bronx464475.1456511.186822e+09MULTIPOLYGON (((1012821.806 229228.265, 101278...{'fillColor': '#00ffff', 'weight': 2, 'color':...
\n", "
" ], "text/plain": [ " BoroCode BoroName Shape_Leng Shape_Area \\\n", "0 5 Staten Island 330454.175933 1.623847e+09 \n", "1 3 Brooklyn 741227.337073 1.937810e+09 \n", "2 4 Queens 896875.396449 3.045079e+09 \n", "3 1 Manhattan 358400.912836 6.364308e+08 \n", "4 2 Bronx 464475.145651 1.186822e+09 \n", "\n", " geometry \\\n", "0 MULTIPOLYGON (((970217.022 145643.332, 970227.... \n", "1 MULTIPOLYGON (((1021176.479 151374.797, 102100... \n", "2 MULTIPOLYGON (((1029606.077 156073.814, 102957... \n", "3 MULTIPOLYGON (((981219.056 188655.316, 980940.... \n", "4 MULTIPOLYGON (((1012821.806 229228.265, 101278... \n", "\n", " style \n", "0 {'fillColor': '#ff0000', 'weight': 2, 'color':... \n", "1 {'fillColor': '#00ff00', 'weight': 2, 'color':... \n", "2 {'fillColor': '#0000ff', 'weight': 2, 'color':... \n", "3 {'fillColor': '#ffff00', 'weight': 2, 'color':... \n", "4 {'fillColor': '#00ffff', 'weight': 2, 'color':... " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "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", "]\n", "\n", "boros" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "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": [ "Folium should work with any object that implements the `__geo_interface__` but be aware that sometimes you may need to convert your data to `epsg='4326'` before sending it to `folium`." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import fiona\n", "import shapely\n", "\n", "fname = f\"{url}/2014_08_05_farol.gpx\"\n", "with fiona.open(fname, \"r\", layer=\"tracks\") as records:\n", " tracks = [shapely.geometry.shape(record[\"geometry\"]) for record in records]\n", "\n", "track = tracks[0]\n", "\n", "m = folium.Map(tiles=\"cartodbpositron\")\n", "folium.GeoJson(track).add_to(m)\n", "\n", "m.fit_bounds(m.get_bounds())\n", "\n", "m" ] } ], "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.9.0" } }, "nbformat": 4, "nbformat_minor": 1 }