2012-10-04 19:33:47 +02:00
# -*- coding: utf-8 -*-
"""
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
GdalUtils . py
- - - - - - - - - - - - - - - - - - - - -
Date : August 2012
Copyright : ( C ) 2012 by Victor Olaya
Email : volayaf at gmail dot com
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* 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 *
* ( at your option ) any later version . *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
"""
2016-09-21 18:24:26 +02:00
from builtins import str
from builtins import range
from builtins import object
2012-10-04 19:33:47 +02:00
__author__ = ' Victor Olaya '
__date__ = ' August 2012 '
__copyright__ = ' (C) 2012, Victor Olaya '
2013-10-01 20:52:22 +03:00
2012-10-04 19:33:47 +02:00
# This will get replaced with a git SHA1 when you do a git archive
2013-10-01 20:52:22 +03:00
2012-10-04 19:33:47 +02:00
__revision__ = ' $Format: % H$ '
2013-04-10 12:15:55 +04:00
import os
2012-09-15 18:25:25 +03:00
import subprocess
2014-06-12 09:48:17 +02:00
import platform
2016-06-22 12:44:30 +03:00
from osgeo import gdal
2016-04-22 10:38:48 +02:00
from qgis . PyQt . QtCore import QSettings
2016-06-22 11:04:10 +03:00
from qgis . core import QgsApplication , QgsVectorFileWriter
2013-08-12 20:44:27 +02:00
from processing . core . ProcessingLog import ProcessingLog
2016-04-11 17:35:11 +02:00
from processing . core . SilentProgress import SilentProgress
2013-01-26 14:16:39 +01:00
try :
2013-02-23 16:00:54 +01:00
from osgeo import gdal
gdalAvailable = True
2013-01-26 14:16:39 +01:00
except :
2013-02-23 16:00:54 +01:00
gdalAvailable = False
2012-09-15 18:25:25 +03:00
2013-10-01 20:52:22 +03:00
2016-09-21 18:24:26 +02:00
class GdalUtils ( object ) :
2012-09-15 18:25:25 +03:00
supportedRasters = None
@staticmethod
2016-04-11 17:35:11 +02:00
def runGdal ( commands , progress = None ) :
if progress is None :
2016-04-11 20:32:32 +02:00
progress = SilentProgress ( )
2016-04-08 14:05:32 +02:00
envval = os . getenv ( ' PATH ' )
2014-06-12 09:48:17 +02:00
# We need to give some extra hints to get things picked up on OS X
2016-04-08 14:05:32 +02:00
isDarwin = False
try :
isDarwin = platform . system ( ) == ' Darwin '
except IOError : # https://travis-ci.org/m-kuhn/QGIS#L1493-L1526
pass
if isDarwin and os . path . isfile ( os . path . join ( QgsApplication . prefixPath ( ) , " bin " , " gdalinfo " ) ) :
# Looks like there's a bundled gdal. Let's use it.
os . environ [ ' PATH ' ] = " {} {} {} " . format ( os . path . join ( QgsApplication . prefixPath ( ) , " bin " ) , os . pathsep , envval )
os . environ [ ' DYLD_LIBRARY_PATH ' ] = os . path . join ( QgsApplication . prefixPath ( ) , " lib " )
2014-06-12 09:48:17 +02:00
else :
# Other platforms should use default gdal finder codepath
settings = QSettings ( )
2016-04-08 14:05:32 +02:00
path = settings . value ( ' /GdalTools/gdalPath ' , ' ' )
2014-06-12 09:48:17 +02:00
if not path . lower ( ) in envval . lower ( ) . split ( os . pathsep ) :
2016-04-08 14:05:32 +02:00
envval + = ' {} {} ' . format ( os . pathsep , path )
2014-06-12 09:48:17 +02:00
os . putenv ( ' PATH ' , envval )
2016-09-21 18:24:26 +02:00
fused_command = ' ' . join ( [ str ( c ) for c in commands ] )
2014-11-22 12:23:16 +02:00
progress . setInfo ( ' GDAL command: ' )
progress . setCommand ( fused_command )
progress . setInfo ( ' GDAL command output: ' )
2016-04-18 08:34:32 +02:00
success = False
retry_count = 0
while success == False :
loglines = [ ]
loglines . append ( ' GDAL execution console output ' )
try :
proc = subprocess . Popen (
fused_command ,
shell = True ,
stdout = subprocess . PIPE ,
stdin = open ( os . devnull ) ,
stderr = subprocess . STDOUT ,
universal_newlines = True ,
) . stdout
for line in proc :
progress . setConsoleInfo ( line )
loglines . append ( line )
success = True
except IOError as e :
if retry_count < 5 :
retry_count + = 1
else :
raise IOError ( e . message + u ' \n Tried 5 times without success. Last iteration stopped after reading {} line(s). \n Last line(s): \n {} ' . format ( len ( loglines ) , u ' \n ' . join ( loglines [ - 10 : ] ) ) )
2013-08-12 20:44:27 +02:00
ProcessingLog . addToLog ( ProcessingLog . LOG_INFO , loglines )
2012-09-15 18:25:25 +03:00
GdalUtils . consoleOutput = loglines
@staticmethod
def getConsoleOutput ( ) :
return GdalUtils . consoleOutput
@staticmethod
def getSupportedRasters ( ) :
2013-01-26 14:16:39 +01:00
if not gdalAvailable :
return { }
2013-10-01 20:52:22 +03:00
if GdalUtils . supportedRasters is not None :
2012-09-15 18:25:25 +03:00
return GdalUtils . supportedRasters
if gdal . GetDriverCount ( ) == 0 :
gdal . AllRegister ( )
GdalUtils . supportedRasters = { }
2013-10-01 20:52:22 +03:00
GdalUtils . supportedRasters [ ' GTiff ' ] = [ ' tif ' ]
2012-09-15 18:25:25 +03:00
for i in range ( gdal . GetDriverCount ( ) ) :
driver = gdal . GetDriver ( i )
2013-10-01 20:52:22 +03:00
if driver is None :
2012-09-15 18:25:25 +03:00
continue
2013-10-01 20:52:22 +03:00
shortName = driver . ShortName
2012-09-15 18:25:25 +03:00
metadata = driver . GetMetadata ( )
2014-09-26 07:45:45 +02:00
#===================================================================
# if gdal.DCAP_CREATE not in metadata \
# or metadata[gdal.DCAP_CREATE] != 'YES':
# continue
#===================================================================
2013-10-01 20:52:22 +03:00
if gdal . DMD_EXTENSION in metadata :
extensions = metadata [ gdal . DMD_EXTENSION ] . split ( ' / ' )
2012-09-15 18:25:25 +03:00
if extensions :
GdalUtils . supportedRasters [ shortName ] = extensions
return GdalUtils . supportedRasters
@staticmethod
def getSupportedRasterExtensions ( ) :
2013-10-01 20:52:22 +03:00
allexts = [ ' tif ' ]
2016-09-21 18:24:26 +02:00
for exts in list ( GdalUtils . getSupportedRasters ( ) . values ( ) ) :
2012-09-15 18:25:25 +03:00
for ext in exts :
2013-10-01 20:52:22 +03:00
if ext not in allexts and ext != ' ' :
2012-09-15 18:25:25 +03:00
allexts . append ( ext )
return allexts
2016-06-22 11:04:10 +03:00
@staticmethod
def getVectorDriverFromFileName ( filename ) :
ext = os . path . splitext ( filename ) [ 1 ]
if ext == ' ' :
return ' ESRI Shapefile '
formats = QgsVectorFileWriter . supportedFiltersAndFormats ( )
2016-09-27 19:51:06 +02:00
for k , v in list ( formats . items ( ) ) :
2016-06-22 11:04:10 +03:00
if ext in k :
return v
return ' ESRI Shapefile '
2012-09-15 18:25:25 +03:00
@staticmethod
def getFormatShortNameFromFilename ( filename ) :
2013-10-01 20:52:22 +03:00
ext = filename [ filename . rfind ( ' . ' ) + 1 : ]
2012-09-15 18:25:25 +03:00
supported = GdalUtils . getSupportedRasters ( )
2016-09-21 18:24:26 +02:00
for name in list ( supported . keys ( ) ) :
2012-09-15 18:25:25 +03:00
exts = supported [ name ]
if ext in exts :
return name
2013-10-01 20:52:22 +03:00
return ' GTiff '
2013-04-10 12:15:55 +04:00
@staticmethod
def escapeAndJoin ( strList ) :
2013-10-01 20:52:22 +03:00
joined = ' '
2013-04-10 12:15:55 +04:00
for s in strList :
2014-07-08 20:08:31 +03:00
if s [ 0 ] != ' - ' and ' ' in s :
2013-10-01 20:52:22 +03:00
escaped = ' " ' + s . replace ( ' \\ ' , ' \\ \\ ' ) . replace ( ' " ' , ' \\ " ' ) \
+ ' " '
2013-06-08 00:46:48 +02:00
else :
escaped = s
2013-10-01 20:52:22 +03:00
joined + = escaped + ' '
2014-07-08 20:08:31 +03:00
return joined . strip ( )
2016-06-22 12:44:30 +03:00
@staticmethod
def version ( ) :
return int ( gdal . VersionInfo ( ' VERSION_NUM ' ) )