Skip to content
Permalink
Browse files

[processing] Fix creation of new layers doesn't default output folder

Fixes #19637
  • Loading branch information
nyalldawson committed Aug 21, 2018
1 parent 8f3ac45 commit 5241ff3c220d60198ba910019636583f0731edeb
@@ -30,7 +30,7 @@
import warnings

from qgis.PyQt import uic
from qgis.PyQt.QtCore import QCoreApplication, QDir, pyqtSignal
from qgis.PyQt.QtCore import QCoreApplication, QDir, pyqtSignal, QFileInfo
from qgis.PyQt.QtWidgets import QDialog, QMenu, QAction, QFileDialog, QInputDialog
from qgis.PyQt.QtGui import QCursor
from qgis.gui import QgsEncodingSelectionDialog
@@ -331,6 +331,17 @@ def getValue(self):
if not key and self.parameter.flags() & QgsProcessingParameterDefinition.FlagOptional:
return None

if key and not key.startswith('memory:') \
and not key.startswith('ogr:') \
and not key.startswith('postgres:') \
and not key.startswith('postgis:'):
# output should be a file path
folder = QFileInfo(key).path()
if folder == '.':
# output name does not include a folder - use default
default_folder = ProcessingConfig.getSetting(ProcessingConfig.OUTPUT_FOLDER)
key = QDir(default_folder).filePath(key)

if isinstance(self.parameter, QgsProcessingParameterFolderDestination):
return key

@@ -29,6 +29,12 @@
from qgis.core import (QgsApplication,
QgsCoordinateReferenceSystem,
QgsProcessingParameterMatrix,
QgsProcessingOutputLayerDefinition,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterFileDestination,
QgsProcessingParameterFolderDestination,
QgsProcessingParameterVectorDestination,
QgsProcessingParameterRasterDestination,
QgsVectorLayer,
QgsProject)
from qgis.analysis import QgsNativeAlgorithms
@@ -37,10 +43,13 @@
from processing.gui.BatchAlgorithmDialog import BatchAlgorithmDialog
from processing.modeler.ModelerParametersDialog import ModelerParametersDialog
from processing.gui.wrappers import *
from processing.gui.DestinationSelectionPanel import DestinationSelectionPanel

start_app()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())

testDataPath = os.path.join(os.path.dirname(__file__), 'testdata')


class AlgorithmDialogTest(unittest.TestCase):

@@ -52,6 +61,10 @@ def testCreation(self):

class WrappersTest(unittest.TestCase):

@classmethod
def setUpClass(cls):
ProcessingConfig.initialize()

def checkConstructWrapper(self, param, expected_wrapper_class):
alg = QgsApplication.processingRegistry().algorithmById('native:centroids')

@@ -270,6 +283,118 @@ def testNumber(self):
def testBand(self):
self.checkConstructWrapper(QgsProcessingParameterBand('test'), BandWidgetWrapper)

def testFeatureSink(self):
param = QgsProcessingParameterFeatureSink('test')
alg = QgsApplication.processingRegistry().algorithmById('native:centroids')
panel = DestinationSelectionPanel(param, alg)

panel.setValue('memory:')
v = panel.getValue()
self.assertIsInstance(v, QgsProcessingOutputLayerDefinition)
self.assertEqual(v.createOptions, {'fileEncoding': 'System'})
self.assertEqual(v.sink.staticValue(), 'memory:')

panel.setValue('''ogr:dbname='/me/a.gpkg' table="d" (geom) sql=''')
v = panel.getValue()
self.assertIsInstance(v, QgsProcessingOutputLayerDefinition)
self.assertEqual(v.createOptions, {'fileEncoding': 'System'})
self.assertEqual(v.sink.staticValue(), '''ogr:dbname='/me/a.gpkg' table="d" (geom) sql=''')

panel.setValue('''postgis:dbname='oraclesux' host=10.1.1.221 port=5432 user='qgis' password='qgis' table="stufff"."output" (the_geom) sql=''')
v = panel.getValue()
self.assertIsInstance(v, QgsProcessingOutputLayerDefinition)
self.assertEqual(v.createOptions, {'fileEncoding': 'System'})
self.assertEqual(v.sink.staticValue(), '''postgis:dbname='oraclesux' host=10.1.1.221 port=5432 user='qgis' password='qgis' table="stufff"."output" (the_geom) sql=''')

panel.setValue('/home/me/test.shp')
v = panel.getValue()
self.assertIsInstance(v, QgsProcessingOutputLayerDefinition)
self.assertEqual(v.createOptions, {'fileEncoding': 'System'})
self.assertEqual(v.sink.staticValue(), '/home/me/test.shp')

ProcessingConfig.setSettingValue(ProcessingConfig.OUTPUT_FOLDER, testDataPath)
panel.setValue('test.shp')
v = panel.getValue()
self.assertIsInstance(v, QgsProcessingOutputLayerDefinition)
self.assertEqual(v.createOptions, {'fileEncoding': 'System'})
self.assertEqual(v.sink.staticValue(), os.path.join(testDataPath, 'test.shp'))

def testVectorDestination(self):
param = QgsProcessingParameterVectorDestination('test')
alg = QgsApplication.processingRegistry().algorithmById('native:centroids')
panel = DestinationSelectionPanel(param, alg)

panel.setValue('''ogr:dbname='/me/a.gpkg' table="d" (geom) sql=''')
v = panel.getValue()
self.assertIsInstance(v, QgsProcessingOutputLayerDefinition)
self.assertEqual(v.createOptions, {'fileEncoding': 'System'})
self.assertEqual(v.sink.staticValue(), '''ogr:dbname='/me/a.gpkg' table="d" (geom) sql=''')

panel.setValue('''postgis:dbname='oraclesux' host=10.1.1.221 port=5432 user='qgis' password='qgis' table="stufff"."output" (the_geom) sql=''')
v = panel.getValue()
self.assertIsInstance(v, QgsProcessingOutputLayerDefinition)
self.assertEqual(v.createOptions, {'fileEncoding': 'System'})
self.assertEqual(v.sink.staticValue(), '''postgis:dbname='oraclesux' host=10.1.1.221 port=5432 user='qgis' password='qgis' table="stufff"."output" (the_geom) sql=''')

panel.setValue('/home/me/test.shp')
v = panel.getValue()
self.assertIsInstance(v, QgsProcessingOutputLayerDefinition)
self.assertEqual(v.createOptions, {'fileEncoding': 'System'})
self.assertEqual(v.sink.staticValue(), '/home/me/test.shp')

ProcessingConfig.setSettingValue(ProcessingConfig.OUTPUT_FOLDER, testDataPath)
panel.setValue('test.shp')
v = panel.getValue()
self.assertIsInstance(v, QgsProcessingOutputLayerDefinition)
self.assertEqual(v.createOptions, {'fileEncoding': 'System'})
self.assertEqual(v.sink.staticValue(), os.path.join(testDataPath, 'test.shp'))

def testRasterDestination(self):
param = QgsProcessingParameterRasterDestination('test')
alg = QgsApplication.processingRegistry().algorithmById('native:centroids')
panel = DestinationSelectionPanel(param, alg)

panel.setValue('/home/me/test.tif')
v = panel.getValue()
self.assertIsInstance(v, QgsProcessingOutputLayerDefinition)
self.assertEqual(v.createOptions, {'fileEncoding': 'System'})
self.assertEqual(v.sink.staticValue(), '/home/me/test.tif')

ProcessingConfig.setSettingValue(ProcessingConfig.OUTPUT_FOLDER, testDataPath)
panel.setValue('test.tif')
v = panel.getValue()
self.assertIsInstance(v, QgsProcessingOutputLayerDefinition)
self.assertEqual(v.createOptions, {'fileEncoding': 'System'})
self.assertEqual(v.sink.staticValue(), os.path.join(testDataPath, 'test.tif'))

def testFolderDestination(self):
param = QgsProcessingParameterFolderDestination('test')
alg = QgsApplication.processingRegistry().algorithmById('native:centroids')
panel = DestinationSelectionPanel(param, alg)

panel.setValue('/home/me/test.tif')
v = panel.getValue()
self.assertEqual(v, '/home/me/test.tif')

ProcessingConfig.setSettingValue(ProcessingConfig.OUTPUT_FOLDER, testDataPath)
panel.setValue('test.tif')
v = panel.getValue()
self.assertEqual(v, os.path.join(testDataPath, 'test.tif'))

def testFileDestination(self):
param = QgsProcessingParameterFileDestination('test')
alg = QgsApplication.processingRegistry().algorithmById('native:centroids')
panel = DestinationSelectionPanel(param, alg)

panel.setValue('/home/me/test.tif')
v = panel.getValue()
self.assertEqual(v, '/home/me/test.tif')

ProcessingConfig.setSettingValue(ProcessingConfig.OUTPUT_FOLDER, testDataPath)
panel.setValue('test.tif')
v = panel.getValue()
self.assertEqual(v, os.path.join(testDataPath, 'test.tif'))


if __name__ == '__main__':
unittest.main()

0 comments on commit 5241ff3

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