{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Geopandas and GeoDataFrame" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import pandas as pd\n", "from geopandas import GeoDataFrame\n", "from lets_plot import *\n", "from shapely.geometry import MultiPolygon, Polygon, LinearRing, Point, MultiPoint, LineString, MultiLineString, mapping\n", "\n", "LetsPlot.setup_html()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Let's draw GeoDataFrame on plot" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "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": {}, "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": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \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": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \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": {}, "outputs": [], "source": [ "df = pd.DataFrame({\n", " 'name': ['A', 'B', 'C'],\n", " 'value': [42, 23, 87],\n", "})" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \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": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \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": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \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": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \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": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \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": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \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": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \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": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \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": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \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": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \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": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \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": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \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": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \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": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \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'])" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = {\n", " \"fruit\": [\"Apple\", \"Apple\", \"Orange\", \"Orange\"],\n", " \"values\": [4.0, 16.0, 6.0, 9.0],\n", " \"nutrients\": [\"Fiber\", \"Carbs\", \"Fiber\", \"Carbs\"]\n", "}\n", "\n", "p1 = Point(11, 22)\n", "p2 = Point(33, 44)\n", "\n", "gdf = GeoDataFrame(\n", " data={\n", " 'name': ['Orange', 'Apple'],\n", " 'coord': [p1, p2]\n", " },\n", " geometry='coord'\n", ")\n", "\n", "pie = geom_pie(aes(x='fruit', weight='values', fill='nutrients'), map=gdf, map_join=['fruit', 'name'])\n", "gggrid([\n", " ggplot(data) + pie,\n", " ggplot(data) + geom_livemap() + pie\n", "])" ] } ], "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.12.11" } }, "nbformat": 4, "nbformat_minor": 4 }