Skip to content

Instantly share code, notes, and snippets.

@agiudiceandrea
Last active September 29, 2021 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agiudiceandrea/2f0ef1e65858de1a35ef4b27b674c4ce to your computer and use it in GitHub Desktop.
Save agiudiceandrea/2f0ef1e65858de1a35ef4b27b674c4ce to your computer and use it in GitHub Desktop.
"""
***************************************************************************
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