问题描述
我试图在 python、GDAl 和/或 QGIS 中以不同的量旋转 geotifs,并保持地理参考信息相同。 有没有办法做到这一点?
解决方法
您可以通过 modifying the GeoTransform 执行此操作。如果您想“就地”修改 GDAL 图像(即不创建新图像),您可以以读写模式打开它:
from osgeo import gdal
Image = gdal.Open('ImageName.tif',1) # 1 = read-write,0 = read-only
GeoTransform = Image.GetGeoTransform()
GeoTransform 是一个包含以下属性的元组: (UpperLeftX,PixelSizeX,RowRotation,UpperLeftY,ColRotation,-PixelSizeY)
元组在 Python 中是不可变的,因此要修改 GeoTransform,您必须将其转换为列表,然后在将其写入 GDAL 图像之前再次恢复为元组:
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