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