{ "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": "2025-11-05T13:47:32.954683Z", "iopub.status.busy": "2025-11-05T13:47:32.954584Z", "iopub.status.idle": "2025-11-05T13:47:32.981696Z", "shell.execute_reply": "2025-11-05T13:47:32.981306Z" } }, "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": "2025-11-05T13:47:32.982773Z", "iopub.status.busy": "2025-11-05T13:47:32.982697Z", "iopub.status.idle": "2025-11-05T13:47:32.984640Z", "shell.execute_reply": "2025-11-05T13:47:32.984405Z" } }, "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": "2025-11-05T13:47:32.985645Z", "iopub.status.busy": "2025-11-05T13:47:32.985574Z", "iopub.status.idle": "2025-11-05T13:47:32.987213Z", "shell.execute_reply": "2025-11-05T13:47:32.986971Z" } }, "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": "2025-11-05T13:47:32.988048Z", "iopub.status.busy": "2025-11-05T13:47:32.987982Z", "iopub.status.idle": "2025-11-05T13:47:32.989345Z", "shell.execute_reply": "2025-11-05T13:47:32.989111Z" } }, "outputs": [], "source": [ "plot_size = 600\n", "subplot_size = 500" ] }, { "cell_type": "code", "execution_count": 5, "id": "73c3ab3c", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:47:32.990077Z", "iopub.status.busy": "2025-11-05T13:47:32.990013Z", "iopub.status.idle": "2025-11-05T13:47:34.116817Z", "shell.execute_reply": "2025-11-05T13:47:34.116342Z" } }, "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": "2025-11-05T13:47:34.118193Z", "iopub.status.busy": "2025-11-05T13:47:34.118078Z", "iopub.status.idle": "2025-11-05T13:47:35.857914Z", "shell.execute_reply": "2025-11-05T13:47:35.857337Z" } }, "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": "2025-11-05T13:47:35.859461Z", "iopub.status.busy": "2025-11-05T13:47:35.859369Z", "iopub.status.idle": "2025-11-05T13:47:38.069495Z", "shell.execute_reply": "2025-11-05T13:47:38.069262Z" } }, "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": "2025-11-05T13:47:38.098547Z", "iopub.status.busy": "2025-11-05T13:47:38.098433Z", "iopub.status.idle": "2025-11-05T13:47:38.113331Z", "shell.execute_reply": "2025-11-05T13:47:38.112991Z" } }, "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": "2025-11-05T13:47:38.114317Z", "iopub.status.busy": "2025-11-05T13:47:38.114237Z", "iopub.status.idle": "2025-11-05T13:47:38.913254Z", "shell.execute_reply": "2025-11-05T13:47:38.912888Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "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", "gggrid([plot, image]) + ggsize(2 * subplot_size, subplot_size)" ] } ], "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 }