Skip to main content
Tweeted twitter.com/StackGIS/status/1233633170517565441
added 650 characters in body
Source Link

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]gt3, 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.

Another Update:

Using the Affine library, I can get the proper rotation, however each image seems translated. I'm not manually creating the Affine object, importing from GDAL:

gt = in_ds.GetGeoTransform()
aff_gt = Affine.from_gdal(*gt)

aff_gt *= Affine.rotation(-rotation)

out_ds.SetGeoTransform(aff_gt.to_gdal())

This in effect rotates the original GeoTransform via the Affine library, rotation angle seems legit, however still not giving me what I expected as each image is translated:

enter image description here Seriously, any direction helps here!

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.

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), gt3, 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.

Another Update:

Using the Affine library, I can get the proper rotation, however each image seems translated. I'm not manually creating the Affine object, importing from GDAL:

gt = in_ds.GetGeoTransform()
aff_gt = Affine.from_gdal(*gt)

aff_gt *= Affine.rotation(-rotation)

out_ds.SetGeoTransform(aff_gt.to_gdal())

This in effect rotates the original GeoTransform via the Affine library, rotation angle seems legit, however still not giving me what I expected as each image is translated:

enter image description here Seriously, any direction helps here!

added 672 characters in body
Source Link

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.

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.

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.

deleted 20 characters in body
Source Link
Kadir Şahbaz
  • 78k
  • 57
  • 257
  • 404

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.

Anything helps!!

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.

Anything helps!!

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.

Source Link
Loading