2012-10-04 19:33:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
GdalAlgorithm.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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
2015-05-07 13:14:01 +02:00
|
|
|
|
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$'
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
import os
|
2016-09-14 16:24:02 +03:00
|
|
|
import re
|
2016-01-22 15:45:09 +02:00
|
|
|
|
2017-06-27 16:15:07 +10:00
|
|
|
from qgis.PyQt.QtCore import QUrl, QCoreApplication
|
2016-01-22 15:45:09 +02:00
|
|
|
|
2017-04-03 15:41:32 +10:00
|
|
|
from qgis.core import (QgsApplication,
|
2017-04-05 14:37:06 +10:00
|
|
|
QgsVectorFileWriter,
|
|
|
|
QgsProcessingUtils,
|
2017-06-27 16:15:07 +10:00
|
|
|
QgsProcessingAlgorithm,
|
2017-04-05 14:37:06 +10:00
|
|
|
QgsProject)
|
2017-01-09 12:34:46 +10:00
|
|
|
|
2014-06-02 00:35:49 +02:00
|
|
|
from processing.core.GeoAlgorithm import GeoAlgorithm
|
2015-05-07 13:14:01 +02:00
|
|
|
from processing.algs.gdal.GdalAlgorithmDialog import GdalAlgorithmDialog
|
|
|
|
from processing.algs.gdal.GdalUtils import GdalUtils
|
2017-07-11 17:20:53 +10:00
|
|
|
from processing.tools.vector import ogrConnectionString, ogrLayerName
|
|
|
|
|
2017-03-04 19:41:23 +01:00
|
|
|
from processing.tools import dataobjects
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2015-05-18 19:51:26 +03:00
|
|
|
pluginPath = os.path.normpath(os.path.join(
|
|
|
|
os.path.split(os.path.dirname(__file__))[0], os.pardir))
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-06-27 16:15:07 +10:00
|
|
|
class GdalAlgorithm(QgsProcessingAlgorithm):
|
2014-06-13 09:03:15 +02:00
|
|
|
|
2016-10-04 19:53:21 +03:00
|
|
|
def __init__(self):
|
2017-06-27 16:15:07 +10:00
|
|
|
super().__init__()
|
2016-10-04 19:53:21 +03:00
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
def icon(self):
|
|
|
|
return QgsApplication.getThemeIcon("/providerGdal.svg")
|
|
|
|
|
|
|
|
def svgIconPath(self):
|
|
|
|
return QgsApplication.iconPath("providerGdal.svg")
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-06-27 16:15:07 +10:00
|
|
|
def createInstance(self, config={}):
|
|
|
|
return self.__class__()
|
|
|
|
|
2017-05-15 12:46:59 +10:00
|
|
|
def createCustomParametersWidget(self, parent):
|
2015-05-07 13:14:01 +02:00
|
|
|
return GdalAlgorithmDialog(self)
|
|
|
|
|
2017-06-27 17:20:57 +10:00
|
|
|
def getConsoleCommands(self, parameters, context, feedback):
|
2017-05-15 16:19:46 +10:00
|
|
|
return None
|
|
|
|
|
2017-07-11 17:20:53 +10:00
|
|
|
def getOgrCompatibleSource(self, parameter_name, parameters, context, feedback):
|
|
|
|
"""
|
|
|
|
Interprets a parameter as an OGR compatible source and layer name
|
|
|
|
"""
|
|
|
|
input_layer = self.parameterAsVectorLayer(parameters, parameter_name, context)
|
|
|
|
ogr_data_path = None
|
|
|
|
ogr_layer_name = None
|
|
|
|
if input_layer is None:
|
|
|
|
# parameter is not a vector layer - try to convert to a source compatible with OGR
|
|
|
|
# and extract selection if required
|
|
|
|
ogr_data_path = self.parameterAsCompatibleSourceLayerPath(parameters, parameter_name, context,
|
|
|
|
QgsVectorFileWriter.supportedFormatExtensions(),
|
|
|
|
feedback=feedback)
|
|
|
|
ogr_layer_name = ogrLayerName(ogr_data_path)
|
|
|
|
elif input_layer.dataProvider().name() == 'ogr':
|
|
|
|
# parameter is a vector layer, with OGR data provider
|
|
|
|
# so extract selection if required
|
|
|
|
ogr_data_path = self.parameterAsCompatibleSourceLayerPath(parameters, parameter_name, context,
|
|
|
|
QgsVectorFileWriter.supportedFormatExtensions(),
|
|
|
|
feedback=feedback)
|
|
|
|
ogr_layer_name = ogrLayerName(input_layer.dataProvider().dataSourceUri())
|
|
|
|
else:
|
|
|
|
# vector layer, but not OGR - get OGR compatible path
|
|
|
|
# TODO - handle "selected features only" mode!!
|
|
|
|
ogr_data_path = ogrConnectionString(input_layer.dataProvider().dataSourceUri(), context)[1:-1]
|
|
|
|
ogr_layer_name = ogrLayerName(input_layer.dataProvider().dataSourceUri())
|
|
|
|
return ogr_data_path, ogr_layer_name
|
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2017-06-27 16:15:07 +10:00
|
|
|
commands = self.getConsoleCommands(parameters, context, feedback)
|
2017-01-06 20:04:00 +10:00
|
|
|
GdalUtils.runGdal(commands, feedback)
|
2015-08-22 14:29:41 +02:00
|
|
|
|
2017-06-27 16:15:07 +10:00
|
|
|
# auto generate outputs
|
|
|
|
results = {}
|
|
|
|
for o in self.outputDefinitions():
|
|
|
|
results[o.name()] = parameters[o.name()]
|
|
|
|
return results
|
|
|
|
|
2017-06-06 10:13:18 +03:00
|
|
|
def helpUrl(self):
|
2017-01-09 15:32:42 +02:00
|
|
|
helpPath = GdalUtils.gdalHelpPath()
|
|
|
|
if helpPath == '':
|
2017-06-06 10:13:18 +03:00
|
|
|
return None
|
2017-01-09 15:32:42 +02:00
|
|
|
|
|
|
|
if os.path.exists(helpPath):
|
2017-06-06 10:13:18 +03:00
|
|
|
return QUrl.fromLocalFile(os.path.join(helpPath, '{}.html'.format(self.commandName()))).toString()
|
2017-01-09 15:32:42 +02:00
|
|
|
else:
|
2017-06-06 10:13:18 +03:00
|
|
|
return helpPath + '{}.html'.format(self.commandName())
|
2015-08-22 14:29:41 +02:00
|
|
|
|
2015-05-21 15:43:47 +02:00
|
|
|
def commandName(self):
|
2017-05-17 08:12:43 +10:00
|
|
|
parameters = {}
|
2017-05-19 10:24:59 +10:00
|
|
|
for output in self.outputs:
|
2015-05-21 15:43:47 +02:00
|
|
|
output.setValue("dummy")
|
2017-05-17 08:12:43 +10:00
|
|
|
for param in self.parameterDefinitions():
|
|
|
|
parameters[param.name()] = "1"
|
2017-05-15 16:19:46 +10:00
|
|
|
name = self.getConsoleCommands(parameters)[0]
|
2015-05-21 15:43:47 +02:00
|
|
|
if name.endswith(".py"):
|
|
|
|
name = name[:-3]
|
|
|
|
return name
|
2017-06-27 16:15:07 +10:00
|
|
|
|
|
|
|
def tr(self, string, context=''):
|
|
|
|
if context == '':
|
|
|
|
context = self.__class__.__name__
|
|
|
|
return QCoreApplication.translate(context, string)
|