Resampling Raster data in python using xarray and rioxarray¶
Resampling of raster data refers to the process of changing the spatial resolution of a raster image.
This can be done to either increase or decrease the size of the cells in the raster, effectively changing the level of detail represented in the image.
Resampling can be necessary when working with raster data from multiple sources with different resolutions, or when trying to balance the trade-off between spatial resolution and computational cost.
The most common methods for resampling include
- nearest neighbor,
- bilinear interpolation, and
- cubic convolution.
xarray is a powerful library for working with labeled multi-dimensional arrays in Python, providing convenient data storage and manipulation for large arrays of numerical data.
rioxarray is a library built on top of xarray, designed for working with geospatial raster data. It provides easy access to geospatial raster data and metadata, as well as transformations between different projections.
import rioxarray
from rasterio.enums import Resampling
topography=rioxarray.open_rasterio("ETopo.tif")
topography
<xarray.DataArray (band: 1, y: 10801, x: 21601)>
[233312401 values with dtype=float32]
Coordinates:
* band (band) int64 1
* x (x) float64 -180.0 -180.0 -180.0 -179.9 ... 180.0 180.0 180.0
* y (y) float64 90.0 89.98 89.97 89.95 ... -89.97 -89.98 -90.0
spatial_ref int64 0
Attributes:
_FillValue: -3.4028234663852886e+38
scale_factor: 1.0
add_offset: 0.0DataArray object representing a single band of a raster dataset. The data is represented by a 2D array with 10801 rows and 21601 columns, for a total of 2,333,124,01 values.
downscale_factor = 1/8
new_width = int(topography.rio.width * downscale_factor)
new_height = int(topography.rio.height * downscale_factor)
topography_downsampled = topography.rio.reproject(
topography.rio.crs,
shape=(new_height, new_width),
resampling=Resampling.bilinear,
)
This code block is an example of how to downscale a raster dataset using the rioxarray library in Python. The downscale factor is set to 1/8, meaning the new spatial resolution of the raster will be 8 times coarser than the original data.
The new_width and new_height variables are calculated by multiplying the original width and height of the raster by the downscale factor. These values represent the new number of columns and rows in the downscaled raster.
The topography_downsampled variable is assigned the result of the reproject method from the rioxarray library, which performs the resampling of the raster data. The crs (coordinate reference system) is passed as an argument to the reproject method to ensure that the spatial reference of the downscaled raster is the same as the original data. The shape argument is set to the new width and height calculated earlier, and the resampling argument is set to Resampling.bilinear, which specifies that bilinear interpolation should be used for the resampling process. The result of the reproject method is a new xarray DataArray object representing the downscaled raster data.
topography_downsampled
<xarray.DataArray (band: 1, y: 1350, x: 2700)>
array([[[-4222.027 , -4222.0156 , -4222.0107 , ..., -4222.2456 ,
-4222.1597 , -4222.1206 ],
[-4200.759 , -4200.4336 , -4200.0664 , ..., -4202.5303 ,
-4201.867 , -4201.2964 ],
[-4186.886 , -4186.4404 , -4185.887 , ..., -4189.7764 ,
-4188.6978 , -4187.727 ],
...,
[ 27.367725, 28.016602, 28.749756, ..., 25.2771 ,
25.998047, 26.632812],
[ -44.917835, -44.826904, -44.68579 , ..., -45.23535 ,
-45.12329 , -44.97198 ],
[ -56.118908, -56.13793 , -56.13793 , ..., -56.08944 ,
-56.10237 , -56.084423]]], dtype=float32)
Coordinates:
* x (x) float64 -179.9 -179.8 -179.7 -179.5 ... 179.7 179.8 179.9
* y (y) float64 89.93 89.8 89.67 89.53 ... -89.67 -89.8 -89.93
* band (band) int64 1
spatial_ref int64 0
Attributes:
scale_factor: 1.0
add_offset: 0.0
_FillValue: -3.4028235e+38Our Downscaled array DataArray object representing a single band of a raster dataset. The data is represented by a 2D array with 1350 rows and 2700 columns, for a total of 3,645,000 values.
