{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.3.0.dev\n" ] } ], "source": [ "import os\n", "import folium\n", "\n", "print(folium.__version__)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# GeoJSON and choropleth\n", "\n", "**A few examples of how to do that with `folium`.**" ] }, { "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": [ "import json\n", "\n", "us_states = os.path.join('data', 'us-states.json')\n", "\n", "geo_json_data = json.load(open(us_states))" ] }, { "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", " | 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", "