{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Example - Resampling\n", "\n", "This example demonstrates how to reproduce `rasterio`'s resampling example [here](https://rasterio.readthedocs.io/en/latest/topics/resampling.html)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from rasterio.enums import Resampling\n", "\n", "import rioxarray\n", "\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load in xarray dataset\n", "\n", "See docs for [rioxarray.open_rasterio](../rioxarray.rst#rioxarray-open-rasterio)\n", " \n", "Notes:\n", "\n", " - `masked=True` will convert from integer to `float64` and fill with `NaN`. If this behavior is not desired, you can skip this." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "xds = rioxarray.open_rasterio(\n", " \"../../test/test_data/compare/small_dem_3m_merged.tif\",\n", " masked=True,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Resampling\n", "\n", "API Reference for `rio.reproject`:\n", "\n", " - [DataArray.reproject](../rioxarray.rst#rioxarray.raster_array.RasterArray.reproject)\n", " - [Dataset.reproject](../rioxarray.rst#rioxarray.raster_dataset.RasterDataset.reproject)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "upscale_factor = 2\n", "new_width = xds.rio.width * upscale_factor\n", "new_height = xds.rio.height * upscale_factor\n", "\n", "xds_upsampled = xds.rio.reproject(\n", " xds.rio.crs, \n", " shape=(new_height, new_width), \n", " resampling=Resampling.bilinear,\n", ")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 245, 574)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "xds.shape" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 490, 1148)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "xds_upsampled.shape" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3.0, -3.0)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "xds.rio.resolution()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1.5, -1.5)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "xds_upsampled.rio.resolution()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.8.8" } }, "nbformat": 4, "nbformat_minor": 4 }