Skip to main content
3 of 4
added 672 characters in body

KML Image Overlay conversion with GDAL

In my industry, KML/KMZ seems to be the standard spatial data format and it is a HASSLE when converting between typical formats.

I have a large number of KML image overlays and am tasked with converting them to TIFF. Typically through QGIS its easy, however I need to automate this given the volume. I've gotten them all written properly, however the image rotations are not preserved.

from gdal import GetDriverByName, OpenEx

tiff_driver = GetDriverByName("GTiff")

in_ds = OpenEx(KML_PATH) 

primary_band = in_ds.GetRasterBand(1)

projection = ds.GetProjection()

geo_transform = in_ds.GetGeoTransform()

out_ds = tiff_driver.Create(
     out_path,
     primary_band.XSize, 
     primary_band.YSize, 
     in_ds.RasterCount, 
     primary_band.DataType)

out_ds.SetProjection(projection)
out_ds.SetGeoTransform(geo_transform)

for i in range(1, ds.RasterCount + 1):
     in_band = ds.GetRasterBand(i)
     in_data = in_band.ReadAsArray()
     out_band = out_ds.GetRasterBand(i)
     out_band.WriteArray(in_data)

out_ds.FlushCache()
out_ds = None

The resulting .TIFF files are oriented true north. I can see a "rotation" tag in the KML, however can't find any GDAL options for rotating the new image.

Google Earth screenshot of proper rotation, kml format:

enter image description here

QGIS screenshot of non-rotated geotiff: enter image description here

I've tried the following:

  • Replacing the 'rotation' values in the GeoTransform to the KML's rotation in radians
  • This simple change of the GeoTransform (gt[0], gt1 * cos(rot), gt2 * -sin(rot), gt[3], gt[4] * sin(rot), gt[5] * cos(rot))

Neither work properly.

I assume this may involve using the Affine library, however a.rotation(ANGLE) isn't giving any useful results.