Example - Resampling

This example demonstrates how to reproduce rasterio’s resampling example here.

[1]:
from rasterio.enums import Resampling

import rioxarray

%matplotlib inline

Load in xarray dataset

See docs for rioxarray.open_rasterio

Notes:

  • masked=True will convert from integer to float64 and fill with NaN. If this behavior is not desired, you can skip this.

[2]:
xds = rioxarray.open_rasterio(
    "../../test/test_data/compare/small_dem_3m_merged.tif",
    masked=True,
)

Resampling

API Reference for rio.reproject:

[3]:
upscale_factor = 2
new_width = xds.rio.width * upscale_factor
new_height = xds.rio.height * upscale_factor

xds_upsampled = xds.rio.reproject(
    xds.rio.crs,
    shape=(new_height, new_width),
    resampling=Resampling.bilinear,
)
[4]:
xds.shape
[4]:
(1, 245, 574)
[5]:
xds_upsampled.shape
[5]:
(1, 490, 1148)
[6]:
xds.rio.resolution()
[6]:
(3.0, -3.0)
[7]:
xds_upsampled.rio.resolution()
[7]:
(1.5, -1.5)