{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# `xarray-leaflet`\n", "\n", "`xarray-leaflet` is an xarray extension for plotting tiled maps. Both [xarray](https://docs.xarray.dev/en/stable/) and [Leaflet](ipyleaflet.ipynb) can work with data fragments, xarray with [dask chunks](https://docs.dask.org/en/latest/array-chunks.html) and leaflet with *map tiles*. With `xarray-leaflet` both work together." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Installation\n", "\n", "You can install `xarray-leaflet` in your Jupyter kernel with:\n", "\n", "``` bash\n", "$ pipenv install xarray-leaflet\n", "Installing xarray-leaflet…\n", "…\n", "``` \n", "\n", "By default, `xarray-leaflet` generates tiles in temporary directories. With dynamic maps, a new directory is created each time the map is interacted with, either by dragging or zooming. This is because there is a direct mapping between the tile directory and the URL under which the tiles are provided. As tiles should not be cached by the browser for dynamic maps, the URL must change constantly. These temporary directories are currently not cleaned up automatically. You should therefore do this regularly yourself. In ix systems they are stored at `/tmp/xarray_leaflet_*`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example\n", "\n", "To be able to run the example, you must also install the following packages in your kernel:\n", "\n", "* requests\n", "* tqdm\n", "* xarray-leaflet\n", "* dask" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "import warnings\n", "import zipfile\n", "\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import requests\n", "import rioxarray\n", "import xarray_leaflet\n", "\n", "from ipyleaflet import LayersControl, Map, WidgetControl, basemaps\n", "from ipywidgets import FloatSlider\n", "from tqdm import tqdm" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will display the DEM (Digital Elevation Model) for Europe from the [HydroSHEDS](https://www.hydrosheds.org) dataset, which represents the terrain. Let’s download the data first:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "url = 'https://edcintl.cr.usgs.gov/downloads/sciweb1/shared/hydrosheds/sa_30s_zip_grid/eu_dem_30s_grid.zip'\n", "filename = os.path.basename(url)\n", "name = filename[:filename.find('_grid')]\n", "adffile = os.path.join(name, name, 'w001001.adf')\n", "\n", "if not os.path.exists(adffile):\n", " r = requests.get(url, stream=True)\n", " with open(filename, 'wb') as f:\n", " total_length = int(r.headers.get('content-length'))\n", " for chunk in tqdm(r.iter_content(chunk_size=1024), total=(total_length/1024) + 1):\n", " if chunk:\n", " f.write(chunk)\n", " f.flush()\n", " zip = zipfile.ZipFile(filename)\n", " zip.extractall('.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It’s a dataset that [Rasterio](https://rasterio.readthedocs.io/en/stable/) can open, but to get a nice `DataArray` with all the metadata, we open it with [rioxarray](https://corteva.github.io/rioxarray/stable/):" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
<xarray.DataArray (band: 1, y: 5760, x: 9480)>\n", "[54604800 values with dtype=float32]\n", "Coordinates:\n", " * band (band) int64 1\n", " * x (x) float64 -14.0 -13.99 -13.98 -13.97 ... 64.98 64.99 65.0\n", " * y (y) float64 60.0 59.99 59.98 59.97 ... 12.03 12.02 12.01 12.0\n", " spatial_ref int64 0\n", "Attributes:\n", " scale_factor: 1.0\n", " add_offset: 0.0