How to use GDAL to save raster files in Python
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.
Today I will write about how to save rasters using GDAL in Python, without using rasterio.
I also have a previous post about reading and saving raster data on Python, that can provide useful context to this one.
Solutions using rasterio work, we all know that. However, installing rasterio and GDAL in the same conda environment is many times a challenge… So I usually try not to install both of them together.
I am showing to you today what I think is, in my experience, the easiest way to save raster data using GDAL.
For reference, I am using Python 3.10, GDAL 3.0, and my IDE of choice is Spyder 5.3.3, running on a Windows 10 machine.
Import GDAL package in Python
from osgeo import gdal
Import OGR Spatial Reference
Choose to save as .tif
driver = gdal.GetDriverByName('GTiff')
Define the location to save your raster
path_output = 'C:/path_to_save.tif'
Define the number of pixels in your raster
Usually, it is defined by the size of the array you want to save as a raster.
Create the space where your dataset will be placed
Attention: the options for your driver might be different from those.
Specifically, be attentive to the data type of the array you are saving as a raster (the fifth argument). Please check all available data types on the GDAL documentation.
The fourth argument, here simply listed as “1”, is the number of bands your output raster should have.
It is also possible to append other options, as strings, after these five mandatory arguments.
Learn more about GDAL Create on GDAL docs.
Set the geographic transformation
Usually, this information will be based on another raster.
Use the snippet below if you need to extract a GeoTransform from another raster.
orig_file = gdal.Open(filename, gdal.GA_ReadOnly) GT_output = orig_file.GetGeoTransform()
Set the GeoTransform:
Set spatial reference for the GeoTIFF
srs = osr.SpatialReference() srs.SetWellKnownGeogCS('WGS84')
Note: Replace “WGS84” for another spatial reference, when it’s the case.
Finally, write the array into the raster.
Free the memory the driver was using
export = None
This should result in a saved raster.
What if I prefer to use rasterio (a.k.a. rio)?
You can definitely keep using your preferred packages. This post simply shows that it is possible to save rasters on Python using GDAL itself.
Why should we set the variable “export” to None?
I believe GDAL does have an explicit way to close the datasets, GDALClose. But by setting the exported data to None value, we flag that we are no longer using that memory space. That also helps to prevent errors such as trying to open a file in QGIS or ArcMap and receiving an error message that the file is in use by another program, or that the raster could not be opened.
Why should we search the GTiff driver by name?
This is the simplest way to pick the right driver, in order to save your file with the desired filetype.
The list of the available drivers can be found on the GDAL documentation.
- How to read grib2 raster data on Python
- Solved: new version rasterio 1.0+ won’t let me use GDAL transforms
- How to open, edit and save raster files (.tif) using Python
- Solved: QGIS 3.20 Python Console shows an error when running Clip Vector by Extent tool
- Solved: GDAL accuses no free disk space when there is plenty