{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# GeoJSON and choropleth\n", "\n", "**A few examples of how to do that with `folium`.**\n", "\n", "\n", "## Using `GeoJson`\n", "\n", "### Loading data\n", "\n", "Let us load a GeoJSON file representing the US states." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import json\n", "\n", "import requests\n", "\n", "url = (\n", " \"https://raw.githubusercontent.com/python-visualization/folium/main/examples/data\"\n", ")\n", "us_states = f\"{url}/us-states.json\"\n", "\n", "geo_json_data = json.loads(requests.get(us_states).text)" ] }, { "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": 2, "metadata": {}, "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", "