{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Geopandas and GeoDataFrame" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:00.908599Z", "iopub.status.busy": "2025-11-05T13:40:00.908524Z", "iopub.status.idle": "2025-11-05T13:40:00.935458Z", "shell.execute_reply": "2025-11-05T13:40:00.935188Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import pandas as pd\n", "from geopandas import GeoDataFrame\n", "from shapely.geometry import MultiPolygon, Polygon, LinearRing, Point, MultiPoint, LineString, MultiLineString, mapping\n", "\n", "from lets_plot import *\n", "LetsPlot.setup_html()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Let's draw GeoDataFrame on plot" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:00.948929Z", "iopub.status.busy": "2025-11-05T13:40:00.948824Z", "iopub.status.idle": "2025-11-05T13:40:00.951884Z", "shell.execute_reply": "2025-11-05T13:40:00.951676Z" } }, "outputs": [], "source": [ "POINT = Point(-5, 17)\n", "\n", "MULTI_POINT = MultiPoint([Point(3, 15), Point(6, 13)])\n", "\n", "LINE = LineString([(0, 0), (5, 5)])\n", "\n", "MULTI_LINE = MultiLineString([\n", " LineString([(10, 0), (15, 5)]),\n", " LineString([(10, 5), (15, 0)])\n", "])\n", "\n", "POLYGON = Polygon(\n", " LinearRing([(1, 1), (1, 9), (9, 9), (9, 1)]),\n", " [LinearRing([(2, 2), (3, 2), (3, 3), (2, 3)]),\n", " LinearRing([(4, 4), (6, 4), (6, 6), (4, 6)])]\n", ")\n", "\n", "MULTIPOLYGON = MultiPolygon([\n", " Polygon(LinearRing([(11, 12), (13, 14), (15, 13), (7, 4)])),\n", " Polygon(LinearRing([(10, 2), (13, 10), (12, 3)]))\n", "])" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:00.952688Z", "iopub.status.busy": "2025-11-05T13:40:00.952609Z", "iopub.status.idle": "2025-11-05T13:40:00.954700Z", "shell.execute_reply": "2025-11-05T13:40:00.954514Z" } }, "outputs": [], "source": [ "gdf = GeoDataFrame(\n", " data={\n", " 'key': ['A', 'B', 'C', 'A', 'B', 'C'],\n", " 'kind': ['Point', 'MPoint', 'Line', 'MLine', 'Polygon', 'MPolygon'],\n", " 'coord': [POINT, MULTI_POINT, LINE, MULTI_LINE, POLYGON, MULTIPOLYGON]\n", " },\n", " geometry='coord'\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Geom kinds that support GeoDataFrame" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### geom_polygon()\n", "GeoDataFrame is supported natively in the 'data' parameter. Geometries of shapes Polygon and MultiPolygon are automatically taken from GeoDataFrame for geom_polygon()." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:00.955530Z", "iopub.status.busy": "2025-11-05T13:40:00.955444Z", "iopub.status.idle": "2025-11-05T13:40:00.962853Z", "shell.execute_reply": "2025-11-05T13:40:00.962645Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_polygon(aes(fill='kind'), data=gdf)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "GeoDataFrame can be used in the 'map' parameter (without 'data' specified). Geometries of shapes Polygon and MultiPolygon are automatically taken from GeoDataFrame for geom_polygon()." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:00.963541Z", "iopub.status.busy": "2025-11-05T13:40:00.963465Z", "iopub.status.idle": "2025-11-05T13:40:00.966558Z", "shell.execute_reply": "2025-11-05T13:40:00.966352Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_polygon(map=gdf)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The data specified in the 'data' and geometries from the 'map' can be joined by a key value from 'map_id' aesthetic." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:00.967262Z", "iopub.status.busy": "2025-11-05T13:40:00.967191Z", "iopub.status.idle": "2025-11-05T13:40:00.968742Z", "shell.execute_reply": "2025-11-05T13:40:00.968549Z" } }, "outputs": [], "source": [ "df = pd.DataFrame({\n", " 'name': ['A', 'B', 'C'],\n", " 'value': [42, 23, 87],\n", "})" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:00.969353Z", "iopub.status.busy": "2025-11-05T13:40:00.969283Z", "iopub.status.idle": "2025-11-05T13:40:00.972537Z", "shell.execute_reply": "2025-11-05T13:40:00.972339Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_polygon(aes(fill='value'), data=df, map=gdf, map_join=['name', 'key'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### geom_point() and geom_text()\n", "\n", "GeoDataFrame is supported natively in the 'data' parameter for geom_point() and geom_text(). Geometries of shapes Point and MultiPoint are automatically taken from GeoDataFrame. \n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:00.973203Z", "iopub.status.busy": "2025-11-05T13:40:00.973134Z", "iopub.status.idle": "2025-11-05T13:40:00.977539Z", "shell.execute_reply": "2025-11-05T13:40:00.977344Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_point(aes(color='kind'), data=gdf, size=10) + \\\n", " geom_text(aes(label='kind'), data=gdf, angle=25)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:00.978178Z", "iopub.status.busy": "2025-11-05T13:40:00.978108Z", "iopub.status.idle": "2025-11-05T13:40:00.981228Z", "shell.execute_reply": "2025-11-05T13:40:00.981038Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_text(aes(label='key', color='kind'), data=gdf)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "GeoDataFrame can be used in the 'map' parameter (without 'data' specified). Geometries of shapes Point and MultiPoint are automatically taken from GeoDataFrame for geom_point()." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:00.981912Z", "iopub.status.busy": "2025-11-05T13:40:00.981839Z", "iopub.status.idle": "2025-11-05T13:40:00.984766Z", "shell.execute_reply": "2025-11-05T13:40:00.984572Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_point(map=gdf, size=10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The data specified in the 'data' and geometries from the 'map' can be joined by a pair of keys from 'map_join' param." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:00.985430Z", "iopub.status.busy": "2025-11-05T13:40:00.985360Z", "iopub.status.idle": "2025-11-05T13:40:00.988864Z", "shell.execute_reply": "2025-11-05T13:40:00.988671Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_point(aes(color='value'), data=df, map=gdf, size=10, map_join=['name','key'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### geom_rect()\n", "\n", "GeoDataFrame is supported natively in the 'data' parameter of geom_rect(). Geometries of shapes MultiPoint, Line, MultiLine, Polygon and MultiPolygon are automatically taken from GeoDataFrame, their bounding boxes will be drawn. " ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:00.989543Z", "iopub.status.busy": "2025-11-05T13:40:00.989472Z", "iopub.status.idle": "2025-11-05T13:40:00.992642Z", "shell.execute_reply": "2025-11-05T13:40:00.992446Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_rect(aes(fill='kind'), data=gdf)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "GeoDataFrame can be used in the 'map' parameter. Geometries of shapes MultiPoint, Line, MultiLine, Polygon and MultiPolygon are automatically taken from GeoDataFrame for geom_rect(), their bounding boxes will be drawn. " ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:00.993302Z", "iopub.status.busy": "2025-11-05T13:40:00.993231Z", "iopub.status.idle": "2025-11-05T13:40:00.996029Z", "shell.execute_reply": "2025-11-05T13:40:00.995837Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_rect(map=gdf)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The data specified in the 'data' and geometries from the 'map' can be joined by a pair of keys from 'map_join' param." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:00.996676Z", "iopub.status.busy": "2025-11-05T13:40:00.996608Z", "iopub.status.idle": "2025-11-05T13:40:00.999760Z", "shell.execute_reply": "2025-11-05T13:40:00.999568Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_rect(aes(fill='value'), data=df, map=gdf, map_join=['name', 'key'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### geom_path()\n", "\n", "GeoDataFrame is supported natively in the 'data' parameter for geom_path(). Geometries of shapes Line and MultiLine are automatically taken from GeoDataFrame.\n" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:01.000432Z", "iopub.status.busy": "2025-11-05T13:40:01.000359Z", "iopub.status.idle": "2025-11-05T13:40:01.003612Z", "shell.execute_reply": "2025-11-05T13:40:01.003419Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_path(aes(color='kind'), data=gdf)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Can be used in the 'map' parameter for geom_path() without specifying 'data'." ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:01.004278Z", "iopub.status.busy": "2025-11-05T13:40:01.004209Z", "iopub.status.idle": "2025-11-05T13:40:01.007030Z", "shell.execute_reply": "2025-11-05T13:40:01.006836Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_path(map=gdf)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The data specified in the 'data' and geometries from the 'map' can be joined by a pair of keys from 'map_join' param." ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:01.007709Z", "iopub.status.busy": "2025-11-05T13:40:01.007640Z", "iopub.status.idle": "2025-11-05T13:40:01.010885Z", "shell.execute_reply": "2025-11-05T13:40:01.010683Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_path(aes(color='value'), data=df, map=gdf, map_join=['name', 'key'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### geom_map()\n", "\n", "GeoDataFrame is supported natively in the 'data' parameter of geom_map(). Geometries of shapes Polygon and MultiPolygon are automatically taken from GeoDataFrame. \n" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:01.011545Z", "iopub.status.busy": "2025-11-05T13:40:01.011470Z", "iopub.status.idle": "2025-11-05T13:40:01.014941Z", "shell.execute_reply": "2025-11-05T13:40:01.014592Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_map(aes(color='kind'), data=gdf, size=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Can be used in the 'map' parameter for geom_map()." ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:01.015603Z", "iopub.status.busy": "2025-11-05T13:40:01.015528Z", "iopub.status.idle": "2025-11-05T13:40:01.018476Z", "shell.execute_reply": "2025-11-05T13:40:01.018237Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_map(map=gdf)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The data specified in the 'data' and geometries from the 'map' can be joined by a pair of keys from 'map_join' param." ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:01.019095Z", "iopub.status.busy": "2025-11-05T13:40:01.019026Z", "iopub.status.idle": "2025-11-05T13:40:01.022529Z", "shell.execute_reply": "2025-11-05T13:40:01.022288Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_map(aes(fill='value'), data=df, map=gdf, map_join=['name', 'key'])" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.13" } }, "nbformat": 4, "nbformat_minor": 4 }