From 3687f1ff272b3fe33b4d7887c2a2b961482ef11f Mon Sep 17 00:00:00 2001 From: Admire Nyakudya Date: Sun, 17 Nov 2024 18:25:07 +0200 Subject: [PATCH 1/2] Fix running algs with virtual layers --- python/plugins/processing/algs/gdal/Buffer.py | 10 +++++++--- python/plugins/processing/algs/gdal/GdalAlgorithm.py | 2 +- python/plugins/processing/algs/gdal/GdalUtils.py | 4 ++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/python/plugins/processing/algs/gdal/Buffer.py b/python/plugins/processing/algs/gdal/Buffer.py index d3f23497bf44..ab1a3b115142 100644 --- a/python/plugins/processing/algs/gdal/Buffer.py +++ b/python/plugins/processing/algs/gdal/Buffer.py @@ -100,9 +100,12 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True): if source is None: raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT)) fields = source.fields() - source_details = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback, executing) - - geometry = self.parameterAsString(parameters, self.GEOMETRY, context) + source_details = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback, executing=True) + # QgsVectorFileWriter defaults to geometry column called geom when exporting virtual, memory and grass layer + if source_details.layer_name == 'INPUT' and 'INPUT.gpkg' in source_details.connection_string: + geometry = parameters[self.GEOMETRY] = 'geom' + else: + geometry = self.parameterAsString(parameters, self.GEOMETRY, context) distance = self.parameterAsDouble(parameters, self.DISTANCE, context) fieldName = self.parameterAsString(parameters, self.FIELD, context) dissolve = self.parameterAsBoolean(parameters, self.DISSOLVE, context) @@ -122,6 +125,7 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True): arguments = [ output_details.connection_string, source_details.connection_string, + '-nlt PROMOTE_TO_MULTI', '-dialect', 'sqlite', '-sql' diff --git a/python/plugins/processing/algs/gdal/GdalAlgorithm.py b/python/plugins/processing/algs/gdal/GdalAlgorithm.py index d896673a45ca..d4bdc52c8529 100644 --- a/python/plugins/processing/algs/gdal/GdalAlgorithm.py +++ b/python/plugins/processing/algs/gdal/GdalAlgorithm.py @@ -84,7 +84,7 @@ def getOgrCompatibleSource(self, parameters = {parameter_name: parameters[parameter_name].source} input_layer = self.parameterAsVectorLayer(parameters, parameter_name, context) - if input_layer is None or input_layer.providerType() in ('memory', 'grass'): + if input_layer is None or input_layer.providerType() in ('virtual', 'memory', 'grass'): if executing: # parameter is not a vector layer - try to convert to a source compatible with OGR # and extract selection if required diff --git a/python/plugins/processing/algs/gdal/GdalUtils.py b/python/plugins/processing/algs/gdal/GdalUtils.py index c4a2f21498bb..56e4584417b2 100644 --- a/python/plugins/processing/algs/gdal/GdalUtils.py +++ b/python/plugins/processing/algs/gdal/GdalUtils.py @@ -524,9 +524,9 @@ def ogrLayerName(uri): if r: return r.groups()[0] elif 'layername' in uri: - regex = re.compile('(layername=)([^|]*)') + regex = re.compile(r'(?<=layername=)[^:|&]+') r = regex.search(uri) - return r.groups()[1] + return r.groups()[0] fields = uri.split('|') basePath = fields[0] From 68b61df1544daaaa6ef7c2d2794c77707f665a49 Mon Sep 17 00:00:00 2001 From: Admire Nyakudya Date: Sun, 17 Nov 2024 20:41:35 +0200 Subject: [PATCH 2/2] revert gdalutis.py --- python/plugins/processing/algs/gdal/GdalUtils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/plugins/processing/algs/gdal/GdalUtils.py b/python/plugins/processing/algs/gdal/GdalUtils.py index 56e4584417b2..c4a2f21498bb 100644 --- a/python/plugins/processing/algs/gdal/GdalUtils.py +++ b/python/plugins/processing/algs/gdal/GdalUtils.py @@ -524,9 +524,9 @@ def ogrLayerName(uri): if r: return r.groups()[0] elif 'layername' in uri: - regex = re.compile(r'(?<=layername=)[^:|&]+') + regex = re.compile('(layername=)([^|]*)') r = regex.search(uri) - return r.groups()[0] + return r.groups()[1] fields = uri.split('|') basePath = fields[0]