{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Geopandas and GeoDataFrame" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:47:12.272743Z", "iopub.status.busy": "2024-04-26T11:47:12.272743Z", "iopub.status.idle": "2024-04-26T11:47:13.144443Z", "shell.execute_reply": "2024-04-26T11:47:13.143634Z" } }, "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": "2024-04-26T11:47:13.159555Z", "iopub.status.busy": "2024-04-26T11:47:13.159555Z", "iopub.status.idle": "2024-04-26T11:47:13.175834Z", "shell.execute_reply": "2024-04-26T11:47:13.175834Z" } }, "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": "2024-04-26T11:47:13.175834Z", "iopub.status.busy": "2024-04-26T11:47:13.175834Z", "iopub.status.idle": "2024-04-26T11:47:13.192923Z", "shell.execute_reply": "2024-04-26T11:47:13.191613Z" } }, "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": "2024-04-26T11:47:13.192923Z", "iopub.status.busy": "2024-04-26T11:47:13.192923Z", "iopub.status.idle": "2024-04-26T11:47:13.223221Z", "shell.execute_reply": "2024-04-26T11:47:13.223221Z" } }, "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": "2024-04-26T11:47:13.223221Z", "iopub.status.busy": "2024-04-26T11:47:13.223221Z", "iopub.status.idle": "2024-04-26T11:47:13.238718Z", "shell.execute_reply": "2024-04-26T11:47:13.238718Z" } }, "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": "2024-04-26T11:47:13.239814Z", "iopub.status.busy": "2024-04-26T11:47:13.239814Z", "iopub.status.idle": "2024-04-26T11:47:13.256326Z", "shell.execute_reply": "2024-04-26T11:47:13.255047Z" } }, "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": "2024-04-26T11:47:13.256326Z", "iopub.status.busy": "2024-04-26T11:47:13.256326Z", "iopub.status.idle": "2024-04-26T11:47:13.270878Z", "shell.execute_reply": "2024-04-26T11:47:13.270878Z" } }, "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": "2024-04-26T11:47:13.274240Z", "iopub.status.busy": "2024-04-26T11:47:13.274240Z", "iopub.status.idle": "2024-04-26T11:47:13.286908Z", "shell.execute_reply": "2024-04-26T11:47:13.286908Z" } }, "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": "2024-04-26T11:47:13.288064Z", "iopub.status.busy": "2024-04-26T11:47:13.288064Z", "iopub.status.idle": "2024-04-26T11:47:13.303674Z", "shell.execute_reply": "2024-04-26T11:47:13.302610Z" } }, "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": "2024-04-26T11:47:13.303674Z", "iopub.status.busy": "2024-04-26T11:47:13.303674Z", "iopub.status.idle": "2024-04-26T11:47:13.318516Z", "shell.execute_reply": "2024-04-26T11:47:13.318516Z" } }, "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": "2024-04-26T11:47:13.318516Z", "iopub.status.busy": "2024-04-26T11:47:13.318516Z", "iopub.status.idle": "2024-04-26T11:47:13.334292Z", "shell.execute_reply": "2024-04-26T11:47:13.334292Z" } }, "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": "2024-04-26T11:47:13.335976Z", "iopub.status.busy": "2024-04-26T11:47:13.335976Z", "iopub.status.idle": "2024-04-26T11:47:13.350117Z", "shell.execute_reply": "2024-04-26T11:47:13.350117Z" } }, "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": "2024-04-26T11:47:13.350117Z", "iopub.status.busy": "2024-04-26T11:47:13.350117Z", "iopub.status.idle": "2024-04-26T11:47:13.366264Z", "shell.execute_reply": "2024-04-26T11:47:13.366264Z" } }, "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": "2024-04-26T11:47:13.366264Z", "iopub.status.busy": "2024-04-26T11:47:13.366264Z", "iopub.status.idle": "2024-04-26T11:47:13.383093Z", "shell.execute_reply": "2024-04-26T11:47:13.382015Z" } }, "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": "2024-04-26T11:47:13.383093Z", "iopub.status.busy": "2024-04-26T11:47:13.383093Z", "iopub.status.idle": "2024-04-26T11:47:13.398226Z", "shell.execute_reply": "2024-04-26T11:47:13.398226Z" } }, "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": "2024-04-26T11:47:13.398226Z", "iopub.status.busy": "2024-04-26T11:47:13.398226Z", "iopub.status.idle": "2024-04-26T11:47:13.413962Z", "shell.execute_reply": "2024-04-26T11:47:13.413962Z" } }, "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": "2024-04-26T11:47:13.413962Z", "iopub.status.busy": "2024-04-26T11:47:13.413962Z", "iopub.status.idle": "2024-04-26T11:47:13.429784Z", "shell.execute_reply": "2024-04-26T11:47:13.429784Z" } }, "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": "2024-04-26T11:47:13.429784Z", "iopub.status.busy": "2024-04-26T11:47:13.429784Z", "iopub.status.idle": "2024-04-26T11:47:13.445571Z", "shell.execute_reply": "2024-04-26T11:47:13.445571Z" } }, "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": "2024-04-26T11:47:13.445571Z", "iopub.status.busy": "2024-04-26T11:47:13.445571Z", "iopub.status.idle": "2024-04-26T11:47:13.461167Z", "shell.execute_reply": "2024-04-26T11:47:13.461167Z" } }, "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": "2024-04-26T11:47:13.461167Z", "iopub.status.busy": "2024-04-26T11:47:13.461167Z", "iopub.status.idle": "2024-04-26T11:47:13.477309Z", "shell.execute_reply": "2024-04-26T11:47:13.477309Z" } }, "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 }