{ "cells": [ { "cell_type": "markdown", "id": "2ac0e74d", "metadata": {}, "source": [ "# Using Exotic Map Projections: `use_crs=\"provided\"`\n", "\n", "Value \"provided\" tells Lets-Plot that the input GeoDataframe already contains coordinates in the desired CRS and should not be reprojected any further.\n", "\n", "This is a workaround for the case when the desired CRS is not naturally supported by Lets-Plot.\n", "\n", "For example, if you try to use the **Robinson** projection (EPSG:54030): `use_crs=\"EPSG:54030\"`, then Lets-Plot will return an error: \n", "> CRSError: Invalid projection: EPSG:54030: (Internal Proj Error: proj_create: crs not found)\n", "\n", "Let's see how can we still plot a map in the **Robinsons** projection in Lets-Plot." ] }, { "cell_type": "code", "execution_count": 1, "id": "8ab5b1ed", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:01:13.376836Z", "iopub.status.busy": "2024-04-26T12:01:13.376836Z", "iopub.status.idle": "2024-04-26T12:01:14.451546Z", "shell.execute_reply": "2024-04-26T12:01:14.451546Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The geodata is provided by © OpenStreetMap contributors and is made available here under the Open Database License (ODbL).\n" ] } ], "source": [ "from lets_plot import *\n", "from lets_plot.geo_data import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "b0b9825c", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:01:14.451546Z", "iopub.status.busy": "2024-04-26T12:01:14.451546Z", "iopub.status.idle": "2024-04-26T12:01:14.467233Z", "shell.execute_reply": "2024-04-26T12:01:14.467233Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "markdown", "id": "5067a158", "metadata": {}, "source": [ "#### 1. Default Presentation" ] }, { "cell_type": "code", "execution_count": 3, "id": "206a0bb9", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:01:14.467233Z", "iopub.status.busy": "2024-04-26T12:01:14.467233Z", "iopub.status.idle": "2024-04-26T12:01:15.904597Z", "shell.execute_reply": "2024-04-26T12:01:15.904597Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "world = geocode_countries().inc_res().get_boundaries()\n", "\n", "ggplot() + geom_map(map=world)" ] }, { "cell_type": "markdown", "id": "b8877904", "metadata": {}, "source": [ "#### 2. Project \"World\" to the Robinson's" ] }, { "cell_type": "code", "execution_count": 4, "id": "a2197755", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:01:15.908984Z", "iopub.status.busy": "2024-04-26T12:01:15.908984Z", "iopub.status.idle": "2024-04-26T12:01:16.486678Z", "shell.execute_reply": "2024-04-26T12:01:16.486678Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from cartopy import crs as ccrs\n", "\n", "# Define the CartoPy CRS object.\n", "crs = ccrs.Robinson()\n", "\n", "# This can be converted into a `proj4` string/dict compatible with GeoPandas\n", "crs_proj4 = crs.proj4_init\n", "world_robinson = world.to_crs(crs_proj4)\n", "\n", "ggplot() + geom_map(map=world_robinson, use_crs=\"provided\")" ] }, { "cell_type": "markdown", "id": "8e816608", "metadata": {}, "source": [ "#### 3. Options" ] }, { "cell_type": "code", "execution_count": 5, "id": "7166ba40", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:01:16.487756Z", "iopub.status.busy": "2024-04-26T12:01:16.487756Z", "iopub.status.idle": "2024-04-26T12:01:16.992607Z", "shell.execute_reply": "2024-04-26T12:01:16.991726Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(ggplot() + \n", " geom_polygon(map=world_robinson, use_crs=\"provided\", color=\"white\") + \n", " theme_void() + theme(plot_title=element_text(face=\"bold-italic\")) + flavor_solarized_light() + \n", " ggsize(2048, 1096) + \n", " xlim(-13E6, None) + \n", " ggtitle(\"World in the Robinson Projection\")\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.10.13" } }, "nbformat": 4, "nbformat_minor": 5 }