Skip to content

Commit

Permalink
[processing][gdal] Fix translate ignores nodata values of 0
Browse files Browse the repository at this point in the history
And add more unit tests to translate alg

(cherry-picked from 70aad93)
  • Loading branch information
nyalldawson committed May 4, 2018
1 parent d833970 commit ba9f27e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 5 deletions.
7 changes: 5 additions & 2 deletions python/plugins/processing/algs/gdal/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ def icon(self):
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 = []

Expand All @@ -122,7 +125,7 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
arguments.append('-a_srs')
arguments.append(crs.authid())

if nodata:
if nodata is not None:
arguments.append('-a_nodata')
arguments.append(nodata)

Expand Down
74 changes: 71 additions & 3 deletions python/plugins/processing/tests/GdalAlgorithmsTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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,
Expand Down Expand Up @@ -91,7 +92,8 @@ def testGetOgrCompatibleSourceFromMemoryLayer(self):
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))
Expand Down Expand Up @@ -180,9 +182,76 @@ def _copyFile(dst):
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):

Expand All @@ -198,7 +267,6 @@ def tearDownClass(cls):

# See https://issues.qgis.org/issues/15706
def test_getConnectionString(self):

obj = OgrToPostGis()
obj.initAlgorithm({})

Expand Down

0 comments on commit ba9f27e

Please sign in to comment.