{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Creating a raster mosaic\n", "\n", "Quite often you need to merge multiple raster files together and create a `raster mosaic`. This can be done easily with the `merge()` -function in Rasterio.\n", "\n", "Here, we will create a mosaic based on 2X2m resolution DEM files (altogether 12 files) covering the Helsinki Metropolitan region. If you have not downloaded the DEM files yet, you can do that by running the script from download-data -section of the tutorial.\n", "\n", "As there are many `tif` files in our folder, it is not really pracical to start listing them manually. Luckily, we have a module and function called `glob` that can be used to create a list of those files that we are interested in based on search criteria.\n", "\n", "Let's start by:\n", " \n", " - Importing required modules\n", " - Finding all `tif` files from the folder where the file starts with `L` -letter.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating a raster mosaic with rioxarray\n", "\n", "Read elevation data from S3 bucket for Kilimanjaro region in Africa." ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [], "source": [ "import xarray as xr\n", "import os\n", "import rioxarray as rxr\n", "from rioxarray.merge import merge_datasets\n", "\n", "# S3 bucket containing the data\n", "bucket = \"https://a3s.fi/swift/v1/AUTH_0914d8aff9684df589041a759b549fc2/PythonGIS\"\n", "\n", "# Generate urls for the elevation files\n", "urls = [\n", " os.path.join(bucket, \"elevation/kilimanjaro/ASTGTMV003_S03E036_dem.tif\"),\n", " os.path.join(bucket, \"elevation/kilimanjaro/ASTGTMV003_S03E037_dem.tif\"),\n", " os.path.join(bucket, \"elevation/kilimanjaro/ASTGTMV003_S04E036_dem.tif\"),\n", " os.path.join(bucket, \"elevation/kilimanjaro/ASTGTMV003_S04E037_dem.tif\"),\n", "]\n", "\n", "# Read the files\n", "datasets = [\n", " xr.open_dataset(url, engine=\"rasterio\").squeeze(\"band\", drop=True) for url in urls\n", "]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Investigate how our data looks like:" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
<xarray.Dataset>\n", "Dimensions: (x: 3601, y: 3601)\n", "Coordinates:\n", " * x (x) float64 36.0 36.0 36.0 36.0 36.0 ... 37.0 37.0 37.0 37.0\n", " * y (y) float64 -2.0 -2.0 -2.001 -2.001 ... -2.999 -2.999 -3.0 -3.0\n", " spatial_ref int64 0\n", "Data variables:\n", " band_data (y, x) float32 ...
array([36. , 36.000278, 36.000556, ..., 36.999444, 36.999722, 37. ])
array([-2. , -2.000278, -2.000556, ..., -2.999444, -2.999722, -3. ])
array(0)
[12967201 values with dtype=float32]
<xarray.Dataset>\n", "Dimensions: (y: 1620, x: 1800)\n", "Coordinates:\n", " * y (y) float64 -2.85 -2.85 -2.851 -2.851 ... -3.299 -3.299 -3.3\n", " * x (x) float64 37.1 37.1 37.1 37.1 37.1 ... 37.6 37.6 37.6 37.6\n", " spatial_ref int64 0\n", "Data variables:\n", " elevation (y, x) float32 1.581e+03 1.581e+03 ... 1.295e+03 1.294e+03
array([-2.85 , -2.850278, -2.850556, ..., -3.299167, -3.299444, -3.299722])
array([37.100278, 37.100556, 37.100833, ..., 37.599444, 37.599722, 37.6 ])
array(0)
array([[1581., 1581., 1581., ..., 1372., 1372., 1372.],\n", " [1587., 1584., 1582., ..., 1370., 1371., 1373.],\n", " [1587., 1588., 1584., ..., 1368., 1368., 1370.],\n", " ...,\n", " [1013., 1010., 1013., ..., 1301., 1301., 1300.],\n", " [1010., 1011., 1011., ..., 1302., 1301., 1301.],\n", " [1007., 1010., 1010., ..., 1302., 1295., 1294.]], dtype=float32)