To alter the grid spacing when resampling a raster with rasterio, you can use the out_transform
argument in the rasterio.warp.reproject
function.
Here is an example code snippet to demonstrate how to do this:
import rasterio
from rasterio.enums import Resampling
# Open the input raster
with rasterio.open('input.tif') as src:
# Define the output grid spacing
out_transform = src.transform.scale(2.0, 1.0)
# Define the output raster dimensions
out_height = int(src.height / 2)
out_width = int(src.width / 1)
# Define the output metadata
profile = src.profile.copy()
profile.update({
'transform': out_transform,
'height': out_height,
'width': out_width,
})
# Resample the raster with the altered grid spacing
with rasterio.open('output.tif', 'w', **profile) as dst:
rasterio.warp.reproject(
source=rasterio.band(src, 1),
destination=rasterio.band(dst, 1),
src_transform=src.transform,
dst_transform=out_transform,
src_crs=src.crs,
dst_crs=src.crs,
resampling=Resampling.bilinear,
)
In this example, the out_transform
argument is defined using the scale
method to change the x-axis grid spacing to 2.0, while keeping the y-axis grid spacing the same at 1.0. The output raster dimensions are calculated based on the new grid spacing, and the output metadata is updated to reflect the changes. Finally, the rasterio.warp.reproject
function is called with the out_transform
argument to resample the input raster with the altered grid spacing.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-06-28 17:28:02 +0000
Seen: 8 times
Last updated: Jun 28 '23