{ "cells": [ { "cell_type": "markdown", "id": "c8f27d2e", "metadata": {}, "source": [ "# Using `geom_imshow()` to draw DEM on map\n", "\n", "[SRTM1N00E012V3](https://earthexplorer.usgs.gov/scene/metadata/full/5e83a3ee1af480c5/SRTM1N00E012V3/) courtesy of the U.S. Geological Survey" ] }, { "cell_type": "code", "execution_count": 1, "id": "86e9b69b", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:33:37.177221Z", "iopub.status.busy": "2024-04-17T07:33:37.177087Z", "iopub.status.idle": "2024-04-17T07:33:37.518844Z", "shell.execute_reply": "2024-04-17T07:33:37.518325Z" } }, "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": [ "import numpy as np\n", "from PIL import Image\n", "\n", "from lets_plot import *\n", "from lets_plot.geo_data import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "fe649753", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:33:37.520270Z", "iopub.status.busy": "2024-04-17T07:33:37.520115Z", "iopub.status.idle": "2024-04-17T07:33:37.522471Z", "shell.execute_reply": "2024-04-17T07:33:37.522292Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "43462276", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:33:37.523434Z", "iopub.status.busy": "2024-04-17T07:33:37.523351Z", "iopub.status.idle": "2024-04-17T07:33:37.524965Z", "shell.execute_reply": "2024-04-17T07:33:37.524780Z" } }, "outputs": [], "source": [ "def get_dem_data(url):\n", " import requests\n", " from osgeo import gdal\n", " r = requests.get(url)\n", " vsipath = '/vsimem/dem'\n", " gdal.FileFromMemBuffer(vsipath, r.content)\n", " return gdal.Open(vsipath)" ] }, { "cell_type": "code", "execution_count": 4, "id": "198ce722", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:33:37.525808Z", "iopub.status.busy": "2024-04-17T07:33:37.525735Z", "iopub.status.idle": "2024-04-17T07:33:37.526995Z", "shell.execute_reply": "2024-04-17T07:33:37.526827Z" } }, "outputs": [], "source": [ "plot_size = 600\n", "subplot_size = 500" ] }, { "cell_type": "code", "execution_count": 5, "id": "73c3ab3c", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:33:37.527773Z", "iopub.status.busy": "2024-04-17T07:33:37.527661Z", "iopub.status.idle": "2024-04-17T07:33:38.541877Z", "shell.execute_reply": "2024-04-17T07:33:38.541405Z" } }, "outputs": [], "source": [ "# Data from geocoding\n", "states = geocode_states().scope(\"Gabon\").inc_res(4).get_boundaries()\n", "cities = geocode_cities().scope(\"Gabon\").get_centroids()" ] }, { "cell_type": "code", "execution_count": 6, "id": "45972976", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:33:38.543174Z", "iopub.status.busy": "2024-04-17T07:33:38.543087Z", "iopub.status.idle": "2024-04-17T07:33:40.975569Z", "shell.execute_reply": "2024-04-17T07:33:40.975087Z" } }, "outputs": [], "source": [ "# Data from DEM\n", "dataset = get_dem_data(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/ivindo_river.tif\")\n", "dataset_array = dataset.ReadAsArray()\n", "transform = dataset.GetGeoTransform()\n", "lon_min = transform[0]\n", "lon_max = lon_min + (transform[1] * dataset_array.shape[1])\n", "lat_max = transform[3]\n", "lat_min = lat_max + (transform[5] * dataset_array.shape[0])" ] }, { "cell_type": "code", "execution_count": 7, "id": "c233fae5", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:33:40.977055Z", "iopub.status.busy": "2024-04-17T07:33:40.976965Z", "iopub.status.idle": "2024-04-17T07:33:42.113835Z", "shell.execute_reply": "2024-04-17T07:33:42.113403Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot = ggplot() + \\\n", " geom_imshow(dataset_array, extent=[lon_min, lon_max, lat_min, lat_max], cmap=\"viridis\") + \\\n", " geom_map(data=states, color=\"black\", alpha=0) + \\\n", " geom_point(data=cities, shape=21, size=4, color=\"black\", fill=\"white\", \\\n", " tooltips=layer_tooltips().line(\"@{found name}\")) + \\\n", " ggsize(plot_size, plot_size) + \\\n", " theme_void()\n", "plot + ggtitle(\"Ivindo river in Gabon\")" ] }, { "cell_type": "code", "execution_count": 8, "id": "3de2c1c6", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:33:42.132217Z", "iopub.status.busy": "2024-04-17T07:33:42.132090Z", "iopub.status.idle": "2024-04-17T07:33:42.146708Z", "shell.execute_reply": "2024-04-17T07:33:42.146504Z" } }, "outputs": [], "source": [ "# Data from image\n", "image_array = np.asarray(Image.open(\"images/ivindo_river_osm_screen.png\"))" ] }, { "cell_type": "code", "execution_count": 9, "id": "4ef3ea01", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:33:42.147712Z", "iopub.status.busy": "2024-04-17T07:33:42.147587Z", "iopub.status.idle": "2024-04-17T07:33:42.712714Z", "shell.execute_reply": "2024-04-17T07:33:42.712281Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "plot += coord_map(xlim=[lon_min, lon_max], ylim=[lat_min, lat_max]) + \\\n", " ggtitle(\"Plot\")\n", "image = ggplot() + \\\n", " geom_imshow(image_array) + \\\n", " ggsize(plot_size, plot_size) + \\\n", " ggtitle(\"Image\") + \\\n", " theme_void()\n", "\n", "bunch = GGBunch()\n", "bunch.add_plot(plot, 0, 0, subplot_size, subplot_size)\n", "bunch.add_plot(image, subplot_size, 0, subplot_size, subplot_size)\n", "bunch.show()" ] } ], "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 }