{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### `geom_imshow()` renders `NaN` values as transparent pixels" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:36:22.794779Z", "iopub.status.busy": "2024-04-17T07:36:22.794695Z", "iopub.status.idle": "2024-04-17T07:36:23.130487Z", "shell.execute_reply": "2024-04-17T07:36:23.130144Z" } }, "outputs": [], "source": [ "import numpy as np\n", "from osgeo import gdal, osr\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:36:23.131885Z", "iopub.status.busy": "2024-04-17T07:36:23.131754Z", "iopub.status.idle": "2024-04-17T07:36:23.133860Z", "shell.execute_reply": "2024-04-17T07:36:23.133689Z" } }, "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:36:23.146965Z", "iopub.status.busy": "2024-04-17T07:36:23.146798Z", "iopub.status.idle": "2024-04-17T07:36:23.148721Z", "shell.execute_reply": "2024-04-17T07:36:23.148519Z" } }, "outputs": [], "source": [ "def get_bounds(data):\n", " xmin, xpixel, _, ymax, _, ypixel = data.GetGeoTransform()\n", " width, height = data.RasterXSize, data.RasterYSize\n", " xmax = xmin + width * xpixel\n", " ymin = ymax + height * ypixel\n", " return xmin, ymin, xmax, ymax\n", "\n", "def get_crs(data):\n", " proj = osr.SpatialReference(wkt=data.GetProjection())\n", " return \"EPSG:{0}\".format(proj.GetAttrValue('AUTHORITY', 1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Load Data - Japan Digital Elevation Model (DEM)\n", "\n", "Data are available at\n", "\n", "[Harvard Dataverse](https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/28762)\n", "\n", "Japan DEM (c) USGS. Processed for Japan by Skinner Regional Systems Analysis. Cambridge: Center for Geographic Analysis, Harvard University, 2015." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:36:23.149641Z", "iopub.status.busy": "2024-04-17T07:36:23.149569Z", "iopub.status.idle": "2024-04-17T07:36:24.446485Z", "shell.execute_reply": "2024-04-17T07:36:24.446126Z" } }, "outputs": [], "source": [ "# Load data with GDAL\n", "\n", "jp_tif = gdal.Open(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/japan_dem_wgs84/japan_dem_wgs84.tif\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:36:24.447848Z", "iopub.status.busy": "2024-04-17T07:36:24.447751Z", "iopub.status.idle": "2024-04-17T07:36:24.464521Z", "shell.execute_reply": "2024-04-17T07:36:24.464222Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "EPSG:4326\n", "(127.9421598662918, 29.201489591057303, 155.88170436191123, 50.02079185141284)\n" ] } ], "source": [ "# Metadata\n", "\n", "jp_crs = get_crs(jp_tif)\n", "jp_bounds = get_bounds(jp_tif)\n", "\n", "print(\"{}\\n{}\".format(jp_crs, jp_bounds))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:36:24.465515Z", "iopub.status.busy": "2024-04-17T07:36:24.465424Z", "iopub.status.idle": "2024-04-17T07:36:24.467270Z", "shell.execute_reply": "2024-04-17T07:36:24.467092Z" } }, "outputs": [], "source": [ "# create the \"extent\" to use in geom_imshow()\n", "\n", "xmin = jp_bounds[0]\n", "xmax = jp_bounds[2]\n", "ymin = jp_bounds[1]\n", "ymax = jp_bounds[3]\n", "\n", "jp_ext=[xmin, xmax, ymin, ymax]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:36:24.468246Z", "iopub.status.busy": "2024-04-17T07:36:24.468103Z", "iopub.status.idle": "2024-04-17T07:36:24.474589Z", "shell.execute_reply": "2024-04-17T07:36:24.474378Z" } }, "outputs": [], "source": [ "# Get the first band as a 2D numpy array\n", "\n", "jp_arr = jp_tif.GetRasterBand(1).ReadAsArray()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:36:24.475705Z", "iopub.status.busy": "2024-04-17T07:36:24.475548Z", "iopub.status.idle": "2024-04-17T07:36:24.478120Z", "shell.execute_reply": "2024-04-17T07:36:24.477948Z" } }, "outputs": [ { "data": { "text/plain": [ "(numpy.ndarray, (2038, 2735))" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(jp_arr), np.shape(jp_arr)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:36:24.479105Z", "iopub.status.busy": "2024-04-17T07:36:24.478991Z", "iopub.status.idle": "2024-04-17T07:36:24.560702Z", "shell.execute_reply": "2024-04-17T07:36:24.560458Z" } }, "outputs": [ { "data": { "text/html": [ " \n", " " ], "text/plain": [ "