Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
"""
***************************************************************************
createGeoTIFF.py
---------------------
Date : September 2021
Copyright : (C) 2021 by Andrea Giudiceandrea
Email : andreaerdna at libero dot it
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* any later version. *
* *
***************************************************************************
"""
from osgeo import gdal
from shutil import copyfile
def createGeoTIFF(src, dst):
try:
copyfile(src, dst)
except Exception as e:
print("Errore durante la copia del file")
print(e)
return
src = gdal.Open(src)
if not src:
print("Errore durante l'apertura del file di input")
src = None
return
spatialref = src.GetSpatialRef()
geotransform = src.GetGeoTransform()
ngcps = src.GetGCPCount()
gcps = src.GetGCPs()
srs = src.GetGCPSpatialRef()
bandsMetadata = [src.GetRasterBand(i+1).GetMetadata() for i in range(src.RasterCount)]
src = None
dst = gdal.Open(dst,1)
if not dst:
print("Errore durante l'apertura del file di output")
dst = None
return
if not gcps:
dst.SetSpatialRef(spatialref)
dst.SetGeoTransform(geotransform)
else:
dst.SetGCPs(gcps, srs)
for i in range(len(bandsMetadata)):
dst.GetRasterBand(i+1).SetMetadata(bandsMetadata[i])
dst = None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment