{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:04.186517Z", "iopub.status.busy": "2024-04-17T07:34:04.186301Z", "iopub.status.idle": "2024-04-17T07:34:04.501260Z", "shell.execute_reply": "2024-04-17T07:34:04.501016Z" } }, "outputs": [], "source": [ "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:04.502699Z", "iopub.status.busy": "2024-04-17T07:34:04.502579Z", "iopub.status.idle": "2024-04-17T07:34:04.504491Z", "shell.execute_reply": "2024-04-17T07:34:04.504297Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:04.517951Z", "iopub.status.busy": "2024-04-17T07:34:04.517739Z", "iopub.status.idle": "2024-04-17T07:34:04.548420Z", "shell.execute_reply": "2024-04-17T07:34:04.548138Z" } }, "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.geo_data import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Sweden and Finland" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Create countries geocoder." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:04.549644Z", "iopub.status.busy": "2024-04-17T07:34:04.549475Z", "iopub.status.idle": "2024-04-17T07:34:04.814823Z", "shell.execute_reply": "2024-04-17T07:34:04.814388Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idcountryfound namecentroidpositionlimit
054224FinlandFinland[27.5426266740774, 64.9500560760498][20.5486419796944, 59.8079609870911, 31.586707...[19.3040004372597, 59.7582100331783, 31.586707...
152822SwedenSweden[14.8960665316738, 62.1977656334639][11.1130450665951, 55.3366836905479, 24.152978...[10.9653197228909, 55.3366836905479, 24.166640...
\n", "
" ], "text/plain": [ " id country found name centroid \\\n", "0 54224 Finland Finland [27.5426266740774, 64.9500560760498] \n", "1 52822 Sweden Sweden [14.8960665316738, 62.1977656334639] \n", "\n", " position \\\n", "0 [20.5486419796944, 59.8079609870911, 31.586707... \n", "1 [11.1130450665951, 55.3366836905479, 24.152978... \n", "\n", " limit \n", "0 [19.3040004372597, 59.7582100331783, 31.586707... \n", "1 [10.9653197228909, 55.3366836905479, 24.166640... " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "country_gcoder = geocode_countries(['Finland', 'Sweden'])\n", "country_gcoder.get_geocodes()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:04.816043Z", "iopub.status.busy": "2024-04-17T07:34:04.815965Z", "iopub.status.idle": "2024-04-17T07:34:04.817658Z", "shell.execute_reply": "2024-04-17T07:34:04.817462Z" } }, "outputs": [], "source": [ "map_options = theme_void() + coord_map()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Plot boundary polygons." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:04.818597Z", "iopub.status.busy": "2024-04-17T07:34:04.818524Z", "iopub.status.idle": "2024-04-17T07:34:05.032768Z", "shell.execute_reply": "2024-04-17T07:34:05.032443Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(ggplot() + \n", " geom_polygon(aes(fill='found name'), \n", " data=country_gcoder.get_boundaries(),\n", " tooltips=layer_tooltips().line(\"^fill\"),\n", " color=\"white\") +\n", " map_options + \n", " scale_fill_hue(name=\"Country\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Plot ploygons on interactive base-map.\n", "\n", "Note: *nbviewer* won't load base-map tiles due to its CSP." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:05.034001Z", "iopub.status.busy": "2024-04-17T07:34:05.033906Z", "iopub.status.idle": "2024-04-17T07:34:05.244206Z", "shell.execute_reply": "2024-04-17T07:34:05.243932Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(ggplot() + geom_livemap() + \n", " geom_polygon(aes(fill='found name'), \n", " data=country_gcoder.get_boundaries(),\n", " tooltips=layer_tooltips().line(\"^fill\"),\n", " color=\"white\") +\n", " scale_fill_hue(name=\"Country\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The World" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Create countries geocoder." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:05.245436Z", "iopub.status.busy": "2024-04-17T07:34:05.245293Z", "iopub.status.idle": "2024-04-17T07:34:05.556879Z", "shell.execute_reply": "2024-04-17T07:34:05.556670Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idcountryfound namecentroidpositionlimit
09407AndorraAndorra[1.58972340139334, 42.542395144701][1.41357779502869, 42.4288237094879, 1.7866636...[1.41357779502869, 42.4288237094879, 1.7866636...
114296SlovakiaSlovakia[19.6367939386235, 48.6726971715689][16.8331897258759, 47.7314288914204, 22.565710...[16.8331897258759, 47.7314288914204, 22.565710...
216239AustriaAustria[14.7548074169977, 47.6964113116264][9.5307482779026, 46.3722757995129, 17.1607759...[9.5307482779026, 46.3722757995129, 17.1607759...
\n", "
" ], "text/plain": [ " id country found name centroid \\\n", "0 9407 Andorra Andorra [1.58972340139334, 42.542395144701] \n", "1 14296 Slovakia Slovakia [19.6367939386235, 48.6726971715689] \n", "2 16239 Austria Austria [14.7548074169977, 47.6964113116264] \n", "\n", " position \\\n", "0 [1.41357779502869, 42.4288237094879, 1.7866636... \n", "1 [16.8331897258759, 47.7314288914204, 22.565710... \n", "2 [9.5307482779026, 46.3722757995129, 17.1607759... \n", "\n", " limit \n", "0 [1.41357779502869, 42.4288237094879, 1.7866636... \n", "1 [16.8331897258759, 47.7314288914204, 22.565710... \n", "2 [9.5307482779026, 46.3722757995129, 17.1607759... " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "world_gcoder = geocode_countries()\n", "world_gcoder.get_geocodes().head(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### World choropleth." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:05.558092Z", "iopub.status.busy": "2024-04-17T07:34:05.557944Z", "iopub.status.idle": "2024-04-17T07:34:06.014416Z", "shell.execute_reply": "2024-04-17T07:34:06.014151Z" }, "scrolled": false }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(ggplot() + \n", " geom_polygon(aes(fill='found name'), \n", " data=world_gcoder.get_boundaries(), \n", " color=\"white\", \n", " tooltips=layer_tooltips().line(\"^fill\"),\n", " show_legend=False) +\n", " map_options +\n", " scale_fill_brewer(name=\"Country\", palette=\"Dark2\") + \n", " ggsize(900, 600))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### New York State and New York City." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:06.016497Z", "iopub.status.busy": "2024-04-17T07:34:06.016222Z", "iopub.status.idle": "2024-04-17T07:34:06.896212Z", "shell.execute_reply": "2024-04-17T07:34:06.895858Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(ggplot() + \n", " geom_polygon(map=geocode_states(\"New York\"), fill=\"#01327A\") +\n", " geom_polygon(map=geocode_cities(\"New York\"), fill=\"#F2C010\") +\n", " map_options)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The US states." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Create state geocoder for the US [48 contiguous states](https://en.wikipedia.org/wiki/Contiguous_United_States) (aka CONUS)." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:06.897443Z", "iopub.status.busy": "2024-04-17T07:34:06.897346Z", "iopub.status.idle": "2024-04-17T07:34:06.899104Z", "shell.execute_reply": "2024-04-17T07:34:06.898900Z" } }, "outputs": [], "source": [ "conus_gcoder = geocode_states('us-48').inc_res()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The US choropleth." ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:06.900244Z", "iopub.status.busy": "2024-04-17T07:34:06.900091Z", "iopub.status.idle": "2024-04-17T07:34:07.444784Z", "shell.execute_reply": "2024-04-17T07:34:07.444416Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "conus_plot = (ggplot() +\n", " geom_polygon(aes(fill='found name'), \n", " data=conus_gcoder.get_boundaries(), \n", " show_legend=False, \n", " color=\"white\",\n", " tooltips=layer_tooltips().line(\"^fill\")) +\n", " map_options +\n", " scale_fill_brewer(name=\"State\", palette=\"Dark2\") + \n", " ggsize(900, 500))\n", "conus_plot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Add the states of Alaska and Hawaii." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:07.445927Z", "iopub.status.busy": "2024-04-17T07:34:07.445837Z", "iopub.status.idle": "2024-04-17T07:34:08.752678Z", "shell.execute_reply": "2024-04-17T07:34:08.752273Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "alaska_plot = (ggplot() + geom_map(map=geocode_states('alaska'), \n", " fill=\"light-blue\", \n", " color=\"#172048\",\n", " tooltips=layer_tooltips().line(\"Alaska\")) +\n", " xlim(-180, -130) + map_options)\n", "hawaii_plot = (ggplot() + geom_map(map=geocode_states('hawaii'), \n", " fill=\"#00A954\", \n", " color=\"#D91023\",\n", " tooltips=layer_tooltips().line(\"Hawaii\")) +\n", " xlim(-162, -154) + ylim(18, 23) +\n", " map_options)\n", "\n", "bunch = GGBunch()\n", "bunch.add_plot(conus_plot, 0, 0, 800, 400)\n", "bunch.add_plot(alaska_plot, 0, 300, 200, 200)\n", "bunch.add_plot(hawaii_plot, 200, 370, 200, 125)\n", "bunch.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Resolving ambiguity." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### There are many cities named \"Warwick\"." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:08.757299Z", "iopub.status.busy": "2024-04-17T07:34:08.757201Z", "iopub.status.idle": "2024-04-17T07:34:09.954265Z", "shell.execute_reply": "2024-04-17T07:34:09.953796Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create a Warwick city geocoder.\n", "world_warwicks = geocode_cities('warwick').allow_ambiguous()\n", "\n", "# Plot Warwicks.\n", "(ggplot() + geom_map(map=geocode_countries())\n", " + geom_point(map=world_warwicks, size=3, color=\"red\")\n", " + coord_map(xlim=[-130, 20])\n", " + theme_void())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Find one Warwick which is closest to Boston, US.\n", "\n", "Note that the name \"Boston\" is not unique either. Therefore we will need two geocoders: one for Boston, US, and the other for the town of Warwick near Boston." ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:09.955660Z", "iopub.status.busy": "2024-04-17T07:34:09.955566Z", "iopub.status.idle": "2024-04-17T07:34:10.379594Z", "shell.execute_reply": "2024-04-17T07:34:10.379392Z" } }, "outputs": [], "source": [ "boston_us = geocode_cities('boston').countries('US')\n", "warwick_near_boston = world_warwicks.where('warwick', closest_to=boston_us)\n", "\n", "# Let's also geocode Massachusetts counties. \n", "counties_ma = geocode_counties().scope(\"Massachusetts\")" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:10.380881Z", "iopub.status.busy": "2024-04-17T07:34:10.380792Z", "iopub.status.idle": "2024-04-17T07:34:11.476778Z", "shell.execute_reply": "2024-04-17T07:34:11.476557Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(ggplot() + geom_map(map=counties_ma)\n", " + geom_point(map=warwick_near_boston, size=7, color=\"red\") \\\n", " + geom_polygon(map=boston_us)\n", " + theme_void())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Filter Warwicks using Shaply `box`." ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:11.477937Z", "iopub.status.busy": "2024-04-17T07:34:11.477849Z", "iopub.status.idle": "2024-04-17T07:34:11.479861Z", "shell.execute_reply": "2024-04-17T07:34:11.479548Z" } }, "outputs": [], "source": [ "from shapely.geometry import box\n", "warwicks_in_box = (world_warwicks\n", " .where('warwick', scope=box(-72, 41.5, -71, 42)))" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:11.480868Z", "iopub.status.busy": "2024-04-17T07:34:11.480715Z", "iopub.status.idle": "2024-04-17T07:34:12.358429Z", "shell.execute_reply": "2024-04-17T07:34:12.358222Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(ggplot() + geom_map(map=counties_ma)\n", " + geom_rect(xmin=-72, ymin=41.5, xmax=-71, ymax=42, alpha=0, color=\"magenta\", linetype=\"dashed\")\n", " + geom_point(map=warwicks_in_box, size=7, color=\"red\")\n", " + geom_polygon(map=boston_us)\n", " + theme_void())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Explore the details on an interactive map.\n", "\n", "Note: nbviewer won't load base-map tiles due to its CSP." ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:12.359587Z", "iopub.status.busy": "2024-04-17T07:34:12.359498Z", "iopub.status.idle": "2024-04-17T07:34:12.758528Z", "shell.execute_reply": "2024-04-17T07:34:12.758305Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(ggplot() + geom_livemap()\n", " + geom_rect(xmin=-72, ymin=41.5, xmax=-71, ymax=42, alpha=0, color=\"magenta\", linetype=\"dashed\")\n", " + geom_point(map=warwicks_in_box, size=7, color=\"red\")\n", " + geom_polygon(map=boston_us))" ] } ], "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 }