[processing][gdal] Fix translate ignores nodata values of 0

And add more unit tests to translate alg
This commit is contained in:
Nyall Dawson 2018-05-03 10:45:29 +10:00
parent d99155f3f0
commit 70aad93a61
2 changed files with 76 additions and 5 deletions

View File

@ -113,7 +113,10 @@ class translate(GdalAlgorithm):
def getConsoleCommands(self, parameters, context, feedback, executing=True):
inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
nodata = self.parameterAsDouble(parameters, self.NODATA, context)
if self.NODATA in parameters and parameters[self.NODATA] is not None:
nodata = self.parameterAsDouble(parameters, self.NODATA, context)
else:
nodata = None
arguments = []
@ -122,7 +125,7 @@ class translate(GdalAlgorithm):
arguments.append('-a_srs')
arguments.append(crs.authid())
if nodata:
if nodata is not None:
arguments.append('-a_nodata')
arguments.append(nodata)

View File

@ -28,6 +28,7 @@ __revision__ = ':%H$'
import AlgorithmsTestBase
from processing.algs.gdal.OgrToPostGis import OgrToPostGis
from processing.algs.gdal.GdalUtils import GdalUtils
from processing.algs.gdal.translate import translate
from qgis.core import (QgsProcessingContext,
QgsProcessingFeedback,
QgsApplication,
@ -91,7 +92,8 @@ class TestGdalAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest):
parameters = {'INPUT': 'testmem'}
feedback = QgsProcessingFeedback()
# check that memory layer is automatically saved out to shape when required by GDAL algorithms
ogr_data_path, ogr_layer_name = alg.getOgrCompatibleSource('INPUT', parameters, context, feedback, executing=True)
ogr_data_path, ogr_layer_name = alg.getOgrCompatibleSource('INPUT', parameters, context, feedback,
executing=True)
self.assertTrue(ogr_data_path)
self.assertTrue(ogr_data_path.endswith('.shp'))
self.assertTrue(os.path.exists(ogr_data_path))
@ -180,9 +182,76 @@ class TestGdalAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest):
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=')
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 testGdalTranslate(self):
context = QgsProcessingContext()
feedback = QgsProcessingFeedback()
source = os.path.join(testDataPath, 'dem.tif')
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',
'-ot Float32 -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 ' +
'-ot Float32 -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 ' +
'-ot Float32 -of JPEG ' +
source + ' ' +
'd:/temp/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 ' +
'-ot Float32 -of JPEG ' +
source + ' ' +
'd:/temp/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 ' +
'-ot Float32 -of JPEG ' +
source + ' ' +
'd:/temp/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 ' +
'-ot Float32 -of GTiff ' +
source + ' ' +
'd:/temp/check.tif'])
class TestGdalOgrToPostGis(unittest.TestCase):
@ -198,7 +267,6 @@ class TestGdalOgrToPostGis(unittest.TestCase):
# See https://issues.qgis.org/issues/15706
def test_getConnectionString(self):
obj = OgrToPostGis()
obj.initAlgorithm({})