def testOgr2Ogr(self): context = QgsProcessingContext() feedback = QgsProcessingFeedback() source = os.path.join(testDataPath, 'polys.gml') alg = ogr2ogr() alg.initAlgorithm() with tempfile.TemporaryDirectory() as outdir: self.assertEqual( alg.getConsoleCommands({'INPUT': source, 'OUTPUT': outdir + '/check.shp'}, context, feedback), ['ogr2ogr', '-f "ESRI Shapefile" ' + outdir + '/check.shp ' + source + ' polys2']) self.assertEqual( alg.getConsoleCommands({'INPUT': source, 'OUTPUT': outdir + '/check.kml'}, context, feedback), ['ogr2ogr', '-f "LIBKML" ' + outdir + '/check.kml ' + source + ' polys2']) self.assertEqual( alg.getConsoleCommands({'INPUT': source, 'OUTPUT': outdir + '/my out/check.kml'}, context, feedback), ['ogr2ogr', '-f "LIBKML" "' + outdir + '/my out/check.kml" ' + source + ' polys2']) self.assertEqual( alg.getConsoleCommands({'INPUT': source, 'OUTPUT': outdir + '/check.gpkg'}, context, feedback), ['ogr2ogr', '-f "GPKG" ' + outdir + '/check.gpkg ' + source + ' polys2'])
def testGetOgrCompatibleSourceFromOgrLayer(self): p = QgsProject() source = os.path.join(testDataPath, 'points.gml') vl = QgsVectorLayer(source) self.assertTrue(vl.isValid()) p.addMapLayer(vl) context = QgsProcessingContext() context.setProject(p) feedback = QgsProcessingFeedback() alg = ogr2ogr() alg.initAlgorithm() path, layer = alg.getOgrCompatibleSource('INPUT', {'INPUT': vl.id()}, context, feedback, True) self.assertEqual(path, source) 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) # with subset string vl.setSubsetString('x') path, layer = alg.getOgrCompatibleSource('INPUT', parameters, context, feedback, False) self.assertEqual(path, source) # subset of layer must be exported path, layer = alg.getOgrCompatibleSource('INPUT', parameters, context, feedback, True) self.assertNotEqual(path, source) self.assertTrue(path) self.assertTrue(path.endswith('.gpkg')) self.assertTrue(os.path.exists(path)) self.assertTrue(layer) # geopackage with layer source = os.path.join(testDataPath, 'custom', 'circular_strings.gpkg') vl2 = QgsVectorLayer(source + '|layername=circular_strings') self.assertTrue(vl2.isValid()) p.addMapLayer(vl2) path, layer = alg.getOgrCompatibleSource('INPUT', {'INPUT': vl2.id()}, context, feedback, True) self.assertEqual(path, source) self.assertEqual(layer, 'circular_strings') vl3 = QgsVectorLayer(source + '|layername=circular_strings_with_line') self.assertTrue(vl3.isValid()) p.addMapLayer(vl3) path, layer = alg.getOgrCompatibleSource('INPUT', {'INPUT': vl3.id()}, context, feedback, True) self.assertEqual(path, source) self.assertEqual(layer, 'circular_strings_with_line')