Skip to main content
2 of 4
deleted 20 characters in body
Kadir Şahbaz
  • 78k
  • 57
  • 257
  • 404

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.