简体   繁体   中英

Geotiff Rotation Python/GDAL/QGIS

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?

You can do this by modifying the GeoTransform . If you want to modify a GDAL image "in-place" (ie 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM