diff --git a/python/plugins/processing/algs/gdal/ogr2ogr.py b/python/plugins/processing/algs/gdal/ogr2ogr.py index 397b758bf686..84cdb7806837 100644 --- a/python/plugins/processing/algs/gdal/ogr2ogr.py +++ b/python/plugins/processing/algs/gdal/ogr2ogr.py @@ -25,6 +25,7 @@ from qgis.core import (QgsProcessing, QgsProcessingException, + QgsProcessingParameterBoolean, QgsProcessingParameterDefinition, QgsProcessingParameterFeatureSource, QgsProcessingParameterString, @@ -35,6 +36,7 @@ class ogr2ogr(GdalAlgorithm): INPUT = 'INPUT' + CONVERT_ALL_LAYERS = 'CONVERT_ALL_LAYERS' OPTIONS = 'OPTIONS' OUTPUT = 'OUTPUT' @@ -46,6 +48,12 @@ def initAlgorithm(self, config=None): self.tr('Input layer'), types=[QgsProcessing.TypeVector])) + convert_all_layers_param = QgsProcessingParameterBoolean(self.CONVERT_ALL_LAYERS, + self.tr('Convert all layers from dataset'), defaultValue=False) + convert_all_layers_param.setHelp(self.tr("Use convert all layers to convert a whole dataset. " + "Supported output formats for this option are GPKG and GML.")) + self.addParameter(convert_all_layers_param) + options_param = QgsProcessingParameterString(self.OPTIONS, self.tr('Additional creation options'), defaultValue='', @@ -73,6 +81,7 @@ def commandName(self): def getConsoleCommands(self, parameters, context, feedback, executing=True): ogrLayer, layerName = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback, executing) + convertAllLayers = self.parameterAsBoolean(parameters, self.CONVERT_ALL_LAYERS, context) options = self.parameterAsString(parameters, self.OPTIONS, context) outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context) self.setOutputValue(self.OUTPUT, outFile) @@ -91,6 +100,15 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True): arguments.append(output) arguments.append(ogrLayer) - arguments.append(layerName) + if not convertAllLayers: + arguments.append(layerName) return ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)] + + def shortHelpString(self): + return self.tr("The algorithm converts simple features data between file formats.\n\n" + "Use convert all layers to convert a whole dataset.\n" + "Supported output formats for this option are:\n" + "- GPKG\n" + "- GML" + ) diff --git a/python/plugins/processing/tests/GdalAlgorithmsVectorTest.py b/python/plugins/processing/tests/GdalAlgorithmsVectorTest.py index 8543b3cb1559..e3c5161fd8b3 100644 --- a/python/plugins/processing/tests/GdalAlgorithmsVectorTest.py +++ b/python/plugins/processing/tests/GdalAlgorithmsVectorTest.py @@ -68,6 +68,7 @@ def testOgr2Ogr(self): context = QgsProcessingContext() feedback = QgsProcessingFeedback() source = os.path.join(testDataPath, 'polys.gml') + multi_source = os.path.join(testDataPath, 'multi_layers.gml') alg = ogr2ogr() alg.initAlgorithm() @@ -100,6 +101,22 @@ def testOgr2Ogr(self): '-f "GPKG" ' + outdir + '/check.gpkg ' + source + ' polys2']) + self.assertEqual( + alg.getConsoleCommands({'INPUT': multi_source + '|layername=lines', + 'CONVERT_ALL_LAYERS': False, + 'OUTPUT': outdir + '/check.gpkg'}, context, feedback), + ['ogr2ogr', + '-f "GPKG" ' + outdir + '/check.gpkg ' + + multi_source + ' lines']) + + self.assertEqual( + alg.getConsoleCommands({'INPUT': multi_source + '|layername=lines', + 'CONVERT_ALL_LAYERS': True, + 'OUTPUT': outdir + '/check.gpkg'}, context, feedback), + ['ogr2ogr', + '-f "GPKG" ' + outdir + '/check.gpkg ' + + multi_source]) + def testOgrInfo(self): context = QgsProcessingContext() feedback = QgsProcessingFeedback() diff --git a/python/plugins/processing/tests/testdata/expected/results_convert_all_layers_points_lines.gml b/python/plugins/processing/tests/testdata/expected/results_convert_all_layers_points_lines.gml new file mode 100644 index 000000000000..87f4c3759c79 --- /dev/null +++ b/python/plugins/processing/tests/testdata/expected/results_convert_all_layers_points_lines.gml @@ -0,0 +1,111 @@ + + + + + -1-5 + 115 + + + + + + 1,1 + 1 + 2 + + + + + 3,3 + 2 + 1 + + + + + 2,2 + 3 + 0 + + + + + 5,2 + 4 + 2 + + + + + 4,1 + 5 + 1 + + + + + 0,-5 + 6 + 0 + + + + + 8,-1 + 7 + 0 + + + + + 7,-1 + 8 + 0 + + + + + 0,-1 + 9 + 0 + + + + + 6,2 9,2 9,3 11,5 + + + + + -1,-1 1,-1 + + + + + 2,0 2,2 3,2 3,3 + + + + + 3,1 5,1 + + + + + 7,-3 10,-3 + + + + + 6,-3 10,1 + + + + + + + diff --git a/python/plugins/processing/tests/testdata/expected/results_convert_all_layers_points_lines.xsd b/python/plugins/processing/tests/testdata/expected/results_convert_all_layers_points_lines.xsd new file mode 100644 index 000000000000..f2eb6e1a9a82 --- /dev/null +++ b/python/plugins/processing/tests/testdata/expected/results_convert_all_layers_points_lines.xsd @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/python/plugins/processing/tests/testdata/expected/results_convert_only_one_layer_points.gml b/python/plugins/processing/tests/testdata/expected/results_convert_only_one_layer_points.gml new file mode 100644 index 000000000000..882ac165ae12 --- /dev/null +++ b/python/plugins/processing/tests/testdata/expected/results_convert_only_one_layer_points.gml @@ -0,0 +1,77 @@ + + + + + 0-5 + 83 + + + + + + 1,1 + 1 + 2 + + + + + 3,3 + 2 + 1 + + + + + 2,2 + 3 + 0 + + + + + 5,2 + 4 + 2 + + + + + 4,1 + 5 + 1 + + + + + 0,-5 + 6 + 0 + + + + + 8,-1 + 7 + 0 + + + + + 7,-1 + 8 + 0 + + + + + 0,-1 + 9 + 0 + + + diff --git a/python/plugins/processing/tests/testdata/expected/results_convert_only_one_layer_points.xsd b/python/plugins/processing/tests/testdata/expected/results_convert_only_one_layer_points.xsd new file mode 100644 index 000000000000..269b0c1e7c7d --- /dev/null +++ b/python/plugins/processing/tests/testdata/expected/results_convert_only_one_layer_points.xsd @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/python/plugins/processing/tests/testdata/gdal_algorithm_vector_tests.yaml b/python/plugins/processing/tests/testdata/gdal_algorithm_vector_tests.yaml index 11968d5d166a..2be504b7213b 100644 --- a/python/plugins/processing/tests/testdata/gdal_algorithm_vector_tests.yaml +++ b/python/plugins/processing/tests/testdata/gdal_algorithm_vector_tests.yaml @@ -248,3 +248,29 @@ tests: OUTPUT: name: expected/gdal/dissolved_points.gml type: vector + + - algorithm: gdal:convertformat + name: Convert only one layer from GPKG to GML (gdal:convertformat) + params: + CONVERT_ALL_LAYERS: false + INPUT: + name: points_lines.gpkg|layername=points + type: vector + OPTIONS: '' + results: + OUTPUT: + name: expected/results_convert_only_one_layer_points.gml + type: vector + + - algorithm: gdal:convertformat + name: Convert all layers from GPKG to GML (gdal:convertformat) + params: + CONVERT_ALL_LAYERS: true + INPUT: + name: points_lines.gpkg|layername=points + type: vector + OPTIONS: '' + results: + OUTPUT: + name: expected/results_convert_all_layers_points_lines.gml + type: vector diff --git a/python/plugins/processing/tests/testdata/multi_layers.gfs b/python/plugins/processing/tests/testdata/multi_layers.gfs new file mode 100644 index 000000000000..d96ca0b7ae1f --- /dev/null +++ b/python/plugins/processing/tests/testdata/multi_layers.gfs @@ -0,0 +1,41 @@ + + true + + points + points + + 1 + EPSG:4326 + + 9 + 0.00000 + 8.00000 + -5.00000 + 3.00000 + + + id + id + Integer + + + id2 + id2 + Integer + + + + lines + lines + + 2 + EPSG:4326 + + 7 + -1.00000 + 11.00000 + -3.00000 + 5.00000 + + + diff --git a/python/plugins/processing/tests/testdata/multi_layers.gml b/python/plugins/processing/tests/testdata/multi_layers.gml new file mode 100644 index 000000000000..87f4c3759c79 --- /dev/null +++ b/python/plugins/processing/tests/testdata/multi_layers.gml @@ -0,0 +1,111 @@ + + + + + -1-5 + 115 + + + + + + 1,1 + 1 + 2 + + + + + 3,3 + 2 + 1 + + + + + 2,2 + 3 + 0 + + + + + 5,2 + 4 + 2 + + + + + 4,1 + 5 + 1 + + + + + 0,-5 + 6 + 0 + + + + + 8,-1 + 7 + 0 + + + + + 7,-1 + 8 + 0 + + + + + 0,-1 + 9 + 0 + + + + + 6,2 9,2 9,3 11,5 + + + + + -1,-1 1,-1 + + + + + 2,0 2,2 3,2 3,3 + + + + + 3,1 5,1 + + + + + 7,-3 10,-3 + + + + + 6,-3 10,1 + + + + + + + diff --git a/python/plugins/processing/tests/testdata/points_lines.gpkg b/python/plugins/processing/tests/testdata/points_lines.gpkg new file mode 100644 index 000000000000..68567a0b06f4 Binary files /dev/null and b/python/plugins/processing/tests/testdata/points_lines.gpkg differ