Unexpected result using rasterio windowed writing - Access window out of range in RasterIO()


Hello everybody,

this is my first post in this group. It is copied directly from my question on gis.stackexchange.com in the hope I can receive a feedback to solve (or understand) my problem.
The question is this one (https://gis.stackexchange.com/questions/349183/unexpected-result-using-rasterio-windowed-writing-access-window-out-of-range-i).

I am trying to write (copy and paste) a raster into another raster using raserio's blocks and windows.

I have two rasters (`A` and `B`):

`A` has a greater extent than `B` and it completely contains B.
The two rasters share the same cell size (roughly 30m) and CRS (EPSG:4326).

The lower and right boundaries of `A` and `B` overlap like below:

I am having hard times understanding the problem of my code. I think the issue may be in the order I am iterating through each block when using `block_windows()` method, but I might be wrong.

Looking at the picture in the rasterio's documentation and printing the block indexes at each iteration, it seems the blocks should write east to west (let's say "horizontally"), and once the upper part is all written, the process should jump to the next lower row.

However, when I run my code, it seems the writing process happens "vertically" (north to south), and then jump to the next column). For this problem, when I reach the end of the first column, the code tries to write into a block that is outside `A` extent and I receive the error:

RasterioIOError('Read or write failed. /home/umberto/test/output/mosaic2.tif: Access window out of range in RasterIO().  Requested (42379,56159) of size 256x256 on raster of 85196x56160.')

Here is what I see in QGIS:

The black rectangle represents raster `A` where I am trying to paste `B` block by block. The whitish part in the middle of the raster represents the 256X256 blocks in the first column of `B` written to `A`.

Unfortunately, the writing process cannot jump to the next column as the the indexes of rows makes the code trying to access a part of the raster which is below the raster extent itself.

How am I doing it

The idea is to write each window of raster `B` into raster `A`. However, as the window object column and row offsets are different when considering the two rasters, I am translating for each block the offsets from `B` to `A` (window and new_window in the below code).

Here is my code (simplified but still relevant):

    with rasterio.open(inputfile, 'r') as src:
        with rasterio.open(
            os.path.join(dest_dir, 'mosaic2.tif'),
        ) as mosaic_raster:
            for ji, window in src.block_windows(1):
                r = src.read(1, window=window)
                left, top = src.xy(
                    window.col_off, window.row_off, offset='ul')
                right, bottom = src.xy(
                    window.col_off + window.width, window.row_off + window.height, offset='ul')
                new_window = from_bounds(
                    left, bottom, right, top, mosaic_raster.transform, window.height, window.width)
                mosaic_raster.write(r, 1, window=Window(int(new_window.col_off), int(
                    new_window.row_off), int(new_window.width), int(new_window.height)))

Join main@rasterio.groups.io to automatically receive all group messages.