Skip to content
Permalink
Browse files

[processing][gdal] Ignore 'selected features only' setting when

creating command preview

Since this has no meaning outside of a QGIS session

Refs #19451
  • Loading branch information
nyalldawson committed Jul 24, 2018
1 parent ed94b69 commit 27d447b79dce73aedc90e3b310dda9985cf8f75f
@@ -32,6 +32,7 @@

from qgis.core import (QgsApplication,
QgsVectorFileWriter,
QgsProcessingFeatureSourceDefinition,
QgsProcessingAlgorithm,
QgsProcessingContext,
QgsProcessingFeedback)
@@ -75,6 +76,12 @@ def getOgrCompatibleSource(self, parameter_name, parameters, context, feedback,
Interprets a parameter as an OGR compatible source and layer name
:param executing:
"""
if not executing and parameter_name in parameters and isinstance(parameters[parameter_name], QgsProcessingFeatureSourceDefinition):
# if not executing, then we throw away all 'selected features only' settings
# since these have no meaning for command line gdal use, and we don't want to force
# an export of selected features only to a temporary file just to show the command!
parameters = {parameter_name: parameters[parameter_name].source}

input_layer = self.parameterAsVectorLayer(parameters, parameter_name, context)
ogr_data_path = None
ogr_layer_name = None
@@ -153,6 +153,16 @@ def testGetOgrCompatibleSourceFromMemoryLayer(self):
self.assertTrue(res.isValid())
self.assertEqual(res.featureCount(), 2)

# with memory layers - if not executing layer source should be ignored and replaced
# with a dummy path, because:
# - it has no meaning for the gdal command outside of QGIS, memory layers don't exist!
# - we don't want to force an export of the whole memory layer to a temp file just to show the command preview
# this might be very slow!
ogr_data_path, ogr_layer_name = alg.getOgrCompatibleSource('INPUT', parameters, context, feedback,
executing=False)
self.assertEqual(ogr_data_path, 'path_to_data_file')
self.assertEqual(ogr_layer_name, 'layer_name')

QgsProject.instance().removeMapLayer(layer)

def testGetOgrCompatibleSourceFromOgrLayer(self):
@@ -173,6 +183,12 @@ def testGetOgrCompatibleSourceFromOgrLayer(self):
path, layer = alg.getOgrCompatibleSource('INPUT', {'INPUT': vl.id()}, context, feedback, False)
self.assertEqual(path, source)

# with selected features only - if not executing, the 'selected features only' setting
# should be ignored (because it has no meaning for the gdal command outside of QGIS!)
parameters = {'INPUT': QgsProcessingFeatureSourceDefinition(vl.id(), True)}
path, layer = alg.getOgrCompatibleSource('INPUT', parameters, context, feedback, False)
self.assertEqual(path, source)

def testGetOgrCompatibleSourceFromFeatureSource(self):
# create a memory layer and add to project and context
layer = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer",

0 comments on commit 27d447b

Please sign in to comment.
You can’t perform that action at this time.