Rasterio: Not boundless (default) read of dataset may lead to wrong output transform

Created on 3 Oct 2019  路  2Comments  路  Source: mapbox/rasterio

Following #1739 I'm cropping images using a not boundless read on a given window:

out_transform = dataset.window_transform(window)
crop = dataset.read(window=window, boundless=False, masked=True)
profile.update({"driver": driver, "height": crop.shape[1], "width": crop.shape[2], "transform": out_transform})

However, my transform gets wrong if the shape I'm cropping is only partially out of the bounding box of the original image. Is there a way to fix the transform or do I need to use using boundless=True ?

Most helpful comment

@nicocti I think your problem might come from the fact that when you do not-boundless reading, the window you pass is cropped to the dataset's size inside the read() function (see https://github.com/mapbox/rasterio/blob/94affc4fe30a6e04e67e5a7a3320f3c99a565a88/rasterio/_io.pyx#L299-L300)

The result is that the window really being used for the read is smaller than the window you pass to read() if your AOI is partially outside your dataset's footprint, and so your dataset.window_transform(window) is wrong as well, because it doesn't have the correct offset.

I think the solution to your problem could simply be to compute your transform using the same cropped window that is being used in read():

out_transform = dataset.window_transform(window.crop(dataset.height, dataset.width))

However, you would need to check carefully that you are getting exactly what you expect, because I'm not really sure what happens when the col_offset and row_offset of window.crop(dataset.height, dataset.width) are not round.

All 2 comments

@nicocti I think your problem might come from the fact that when you do not-boundless reading, the window you pass is cropped to the dataset's size inside the read() function (see https://github.com/mapbox/rasterio/blob/94affc4fe30a6e04e67e5a7a3320f3c99a565a88/rasterio/_io.pyx#L299-L300)

The result is that the window really being used for the read is smaller than the window you pass to read() if your AOI is partially outside your dataset's footprint, and so your dataset.window_transform(window) is wrong as well, because it doesn't have the correct offset.

I think the solution to your problem could simply be to compute your transform using the same cropped window that is being used in read():

out_transform = dataset.window_transform(window.crop(dataset.height, dataset.width))

However, you would need to check carefully that you are getting exactly what you expect, because I'm not really sure what happens when the col_offset and row_offset of window.crop(dataset.height, dataset.width) are not round.

That's it, closing the issue as it is solved for me. If I'm not this only one getting here, it might we worth adding it to the documentation.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sgillies picture sgillies  路  5Comments

snowman2 picture snowman2  路  3Comments

lwasser picture lwasser  路  3Comments

vincentsarago picture vincentsarago  路  3Comments

ungarj picture ungarj  路  4Comments