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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'August 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2013-04-10 12:15:55 +04:00
|
|
|
import os
|
2012-09-15 18:25:25 +03:00
|
|
|
import subprocess
|
2013-04-10 12:15:55 +04:00
|
|
|
|
|
|
|
from PyQt4.QtCore import *
|
|
|
|
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.core.ProcessingLog import ProcessingLog
|
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-04-10 12:15:55 +04:00
|
|
|
class GdalUtils:
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
supportedRasters = None
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def runGdal(commands, progress):
|
|
|
|
settings = QSettings()
|
2013-06-03 21:25:22 +02:00
|
|
|
path = unicode(settings.value( "/GdalTools/gdalPath", ""))
|
|
|
|
envval = unicode(os.getenv("PATH"))
|
2012-09-15 18:25:25 +03:00
|
|
|
if not path.lower() in envval.lower().split(os.pathsep):
|
2013-06-03 21:25:22 +02:00
|
|
|
envval += "%s%s" % (os.pathsep, path)
|
2012-09-15 18:25:25 +03:00
|
|
|
os.putenv( "PATH", envval )
|
|
|
|
loglines = []
|
|
|
|
loglines.append("GDAL execution console output")
|
|
|
|
fused_command = ''.join(['%s ' % c for c in commands])
|
|
|
|
proc = subprocess.Popen(fused_command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE,stderr=subprocess.STDOUT, universal_newlines=False).stdout
|
|
|
|
for line in iter(proc.readline, ""):
|
|
|
|
loglines.append(line)
|
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 {}
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
'''this has been adapted from GdalTools plugin'''
|
|
|
|
if GdalUtils.supportedRasters != None:
|
|
|
|
return GdalUtils.supportedRasters
|
|
|
|
|
|
|
|
if gdal.GetDriverCount() == 0:
|
|
|
|
gdal.AllRegister()
|
|
|
|
|
|
|
|
GdalUtils.supportedRasters = {}
|
|
|
|
GdalUtils.supportedRasters["GTiff"] = ["tif"]
|
|
|
|
for i in range(gdal.GetDriverCount()):
|
|
|
|
driver = gdal.GetDriver(i)
|
|
|
|
if driver == None:
|
|
|
|
continue
|
2013-06-09 16:04:44 +02:00
|
|
|
shortName = driver.ShortName#.remove( QRegExp( '\(.*$' ) ).trimmed())
|
2012-09-15 18:25:25 +03:00
|
|
|
metadata = driver.GetMetadata()
|
2013-01-21 23:05:17 +01:00
|
|
|
if not metadata.has_key(gdal.DCAP_CREATE) or metadata[gdal.DCAP_CREATE] != 'YES':
|
|
|
|
continue
|
2012-09-15 18:25:25 +03:00
|
|
|
if metadata.has_key(gdal.DMD_EXTENSION):
|
2013-01-26 14:16:39 +01:00
|
|
|
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():
|
|
|
|
allexts = ["tif"]
|
|
|
|
for exts in GdalUtils.getSupportedRasters().values():
|
|
|
|
for ext in exts:
|
2013-01-21 23:05:17 +01:00
|
|
|
if ext not in allexts and ext != "":
|
2012-09-15 18:25:25 +03:00
|
|
|
allexts.append(ext)
|
|
|
|
return allexts
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def getFormatShortNameFromFilename(filename):
|
|
|
|
ext = filename[filename.rfind(".")+1:]
|
|
|
|
supported = GdalUtils.getSupportedRasters()
|
|
|
|
for name in supported.keys():
|
|
|
|
exts = supported[name]
|
|
|
|
if ext in exts:
|
|
|
|
return name
|
|
|
|
return "GTiff"
|
2013-04-10 12:15:55 +04:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def escapeAndJoin(strList):
|
2013-06-08 00:46:48 +02:00
|
|
|
joined = ""
|
2013-04-10 12:15:55 +04:00
|
|
|
for s in strList:
|
2013-06-08 00:46:48 +02:00
|
|
|
if " " in s:
|
|
|
|
escaped = '"' + s.replace('\\', '\\\\').replace('"', '\\"') + '"'
|
|
|
|
else:
|
|
|
|
escaped = s
|
|
|
|
joined += escaped + " "
|
|
|
|
return joined.strip()
|