From 239e93deeba4c73b18cfbace263b9f1577cbeeb3 Mon Sep 17 00:00:00 2001 From: "Juergen E. Fischer" Date: Sun, 16 Sep 2018 21:55:10 +0200 Subject: [PATCH] run gdal_calc on Windows and gdal_calc.py on othes platforms (fixes #19482) --- .../plugins/processing/algs/gdal/gdalcalc.py | 2 +- .../processing/tests/GdalAlgorithmsTest.py | 2126 +++++++++-------- 2 files changed, 1075 insertions(+), 1053 deletions(-) diff --git a/python/plugins/processing/algs/gdal/gdalcalc.py b/python/plugins/processing/algs/gdal/gdalcalc.py index 6ce485a1a6a..2e3f7ec9396 100644 --- a/python/plugins/processing/algs/gdal/gdalcalc.py +++ b/python/plugins/processing/algs/gdal/gdalcalc.py @@ -176,7 +176,7 @@ class gdalcalc(GdalAlgorithm): return 'rastermiscellaneous' def commandName(self): - return 'gdal_calc' + return 'gdal_calc' if isWindows() else 'gdal_calc.py' def getConsoleCommands(self, parameters, context, feedback, executing=True): out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context) diff --git a/python/plugins/processing/tests/GdalAlgorithmsTest.py b/python/plugins/processing/tests/GdalAlgorithmsTest.py index 5b23f937370..96fd5e2169a 100644 --- a/python/plugins/processing/tests/GdalAlgorithmsTest.py +++ b/python/plugins/processing/tests/GdalAlgorithmsTest.py @@ -53,12 +53,12 @@ from processing.algs.gdal.translate import translate from processing.algs.gdal.warp import warp from processing.algs.gdal.fillnodata import fillnodata from processing.algs.gdal.rearrange_bands import rearrange_bands +from processing.tools.system import isWindows from qgis.core import (QgsProcessingContext, QgsProcessingFeedback, QgsCoordinateReferenceSystem, QgsApplication, - QgsVectorLayer, QgsFeature, QgsGeometry, QgsPointXY, @@ -233,44 +233,42 @@ class TestGdalAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest): QgsProject.instance().removeMapLayer(layer) def testOgrLayerNameExtraction(self): - outdir = tempfile.mkdtemp() - self.cleanup_paths.append(outdir) + with tempfile.TemporaryDirectory() as outdir: + def _copyFile(dst): + shutil.copyfile(os.path.join(testDataPath, 'custom', 'grass7', 'weighted.csv'), dst) - def _copyFile(dst): - shutil.copyfile(os.path.join(testDataPath, 'custom', 'grass7', 'weighted.csv'), dst) + # OGR provider - single layer + _copyFile(os.path.join(outdir, 'a.csv')) + name = GdalUtils.ogrLayerName(outdir) + self.assertEqual(name, 'a') - # OGR provider - single layer - _copyFile(os.path.join(outdir, 'a.csv')) - name = GdalUtils.ogrLayerName(outdir) - self.assertEqual(name, 'a') + # OGR provider - multiple layers + _copyFile(os.path.join(outdir, 'b.csv')) + name1 = GdalUtils.ogrLayerName(outdir + '|layerid=0') + name2 = GdalUtils.ogrLayerName(outdir + '|layerid=1') + self.assertEqual(sorted([name1, name2]), ['a', 'b']) - # OGR provider - multiple layers - _copyFile(os.path.join(outdir, 'b.csv')) - name1 = GdalUtils.ogrLayerName(outdir + '|layerid=0') - name2 = GdalUtils.ogrLayerName(outdir + '|layerid=1') - self.assertEqual(sorted([name1, name2]), ['a', 'b']) + name = GdalUtils.ogrLayerName(outdir + '|layerid=2') + self.assertIsNone(name) - name = GdalUtils.ogrLayerName(outdir + '|layerid=2') - self.assertIsNone(name) + # OGR provider - layername takes precedence + name = GdalUtils.ogrLayerName(outdir + '|layername=f') + self.assertEqual(name, 'f') - # OGR provider - layername takes precedence - name = GdalUtils.ogrLayerName(outdir + '|layername=f') - self.assertEqual(name, 'f') + name = GdalUtils.ogrLayerName(outdir + '|layerid=0|layername=f') + self.assertEqual(name, 'f') - name = GdalUtils.ogrLayerName(outdir + '|layerid=0|layername=f') - self.assertEqual(name, 'f') + name = GdalUtils.ogrLayerName(outdir + '|layername=f|layerid=0') + self.assertEqual(name, 'f') - name = GdalUtils.ogrLayerName(outdir + '|layername=f|layerid=0') - self.assertEqual(name, 'f') + # SQLite provider + name = GdalUtils.ogrLayerName('dbname=\'/tmp/x.sqlite\' table="t" (geometry) sql=') + self.assertEqual(name, 't') - # SQLiite provider - name = GdalUtils.ogrLayerName('dbname=\'/tmp/x.sqlite\' table="t" (geometry) sql=') - self.assertEqual(name, 't') - - # PostgreSQL provider - name = GdalUtils.ogrLayerName( - 'port=5493 sslmode=disable key=\'edge_id\' srid=0 type=LineString table="city_data"."edge" (geom) sql=') - self.assertEqual(name, 'city_data.edge') + # PostgreSQL provider + name = GdalUtils.ogrLayerName( + 'port=5493 sslmode=disable key=\'edge_id\' srid=0 type=LineString table="city_data"."edge" (geom) sql=') + self.assertEqual(name, 'city_data.edge') def testOgrConnectionStringAndFormat(self): context = QgsProcessingContext() @@ -343,111 +341,112 @@ class TestGdalAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest): translate_alg = translate() translate_alg.initAlgorithm() - # with no NODATA value - self.assertEqual( - translate_alg.getConsoleCommands({'INPUT': source, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_translate', - '-of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) - # with None NODATA value - self.assertEqual( - translate_alg.getConsoleCommands({'INPUT': source, - 'NODATA': None, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_translate', - '-of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) - # with NODATA value - self.assertEqual( - translate_alg.getConsoleCommands({'INPUT': source, - 'NODATA': 9999, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_translate', - '-a_nodata 9999.0 ' + - '-of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) - # with "0" NODATA value - self.assertEqual( - translate_alg.getConsoleCommands({'INPUT': source, - 'NODATA': 0, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_translate', - '-a_nodata 0.0 ' + - '-of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) - # with "0" NODATA value and custom data type - self.assertEqual( - translate_alg.getConsoleCommands({'INPUT': source, - 'NODATA': 0, - 'DATA_TYPE': 6, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_translate', - '-a_nodata 0.0 ' + - '-ot Float32 -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) + with tempfile.TemporaryDirectory() as outdir: + # with no NODATA value + self.assertEqual( + translate_alg.getConsoleCommands({'INPUT': source, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_translate', + '-of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) + # with None NODATA value + self.assertEqual( + translate_alg.getConsoleCommands({'INPUT': source, + 'NODATA': None, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_translate', + '-of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) + # with NODATA value + self.assertEqual( + translate_alg.getConsoleCommands({'INPUT': source, + 'NODATA': 9999, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_translate', + '-a_nodata 9999.0 ' + + '-of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) + # with "0" NODATA value + self.assertEqual( + translate_alg.getConsoleCommands({'INPUT': source, + 'NODATA': 0, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_translate', + '-a_nodata 0.0 ' + + '-of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) + # with "0" NODATA value and custom data type + self.assertEqual( + translate_alg.getConsoleCommands({'INPUT': source, + 'NODATA': 0, + 'DATA_TYPE': 6, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_translate', + '-a_nodata 0.0 ' + + '-ot Float32 -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) - # with target srs - self.assertEqual( - translate_alg.getConsoleCommands({'INPUT': source, - 'TARGET_CRS': 'EPSG:3111', - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_translate', - '-a_srs EPSG:3111 ' + - '-of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) + # with target srs + self.assertEqual( + translate_alg.getConsoleCommands({'INPUT': source, + 'TARGET_CRS': 'EPSG:3111', + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_translate', + '-a_srs EPSG:3111 ' + + '-of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) - # with target using proj string - custom_crs = 'proj4: +proj=utm +zone=36 +south +a=6378249.145 +b=6356514.966398753 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs' - self.assertEqual( - translate_alg.getConsoleCommands({'INPUT': source, - 'TARGET_CRS': custom_crs, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_translate', - '-a_srs EPSG:20936 ' + - '-of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) + # with target using proj string + custom_crs = 'proj4: +proj=utm +zone=36 +south +a=6378249.145 +b=6356514.966398753 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs' + self.assertEqual( + translate_alg.getConsoleCommands({'INPUT': source, + 'TARGET_CRS': custom_crs, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_translate', + '-a_srs EPSG:20936 ' + + '-of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) - # with target using custom projection - custom_crs = 'proj4: +proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs' - self.assertEqual( - translate_alg.getConsoleCommands({'INPUT': source, - 'TARGET_CRS': custom_crs, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_translate', - '-a_srs "+proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs" ' + - '-of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) + # with target using custom projection + custom_crs = 'proj4: +proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs' + self.assertEqual( + translate_alg.getConsoleCommands({'INPUT': source, + 'TARGET_CRS': custom_crs, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_translate', + '-a_srs "+proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs" ' + + '-of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) - # with non-EPSG crs code - self.assertEqual( - translate_alg.getConsoleCommands({'INPUT': source, - 'TARGET_CRS': 'POSTGIS:3111', - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_translate', - '-a_srs EPSG:3111 ' + - '-of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) + # with non-EPSG crs code + self.assertEqual( + translate_alg.getConsoleCommands({'INPUT': source, + 'TARGET_CRS': 'POSTGIS:3111', + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_translate', + '-a_srs EPSG:3111 ' + + '-of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) - # with copy subdatasets - self.assertEqual( - translate_alg.getConsoleCommands({'INPUT': source, - 'COPY_SUBDATASETS': True, - 'OUTPUT': 'd:/temp/check.tif'}, context, feedback), - ['gdal_translate', - '-sds ' + - '-of GTiff ' + - source + ' ' + - 'd:/temp/check.tif']) + # with copy subdatasets + self.assertEqual( + translate_alg.getConsoleCommands({'INPUT': source, + 'COPY_SUBDATASETS': True, + 'OUTPUT': outdir + '/check.tif'}, context, feedback), + ['gdal_translate', + '-sds ' + + '-of GTiff ' + + source + ' ' + + outdir + '/check.tif']) def testClipRasterByExtent(self): context = QgsProcessingContext() @@ -457,46 +456,47 @@ class TestGdalAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest): alg.initAlgorithm() extent = QgsRectangle(1, 2, 3, 4) - # with no NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'EXTENT': extent, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_translate', - '-projwin 0.0 0.0 0.0 0.0 -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) - # with NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'EXTENT': extent, - 'NODATA': 9999, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_translate', - '-projwin 0.0 0.0 0.0 0.0 -a_nodata 9999.0 -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) - # with "0" NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'EXTENT': extent, - 'NODATA': 0, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_translate', - '-projwin 0.0 0.0 0.0 0.0 -a_nodata 0.0 -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) - # with "0" NODATA value and custom data type - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'EXTENT': extent, - 'NODATA': 0, - 'DATA_TYPE': 6, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_translate', - '-projwin 0.0 0.0 0.0 0.0 -a_nodata 0.0 -ot Float32 -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) + with tempfile.TemporaryDirectory() as outdir: + # with no NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'EXTENT': extent, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_translate', + '-projwin 0.0 0.0 0.0 0.0 -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) + # with NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'EXTENT': extent, + 'NODATA': 9999, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_translate', + '-projwin 0.0 0.0 0.0 0.0 -a_nodata 9999.0 -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) + # with "0" NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'EXTENT': extent, + 'NODATA': 0, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_translate', + '-projwin 0.0 0.0 0.0 0.0 -a_nodata 0.0 -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) + # with "0" NODATA value and custom data type + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'EXTENT': extent, + 'NODATA': 0, + 'DATA_TYPE': 6, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_translate', + '-projwin 0.0 0.0 0.0 0.0 -a_nodata 0.0 -ot Float32 -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) def testClipRasterByMask(self): context = QgsProcessingContext() @@ -506,46 +506,47 @@ class TestGdalAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest): alg = ClipRasterByMask() alg.initAlgorithm() - # with no NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'MASK': mask, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdalwarp', - '-of JPEG -cutline ' + - mask + ' -crop_to_cutline ' + source + ' ' + - 'd:/temp/check.jpg']) - # with NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'MASK': mask, - 'NODATA': 9999, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdalwarp', - '-of JPEG -cutline ' + - mask + ' -crop_to_cutline -dstnodata 9999.0 ' + source + ' ' + - 'd:/temp/check.jpg']) - # with "0" NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'MASK': mask, - 'NODATA': 0, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdalwarp', - '-of JPEG -cutline ' + - mask + ' -crop_to_cutline -dstnodata 0.0 ' + source + ' ' + - 'd:/temp/check.jpg']) - # with "0" NODATA value and custom data type - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'MASK': mask, - 'NODATA': 0, - 'DATA_TYPE': 6, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdalwarp', - '-ot Float32 -of JPEG -cutline ' + - mask + ' -crop_to_cutline -dstnodata 0.0 ' + source + ' ' + - 'd:/temp/check.jpg']) + with tempfile.TemporaryDirectory() as outdir: + # with no NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'MASK': mask, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdalwarp', + '-of JPEG -cutline ' + + mask + ' -crop_to_cutline ' + source + ' ' + + outdir + '/check.jpg']) + # with NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'MASK': mask, + 'NODATA': 9999, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdalwarp', + '-of JPEG -cutline ' + + mask + ' -crop_to_cutline -dstnodata 9999.0 ' + source + ' ' + + outdir + '/check.jpg']) + # with "0" NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'MASK': mask, + 'NODATA': 0, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdalwarp', + '-of JPEG -cutline ' + + mask + ' -crop_to_cutline -dstnodata 0.0 ' + source + ' ' + + outdir + '/check.jpg']) + # with "0" NODATA value and custom data type + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'MASK': mask, + 'NODATA': 0, + 'DATA_TYPE': 6, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdalwarp', + '-ot Float32 -of JPEG -cutline ' + + mask + ' -crop_to_cutline -dstnodata 0.0 ' + source + ' ' + + outdir + '/check.jpg']) def testContour(self): context = QgsProcessingContext() @@ -554,53 +555,54 @@ class TestGdalAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest): alg = contour() alg.initAlgorithm() - # with no NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'BAND': 1, - 'FIELD_NAME': 'elev', - 'INTERVAL': 5, - 'OUTPUT': 'd:/temp/check.shp'}, context, feedback), - ['gdal_contour', - '-b 1 -a elev -i 5.0 -f "ESRI Shapefile" ' + - source + ' ' + - 'd:/temp/check.shp']) - # with NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'BAND': 1, - 'FIELD_NAME': 'elev', - 'INTERVAL': 5, - 'NODATA': 9999, - 'OUTPUT': 'd:/temp/check.shp'}, context, feedback), - ['gdal_contour', - '-b 1 -a elev -i 5.0 -snodata 9999.0 -f "ESRI Shapefile" ' + - source + ' ' + - 'd:/temp/check.shp']) - # with "0" NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'BAND': 1, - 'FIELD_NAME': 'elev', - 'INTERVAL': 5, - 'NODATA': 0, - 'OUTPUT': 'd:/temp/check.gpkg'}, context, feedback), - ['gdal_contour', - '-b 1 -a elev -i 5.0 -snodata 0.0 -f "GPKG" ' + - source + ' ' + - 'd:/temp/check.gpkg']) - # fixed level contours - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'BAND': 1, - 'FIELD_NAME': 'elev', - 'INTERVAL': 0, - 'OPTIONS': '-fl 100 125 150 200', - 'OUTPUT': 'd:/temp/check.shp'}, context, feedback), - ['gdal_contour', - '-b 1 -a elev -i 0.0 -fl 100 125 150 200 -f "ESRI Shapefile" ' + - source + ' ' + - 'd:/temp/check.shp']) + with tempfile.TemporaryDirectory() as outdir: + # with no NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'BAND': 1, + 'FIELD_NAME': 'elev', + 'INTERVAL': 5, + 'OUTPUT': outdir + '/check.shp'}, context, feedback), + ['gdal_contour', + '-b 1 -a elev -i 5.0 -f "ESRI Shapefile" ' + + source + ' ' + + outdir + '/check.shp']) + # with NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'BAND': 1, + 'FIELD_NAME': 'elev', + 'INTERVAL': 5, + 'NODATA': 9999, + 'OUTPUT': outdir + '/check.shp'}, context, feedback), + ['gdal_contour', + '-b 1 -a elev -i 5.0 -snodata 9999.0 -f "ESRI Shapefile" ' + + source + ' ' + + outdir + '/check.shp']) + # with "0" NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'BAND': 1, + 'FIELD_NAME': 'elev', + 'INTERVAL': 5, + 'NODATA': 0, + 'OUTPUT': outdir + '/check.gpkg'}, context, feedback), + ['gdal_contour', + '-b 1 -a elev -i 5.0 -snodata 0.0 -f "GPKG" ' + + source + ' ' + + outdir + '/check.gpkg']) + # fixed level contours + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'BAND': 1, + 'FIELD_NAME': 'elev', + 'INTERVAL': 0, + 'OPTIONS': '-fl 100 125 150 200', + 'OUTPUT': outdir + '/check.shp'}, context, feedback), + ['gdal_contour', + '-b 1 -a elev -i 0.0 -fl 100 125 150 200 -f "ESRI Shapefile" ' + + source + ' ' + + outdir + '/check.shp']) def testGdal2Tiles(self): context = QgsProcessingContext() @@ -609,74 +611,75 @@ class TestGdalAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest): alg = gdal2tiles() alg.initAlgorithm() - # with no NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'OUTPUT': 'd:/temp/'}, context, feedback), - ['gdal2tiles.py', - '-p mercator -w all -r average ' + - source + ' ' + - 'd:/temp/']) - # with NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'NODATA': -9999, - 'OUTPUT': 'd:/temp/'}, context, feedback), - ['gdal2tiles.py', - '-p mercator -w all -r average -a -9999.0 ' + - source + ' ' + - 'd:/temp/']) - # with "0" NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'NODATA': 0, - 'OUTPUT': 'd:/temp/'}, context, feedback), - ['gdal2tiles.py', - '-p mercator -w all -r average -a 0.0 ' + - source + ' ' + - 'd:/temp/']) + with tempfile.TemporaryDirectory() as outdir: + # with no NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'OUTPUT': outdir + '/'}, context, feedback), + ['gdal2tiles.py', + '-p mercator -w all -r average ' + + source + ' ' + + outdir + '/']) + # with NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'NODATA': -9999, + 'OUTPUT': outdir + '/'}, context, feedback), + ['gdal2tiles.py', + '-p mercator -w all -r average -a -9999.0 ' + + source + ' ' + + outdir + '/']) + # with "0" NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'NODATA': 0, + 'OUTPUT': outdir + '/'}, context, feedback), + ['gdal2tiles.py', + '-p mercator -w all -r average -a 0.0 ' + + source + ' ' + + outdir + '/']) - # with input srs - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'SOURCE_CRS': 'EPSG:3111', - 'OUTPUT': 'd:/temp/'}, context, feedback), - ['gdal2tiles.py', - '-p mercator -w all -r average -s EPSG:3111 ' + - source + ' ' + - 'd:/temp/']) + # with input srs + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'SOURCE_CRS': 'EPSG:3111', + 'OUTPUT': outdir + '/'}, context, feedback), + ['gdal2tiles.py', + '-p mercator -w all -r average -s EPSG:3111 ' + + source + ' ' + + outdir + '/']) - # with target using proj string - custom_crs = 'proj4: +proj=utm +zone=36 +south +a=6378249.145 +b=6356514.966398753 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs' - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'SOURCE_CRS': custom_crs, - 'OUTPUT': 'd:/temp/'}, context, feedback), - ['gdal2tiles.py', - '-p mercator -w all -r average -s EPSG:20936 ' + - source + ' ' + - 'd:/temp/']) + # with target using proj string + custom_crs = 'proj4: +proj=utm +zone=36 +south +a=6378249.145 +b=6356514.966398753 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs' + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'SOURCE_CRS': custom_crs, + 'OUTPUT': outdir + '/'}, context, feedback), + ['gdal2tiles.py', + '-p mercator -w all -r average -s EPSG:20936 ' + + source + ' ' + + outdir + '/']) - # with target using custom projection - custom_crs = 'proj4: +proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs' - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'SOURCE_CRS': custom_crs, - 'OUTPUT': 'd:/temp/'}, context, feedback), - ['gdal2tiles.py', - '-p mercator -w all -r average -s "+proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs" ' + - source + ' ' + - 'd:/temp/']) + # with target using custom projection + custom_crs = 'proj4: +proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs' + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'SOURCE_CRS': custom_crs, + 'OUTPUT': outdir + '/'}, context, feedback), + ['gdal2tiles.py', + '-p mercator -w all -r average -s "+proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs" ' + + source + ' ' + + outdir + '/']) - # with non-EPSG crs code - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'SOURCE_CRS': 'POSTGIS:3111', - 'OUTPUT': 'd:/temp/'}, context, feedback), - ['gdal2tiles.py', - '-p mercator -w all -r average -s EPSG:3111 ' + - source + ' ' + - 'd:/temp/']) + # with non-EPSG crs code + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'SOURCE_CRS': 'POSTGIS:3111', + 'OUTPUT': outdir + '/'}, context, feedback), + ['gdal2tiles.py', + '-p mercator -w all -r average -s EPSG:3111 ' + + source + ' ' + + outdir + '/']) def testGdalCalc(self): context = QgsProcessingContext() @@ -685,51 +688,52 @@ class TestGdalAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest): alg = gdalcalc() alg.initAlgorithm() - output = 'd:/temp/check.jpg' + with tempfile.TemporaryDirectory() as outdir: + output = outdir + '/check.jpg' - # default execution - formula = 'A*2' # default formula - self.assertEqual( - alg.getConsoleCommands({ - 'INPUT_A': source, - 'BAND_A': 1, - 'FORMULA': formula, - 'BAND_D': -1, - 'NO_DATA': None, - 'BAND_F': -1, - 'BAND_B': -1, - 'RTYPE': 5, - 'INPUT_F': None, - 'BAND_E': -1, - 'INPUT_D': None, - 'INPUT_B': None, - 'BAND_C': -1, - 'INPUT_E': None, - 'INPUT_C': None, - 'OUTPUT': output}, context, feedback), - ['gdal_calc', '--calc "{}" --format JPEG --type Float32 -A {} --A_band 1 --outfile {}'.format(formula, source, output)]) + # default execution + formula = 'A*2' # default formula + self.assertEqual( + alg.getConsoleCommands({ + 'INPUT_A': source, + 'BAND_A': 1, + 'FORMULA': formula, + 'BAND_D': -1, + 'NO_DATA': None, + 'BAND_F': -1, + 'BAND_B': -1, + 'RTYPE': 5, + 'INPUT_F': None, + 'BAND_E': -1, + 'INPUT_D': None, + 'INPUT_B': None, + 'BAND_C': -1, + 'INPUT_E': None, + 'INPUT_C': None, + 'OUTPUT': output}, context, feedback), + ['gdal_calc' if isWindows() else 'gdal_calc.py', '--calc "{}" --format JPEG --type Float32 -A {} --A_band 1 --outfile {}'.format(formula, source, output)]) - # check that formula is not escaped and formula is returned as it is - formula = 'A * 2' # <--- add spaces in the formula - self.assertEqual( - alg.getConsoleCommands({ - 'INPUT_A': source, - 'BAND_A': 1, - 'FORMULA': formula, - 'BAND_D': -1, - 'NO_DATA': None, - 'BAND_F': -1, - 'BAND_B': -1, - 'RTYPE': 5, - 'INPUT_F': None, - 'BAND_E': -1, - 'INPUT_D': None, - 'INPUT_B': None, - 'BAND_C': -1, - 'INPUT_E': None, - 'INPUT_C': None, - 'OUTPUT': output}, context, feedback), - ['gdal_calc', '--calc "{}" --format JPEG --type Float32 -A {} --A_band 1 --outfile {}'.format(formula, source, output)]) + # check that formula is not escaped and formula is returned as it is + formula = 'A * 2' # <--- add spaces in the formula + self.assertEqual( + alg.getConsoleCommands({ + 'INPUT_A': source, + 'BAND_A': 1, + 'FORMULA': formula, + 'BAND_D': -1, + 'NO_DATA': None, + 'BAND_F': -1, + 'BAND_B': -1, + 'RTYPE': 5, + 'INPUT_F': None, + 'BAND_E': -1, + 'INPUT_D': None, + 'INPUT_B': None, + 'BAND_C': -1, + 'INPUT_E': None, + 'INPUT_C': None, + 'OUTPUT': output}, context, feedback), + ['gdal_calc' if isWindows() else 'gdal_calc.py', '--calc "{}" --format JPEG --type Float32 -A {} --A_band 1 --outfile {}'.format(formula, source, output)]) def testBuildVrt(self): context = QgsProcessingContext() @@ -738,117 +742,118 @@ class TestGdalAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest): alg = buildvrt() alg.initAlgorithm() - commands = alg.getConsoleCommands({'LAYERS': [source], - 'OUTPUT': 'd:/temp/test.vrt'}, context, feedback) - self.assertEqual(len(commands), 2) - self.assertEqual(commands[0], 'gdalbuildvrt') - self.assertIn('-resolution average', commands[1]) - self.assertIn('-separate', commands[1]) - self.assertNotIn('-allow_projection_difference', commands[1]) - self.assertNotIn('-add_alpha', commands[1]) - self.assertNotIn('-a_srs', commands[1]) - self.assertIn('-r nearest', commands[1]) - self.assertIn('-input_file_list', commands[1]) - self.assertIn('d:/temp/test.vrt', commands[1]) + with tempfile.TemporaryDirectory() as outdir: + commands = alg.getConsoleCommands({'LAYERS': [source], + 'OUTPUT': outdir + '/test.vrt'}, context, feedback) + self.assertEqual(len(commands), 2) + self.assertEqual(commands[0], 'gdalbuildvrt') + self.assertIn('-resolution average', commands[1]) + self.assertIn('-separate', commands[1]) + self.assertNotIn('-allow_projection_difference', commands[1]) + self.assertNotIn('-add_alpha', commands[1]) + self.assertNotIn('-a_srs', commands[1]) + self.assertIn('-r nearest', commands[1]) + self.assertIn('-input_file_list', commands[1]) + self.assertIn(outdir + '/test.vrt', commands[1]) - commands = alg.getConsoleCommands({'LAYERS': [source], - 'RESOLUTION': 2, - 'OUTPUT': 'd:/temp/test.vrt'}, context, feedback) - self.assertEqual(len(commands), 2) - self.assertEqual(commands[0], 'gdalbuildvrt') - self.assertIn('-resolution lowest', commands[1]) - self.assertIn('-separate', commands[1]) - self.assertNotIn('-allow_projection_difference', commands[1]) - self.assertNotIn('-add_alpha', commands[1]) - self.assertNotIn('-a_srs', commands[1]) - self.assertIn('-r nearest', commands[1]) - self.assertIn('-input_file_list', commands[1]) - self.assertIn('d:/temp/test.vrt', commands[1]) + commands = alg.getConsoleCommands({'LAYERS': [source], + 'RESOLUTION': 2, + 'OUTPUT': outdir + '/test.vrt'}, context, feedback) + self.assertEqual(len(commands), 2) + self.assertEqual(commands[0], 'gdalbuildvrt') + self.assertIn('-resolution lowest', commands[1]) + self.assertIn('-separate', commands[1]) + self.assertNotIn('-allow_projection_difference', commands[1]) + self.assertNotIn('-add_alpha', commands[1]) + self.assertNotIn('-a_srs', commands[1]) + self.assertIn('-r nearest', commands[1]) + self.assertIn('-input_file_list', commands[1]) + self.assertIn(outdir + '/test.vrt', commands[1]) - commands = alg.getConsoleCommands({'LAYERS': [source], - 'SEPARATE': False, - 'OUTPUT': 'd:/temp/test.vrt'}, context, feedback) - self.assertEqual(len(commands), 2) - self.assertEqual(commands[0], 'gdalbuildvrt') - self.assertIn('-resolution average', commands[1]) - self.assertNotIn('-allow_projection_difference', commands[1]) - self.assertNotIn('-separate', commands[1]) - self.assertNotIn('-add_alpha', commands[1]) - self.assertNotIn('-a_srs', commands[1]) - self.assertIn('-r nearest', commands[1]) - self.assertIn('-input_file_list', commands[1]) - self.assertIn('d:/temp/test.vrt', commands[1]) + commands = alg.getConsoleCommands({'LAYERS': [source], + 'SEPARATE': False, + 'OUTPUT': outdir + '/test.vrt'}, context, feedback) + self.assertEqual(len(commands), 2) + self.assertEqual(commands[0], 'gdalbuildvrt') + self.assertIn('-resolution average', commands[1]) + self.assertNotIn('-allow_projection_difference', commands[1]) + self.assertNotIn('-separate', commands[1]) + self.assertNotIn('-add_alpha', commands[1]) + self.assertNotIn('-a_srs', commands[1]) + self.assertIn('-r nearest', commands[1]) + self.assertIn('-input_file_list', commands[1]) + self.assertIn(outdir + '/test.vrt', commands[1]) - commands = alg.getConsoleCommands({'LAYERS': [source], - 'PROJ_DIFFERENCE': True, - 'OUTPUT': 'd:/temp/test.vrt'}, context, feedback) - self.assertEqual(len(commands), 2) - self.assertEqual(commands[0], 'gdalbuildvrt') - self.assertIn('-resolution average', commands[1]) - self.assertIn('-allow_projection_difference', commands[1]) - self.assertIn('-separate', commands[1]) - self.assertNotIn('-add_alpha', commands[1]) - self.assertNotIn('-a_srs', commands[1]) - self.assertIn('-r nearest', commands[1]) - self.assertIn('-input_file_list', commands[1]) - self.assertIn('d:/temp/test.vrt', commands[1]) + commands = alg.getConsoleCommands({'LAYERS': [source], + 'PROJ_DIFFERENCE': True, + 'OUTPUT': outdir + '/test.vrt'}, context, feedback) + self.assertEqual(len(commands), 2) + self.assertEqual(commands[0], 'gdalbuildvrt') + self.assertIn('-resolution average', commands[1]) + self.assertIn('-allow_projection_difference', commands[1]) + self.assertIn('-separate', commands[1]) + self.assertNotIn('-add_alpha', commands[1]) + self.assertNotIn('-a_srs', commands[1]) + self.assertIn('-r nearest', commands[1]) + self.assertIn('-input_file_list', commands[1]) + self.assertIn(outdir + '/test.vrt', commands[1]) - commands = alg.getConsoleCommands({'LAYERS': [source], - 'ADD_ALPHA': True, - 'OUTPUT': 'd:/temp/test.vrt'}, context, feedback) - self.assertEqual(len(commands), 2) - self.assertEqual(commands[0], 'gdalbuildvrt') - self.assertIn('-resolution average', commands[1]) - self.assertIn('-separate', commands[1]) - self.assertNotIn('-allow_projection_difference', commands[1]) - self.assertNotIn('-add_alpha', commands[1]) - self.assertNotIn('-a_srs', commands[1]) - self.assertIn('-r nearest', commands[1]) - self.assertIn('-input_file_list', commands[1]) - self.assertIn('d:/temp/test.vrt', commands[1]) + commands = alg.getConsoleCommands({'LAYERS': [source], + 'ADD_ALPHA': True, + 'OUTPUT': outdir + '/test.vrt'}, context, feedback) + self.assertEqual(len(commands), 2) + self.assertEqual(commands[0], 'gdalbuildvrt') + self.assertIn('-resolution average', commands[1]) + self.assertIn('-separate', commands[1]) + self.assertNotIn('-allow_projection_difference', commands[1]) + self.assertNotIn('-add_alpha', commands[1]) + self.assertNotIn('-a_srs', commands[1]) + self.assertIn('-r nearest', commands[1]) + self.assertIn('-input_file_list', commands[1]) + self.assertIn(outdir + '/test.vrt', commands[1]) - commands = alg.getConsoleCommands({'LAYERS': [source], - 'ASSIGN_CRS': 'EPSG:3111', - 'OUTPUT': 'd:/temp/test.vrt'}, context, feedback) - self.assertEqual(len(commands), 2) - self.assertEqual(commands[0], 'gdalbuildvrt') - self.assertIn('-resolution average', commands[1]) - self.assertIn('-separate', commands[1]) - self.assertNotIn('-allow_projection_difference', commands[1]) - self.assertNotIn('-add_alpha', commands[1]) - self.assertIn('-a_srs EPSG:3111', commands[1]) - self.assertIn('-r nearest', commands[1]) - self.assertIn('-input_file_list', commands[1]) - self.assertIn('d:/temp/test.vrt', commands[1]) + commands = alg.getConsoleCommands({'LAYERS': [source], + 'ASSIGN_CRS': 'EPSG:3111', + 'OUTPUT': outdir + '/test.vrt'}, context, feedback) + self.assertEqual(len(commands), 2) + self.assertEqual(commands[0], 'gdalbuildvrt') + self.assertIn('-resolution average', commands[1]) + self.assertIn('-separate', commands[1]) + self.assertNotIn('-allow_projection_difference', commands[1]) + self.assertNotIn('-add_alpha', commands[1]) + self.assertIn('-a_srs EPSG:3111', commands[1]) + self.assertIn('-r nearest', commands[1]) + self.assertIn('-input_file_list', commands[1]) + self.assertIn(outdir + '/test.vrt', commands[1]) - custom_crs = 'proj4: +proj=utm +zone=36 +south +a=6378249.145 +b=6356514.966398753 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs' - commands = alg.getConsoleCommands({'LAYERS': [source], - 'ASSIGN_CRS': custom_crs, - 'OUTPUT': 'd:/temp/test.vrt'}, context, feedback) - self.assertEqual(len(commands), 2) - self.assertEqual(commands[0], 'gdalbuildvrt') - self.assertIn('-resolution average', commands[1]) - self.assertIn('-separate', commands[1]) - self.assertNotIn('-allow_projection_difference', commands[1]) - self.assertNotIn('-add_alpha', commands[1]) - self.assertIn('-a_srs EPSG:20936', commands[1]) - self.assertIn('-r nearest', commands[1]) - self.assertIn('-input_file_list', commands[1]) - self.assertIn('d:/temp/test.vrt', commands[1]) + custom_crs = 'proj4: +proj=utm +zone=36 +south +a=6378249.145 +b=6356514.966398753 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs' + commands = alg.getConsoleCommands({'LAYERS': [source], + 'ASSIGN_CRS': custom_crs, + 'OUTPUT': outdir + '/test.vrt'}, context, feedback) + self.assertEqual(len(commands), 2) + self.assertEqual(commands[0], 'gdalbuildvrt') + self.assertIn('-resolution average', commands[1]) + self.assertIn('-separate', commands[1]) + self.assertNotIn('-allow_projection_difference', commands[1]) + self.assertNotIn('-add_alpha', commands[1]) + self.assertIn('-a_srs EPSG:20936', commands[1]) + self.assertIn('-r nearest', commands[1]) + self.assertIn('-input_file_list', commands[1]) + self.assertIn(outdir + '/test.vrt', commands[1]) - commands = alg.getConsoleCommands({'LAYERS': [source], - 'RESAMPLING': 4, - 'OUTPUT': 'd:/temp/test.vrt'}, context, feedback) - self.assertEqual(len(commands), 2) - self.assertEqual(commands[0], 'gdalbuildvrt') - self.assertIn('-resolution average', commands[1]) - self.assertIn('-separate', commands[1]) - self.assertNotIn('-allow_projection_difference', commands[1]) - self.assertNotIn('-add_alpha', commands[1]) - self.assertNotIn('-a_srs', commands[1]) - self.assertIn('-r lanczos', commands[1]) - self.assertIn('-input_file_list', commands[1]) - self.assertIn('d:/temp/test.vrt', commands[1]) + commands = alg.getConsoleCommands({'LAYERS': [source], + 'RESAMPLING': 4, + 'OUTPUT': outdir + '/test.vrt'}, context, feedback) + self.assertEqual(len(commands), 2) + self.assertEqual(commands[0], 'gdalbuildvrt') + self.assertIn('-resolution average', commands[1]) + self.assertIn('-separate', commands[1]) + self.assertNotIn('-allow_projection_difference', commands[1]) + self.assertNotIn('-add_alpha', commands[1]) + self.assertNotIn('-a_srs', commands[1]) + self.assertIn('-r lanczos', commands[1]) + self.assertIn('-input_file_list', commands[1]) + self.assertIn(outdir + '/test.vrt', commands[1]) def testGdalInfo(self): context = QgsProcessingContext() @@ -919,55 +924,56 @@ class TestGdalAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest): alg = gdaltindex() alg.initAlgorithm() - self.assertEqual( - alg.getConsoleCommands({'LAYERS': [source], - 'OUTPUT': 'd:/temp/test.shp'}, context, feedback), - ['gdaltindex', - '-tileindex location -f "ESRI Shapefile" ' + - 'd:/temp/test.shp ' + - source]) + with tempfile.TemporaryDirectory() as outdir: + self.assertEqual( + alg.getConsoleCommands({'LAYERS': [source], + 'OUTPUT': outdir + '/test.shp'}, context, feedback), + ['gdaltindex', + '-tileindex location -f "ESRI Shapefile" ' + + outdir + '/test.shp ' + + source]) - # with input srs - self.assertEqual( - alg.getConsoleCommands({'LAYERS': [source], - 'TARGET_CRS': 'EPSG:3111', - 'OUTPUT': 'd:/temp/test.shp'}, context, feedback), - ['gdaltindex', - '-tileindex location -t_srs EPSG:3111 -f "ESRI Shapefile" ' + - 'd:/temp/test.shp ' + - source]) + # with input srs + self.assertEqual( + alg.getConsoleCommands({'LAYERS': [source], + 'TARGET_CRS': 'EPSG:3111', + 'OUTPUT': outdir + '/test.shp'}, context, feedback), + ['gdaltindex', + '-tileindex location -t_srs EPSG:3111 -f "ESRI Shapefile" ' + + outdir + '/test.shp ' + + source]) - # with target using proj string - custom_crs = 'proj4: +proj=utm +zone=36 +south +a=6378249.145 +b=6356514.966398753 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs' - self.assertEqual( - alg.getConsoleCommands({'LAYERS': [source], - 'TARGET_CRS': custom_crs, - 'OUTPUT': 'd:/temp/test.shp'}, context, feedback), - ['gdaltindex', - '-tileindex location -t_srs EPSG:20936 -f "ESRI Shapefile" ' + - 'd:/temp/test.shp ' + - source]) + # with target using proj string + custom_crs = 'proj4: +proj=utm +zone=36 +south +a=6378249.145 +b=6356514.966398753 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs' + self.assertEqual( + alg.getConsoleCommands({'LAYERS': [source], + 'TARGET_CRS': custom_crs, + 'OUTPUT': outdir + '/test.shp'}, context, feedback), + ['gdaltindex', + '-tileindex location -t_srs EPSG:20936 -f "ESRI Shapefile" ' + + outdir + '/test.shp ' + + source]) - # with target using custom projection - custom_crs = 'proj4: +proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs' - self.assertEqual( - alg.getConsoleCommands({'LAYERS': [source], - 'TARGET_CRS': custom_crs, - 'OUTPUT': 'd:/temp/test.shp'}, context, feedback), - ['gdaltindex', - '-tileindex location -t_srs "+proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs" -f "ESRI Shapefile" ' + - 'd:/temp/test.shp ' + - source]) + # with target using custom projection + custom_crs = 'proj4: +proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs' + self.assertEqual( + alg.getConsoleCommands({'LAYERS': [source], + 'TARGET_CRS': custom_crs, + 'OUTPUT': outdir + '/test.shp'}, context, feedback), + ['gdaltindex', + '-tileindex location -t_srs "+proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs" -f "ESRI Shapefile" ' + + outdir + '/test.shp ' + + source]) - # with non-EPSG crs code - self.assertEqual( - alg.getConsoleCommands({'LAYERS': [source], - 'TARGET_CRS': 'POSTGIS:3111', - 'OUTPUT': 'd:/temp/test.shp'}, context, feedback), - ['gdaltindex', - '-tileindex location -t_srs EPSG:3111 -f "ESRI Shapefile" ' + - 'd:/temp/test.shp ' + - source]) + # with non-EPSG crs code + self.assertEqual( + alg.getConsoleCommands({'LAYERS': [source], + 'TARGET_CRS': 'POSTGIS:3111', + 'OUTPUT': outdir + '/test.shp'}, context, feedback), + ['gdaltindex', + '-tileindex location -t_srs EPSG:3111 -f "ESRI Shapefile" ' + + outdir + '/test.shp ' + + source]) def testGridAverage(self): context = QgsProcessingContext() @@ -976,32 +982,33 @@ class TestGdalAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest): alg = GridAverage() alg.initAlgorithm() - # with no NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_grid', - '-l points -a average:radius1=0.0:radius2=0.0:angle=0.0:min_points=0:nodata=0.0 -ot Float32 -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) - # with NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'NODATA': 9999, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_grid', - '-l points -a average:radius1=0.0:radius2=0.0:angle=0.0:min_points=0:nodata=9999.0 -ot Float32 -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) - # with "0" NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'NODATA': 0, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_grid', - '-l points -a average:radius1=0.0:radius2=0.0:angle=0.0:min_points=0:nodata=0.0 -ot Float32 -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) + with tempfile.TemporaryDirectory() as outdir: + # with no NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_grid', + '-l points -a average:radius1=0.0:radius2=0.0:angle=0.0:min_points=0:nodata=0.0 -ot Float32 -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) + # with NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'NODATA': 9999, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_grid', + '-l points -a average:radius1=0.0:radius2=0.0:angle=0.0:min_points=0:nodata=9999.0 -ot Float32 -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) + # with "0" NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'NODATA': 0, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_grid', + '-l points -a average:radius1=0.0:radius2=0.0:angle=0.0:min_points=0:nodata=0.0 -ot Float32 -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) def testGridDataMetrics(self): context = QgsProcessingContext() @@ -1010,32 +1017,33 @@ class TestGdalAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest): alg = GridDataMetrics() alg.initAlgorithm() - # with no NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_grid', - '-l points -a minimum:radius1=0.0:radius2=0.0:angle=0.0:min_points=0:nodata=0.0 -ot Float32 -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) - # with NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'NODATA': 9999, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_grid', - '-l points -a minimum:radius1=0.0:radius2=0.0:angle=0.0:min_points=0:nodata=9999.0 -ot Float32 -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) - # with "0" NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'NODATA': 0, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_grid', - '-l points -a minimum:radius1=0.0:radius2=0.0:angle=0.0:min_points=0:nodata=0.0 -ot Float32 -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) + with tempfile.TemporaryDirectory() as outdir: + # with no NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_grid', + '-l points -a minimum:radius1=0.0:radius2=0.0:angle=0.0:min_points=0:nodata=0.0 -ot Float32 -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) + # with NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'NODATA': 9999, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_grid', + '-l points -a minimum:radius1=0.0:radius2=0.0:angle=0.0:min_points=0:nodata=9999.0 -ot Float32 -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) + # with "0" NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'NODATA': 0, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_grid', + '-l points -a minimum:radius1=0.0:radius2=0.0:angle=0.0:min_points=0:nodata=0.0 -ot Float32 -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) def testGridInverseDistance(self): context = QgsProcessingContext() @@ -1044,32 +1052,33 @@ class TestGdalAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest): alg = GridInverseDistance() alg.initAlgorithm() - # with no NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_grid', - '-l points -a invdist:power=2.0:smothing=0.0:radius1=0.0:radius2=0.0:angle=0.0:max_points=0:min_points=0:nodata=0.0 -ot Float32 -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) - # with NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'NODATA': 9999, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_grid', - '-l points -a invdist:power=2.0:smothing=0.0:radius1=0.0:radius2=0.0:angle=0.0:max_points=0:min_points=0:nodata=9999.0 -ot Float32 -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) - # with "0" NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'NODATA': 0, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_grid', - '-l points -a invdist:power=2.0:smothing=0.0:radius1=0.0:radius2=0.0:angle=0.0:max_points=0:min_points=0:nodata=0.0 -ot Float32 -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) + with tempfile.TemporaryDirectory() as outdir: + # with no NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_grid', + '-l points -a invdist:power=2.0:smothing=0.0:radius1=0.0:radius2=0.0:angle=0.0:max_points=0:min_points=0:nodata=0.0 -ot Float32 -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) + # with NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'NODATA': 9999, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_grid', + '-l points -a invdist:power=2.0:smothing=0.0:radius1=0.0:radius2=0.0:angle=0.0:max_points=0:min_points=0:nodata=9999.0 -ot Float32 -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) + # with "0" NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'NODATA': 0, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_grid', + '-l points -a invdist:power=2.0:smothing=0.0:radius1=0.0:radius2=0.0:angle=0.0:max_points=0:min_points=0:nodata=0.0 -ot Float32 -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) def testGridInverseDistanceNearestNeighbour(self): context = QgsProcessingContext() @@ -1078,32 +1087,33 @@ class TestGdalAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest): alg = GridInverseDistanceNearestNeighbor() alg.initAlgorithm() - # with no NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_grid', - '-l points -a invdistnn:power=2.0:smothing=0.0:radius=1.0:max_points=12:min_points=0:nodata=0.0 -ot Float32 -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) - # with NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'NODATA': 9999, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_grid', - '-l points -a invdistnn:power=2.0:smothing=0.0:radius=1.0:max_points=12:min_points=0:nodata=9999.0 -ot Float32 -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) - # with "0" NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'NODATA': 0, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_grid', - '-l points -a invdistnn:power=2.0:smothing=0.0:radius=1.0:max_points=12:min_points=0:nodata=0.0 -ot Float32 -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) + with tempfile.TemporaryDirectory() as outdir: + # with no NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_grid', + '-l points -a invdistnn:power=2.0:smothing=0.0:radius=1.0:max_points=12:min_points=0:nodata=0.0 -ot Float32 -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) + # with NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'NODATA': 9999, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_grid', + '-l points -a invdistnn:power=2.0:smothing=0.0:radius=1.0:max_points=12:min_points=0:nodata=9999.0 -ot Float32 -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) + # with "0" NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'NODATA': 0, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_grid', + '-l points -a invdistnn:power=2.0:smothing=0.0:radius=1.0:max_points=12:min_points=0:nodata=0.0 -ot Float32 -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) def testGridLinear(self): context = QgsProcessingContext() @@ -1112,32 +1122,33 @@ class TestGdalAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest): alg = GridLinear() alg.initAlgorithm() - # with no NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_grid', - '-l points -a linear:radius=-1.0:nodata=0.0 -ot Float32 -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) - # with NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'NODATA': 9999, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_grid', - '-l points -a linear:radius=-1.0:nodata=9999.0 -ot Float32 -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) - # with "0" NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'NODATA': 0, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_grid', - '-l points -a linear:radius=-1.0:nodata=0.0 -ot Float32 -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) + with tempfile.TemporaryDirectory() as outdir: + # with no NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_grid', + '-l points -a linear:radius=-1.0:nodata=0.0 -ot Float32 -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) + # with NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'NODATA': 9999, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_grid', + '-l points -a linear:radius=-1.0:nodata=9999.0 -ot Float32 -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) + # with "0" NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'NODATA': 0, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_grid', + '-l points -a linear:radius=-1.0:nodata=0.0 -ot Float32 -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) def testGridNearestNeighbour(self): context = QgsProcessingContext() @@ -1146,32 +1157,33 @@ class TestGdalAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest): alg = GridNearestNeighbor() alg.initAlgorithm() - # with no NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_grid', - '-l points -a nearest:radius1=0.0:radius2=0.0:angle=0.0:nodata=0.0 -ot Float32 -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) - # with NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'NODATA': 9999, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_grid', - '-l points -a nearest:radius1=0.0:radius2=0.0:angle=0.0:nodata=9999.0 -ot Float32 -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) - # with "0" NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'NODATA': 0, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_grid', - '-l points -a nearest:radius1=0.0:radius2=0.0:angle=0.0:nodata=0.0 -ot Float32 -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) + with tempfile.TemporaryDirectory() as outdir: + # with no NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_grid', + '-l points -a nearest:radius1=0.0:radius2=0.0:angle=0.0:nodata=0.0 -ot Float32 -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) + # with NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'NODATA': 9999, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_grid', + '-l points -a nearest:radius1=0.0:radius2=0.0:angle=0.0:nodata=9999.0 -ot Float32 -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) + # with "0" NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'NODATA': 0, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_grid', + '-l points -a nearest:radius1=0.0:radius2=0.0:angle=0.0:nodata=0.0 -ot Float32 -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) def testOgr2Ogr(self): context = QgsProcessingContext() @@ -1180,26 +1192,27 @@ class TestGdalAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest): alg = ogr2ogr() alg.initAlgorithm() - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'OUTPUT': 'd:/temp/check.shp'}, context, feedback), - ['ogr2ogr', - '-f "ESRI Shapefile" d:/temp/check.shp ' + - source + ' polys2']) + 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': 'd:/temp/check.kml'}, context, feedback), - ['ogr2ogr', - '-f "LIBKML" d:/temp/check.kml ' + - 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': 'd:/temp/my out/check.kml'}, context, feedback), - ['ogr2ogr', - '-f "LIBKML" "d:/temp/my out/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']) def testOgrInfo(self): context = QgsProcessingContext() @@ -1250,93 +1263,94 @@ class TestGdalAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest): alg = hillshade() alg.initAlgorithm() - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'BAND': 1, - 'Z_FACTOR': 5, - 'SCALE': 2, - 'AZIMUTH': 90, - 'ALTITUDE': 20, - 'OUTPUT': 'd:/temp/check.tif'}, context, feedback), - ['gdaldem', - 'hillshade ' + - source + ' ' + - 'd:/temp/check.tif -of GTiff -b 1 -z 5.0 -s 2.0 -az 90.0 -alt 20.0']) + with tempfile.TemporaryDirectory() as outdir: + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'BAND': 1, + 'Z_FACTOR': 5, + 'SCALE': 2, + 'AZIMUTH': 90, + 'ALTITUDE': 20, + 'OUTPUT': outdir + '/check.tif'}, context, feedback), + ['gdaldem', + 'hillshade ' + + source + ' ' + + outdir + '/check.tif -of GTiff -b 1 -z 5.0 -s 2.0 -az 90.0 -alt 20.0']) - #paths with space - source_with_space = os.path.join(testDataPath, 'raster with spaces.tif') - self.assertEqual( - alg.getConsoleCommands({'INPUT': source_with_space, - 'BAND': 1, - 'Z_FACTOR': 5, - 'SCALE': 2, - 'AZIMUTH': 90, - 'ALTITUDE': 20, - 'OUTPUT': 'd:/temp/check out.tif'}, context, feedback), - ['gdaldem', - 'hillshade ' + - '"' + source_with_space + '" ' + - '"d:/temp/check out.tif" -of GTiff -b 1 -z 5.0 -s 2.0 -az 90.0 -alt 20.0']) + # paths with space + source_with_space = os.path.join(testDataPath, 'raster with spaces.tif') + self.assertEqual( + alg.getConsoleCommands({'INPUT': source_with_space, + 'BAND': 1, + 'Z_FACTOR': 5, + 'SCALE': 2, + 'AZIMUTH': 90, + 'ALTITUDE': 20, + 'OUTPUT': outdir + '/check out.tif'}, context, feedback), + ['gdaldem', + 'hillshade ' + + '"' + source_with_space + '" ' + + '"{}/check out.tif" -of GTiff -b 1 -z 5.0 -s 2.0 -az 90.0 -alt 20.0'.format(outdir)]) - # compute edges - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'BAND': 1, - 'Z_FACTOR': 5, - 'SCALE': 2, - 'AZIMUTH': 90, - 'ALTITUDE': 20, - 'COMPUTE_EDGES': True, - 'OUTPUT': 'd:/temp/check.tif'}, context, feedback), - ['gdaldem', - 'hillshade ' + - source + ' ' + - 'd:/temp/check.tif -of GTiff -b 1 -z 5.0 -s 2.0 -az 90.0 -alt 20.0 -compute_edges']) + # compute edges + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'BAND': 1, + 'Z_FACTOR': 5, + 'SCALE': 2, + 'AZIMUTH': 90, + 'ALTITUDE': 20, + 'COMPUTE_EDGES': True, + 'OUTPUT': outdir + '/check.tif'}, context, feedback), + ['gdaldem', + 'hillshade ' + + source + ' ' + + outdir + '/check.tif -of GTiff -b 1 -z 5.0 -s 2.0 -az 90.0 -alt 20.0 -compute_edges']) - # with ZEVENBERGEN - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'BAND': 1, - 'Z_FACTOR': 5, - 'SCALE': 2, - 'AZIMUTH': 90, - 'ALTITUDE': 20, - 'ZEVENBERGEN': True, - 'OUTPUT': 'd:/temp/check.tif'}, context, feedback), - ['gdaldem', - 'hillshade ' + - source + ' ' + - 'd:/temp/check.tif -of GTiff -b 1 -z 5.0 -s 2.0 -az 90.0 -alt 20.0 -alg ZevenbergenThorne']) + # with ZEVENBERGEN + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'BAND': 1, + 'Z_FACTOR': 5, + 'SCALE': 2, + 'AZIMUTH': 90, + 'ALTITUDE': 20, + 'ZEVENBERGEN': True, + 'OUTPUT': outdir + '/check.tif'}, context, feedback), + ['gdaldem', + 'hillshade ' + + source + ' ' + + outdir + '/check.tif -of GTiff -b 1 -z 5.0 -s 2.0 -az 90.0 -alt 20.0 -alg ZevenbergenThorne']) - # with COMBINED - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'BAND': 1, - 'Z_FACTOR': 5, - 'SCALE': 2, - 'AZIMUTH': 90, - 'ALTITUDE': 20, - 'COMBINED': True, - 'OUTPUT': 'd:/temp/check.tif'}, context, feedback), - ['gdaldem', - 'hillshade ' + - source + ' ' + - 'd:/temp/check.tif -of GTiff -b 1 -z 5.0 -s 2.0 -az 90.0 -alt 20.0 -combined']) + # with COMBINED + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'BAND': 1, + 'Z_FACTOR': 5, + 'SCALE': 2, + 'AZIMUTH': 90, + 'ALTITUDE': 20, + 'COMBINED': True, + 'OUTPUT': outdir + '/check.tif'}, context, feedback), + ['gdaldem', + 'hillshade ' + + source + ' ' + + outdir + '/check.tif -of GTiff -b 1 -z 5.0 -s 2.0 -az 90.0 -alt 20.0 -combined']) - # with multidirectional - "az" argument is not allowed! - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'BAND': 1, - 'Z_FACTOR': 5, - 'SCALE': 2, - 'AZIMUTH': 90, - 'ALTITUDE': 20, - 'MULTIDIRECTIONAL': True, - 'OUTPUT': 'd:/temp/check.tif'}, context, feedback), - ['gdaldem', - 'hillshade ' + - source + ' ' + - 'd:/temp/check.tif -of GTiff -b 1 -z 5.0 -s 2.0 -alt 20.0 -multidirectional']) + # with multidirectional - "az" argument is not allowed! + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'BAND': 1, + 'Z_FACTOR': 5, + 'SCALE': 2, + 'AZIMUTH': 90, + 'ALTITUDE': 20, + 'MULTIDIRECTIONAL': True, + 'OUTPUT': outdir + '/check.tif'}, context, feedback), + ['gdaldem', + 'hillshade ' + + source + ' ' + + outdir + '/check.tif -of GTiff -b 1 -z 5.0 -s 2.0 -alt 20.0 -multidirectional']) def testProximity(self): context = QgsProcessingContext() @@ -1345,35 +1359,36 @@ class TestGdalAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest): alg = proximity() alg.initAlgorithm() - # with no NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'BAND': 1, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_proximity.py', - '-srcband 1 -distunits PIXEL -ot Float32 -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) - # with NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'NODATA': 9999, - 'BAND': 2, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_proximity.py', - '-srcband 2 -distunits PIXEL -nodata 9999.0 -ot Float32 -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) - # with "0" NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'NODATA': 0, - 'BAND': 1, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_proximity.py', - '-srcband 1 -distunits PIXEL -nodata 0.0 -ot Float32 -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) + with tempfile.TemporaryDirectory() as outdir: + # with no NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'BAND': 1, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_proximity.py', + '-srcband 1 -distunits PIXEL -ot Float32 -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) + # with NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'NODATA': 9999, + 'BAND': 2, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_proximity.py', + '-srcband 2 -distunits PIXEL -nodata 9999.0 -ot Float32 -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) + # with "0" NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'NODATA': 0, + 'BAND': 1, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_proximity.py', + '-srcband 1 -distunits PIXEL -nodata 0.0 -ot Float32 -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) def testRasterize(self): context = QgsProcessingContext() @@ -1382,35 +1397,36 @@ class TestGdalAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest): alg = rasterize() alg.initAlgorithm() - # with no NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'FIELD': 'id', - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_rasterize', - '-l polys2 -a id -ts 0.0 0.0 -ot Float32 -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) - # with NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'NODATA': 9999, - 'FIELD': 'id', - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_rasterize', - '-l polys2 -a id -ts 0.0 0.0 -a_nodata 9999.0 -ot Float32 -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) - # with "0" NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'NODATA': 0, - 'FIELD': 'id', - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdal_rasterize', - '-l polys2 -a id -ts 0.0 0.0 -a_nodata 0.0 -ot Float32 -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) + with tempfile.TemporaryDirectory() as outdir: + # with no NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'FIELD': 'id', + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_rasterize', + '-l polys2 -a id -ts 0.0 0.0 -ot Float32 -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) + # with NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'NODATA': 9999, + 'FIELD': 'id', + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_rasterize', + '-l polys2 -a id -ts 0.0 0.0 -a_nodata 9999.0 -ot Float32 -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) + # with "0" NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'NODATA': 0, + 'FIELD': 'id', + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdal_rasterize', + '-l polys2 -a id -ts 0.0 0.0 -a_nodata 0.0 -ot Float32 -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) def testRetile(self): context = QgsProcessingContext() @@ -1419,50 +1435,51 @@ class TestGdalAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest): alg = retile() alg.initAlgorithm() - self.assertEqual( - alg.getConsoleCommands({'INPUT': [source], - 'OUTPUT': 'd:/temp'}, context, feedback), - ['gdal_retile.py', - '-ps 256 256 -overlap 0 -levels 1 -r near -ot Float32 -targetDir d:/temp ' + - source]) + with tempfile.TemporaryDirectory() as outdir: + self.assertEqual( + alg.getConsoleCommands({'INPUT': [source], + 'OUTPUT': outdir}, context, feedback), + ['gdal_retile.py', + '-ps 256 256 -overlap 0 -levels 1 -r near -ot Float32 -targetDir {} '.format(outdir) + + source]) - # with input srs - self.assertEqual( - alg.getConsoleCommands({'INPUT': [source], - 'SOURCE_CRS': 'EPSG:3111', - 'OUTPUT': 'd:/temp'}, context, feedback), - ['gdal_retile.py', - '-ps 256 256 -overlap 0 -levels 1 -s_srs EPSG:3111 -r near -ot Float32 -targetDir d:/temp ' + - source]) + # with input srs + self.assertEqual( + alg.getConsoleCommands({'INPUT': [source], + 'SOURCE_CRS': 'EPSG:3111', + 'OUTPUT': outdir}, context, feedback), + ['gdal_retile.py', + '-ps 256 256 -overlap 0 -levels 1 -s_srs EPSG:3111 -r near -ot Float32 -targetDir {} {}'.format(outdir, source) + ]) - # with target using proj string - custom_crs = 'proj4: +proj=utm +zone=36 +south +a=6378249.145 +b=6356514.966398753 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs' - self.assertEqual( - alg.getConsoleCommands({'INPUT': [source], - 'SOURCE_CRS': custom_crs, - 'OUTPUT': 'd:/temp'}, context, feedback), - ['gdal_retile.py', - '-ps 256 256 -overlap 0 -levels 1 -s_srs EPSG:20936 -r near -ot Float32 -targetDir d:/temp ' + - source]) + # with target using proj string + custom_crs = 'proj4: +proj=utm +zone=36 +south +a=6378249.145 +b=6356514.966398753 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs' + self.assertEqual( + alg.getConsoleCommands({'INPUT': [source], + 'SOURCE_CRS': custom_crs, + 'OUTPUT': outdir}, context, feedback), + ['gdal_retile.py', + '-ps 256 256 -overlap 0 -levels 1 -s_srs EPSG:20936 -r near -ot Float32 -targetDir {} {}'.format(outdir, source) + ]) - # with target using custom projection - custom_crs = 'proj4: +proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs' - self.assertEqual( - alg.getConsoleCommands({'INPUT': [source], - 'SOURCE_CRS': custom_crs, - 'OUTPUT': 'd:/temp'}, context, feedback), - ['gdal_retile.py', - '-ps 256 256 -overlap 0 -levels 1 -s_srs "+proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs" -r near -ot Float32 -targetDir d:/temp ' + - source]) + # with target using custom projection + custom_crs = 'proj4: +proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs' + self.assertEqual( + alg.getConsoleCommands({'INPUT': [source], + 'SOURCE_CRS': custom_crs, + 'OUTPUT': outdir}, context, feedback), + ['gdal_retile.py', + '-ps 256 256 -overlap 0 -levels 1 -s_srs "+proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs" -r near -ot Float32 -targetDir {} {}'.format(outdir, source) + ]) - # with non-EPSG crs code - self.assertEqual( - alg.getConsoleCommands({'INPUT': [source], - 'SOURCE_CRS': 'POSTGIS:3111', - 'OUTPUT': 'd:/temp'}, context, feedback), - ['gdal_retile.py', - '-ps 256 256 -overlap 0 -levels 1 -s_srs EPSG:3111 -r near -ot Float32 -targetDir d:/temp ' + - source]) + # with non-EPSG crs code + self.assertEqual( + alg.getConsoleCommands({'INPUT': [source], + 'SOURCE_CRS': 'POSTGIS:3111', + 'OUTPUT': outdir}, context, feedback), + ['gdal_retile.py', + '-ps 256 256 -overlap 0 -levels 1 -s_srs EPSG:3111 -r near -ot Float32 -targetDir {} {}'.format(outdir, source) + ]) def testWarp(self): context = QgsProcessingContext() @@ -1471,212 +1488,217 @@ class TestGdalAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest): alg = warp() alg.initAlgorithm() - # with no NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'SOURCE_CRS': 'EPSG:3111', - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdalwarp', - '-s_srs EPSG:3111 -t_srs EPSG:4326 -r near -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) - # with None NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'NODATA': None, - 'SOURCE_CRS': 'EPSG:3111', - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdalwarp', - '-s_srs EPSG:3111 -t_srs EPSG:4326 -r near -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) - # with NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'NODATA': 9999, - 'SOURCE_CRS': 'EPSG:3111', - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdalwarp', - '-s_srs EPSG:3111 -t_srs EPSG:4326 -dstnodata 9999.0 -r near -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) - # with "0" NODATA value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'NODATA': 0, - 'SOURCE_CRS': 'EPSG:3111', - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdalwarp', - '-s_srs EPSG:3111 -t_srs EPSG:4326 -dstnodata 0.0 -r near -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) - # with "0" NODATA value and custom data type - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'NODATA': 0, - 'DATA_TYPE': 6, - 'SOURCE_CRS': 'EPSG:3111', - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdalwarp', - '-s_srs EPSG:3111 -t_srs EPSG:4326 -dstnodata 0.0 -r near -ot Float32 -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) + with tempfile.TemporaryDirectory() as outdir: + # with no NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'SOURCE_CRS': 'EPSG:3111', + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdalwarp', + '-s_srs EPSG:3111 -t_srs EPSG:4326 -r near -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) + # with None NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'NODATA': None, + 'SOURCE_CRS': 'EPSG:3111', + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdalwarp', + '-s_srs EPSG:3111 -t_srs EPSG:4326 -r near -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) + # with NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'NODATA': 9999, + 'SOURCE_CRS': 'EPSG:3111', + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdalwarp', + '-s_srs EPSG:3111 -t_srs EPSG:4326 -dstnodata 9999.0 -r near -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) + # with "0" NODATA value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'NODATA': 0, + 'SOURCE_CRS': 'EPSG:3111', + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdalwarp', + '-s_srs EPSG:3111 -t_srs EPSG:4326 -dstnodata 0.0 -r near -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) + # with "0" NODATA value and custom data type + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'NODATA': 0, + 'DATA_TYPE': 6, + 'SOURCE_CRS': 'EPSG:3111', + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdalwarp', + '-s_srs EPSG:3111 -t_srs EPSG:4326 -dstnodata 0.0 -r near -ot Float32 -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) - # with target using proj string - custom_crs = 'proj4: +proj=utm +zone=36 +south +a=6378249.145 +b=6356514.966398753 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs' - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'SOURCE_CRS': custom_crs, - 'TARGET_CRS': custom_crs, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdalwarp', - '-s_srs EPSG:20936 -t_srs EPSG:20936 -r near -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) + # with target using proj string + custom_crs = 'proj4: +proj=utm +zone=36 +south +a=6378249.145 +b=6356514.966398753 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs' + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'SOURCE_CRS': custom_crs, + 'TARGET_CRS': custom_crs, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdalwarp', + '-s_srs EPSG:20936 -t_srs EPSG:20936 -r near -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) - # with target using custom projection - custom_crs = 'proj4: +proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs' - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'SOURCE_CRS': custom_crs, - 'TARGET_CRS': custom_crs, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdalwarp', - '-s_srs "+proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs" -t_srs "+proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs" -r near -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) + # with target using custom projection + custom_crs = 'proj4: +proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs' + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'SOURCE_CRS': custom_crs, + 'TARGET_CRS': custom_crs, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdalwarp', + '-s_srs "+proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs" -t_srs "+proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs" -r near -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) - # with non-EPSG crs code - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'SOURCE_CRS': 'POSTGIS:3111', - 'TARGET_CRS': 'POSTGIS:3111', - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdalwarp', - '-s_srs EPSG:3111 -t_srs EPSG:3111 -r near -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) + # with non-EPSG crs code + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'SOURCE_CRS': 'POSTGIS:3111', + 'TARGET_CRS': 'POSTGIS:3111', + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdalwarp', + '-s_srs EPSG:3111 -t_srs EPSG:3111 -r near -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) - # with target resolution with None value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'SOURCE_CRS': 'EPSG:3111', - 'TARGET_RESOLUTION': None, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdalwarp', - '-s_srs EPSG:3111 -t_srs EPSG:4326 -r near -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) + # with target resolution with None value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'SOURCE_CRS': 'EPSG:3111', + 'TARGET_RESOLUTION': None, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdalwarp', + '-s_srs EPSG:3111 -t_srs EPSG:4326 -r near -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) - # test target resolution with a valid value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'SOURCE_CRS': 'EPSG:3111', - 'TARGET_RESOLUTION': 10.0, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdalwarp', - '-s_srs EPSG:3111 -t_srs EPSG:4326 -tr 10.0 10.0 -r near -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) + # test target resolution with a valid value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'SOURCE_CRS': 'EPSG:3111', + 'TARGET_RESOLUTION': 10.0, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdalwarp', + '-s_srs EPSG:3111 -t_srs EPSG:4326 -tr 10.0 10.0 -r near -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) - # test target resolution with a value of zero, to be ignored - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'SOURCE_CRS': 'EPSG:3111', - 'TARGET_RESOLUTION': 0.0, - 'OUTPUT': 'd:/temp/check.jpg'}, context, feedback), - ['gdalwarp', - '-s_srs EPSG:3111 -t_srs EPSG:4326 -r near -of JPEG ' + - source + ' ' + - 'd:/temp/check.jpg']) + # test target resolution with a value of zero, to be ignored + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'SOURCE_CRS': 'EPSG:3111', + 'TARGET_RESOLUTION': 0.0, + 'OUTPUT': outdir + '/check.jpg'}, context, feedback), + ['gdalwarp', + '-s_srs EPSG:3111 -t_srs EPSG:4326 -r near -of JPEG ' + + source + ' ' + + outdir + '/check.jpg']) def testRearrangeBands(self): context = QgsProcessingContext() feedback = QgsProcessingFeedback() source = os.path.join(testDataPath, 'dem.tif') - outsource = 'd:/temp/check.tif' - alg = rearrange_bands() - alg.initAlgorithm() + with tempfile.TemporaryDirectory() as outdir: + outsource = outdir + '/check.tif' - # single band - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'BANDS': 1, - 'OUTPUT': outsource}, context, feedback), - ['gdal_translate', '-b 1 ' + - '-of GTiff ' + - source + ' ' + outsource]) - # three bands, re-ordered - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'BANDS': [3, 2, 1], - 'OUTPUT': outsource}, context, feedback), - ['gdal_translate', '-b 3 -b 2 -b 1 ' + - '-of GTiff ' + - source + ' ' + outsource]) - # three bands, re-ordered with custom data type - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'BANDS': [3, 2, 1], - 'DATA_TYPE': 6, - 'OUTPUT': outsource}, context, feedback), - ['gdal_translate', '-b 3 -b 2 -b 1 ' + - '-ot Float32 -of GTiff ' + - source + ' ' + outsource]) + alg = rearrange_bands() + alg.initAlgorithm() + + # single band + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'BANDS': 1, + 'OUTPUT': outsource}, context, feedback), + ['gdal_translate', '-b 1 ' + + '-of GTiff ' + + source + ' ' + outsource]) + # three bands, re-ordered + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'BANDS': [3, 2, 1], + 'OUTPUT': outsource}, context, feedback), + ['gdal_translate', '-b 3 -b 2 -b 1 ' + + '-of GTiff ' + + source + ' ' + outsource]) + # three bands, re-ordered with custom data type + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'BANDS': [3, 2, 1], + 'DATA_TYPE': 6, + 'OUTPUT': outsource}, context, feedback), + ['gdal_translate', '-b 3 -b 2 -b 1 ' + + '-ot Float32 -of GTiff ' + + source + ' ' + outsource]) def testFillnodata(self): context = QgsProcessingContext() feedback = QgsProcessingFeedback() source = os.path.join(testDataPath, 'dem.tif') mask = os.path.join(testDataPath, 'raster.tif') - outsource = 'd:/temp/check.tif' - alg = fillnodata() - alg.initAlgorithm() - # with mask value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'BAND': 1, - 'DISTANCE': 10, - 'ITERATIONS': 0, - 'MASK_LAYER': mask, - 'NO_MASK': False, - 'OUTPUT': outsource}, context, feedback), - ['gdal_fillnodata.py', - '-md 10 -b 1 -mask ' + - mask + - ' -of GTiff ' + - source + ' ' + - outsource]) + with tempfile.TemporaryDirectory() as outdir: + outsource = outdir + '/check.tif' + alg = fillnodata() + alg.initAlgorithm() - # without mask value - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'BAND': 1, - 'DISTANCE': 10, - 'ITERATIONS': 0, - 'NO_MASK': False, - 'OUTPUT': outsource}, context, feedback), - ['gdal_fillnodata.py', - '-md 10 -b 1 ' + - '-of GTiff ' + - source + ' ' + - outsource]) + # with mask value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'BAND': 1, + 'DISTANCE': 10, + 'ITERATIONS': 0, + 'MASK_LAYER': mask, + 'NO_MASK': False, + 'OUTPUT': outsource}, context, feedback), + ['gdal_fillnodata.py', + '-md 10 -b 1 -mask ' + + mask + + ' -of GTiff ' + + source + ' ' + + outsource]) - # nomask true - self.assertEqual( - alg.getConsoleCommands({'INPUT': source, - 'BAND': 1, - 'DISTANCE': 10, - 'ITERATIONS': 0, - 'NO_MASK': True, - 'OUTPUT': outsource}, context, feedback), - ['gdal_fillnodata.py', - '-md 10 -b 1 -nomask ' + - '-of GTiff ' + - source + ' ' + - outsource]) + # without mask value + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'BAND': 1, + 'DISTANCE': 10, + 'ITERATIONS': 0, + 'NO_MASK': False, + 'OUTPUT': outsource}, context, feedback), + ['gdal_fillnodata.py', + '-md 10 -b 1 ' + + '-of GTiff ' + + source + ' ' + + outsource]) + + # nomask true + self.assertEqual( + alg.getConsoleCommands({'INPUT': source, + 'BAND': 1, + 'DISTANCE': 10, + 'ITERATIONS': 0, + 'NO_MASK': True, + 'OUTPUT': outsource}, context, feedback), + ['gdal_fillnodata.py', + '-md 10 -b 1 -nomask ' + + '-of GTiff ' + + source + ' ' + + outsource]) class TestGdalOgrToPostGis(unittest.TestCase):