Solved: new version rasterio 1.0+ won’t let me use GDAL transforms
Fellow researchers and open-source GIS enthusiasts,
Welcome to my blog!
I’d like to start with a disclaimer – I may be a researcher of this very area but that doesn’t mean everything I do or write here will work for you, in your own desktop configurations and package versions. I have no responsibility if you lose data or mess up your installation. I also do not authorize any copies of my content.
This week, I am discussing an error which used to be a warning on the old rasterio - and I ignored it. Lesson learned!
I was doing my usual savings of GeoTIFF rasters in an updated python environment (with rasterio 1.2.6) when I saw this error:
TypeError: GDAL-style transforms have been deprecated. This exception will be raised for a period of time to highlight potentially confusing errors, but will eventually be removed.
As we read, in future versions, this error won’t be shown, and rasterio will either try to interpret the provided GDAL geotransform as an Affine transform, or provide a different error message. If you are reading this post while using posterior releases of rasterio (that, at this moment, don’t exist yet), know that a general error or unexpected results of rasterio used with GDAL-type transforms can be indicatives of the same problem I am describing here.
What caused the error for me
The code I was trying to run was somewhat similar to the ones shown in this post.
First, I called rasterio to know the transform of an analogous .tif file (so I could use the same transform).
with rasterio.open('…testtif.tif') as dataset: print(dataset.crs)
Copied the geotransform and added it to a variable:
Transform=(-120.10000000000001, 0.1, 0.0, 32.3, 0.0, -0.09999999999999999)
And then tried to save my analogous file using this same transform.
saida = np.float32(prec) with rasterio.open( 'prec1.tif', 'w', driver='GTiff', height=saida.shape, width=saida.shape, count=1, dtype=np.float32, crs=dataset.crs, transform=Transform, ) as dest_file: dest_file.write(saida, 1) dest_file.close()
but… the error appeared!
Luckily, GDAL style transforms are deprecated, but it is so easy to make them into Affine style transforms! This StackExchange answer gave me what I needed to know for a quick solve. To quickly do what you need to do, just run:
from affine import aff af_transf = aff.from_gdal(*Transform)
and then run the rest of your code, but call the affine transform instead of the GDAL one. There was no need to change the CRS reference when saving.
saida = np.float32(prec) with rasterio.open( 'prec1.tif', 'w', driver='GTiff', height=saida.shape, width=saida.shape, count=1, dtype=np.float32, crs=dataset.crs, transform=af_transform, ) as dest_file: dest_file.write(saida, 1) dest_file.close()
You don’t need to install anything to load affine, if you are already using rasterio.
Why does it happen?
These errors are starting to appear now as we migrate from old rasterio versions to the newer ones. This error was implemented in version 1.0 of rasterio as to deprecate the GDAL style geotransforms, in favor of affine-style geotransforms. This can be seen on the documentation of rasterio 1.0.
Essentially, GDAL and affine geotransforms have the same information, in a different order. GDAL transforms are like: (c, a, b, f, d, e) while affine are (a, b, c, d, e, f). So, another possibility would be to rearrange these values yourself before calling the transform. More informaion about the differences of GDAL and rasterio geotransforms can be found on the rasterio documentation.
- How to read grib2 raster data on Python
- How to open, edit and save raster files (.tif) using Python
- Solved: GDAL accuses no free disk space when there is plenty
- How and when to use QGIS 3 tools “Warp (reproject)”, “Clip raster by extent” and “Clip raster by mask layer”
- Batch processing – automatizing the use of GDAL and SAGA GIS tools using Bash Shell scripts