Created
September 29, 2021 16:26
-
-
Save agiudiceandrea/fc547523a45506d15243b7627be5cc89 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
*************************************************************************** | |
convertToGeoTIFFs.py | |
--------------------- | |
Date : 29/09/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 | |
from glob import iglob | |
from os import path | |
def convertToGeoTIFFs(src_path_wildcards, dst_dir, overwrite=False): | |
""" | |
src_path_wildcards: un percorso con wildcards (es. 'C:\\path\\*.tif') | |
dst_dir: il percorso della directory di ouptut (es. 'C:\\path\\output\\') | |
overwrite: True/False per sovrascrivere oppure no i file di output già esistenti (default: False) | |
""" | |
def convert(src, dst): | |
try: | |
copyfile(src, dst) | |
except Exception as e: | |
print("Errore durante la copia del file: {}".format(src)) | |
print(e) | |
return | |
src = gdal.Open(src) | |
if not src: | |
print("Errore durante l'apertura del file di input: {}".format(src)) | |
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: {}".format(dst)) | |
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 | |
if not path.isdir(dst_dir): | |
try: | |
os.mkdir(dst_dir) | |
except Exception as e: | |
print("Errore durante la creazione della directory di output: {}".format(dst_dir)) | |
print(e) | |
return | |
iter_src_files = iglob(src_path_wildcards) | |
nessun_file = True | |
for src_file in iter_src_files: | |
nessun_file = False | |
src_filename = path.basename(src_file) | |
dst_file = path.join(dst_dir, src_filename) | |
if not overwrite and path.isfile(dst_file): | |
print('File di output già esistente: {}'.format(dst_file)) | |
continue | |
convert(src_file, dst_file) | |
if nessun_file: | |
print("Nessun file di input trovato") | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment