Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[processing][gdal] Ignore 'selected features only' setting when
creating command preview

Since this has no meaning outside of a QGIS session

Refs #19451

(cherry-picked from 27d447b)
  • Loading branch information
nyalldawson committed Jul 25, 2018
1 parent 9ba794c commit 3d3be64
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions python/plugins/processing/algs/gdal/GdalAlgorithm.py
Expand Up @@ -32,6 +32,7 @@

from qgis.core import (QgsApplication,
QgsVectorFileWriter,
QgsProcessingFeatureSourceDefinition,
QgsProcessingAlgorithm,
QgsProcessingContext,
QgsProcessingFeedback)
Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions python/plugins/processing/tests/GdalAlgorithmsTest.py
Expand Up @@ -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):
Expand All @@ -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",
Expand Down

0 comments on commit 3d3be64

Please sign in to comment.