{ "cells": [ { "cell_type": "markdown", "id": "51d818d9", "metadata": {}, "source": [ "# Visualize Germany's Population using lonboard\n", "In this notebook we’ll use the [`lonboard`](https://developmentseed.org/lonboard/) library to visualize Germany’s population data, which consists of approximately 3 million polygons. By the end, you'll know how to create interactive maps of large spatial datasets directly in Jupyter notebooks." ] }, { "cell_type": "markdown", "id": "908f9777", "metadata": {}, "source": [ "## Imports\n", "`lonboard` is designed to work within Jupyter Notebooks. Please make sure you're working in a Jupyter environment to follow along.\n", "\n", "Before starting, make sure you have the following packages installed:\n", "- `lonboard` and its dependencies: `anywidget`, `geopandas`, `matplotlib`, `palettable`, `pandas` (which requires `numpy`), `pyarrow` and `shapely`.\n", "- `requests`: For fetching data.\n", "- `seaborn`: For statistical data visualization.\n", "\n", "The following modules come standard with Python, so no need for separate installation:\n", "- `zipfile`\n", "- `io`" ] }, { "cell_type": "code", "execution_count": 1, "id": "0ffbe742-9fdd-4efe-9200-8efd8f78f168", "metadata": { "scrolled": true }, "outputs": [], "source": [ "import requests\n", "import zipfile\n", "import io\n", "\n", "from lonboard import Map, SolidPolygonLayer\n", "from lonboard.layer_extension import DataFilterExtension\n", "import ipywidgets as widgets\n", "\n", "import pandas as pd\n", "import numpy as np\n", "import geopandas as gpd\n", "\n", "import matplotlib as mpl\n", "import matplotlib.pyplot as plt\n", "from matplotlib.colors import LogNorm\n", "import matplotlib.ticker as mticker\n", "\n", "from shapely.geometry import Polygon\n", "\n", "import seaborn as sns" ] }, { "cell_type": "markdown", "id": "398705b0", "metadata": {}, "source": [ "## Fetch Population Data\n", "The population data we’ll be using is stored in a `.csv` file inside a `.zip` folder. It consists of 100m x 100m grid cells covering the area of Germany, with population counts for each cell. We'll download this file, extract the data, and load it into a `pandas` DataFrame." ] }, { "cell_type": "code", "execution_count": 2, "id": "c2d4910d", "metadata": {}, "outputs": [], "source": [ "url = \"https://www.zensus2022.de/static/Zensus_Veroeffentlichung/Zensus2022_Bevoelkerungszahl.zip\"\n", "target_file = \"Zensus2022_Bevoelkerungszahl_100m-Gitter.csv\"\n", "\n", "response = requests.get(url)\n", "\n", "if response.status_code == 200:\n", " with zipfile.ZipFile(io.BytesIO(response.content)) as zip_ref: # Open the ZIP file in memory\n", " if target_file in zip_ref.namelist():\n", " with zip_ref.open(target_file) as csv_file:\n", " df_pop = pd.read_csv(csv_file, delimiter=';') \n", " else:\n", " print(f\"{target_file} not found in the ZIP archive.\")\n", "else:\n", " print(f\"Failed to download file: {response.status_code}\")" ] }, { "cell_type": "markdown", "id": "c0cc550b", "metadata": {}, "source": [ "Let’s take a quick look at the first few rows of the data to understand its structure:" ] }, { "cell_type": "code", "execution_count": 3, "id": "fbf9ac13", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | GITTER_ID_100m | \n", "x_mp_100m | \n", "y_mp_100m | \n", "Einwohner | \n", "
---|---|---|---|---|
0 | \n", "CRS3035RES100mN2689100E4337000 | \n", "4337050 | \n", "2689150 | \n", "4 | \n", "
1 | \n", "CRS3035RES100mN2689100E4341100 | \n", "4341150 | \n", "2689150 | \n", "11 | \n", "
2 | \n", "CRS3035RES100mN2690800E4341200 | \n", "4341250 | \n", "2690850 | \n", "4 | \n", "
3 | \n", "CRS3035RES100mN2691200E4341200 | \n", "4341250 | \n", "2691250 | \n", "12 | \n", "
4 | \n", "CRS3035RES100mN2691300E4341200 | \n", "4341250 | \n", "2691350 | \n", "3 | \n", "
\n", " | Population | \n", "geometry | \n", "
---|---|---|
0 | \n", "4 | \n", "POLYGON ((10.21146 47.31529, 10.21278 47.31529... | \n", "
1 | \n", "11 | \n", "POLYGON ((10.26565 47.31517, 10.26697 47.31517... | \n", "
2 | \n", "4 | \n", "POLYGON ((10.26705 47.33047, 10.26837 47.33046... | \n", "
3 | \n", "12 | \n", "POLYGON ((10.26707 47.33407, 10.26839 47.33407... | \n", "
4 | \n", "3 | \n", "POLYGON ((10.26707 47.33497, 10.26839 47.33497... | \n", "
... | \n", "... | \n", "... | \n", "
3088032 | \n", "14 | \n", "POLYGON ((8.42288 55.02299, 8.42445 55.02301, ... | \n", "
3088033 | \n", "4 | \n", "POLYGON ((8.41816 55.02383, 8.41972 55.02385, ... | \n", "
3088034 | \n", "10 | \n", "POLYGON ((8.41972 55.02385, 8.42129 55.02387, ... | \n", "
3088035 | \n", "3 | \n", "POLYGON ((8.42129 55.02387, 8.42285 55.02389, ... | \n", "
3088036 | \n", "3 | \n", "POLYGON ((8.42285 55.02389, 8.42441 55.02391, ... | \n", "
3088037 rows × 2 columns
\n", "