reproject: partially covered raster cells reset fully to nodata

Fabian Hofmann

Dear community,

I'm trying to tackle the following problem: Raster A has a high resolution and a small extent. I should be projected to Raster B with small resolution and large extent. Cells from Raster A which partially cover cells in Raster B are getting ignored in the reprojection. Only cells in Raster B which are fully (!) covered by Raster A are getting non-nodata entries. I am not sure if this is intended.

Reproduce the behavior with


import numpy as np
import rasterio as rio
from rasterio.warp import reproject, transform_bounds

src_crs = 3035
src_top = 3200000.0
src_left = 4000000.0
src_res = 100
src_shape = (100, 100)
src_transform = rio.Affine(src_res, 0.0, src_left, 0.0, -src_res, src_top)
src_right, src_bottom = src_transform * src_shape
src = np.ones(src_shape)

dst_crs = 4326
dst_top = 52
dst_left = 5
dst_res = 0.25
dst_shape = (10, 10)
dst_transform = rio.Affine(dst_res, 0, dst_left, 0, -dst_res, dst_top)
dst_right, dst_bottom = dst_transform * dst_shape
dst = np.zeros(dst_shape)

# check destination bounds comprise source bounds
covered = prj_left, prj_bottom, prj_right, prj_top = (
    transform_bounds(src_crs, dst_crs, src_left, src_bottom, src_right, src_top))
assert prj_left > dst_left and prj_bottom > dst_bottom
assert prj_right < dst_right and prj_top < dst_top

reproject(src, dst,
          src_transform=src_transform, src_crs=src_crs,
          dst_transform=dst_transform, dst_crs=dst_crs,
          src_nodata=0, dst_nodata=0, init_dest_nodata=True)

assert dst.max() > 0


As soon as you change the target resolution to dst_res=0.1, the reprojected values contain non-zero values. However cells which should have some values, because Raster A partially covers it, get only nodata (in this case 0).

What I'm doing to avoid the issue is to pad around the src values. But this needs an extra calculation to get the needed pad_width in order to cover the whole cells in Raster B. (The operation is done many times). Is there a better way? Or is this even a bug?

Hope the explanation was clear, thanks in advance!


Fabian Hofmann

Fabian Hofmann
Frankfurt Institute for Adavanced Studies (FIAS)
FIAS Renewable Energy Systems & Network Analysis (FRESNA)

Ruth-Moufang-Straße 1, 60438 Frankfurt am Main
T +49 69 798 47605 | hofmann@...