I am trying to rotate geotifs by different amounts in python, GDAl and/or QGIS and keep the georeference information the same. Is there a way to do this?
Asked
Active
Viewed 1,453 times
1 Answers
0
You can do this by modifying the GeoTransform. If you want to modify a GDAL image "in-place" (i.e. without creating a new image), you can open it in read-write mode:
from osgeo import gdal
Image = gdal.Open('ImageName.tif', 1) # 1 = read-write, 0 = read-only
GeoTransform = Image.GetGeoTransform()
The GeoTransform is a tuple containing the following attributes: (UpperLeftX, PixelSizeX, RowRotation, UpperLeftY, ColRotation, -PixelSizeY)
Tuples are immutable in Python, so to modify the GeoTransform you will have to convert it into a list, then revert back again into a tuple before writing it to the GDAL image:
GeoTransform = list(GeoTransform)
GeoTransform[2] = 0.0 # set the new RowRotation here!
GeoTransform[-2] = 0.0 # set the new ColRotation here!
GeoTransform = tuple(GeoTransform)
Image.SetGeoTransform(GeoTransform) # write GeoTransform to image
del Image # close the dataset