{ "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": [ "