{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# GeoJSON and choropleth\n", "\n", "**A few examples of how to do that with `folium`.**" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "../folium/__init__.py\n", "0.2.0.dev\n" ] } ], "source": [ "import pandas as pd\n", "import json\n", "import sys\n", "sys.path.append('..')\n", "import folium\n", "print (folium.__file__)\n", "print (folium.__version__)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using `GeoJson`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let us load a GeoJSON file reprenseting the US states." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "geo_json_data = json.load(open('us-states.json'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is a classical GeoJSON `FeatureCollection` (see https://en.wikipedia.org/wiki/GeoJSON) of the form :\n", "\n", " {\n", " \"type\" : \"FeatureCollection\",\n", " \"features\" : [\n", " {\n", " \"properties\" : {\"name\": \"Alabama\"},\n", " \"id\" : \"AL\",\n", " \"type\" : \"Feature\",\n", " \"geometry\" : {\n", " \"type\": \"Polygon\",\n", " \"coordinates\": [[[-87.359296, 35.00118], ...]]\n", " }\n", " },\n", " {\n", " \"properties\" : {\"name\": \"Alaska\"},\n", " \"id\" : \"AK\",\n", " \"type\" : \"Feature\",\n", " \"geometry\" : {\n", " \"type\": \"MultiPolygon\",\n", " \"coordinates\": [[[[-131.602021, 55.117982], ... ]]]\n", " }\n", " },\n", " ...\n", " ]\n", " }\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A first way of drawing it on a map, is simply to use `folium.GeoJson` :" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " | State | \n", "Unemployment | \n", "
---|---|---|
0 | \n", "AL | \n", "7.1 | \n", "
1 | \n", "AK | \n", "6.8 | \n", "
2 | \n", "AZ | \n", "8.1 | \n", "
3 | \n", "AR | \n", "7.2 | \n", "
4 | \n", "CA | \n", "10.1 | \n", "