Calculate transformation using output dimensions

Denis Rykov

In gdalwarp we have at least two options for managing of output size of a raster:

  • -tr xres yres: set output file resolution (in target georeferenced units)
  • -ts width height: set output file size in pixels and lines
In rasterio we have the following function:
  • rasterio.warp.calculate_default_transform(src_crs, dst_crs, width, height, left=None, bottom=None, right=None, top=None, gcps=None, resolution=None)
If I understood correctly resolution argument has the same meaning as tr in gdalwarp.
But how I can calculate transformation (destination affine first of all) if I know output dimensions (width and height)?

In other words I'm looking for how I can achieve the same result with rasterio as with gdalwarp ts option.

I hope what I have requested is possible.

Alan Snow

Looks like you are most of the way there.

If you have a dataset open with ``, then you should be able to get the information you need for the input:

with'/path/do/dataset.tif') as rds:
    left, bottom, right, top = rds.bounds
    src_crs =
    width = rds.width
    height = rds.height

Alan Snow

This may be what you are looking for as well:

Denis Rykov

Thanks for reply.

I could not find what I need in rasterio and have created PR: