From d45215ebc136c5f727378b94d8d2557e63a7def6 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Tue, 11 Jul 2017 17:20:53 +1000 Subject: [PATCH] Cleanup conversion of input layers to OGR compatible sources --- .../processing/algs/gdal/GdalAlgorithm.py | 45 ++++++++++++------- .../algs/gdal/ogr2ogrpointsonlines.py | 9 +--- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/python/plugins/processing/algs/gdal/GdalAlgorithm.py b/python/plugins/processing/algs/gdal/GdalAlgorithm.py index b970cf278d4..941238287cd 100644 --- a/python/plugins/processing/algs/gdal/GdalAlgorithm.py +++ b/python/plugins/processing/algs/gdal/GdalAlgorithm.py @@ -39,6 +39,8 @@ from qgis.core import (QgsApplication, from processing.core.GeoAlgorithm import GeoAlgorithm from processing.algs.gdal.GdalAlgorithmDialog import GdalAlgorithmDialog from processing.algs.gdal.GdalUtils import GdalUtils +from processing.tools.vector import ogrConnectionString, ogrLayerName + from processing.tools import dataobjects pluginPath = os.path.normpath(os.path.join( @@ -65,23 +67,36 @@ class GdalAlgorithm(QgsProcessingAlgorithm): def getConsoleCommands(self, parameters, context, feedback): return None + 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 + def processAlgorithm(self, parameters, context, feedback): commands = self.getConsoleCommands(parameters, context, feedback) - #layers = QgsProcessingUtils.compatibleVectorLayers(QgsProject.instance()) - #supported = QgsVectorFileWriter.supportedFormatExtensions() - #for i, c in enumerate(commands): - # for layer in layers: - # if layer.source() in c: - # exported = dataobjects.exportVectorLayer(layer, supported) - # exportedFileName = os.path.splitext(os.path.split(exported)[1])[0] - # c = c.replace(layer.source(), exported) - # if os.path.isfile(layer.source()): - # fileName = os.path.splitext(os.path.split(layer.source())[1])[0] - # c = re.sub('[\s]{}[\s]'.format(fileName), ' ' + exportedFileName + ' ', c) - # c = re.sub('[\s]{}'.format(fileName), ' ' + exportedFileName, c) - # c = re.sub('["\']{}["\']'.format(fileName), "'" + exportedFileName + "'", c) - - # commands[i] = c GdalUtils.runGdal(commands, feedback) # auto generate outputs diff --git a/python/plugins/processing/algs/gdal/ogr2ogrpointsonlines.py b/python/plugins/processing/algs/gdal/ogr2ogrpointsonlines.py index 773f965a767..9cff0062aa5 100644 --- a/python/plugins/processing/algs/gdal/ogr2ogrpointsonlines.py +++ b/python/plugins/processing/algs/gdal/ogr2ogrpointsonlines.py @@ -83,15 +83,8 @@ class Ogr2OgrPointsOnLines(GdalAlgorithm): return self.tr('Vector geoprocessing') def getConsoleCommands(self, parameters, context, feedback): - inLayer = self.parameterAsVectorLayer(parameters, self.INPUT_LAYER, context) - if inLayer is None or inLayer.dataProvider().name() == 'ogr': - ogrLayer = self.parameterAsCompatibleSourceLayerPath(parameters, self.INPUT_LAYER, context, QgsVectorFileWriter.supportedFormatExtensions(), feedback=feedback) - if inLayer is None: - inLayer = ogrLayer - else: - ogrLayer = ogrConnectionString(inLayer, context)[1:-1] + ogrLayer, layername = self.getOgrCompatibleSource(self.INPUT_LAYER, parameters, context, feedback) - layername = "'" + ogrLayerName(inLayer) + "'" distance = str(self.parameterAsDouble(parameters, self.DISTANCE, context)) geometry = str(self.parameterAsString(parameters, self.GEOMETRY, context))